From 3d71ca44d8e72fe9dcd549a947729b790e7e73d4 Mon Sep 17 00:00:00 2001 From: 5lliot Date: Thu, 31 Aug 2023 23:22:25 +0700 Subject: [PATCH] map keys must implement unMarshalText --- core/version.go | 17 ++++++++++++++--- utils/string.go | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/core/version.go b/core/version.go index 39451ccda..9e38a7f5e 100644 --- a/core/version.go +++ b/core/version.go @@ -5,6 +5,7 @@ import ( "fmt" "math/big" "strconv" + "strings" "github.com/Gearbox-protocol/sdk-go/log" "github.com/ethereum/go-ethereum/accounts/abi" @@ -57,7 +58,7 @@ func (v VersionType) Decimals() int8 { } func (v VersionType) IsGBv1() bool { - return v.v == 2 + return v.v == 1 } func (v VersionType) IsPriceInUSD() bool { @@ -97,15 +98,25 @@ func (z VersionType) MarshalJSON() ([]byte, error) { } func (z *VersionType) UnmarshalJSON(b []byte) error { - v, err := strconv.Atoi(string(b)) + str := strings.Trim(string(b), "\"") + v, err := strconv.Atoi(str) if err != nil { return fmt.Errorf("can unmarshal versiontype %s", err) } - *z = NewVersion(int16(v)) + z.v = int16(v) return nil +} + +// https://stackoverflow.com/questions/55335296/problem-with-marshal-unmarshal-when-key-of-map-is-a-struct for map +func (z VersionType) MarshalText() (text []byte, err error) { + return z.MarshalJSON() +} +func (s *VersionType) UnmarshalText(text []byte) error { + return s.UnmarshalJSON(text) } + func (v VersionType) Eq(in int16) bool { return v.v == in } diff --git a/utils/string.go b/utils/string.go index 691c9cdb9..89482e7b7 100644 --- a/utils/string.go +++ b/utils/string.go @@ -121,7 +121,7 @@ func ReadFromEnv(val interface{}) { switch envVarDS.Type.Kind() { case reflect.String: rv.Field(i).SetString(value) - case reflect.Int64, reflect.Int32, reflect.Int: + case reflect.Int64, reflect.Int32, reflect.Int, reflect.Int16: num, err := strconv.ParseInt(value, 10, 64) if err != nil { log.Fatalf("Err(%v) while getting env %s", err, envField)