mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
types: add StrInt64 type for unmarshalling integer in string
This commit is contained in:
parent
10311f5c93
commit
d7183cb38f
38
pkg/types/strint.go
Normal file
38
pkg/types/strint.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user