This repository has been archived by the owner on Nov 8, 2017. It is now read-only.
forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scalars.go
177 lines (164 loc) · 3.45 KB
/
scalars.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package graphql
import (
"fmt"
"strconv"
"github.com/chris-ramon/graphql/language/ast"
)
var (
MaxInt = 9007199254740991
MinInt = -9007199254740991
)
func coerceInt(value interface{}) interface{} {
switch value := value.(type) {
case bool:
if value == true {
return int(1)
}
return int(0)
case int:
return value
case float32:
return intOrNil(int(value))
case float64:
return intOrNil(int(value))
case string:
val, err := strconv.ParseFloat(value, 0)
if err != nil {
return nil
}
return coerceInt(val)
}
return int(0)
}
// Integers are only safe when between -(2^53 - 1) and 2^53 - 1 due to being
// encoded in JavaScript and represented in JSON as double-precision floating
// point numbers, as specified by IEEE 754.
func intOrNil(value int) interface{} {
if value <= MaxInt && value >= MinInt {
return value
}
return nil
}
var Int *Scalar = NewScalar(ScalarConfig{
Name: "Int",
Serialize: coerceInt,
ParseValue: coerceInt,
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 coerceFloat32(value interface{}) interface{} {
switch value := value.(type) {
case bool:
if value == true {
return float32(1)
}
return float32(0)
case int:
return float32(value)
case float32:
return value
case float64:
return float32(value)
case string:
val, err := strconv.ParseFloat(value, 0)
if err != nil {
return nil
}
return coerceFloat32(val)
}
return float32(0)
}
var Float *Scalar = NewScalar(ScalarConfig{
Name: "Float",
Serialize: coerceFloat32,
ParseValue: coerceFloat32,
ParseLiteral: func(valueAST ast.Value) interface{} {
switch valueAST := valueAST.(type) {
case *ast.FloatValue:
if floatValue, err := strconv.ParseFloat(valueAST.Value, 32); err == nil {
return floatValue
}
case *ast.IntValue:
if floatValue, err := strconv.ParseFloat(valueAST.Value, 32); err == nil {
return floatValue
}
}
return float32(0)
},
})
func coerceString(value interface{}) interface{} {
return fmt.Sprintf("%v", value)
}
var String *Scalar = NewScalar(ScalarConfig{
Name: "String",
Serialize: coerceString,
ParseValue: coerceString,
ParseLiteral: func(valueAST ast.Value) interface{} {
switch valueAST := valueAST.(type) {
case *ast.StringValue:
return valueAST.Value
}
return ""
},
})
func coerceBool(value interface{}) interface{} {
switch value := value.(type) {
case bool:
return value
case string:
switch value {
case "", "false":
return false
}
return true
case float64:
if value != 0 {
return true
}
return false
case float32:
if value != 0 {
return true
}
return false
case int:
if value != 0 {
return true
}
return false
}
return false
}
var Boolean *Scalar = NewScalar(ScalarConfig{
Name: "Boolean",
Serialize: coerceBool,
ParseValue: coerceBool,
ParseLiteral: func(valueAST ast.Value) interface{} {
switch valueAST := valueAST.(type) {
case *ast.BooleanValue:
return valueAST.Value
}
return false
},
})
var ID *Scalar = NewScalar(ScalarConfig{
Name: "ID",
Serialize: coerceString,
ParseValue: coerceString,
ParseLiteral: func(valueAST ast.Value) interface{} {
switch valueAST := valueAST.(type) {
case *ast.IntValue:
return valueAST.Value
case *ast.StringValue:
return valueAST.Value
}
return ""
},
})