mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-01 15:10:44 +00:00
Asset package update (#581)
* Rewrite new function and deploy where we can minimise the chance of setting an asset type that is different to supported list - sets validation to exact supported list * change wording
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package asset
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -59,12 +60,12 @@ func (a Items) Strings() []string {
|
||||
// Contains returns whether or not the supplied asset exists
|
||||
// in the list of Items
|
||||
func (a Items) Contains(i Item) bool {
|
||||
if !IsValid(i) {
|
||||
if !i.IsValid() {
|
||||
return false
|
||||
}
|
||||
|
||||
for x := range a {
|
||||
if strings.EqualFold(a[x].String(), i.String()) {
|
||||
if a[x].String() == i.String() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -79,35 +80,24 @@ func (a Items) JoinToString(separator string) string {
|
||||
|
||||
// IsValid returns whether or not the supplied asset type is valid or
|
||||
// not
|
||||
func IsValid(input Item) bool {
|
||||
a := Supported()
|
||||
for x := range a {
|
||||
if strings.EqualFold(a[x].String(), input.String()) {
|
||||
func (a Item) IsValid() bool {
|
||||
for x := range supported {
|
||||
if supported[x].String() == a.String() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// New takes an input of asset types as string and returns an Items
|
||||
// array
|
||||
func New(input string) Items {
|
||||
if !strings.Contains(input, ",") {
|
||||
if IsValid(Item(input)) {
|
||||
return Items{
|
||||
Item(input),
|
||||
}
|
||||
// New takes an input matches to relevant package assets
|
||||
func New(input string) (Item, error) {
|
||||
input = strings.ToLower(input)
|
||||
for i := range supported {
|
||||
if string(supported[i]) == input {
|
||||
return supported[i], nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
assets := strings.Split(input, ",")
|
||||
var result Items
|
||||
for x := range assets {
|
||||
if !IsValid(Item(assets[x])) {
|
||||
return nil
|
||||
}
|
||||
result = append(result, Item(assets[x]))
|
||||
}
|
||||
return result
|
||||
return "", fmt.Errorf("cannot create new asset: input %s mismatch to supported asset list %s",
|
||||
input,
|
||||
supported)
|
||||
}
|
||||
|
||||
@@ -37,7 +37,9 @@ func TestContains(t *testing.T) {
|
||||
t.Fatal("TestContains returned an unexpected result")
|
||||
}
|
||||
|
||||
if !a.Contains("SpOt") {
|
||||
// Every asset should be created and matched with func New so this should
|
||||
// not be matched against list
|
||||
if a.Contains("SpOt") {
|
||||
t.Error("TestContains returned an unexpected result")
|
||||
}
|
||||
}
|
||||
@@ -50,36 +52,39 @@ func TestJoinToString(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestIsValid(t *testing.T) {
|
||||
if IsValid("rawr") {
|
||||
if Item("rawr").IsValid() {
|
||||
t.Fatal("TestIsValid returned an unexpected result")
|
||||
}
|
||||
|
||||
if !IsValid(Spot) {
|
||||
if !Spot.IsValid() {
|
||||
t.Fatal("TestIsValid returned an unexpected result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
a := New("Spota")
|
||||
if a != nil {
|
||||
_, err := New("Spota")
|
||||
if err == nil {
|
||||
t.Fatal("TestNew returned an unexpected result")
|
||||
}
|
||||
|
||||
a = New("SpOt")
|
||||
if a == nil {
|
||||
t.Fatal("TestNew returned an unexpected result")
|
||||
a, err := New("SpOt")
|
||||
if err != nil {
|
||||
t.Fatal("TestNew returned an unexpected result", err)
|
||||
}
|
||||
|
||||
a = New("spot,futures")
|
||||
if a.JoinToString(",") != "spot,futures" {
|
||||
t.Fatal("TestNew returned an unexpected result")
|
||||
}
|
||||
|
||||
if a := New("Spot_rawr"); a != nil {
|
||||
t.Fatal("TestNew returned an unexpected result")
|
||||
}
|
||||
|
||||
if a := New("Spot,Rawr"); a != nil {
|
||||
if a != Spot {
|
||||
t.Fatal("TestNew returned an unexpected result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSupported(t *testing.T) {
|
||||
s := Supported()
|
||||
if len(supported) != len(s) {
|
||||
t.Fatal("TestSupported mismatched lengths")
|
||||
}
|
||||
for i := 0; i < len(supported); i++ {
|
||||
if s[i] != supported[i] {
|
||||
t.Fatal("TestSupported returned an unexpected result")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user