-
Notifications
You must be signed in to change notification settings - Fork 1
/
bigint.go
61 lines (50 loc) · 1.16 KB
/
bigint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package scalar
import (
"encoding/json"
"strconv"
"github.com/hasura/ndc-sdk-go/utils"
)
// BigInt wraps the scalar implementation for big integer,
// with string representation
//
// @scalar BigInt string
type BigInt int64
// NewBigInt creates a BigInt instance
func NewBigInt(value int64) BigInt {
return BigInt(value)
}
// Stringer implements fmt.Stringer interface.
func (bi BigInt) String() string {
return strconv.FormatInt(int64(bi), 10)
}
// MarshalJSON implements json.Marshaler.
func (bi BigInt) MarshalJSON() ([]byte, error) {
return json.Marshal(strconv.FormatInt(int64(bi), 10))
}
// UnmarshalJSON implements json.Unmarshaler.
func (bi *BigInt) UnmarshalJSON(b []byte) error {
var value any
if err := json.Unmarshal(b, &value); err != nil {
return err
}
iValue, err := utils.DecodeNullableInt[int64](value)
if err != nil {
return err
}
if iValue == nil {
return nil
}
*bi = BigInt(*iValue)
return nil
}
// FromValue decode any value to Int64.
func (bi *BigInt) FromValue(value any) error {
iValue, err := utils.DecodeNullableInt[int64](value)
if err != nil {
return err
}
if iValue != nil {
*bi = BigInt(*iValue)
}
return nil
}