Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add types.StrInt64 for decoding integer in JSON string format #1171

Merged
merged 2 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions pkg/exchange/bitget/bitgetapi/get_account_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/exchange/bitget/bitgetapi/get_fills_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/exchange/bitget/bitgetapi/get_order_detail_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
43 changes: 43 additions & 0 deletions pkg/types/strint.go
Original file line number Diff line number Diff line change
@@ -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
}