-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package asset | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type CoinType string | ||
|
||
const ( | ||
Coin CoinType = "coin" | ||
Token CoinType = "token" | ||
|
||
coinPrefix = 'c' | ||
tokenPrefix = 't' | ||
) | ||
|
||
func ParseID(id string) (uint, string, error) { | ||
rawResult := strings.Split(id, "_") | ||
resLen := len(rawResult) | ||
if resLen < 1 { | ||
return 0, "", errors.New("bad ID") | ||
} | ||
|
||
coin, err := findCoinID(rawResult) | ||
if err != nil { | ||
return 0, "", errors.New("bad ID") | ||
} | ||
|
||
token := findTokenID(rawResult) | ||
|
||
if token != "" { | ||
return coin, token, nil | ||
} | ||
|
||
return coin, "", nil | ||
} | ||
|
||
func BuildID(coin uint, token string) string { | ||
c := strconv.Itoa(int(coin)) | ||
if token != "" { | ||
return string(coinPrefix) + c + "_" + string(tokenPrefix) + token | ||
} | ||
return string(coinPrefix) + c | ||
} | ||
|
||
func findCoinID(words []string) (uint, error) { | ||
for _, w := range words { | ||
if w[0] == coinPrefix { | ||
rawCoin := removeFirstChar(w) | ||
coin, err := strconv.Atoi(rawCoin) | ||
if err != nil { | ||
return 0, errors.New("bad coin") | ||
} | ||
return uint(coin), nil | ||
} | ||
} | ||
return 0, errors.New("no coin") | ||
} | ||
|
||
func findTokenID(words []string) string { | ||
for _, w := range words { | ||
if w[0] == tokenPrefix { | ||
token := removeFirstChar(w) | ||
if len(token) > 0 { | ||
return token | ||
} | ||
return "" | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func removeFirstChar(input string) string { | ||
if len(input) <= 1 { | ||
return "" | ||
} | ||
return string([]rune(input)[1:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package asset | ||
|
||
import ( | ||
"errors" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestParseID(t *testing.T) { | ||
testStruct := []struct { | ||
givenID string | ||
wantedCoin uint | ||
wantedToken string | ||
wantedType CoinType | ||
wantedError error | ||
}{ | ||
{"c714_tTWT-8C2", | ||
714, | ||
"TWT-8C2", | ||
Token, | ||
nil, | ||
}, | ||
{"tTWT-8C2_c714", | ||
714, | ||
"TWT-8C2", | ||
Token, | ||
nil, | ||
}, | ||
{"c714", | ||
714, | ||
"", | ||
Coin, | ||
nil, | ||
}, | ||
{"tTWT-8C2", | ||
0, | ||
"", | ||
Coin, | ||
errors.New("bad ID"), | ||
}, | ||
{"c714_TWT-8C2", | ||
714, | ||
"", | ||
Coin, | ||
nil, | ||
}, | ||
} | ||
|
||
for _, tt := range testStruct { | ||
coin, token, err := ParseID(tt.givenID) | ||
assert.Equal(t, tt.wantedCoin, coin) | ||
assert.Equal(t, tt.wantedToken, token) | ||
assert.Equal(t, tt.wantedError, err) | ||
} | ||
} | ||
|
||
func TestBuildID(t *testing.T) { | ||
testStruct := []struct { | ||
wantedID string | ||
givenCoin uint | ||
givenToken string | ||
}{ | ||
{"c714_tTWT-8C2", | ||
714, | ||
"TWT-8C2", | ||
}, | ||
{"c60", | ||
60, | ||
"", | ||
}, | ||
{"c0", | ||
0, | ||
"", | ||
}, | ||
{"c0_t:fnfjunwpiucU#*0! 02", | ||
0, | ||
":fnfjunwpiucU#*0! 02", | ||
}, | ||
} | ||
|
||
for _, tt := range testStruct { | ||
id := BuildID(tt.givenCoin, tt.givenToken) | ||
assert.Equal(t, tt.wantedID, id) | ||
} | ||
} | ||
|
||
func Test_removeFirstChar(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{"Normal case", "Bob", "ob"}, | ||
{"Empty String", "", ""}, | ||
{"One Char String Test", "A", ""}, | ||
{"Another normaal", "abcdef", "bcdef"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
var got = removeFirstChar(tt.input) | ||
if got != tt.expected { | ||
t.Fatalf("Got %v, Expected %v.", got, tt.expected) | ||
} | ||
} | ||
} | ||
|
||
func Test_findCoinID(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
words []string | ||
expected uint | ||
expectedErr error | ||
}{ | ||
{"Normal case", []string{"c100", "t60", "e30"}, 100, nil}, | ||
{"Empty coin", []string{"d100", "t60", "e30"}, 0, errors.New("no coin")}, | ||
{"Empty words", []string{}, 0, errors.New("no coin")}, | ||
{"Bad coin", []string{"cd100", "t60", "e30"}, 0, errors.New("bad coin")}, | ||
{"Bad coin #2", []string{"c", "t60", "e30"}, 0, errors.New("bad coin")}, | ||
} | ||
|
||
for _, tt := range tests { | ||
got, err := findCoinID(tt.words) | ||
assert.Equal(t, tt.expected, got) | ||
assert.Equal(t, tt.expectedErr, err) | ||
} | ||
} | ||
|
||
func Test_findTokenID(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
words []string | ||
expected string | ||
}{ | ||
{"Normal case", []string{"c100", "t60", "e30"}, "60"}, | ||
{"Empty token", []string{"d100", "a", "e30"}, ""}, | ||
{"Empty words", []string{}, ""}, | ||
{"Bad token", []string{"cd100", "t", "e30"}, ""}, | ||
} | ||
|
||
for _, tt := range tests { | ||
got := findTokenID(tt.words) | ||
assert.Equal(t, tt.expected, got) | ||
} | ||
} |