Skip to content

Commit

Permalink
types.StrInt64: support UnmarshalYAML
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Feb 2, 2025
1 parent 3efdd45 commit 10968e0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/strategy/xmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,7 @@ func (s *Strategy) canDelayHedge(hedgeSide types.SideType, pos fixedpoint.Value)
return false
}

// TODO: move this method to SpreadMaker
func (s *Strategy) cancelSpreadMakerOrderAndReturnCoveredPos(ctx context.Context, coveredPosition *fixedpoint.MutexValue) {
s.logger.Infof("canceling current spread maker order...")

Expand Down
36 changes: 33 additions & 3 deletions pkg/types/strint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,36 @@ import (
// StrInt64 is a string type for int64
type StrInt64 int64

func NewStrInt64FromString(ss string) (StrInt64, error) {
i, err := strconv.ParseInt(ss, 10, 64)
if err != nil {
return 0, err
}

return StrInt64(i), nil
}

func (s *StrInt64) UnmarshalYAML(unmarshal func(a interface{}) error) (err error) {
var a int64
if err = unmarshal(&a); err == nil {
*s = StrInt64(a)
return
}

var ss string
if err = unmarshal(&ss); err == nil {
s2, err := NewStrInt64FromString(ss)

Check failure on line 30 in pkg/types/strint.go

View workflow job for this annotation

GitHub Actions / build (6.2, 1.21)

inner declaration of var err error

Check failure on line 30 in pkg/types/strint.go

View workflow job for this annotation

GitHub Actions / lint

inner declaration of var err error) (typecheck)

Check failure on line 30 in pkg/types/strint.go

View workflow job for this annotation

GitHub Actions / lint

inner declaration of var err error) (typecheck)

Check failure on line 30 in pkg/types/strint.go

View workflow job for this annotation

GitHub Actions / lint

inner declaration of var err error) (typecheck)
if err != nil {
return err
}

*s = s2
return

Check failure on line 36 in pkg/types/strint.go

View workflow job for this annotation

GitHub Actions / build (6.2, 1.21)

result parameter err not in scope at return

Check failure on line 36 in pkg/types/strint.go

View workflow job for this annotation

GitHub Actions / lint

result parameter err not in scope at return

Check failure on line 36 in pkg/types/strint.go

View workflow job for this annotation

GitHub Actions / lint

result parameter err not in scope at return

Check failure on line 36 in pkg/types/strint.go

View workflow job for this annotation

GitHub Actions / lint

result parameter err not in scope at return
}

return fmt.Errorf("StrInt64.UnmarshalYAML error: unsupported value type, not int64 or string")
}

func (s *StrInt64) MarshalJSON() ([]byte, error) {
ss := strconv.FormatInt(int64(*s), 10)
return json.Marshal(ss)
Expand All @@ -22,12 +52,12 @@ func (s *StrInt64) UnmarshalJSON(body []byte) error {

switch ta := arg.(type) {
case string:
// parse string
i, err := strconv.ParseInt(ta, 10, 64)
s2, err := NewStrInt64FromString(ta)
if err != nil {
return err
}
*s = StrInt64(i)

*s = StrInt64(s2)

case int64:
*s = StrInt64(ta)
Expand Down

0 comments on commit 10968e0

Please sign in to comment.