Merge pull request #1171 from c9s/feature/bitget

FEATURE: Add types.StrInt64 for decoding integer in JSON string format
This commit is contained in:
Yo-An Lin 2023-05-18 18:19:12 +08:00 committed by GitHub
commit df842a04ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 12 deletions

View File

@ -3,14 +3,18 @@ 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"`
UserId types.StrInt64 `json:"user_id"`
InviterId types.StrInt64 `json:"inviter_id"`
Ips string `json:"ips"`
Authorities []string `json:"authorities"`
ParentId string `json:"parentId"`
ParentId types.StrInt64 `json:"parentId"`
Trader bool `json:"trader"`
}

View File

@ -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"`

View File

@ -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"`

43
pkg/types/strint.go Normal file
View File

@ -0,0 +1,43 @@
package types
import (
"encoding/json"
"fmt"
"strconv"
)
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 {
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
}