forked from jalavosus/graphql-go-bigint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigint.go
159 lines (154 loc) · 2.93 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package bigint
import (
"math"
"strconv"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
)
var BigInt = graphql.NewScalar(graphql.ScalarConfig{
Name: "BigInt",
Description: "The `BigInt` scalar type represents non-fractional signed whole numeric. " +
"values. BigInt can represent values between -(2^53) + 1 and 2^53 - 1. ",
Serialize: coerceBigInt,
ParseValue: coerceBigInt,
ParseLiteral: func(valueAST ast.Value) interface{} {
switch valueAST := valueAST.(type) {
case *ast.IntValue:
if intValue, err := strconv.Atoi(valueAST.Value); err == nil {
return intValue
}
}
return nil
},
})
func coerceBigInt(value interface{}) interface{} {
switch value := value.(type) {
case bool:
if value == true {
return 1
}
return 0
case *bool:
if value == nil {
return nil
}
return coerceBigInt(*value)
case int:
if value < int(math.MinInt64) || value > int(math.MaxInt64) {
return nil
}
return value
case *int:
if value == nil {
return nil
}
return coerceBigInt(*value)
case int8:
return int64(value)
case *int8:
if value == nil {
return nil
}
return int64(*value)
case int16:
return int64(value)
case *int16:
if value == nil {
return nil
}
return int64(*value)
case int32:
return int64(value)
case *int32:
if value == nil {
return nil
}
return int64(*value)
case int64:
if value < int64(math.MinInt64) || value > int64(math.MaxInt64) {
return nil
}
return int64(value)
case *int64:
if value == nil {
return nil
}
return coerceBigInt(*value)
case uint:
if value > math.MaxInt64 {
return nil
}
return int(value)
case *uint:
if value == nil {
return nil
}
return coerceBigInt(*value)
case uint8:
return int64(value)
case *uint8:
if value == nil {
return nil
}
return int(*value)
case uint16:
return int(value)
case *uint16:
if value == nil {
return nil
}
return int64(*value)
case uint32:
if uint64(value) > uint64(math.MaxInt64) {
return nil
}
return int64(value)
case *uint32:
if value == nil {
return nil
}
return coerceBigInt(*value)
case uint64:
if value > uint64(math.MaxInt64) {
return nil
}
return int64(value)
case *uint64:
if value == nil {
return nil
}
return coerceBigInt(*value)
case float32:
if value < float32(math.MinInt64) || value > float32(math.MaxInt64) {
return nil
}
return math.RoundToEven(float64(value))
case *float32:
if value == nil {
return nil
}
return coerceBigInt(*value)
case float64:
if value < float64(math.MinInt64) || value > float64(math.MaxInt64) {
return nil
}
return math.RoundToEven(value)
case *float64:
if value == nil {
return nil
}
return coerceBigInt(*value)
case string:
val, err := strconv.ParseFloat(value, 0)
if err != nil {
return nil
}
return coerceBigInt(val)
case *string:
if value == nil {
return nil
}
return coerceBigInt(*value)
}
return nil
}