From 9c6de12e19d38fe35ff099642dfb4cffbe88cb65 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 18 May 2023 17:32:15 +0800 Subject: [PATCH 1/2] types: add StrInt64 type for unmarshalling integer in string --- pkg/types/strint.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 pkg/types/strint.go diff --git a/pkg/types/strint.go b/pkg/types/strint.go new file mode 100644 index 000000000..c69a69507 --- /dev/null +++ b/pkg/types/strint.go @@ -0,0 +1,38 @@ +package types + +import ( + "encoding/json" + "fmt" + "strconv" +) + +type StrInt64 int64 + +func (s *StrInt64) UnmarshalJSON(body []byte) error { + var arg interface{} + if err := json.Unmarshal(body, &arg); err != nil { + return err + } + + switch ta := arg.(type) { + case string: + // parse string + i, err := strconv.ParseInt(ta, 10, 64) + if err != nil { + return err + } + *s = StrInt64(i) + + case int64: + *s = StrInt64(ta) + case int32: + *s = StrInt64(ta) + case int: + *s = StrInt64(ta) + + default: + return fmt.Errorf("StrInt64 error: unsupported value type %T", ta) + } + + return nil +} From 2fe915f73a7ad437ac019fb880d09f430831cf62 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 18 May 2023 18:08:40 +0800 Subject: [PATCH 2/2] types: add MarshalJSON method on strint64 --- .../bitget/bitgetapi/get_account_request.go | 18 +++++++++++------- .../bitget/bitgetapi/get_fills_request.go | 6 +++--- .../bitgetapi/get_order_detail_request.go | 4 ++-- pkg/types/strint.go | 5 +++++ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/pkg/exchange/bitget/bitgetapi/get_account_request.go b/pkg/exchange/bitget/bitgetapi/get_account_request.go index a3ad8743a..713eb13d7 100644 --- a/pkg/exchange/bitget/bitgetapi/get_account_request.go +++ b/pkg/exchange/bitget/bitgetapi/get_account_request.go @@ -3,15 +3,19 @@ package bitgetapi //go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data //go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data -import "github.com/c9s/requestgen" +import ( + "github.com/c9s/requestgen" + + "github.com/c9s/bbgo/pkg/types" +) type Account struct { - UserId string `json:"user_id"` - InviterId string `json:"inviter_id"` - Ips string `json:"ips"` - Authorities []string `json:"authorities"` - ParentId string `json:"parentId"` - Trader bool `json:"trader"` + UserId types.StrInt64 `json:"user_id"` + InviterId types.StrInt64 `json:"inviter_id"` + Ips string `json:"ips"` + Authorities []string `json:"authorities"` + ParentId types.StrInt64 `json:"parentId"` + Trader bool `json:"trader"` } //go:generate GetRequest -url "/api/spot/v1/account/getInfo" -type GetAccountRequest -responseDataType .Account diff --git a/pkg/exchange/bitget/bitgetapi/get_fills_request.go b/pkg/exchange/bitget/bitgetapi/get_fills_request.go index 2d80b7675..0172f0cfb 100644 --- a/pkg/exchange/bitget/bitgetapi/get_fills_request.go +++ b/pkg/exchange/bitget/bitgetapi/get_fills_request.go @@ -11,10 +11,10 @@ import ( ) type Fill struct { - AccountId string `json:"accountId"` + AccountId types.StrInt64 `json:"accountId"` Symbol string `json:"symbol"` - OrderId string `json:"orderId"` - FillId string `json:"fillId"` + OrderId types.StrInt64 `json:"orderId"` + FillId types.StrInt64 `json:"fillId"` OrderType OrderType `json:"orderType"` Side OrderSide `json:"side"` FillPrice fixedpoint.Value `json:"fillPrice"` diff --git a/pkg/exchange/bitget/bitgetapi/get_order_detail_request.go b/pkg/exchange/bitget/bitgetapi/get_order_detail_request.go index 4f7e09b32..aa5cca0a8 100644 --- a/pkg/exchange/bitget/bitgetapi/get_order_detail_request.go +++ b/pkg/exchange/bitget/bitgetapi/get_order_detail_request.go @@ -11,9 +11,9 @@ import ( ) type OrderDetail struct { - AccountId string `json:"accountId"` + AccountId types.StrInt64 `json:"accountId"` Symbol string `json:"symbol"` - OrderId string `json:"orderId"` + OrderId types.StrInt64 `json:"orderId"` ClientOrderId string `json:"clientOrderId"` Price fixedpoint.Value `json:"price"` Quantity fixedpoint.Value `json:"quantity"` diff --git a/pkg/types/strint.go b/pkg/types/strint.go index c69a69507..c5270d525 100644 --- a/pkg/types/strint.go +++ b/pkg/types/strint.go @@ -8,6 +8,11 @@ import ( type StrInt64 int64 +func (s *StrInt64) MarshalJSON() ([]byte, error) { + ss := strconv.FormatInt(int64(*s), 10) + return json.Marshal(ss) +} + func (s *StrInt64) UnmarshalJSON(body []byte) error { var arg interface{} if err := json.Unmarshal(body, &arg); err != nil {