Skip to content

Commit

Permalink
map keys must implement unMarshalText
Browse files Browse the repository at this point in the history
  • Loading branch information
5lliot committed Aug 31, 2023
1 parent 60f1225 commit 3d71ca4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/big"
"strconv"
"strings"

"github.com/Gearbox-protocol/sdk-go/log"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion utils/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3d71ca4

Please sign in to comment.