Skip to content

Commit

Permalink
Extract the general ID parse to id.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mehcode committed Dec 13, 2019
1 parent 6a1ce9e commit bcbbfb2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 80 deletions.
21 changes: 1 addition & 20 deletions account_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package hedera

import (
"fmt"
"strconv"
"strings"

"github.com/hashgraph/hedera-sdk-go/proto"
)

Expand All @@ -15,23 +12,7 @@ type AccountID struct {
}

func AccountIDFromString(s string) (AccountID, error) {
values := strings.SplitN(s, ".", 3)
if len(values) != 3 {
// Was not three values separated by periods
return AccountID{}, fmt.Errorf("expected {shard}.{realm}.{num}")
}

shard, err := strconv.Atoi(values[0])
if err != nil {
return AccountID{}, err
}

realm, err := strconv.Atoi(values[1])
if err != nil {
return AccountID{}, err
}

num, err := strconv.Atoi(values[2])
shard, realm, num, err := idFromString(s)
if err != nil {
return AccountID{}, err
}
Expand Down
21 changes: 1 addition & 20 deletions consensus_topic_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package hedera

import (
"fmt"
"strconv"
"strings"

"github.com/hashgraph/hedera-sdk-go/proto"
)

Expand All @@ -15,23 +12,7 @@ type ConsensusTopicID struct {
}

func ConsensusTopicIDFromString(s string) (ConsensusTopicID, error) {
values := strings.SplitN(s, ".", 3)
if len(values) != 3 {
// Was not three values separated by periods
return ConsensusTopicID{}, fmt.Errorf("expected {shard}.{realm}.{num}")
}

shard, err := strconv.Atoi(values[0])
if err != nil {
return ConsensusTopicID{}, err
}

realm, err := strconv.Atoi(values[1])
if err != nil {
return ConsensusTopicID{}, err
}

num, err := strconv.Atoi(values[2])
shard, realm, num, err := idFromString(s)
if err != nil {
return ConsensusTopicID{}, err
}
Expand Down
21 changes: 1 addition & 20 deletions contract_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package hedera

import (
"fmt"
"strconv"
"strings"

"github.com/hashgraph/hedera-sdk-go/proto"
)

Expand All @@ -15,23 +12,7 @@ type ContractID struct {
}

func ContractIDFromString(s string) (ContractID, error) {
values := strings.SplitN(s, ".", 3)
if len(values) != 3 {
// Was not three values separated by periods
return ContractID{}, fmt.Errorf("expected {shard}.{realm}.{num}")
}

shard, err := strconv.Atoi(values[0])
if err != nil {
return ContractID{}, err
}

realm, err := strconv.Atoi(values[1])
if err != nil {
return ContractID{}, err
}

num, err := strconv.Atoi(values[2])
shard, realm, num, err := idFromString(s)
if err != nil {
return ContractID{}, err
}
Expand Down
21 changes: 1 addition & 20 deletions file_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package hedera

import (
"fmt"
"strconv"
"strings"

"github.com/hashgraph/hedera-sdk-go/proto"
)

Expand All @@ -15,23 +12,7 @@ type FileID struct {
}

func FileIDFromString(s string) (FileID, error) {
values := strings.SplitN(s, ".", 3)
if len(values) != 3 {
// Was not three values separated by periods
return FileID{}, fmt.Errorf("expected {shard}.{realm}.{num}")
}

shard, err := strconv.Atoi(values[0])
if err != nil {
return FileID{}, err
}

realm, err := strconv.Atoi(values[1])
if err != nil {
return FileID{}, err
}

num, err := strconv.Atoi(values[2])
shard, realm, num, err := idFromString(s)
if err != nil {
return FileID{}, err
}
Expand Down
32 changes: 32 additions & 0 deletions id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package hedera

import (
"fmt"
"strconv"
"strings"
)

func idFromString(s string) (shard int, realm int, num int, err error) {
values := strings.SplitN(s, ".", 3)
if len(values) != 3 {
// Was not three values separated by periods
return 0, 0, 0, fmt.Errorf("expected {shard}.{realm}.{num}")
}

shard, err = strconv.Atoi(values[0])
if err != nil {
return 0, 0, 0, err
}

realm, err = strconv.Atoi(values[1])
if err != nil {
return 0, 0, 0, err
}

num, err = strconv.Atoi(values[2])
if err != nil {
return 0, 0, 0, err
}

return
}

0 comments on commit bcbbfb2

Please sign in to comment.