forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScalars.java
More file actions
372 lines (324 loc) · 13.2 KB
/
Scalars.java
File metadata and controls
372 lines (324 loc) · 13.2 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package graphql;
import graphql.language.BooleanValue;
import graphql.language.FloatValue;
import graphql.language.IntValue;
import graphql.language.StringValue;
import graphql.schema.Coercing;
import graphql.schema.GraphQLScalarType;
import java.math.BigDecimal;
import java.math.BigInteger;
public class Scalars {
private static final BigInteger LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE);
private static final BigInteger LONG_MIN = BigInteger.valueOf(Long.MIN_VALUE);
private static final BigInteger INT_MAX = BigInteger.valueOf(Integer.MAX_VALUE);
private static final BigInteger INT_MIN = BigInteger.valueOf(Integer.MIN_VALUE);
private static final BigInteger BYTE_MAX = BigInteger.valueOf(Byte.MAX_VALUE);
private static final BigInteger BYTE_MIN = BigInteger.valueOf(Byte.MIN_VALUE);
private static final BigInteger SHORT_MAX = BigInteger.valueOf(Short.MAX_VALUE);
private static final BigInteger SHORT_MIN = BigInteger.valueOf(Short.MIN_VALUE);
private static boolean isWholeNumber(Object input) {
return input instanceof Long
|| input instanceof Integer
|| input instanceof Short
|| input instanceof Byte;
}
// true if its a number or string that we will attempt to convert to a number via toNumber()
private static boolean isNumberIsh(Object input) {
return input instanceof Number || input instanceof String;
}
private static Number toNumber(Object input) {
if (input instanceof Number) {
return (Number) input;
}
if (input instanceof String) {
// we go to double and then let each scalar type decide what precision they want from it. This
// will allow lenient behavior in string input as well as Number input... eg "42.3" as a string to a Long
// scalar is the same as new Double(42.3) to a Long scalar.
//
// each type will use Java Narrow casting to turn this into the desired type (Long, Integer, Short etc...)
//
// See http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3
//
return Double.parseDouble((String) input);
}
// we never expect this and if we do, the code is wired wrong
throw new AssertException("Unexpected case - this call should be protected by a previous call to isNumberIsh()");
}
public static GraphQLScalarType GraphQLInt = new GraphQLScalarType("Int", "Built-in Int", new Coercing<Integer, Integer>() {
@Override
public Integer serialize(Object input) {
if (input instanceof Integer) {
return (Integer) input;
} else if (isNumberIsh(input)) {
return toNumber(input).intValue();
} else {
return null;
}
}
@Override
public Integer parseValue(Object input) {
return serialize(input);
}
@Override
public Integer parseLiteral(Object input) {
if (!(input instanceof IntValue)) return null;
BigInteger value = ((IntValue) input).getValue();
if (value.compareTo(INT_MIN) == -1 || value.compareTo(INT_MAX) == 1) {
throw new GraphQLException("Int literal is too big or too small");
}
return value.intValue();
}
});
public static GraphQLScalarType GraphQLLong = new GraphQLScalarType("Long", "Long type", new Coercing<Long, Long>() {
@Override
public Long serialize(Object input) {
if (input instanceof Long) {
return (Long) input;
} else if (isNumberIsh(input)) {
return toNumber(input).longValue();
} else {
return null;
}
}
@Override
public Long parseValue(Object input) {
return serialize(input);
}
@Override
public Long parseLiteral(Object input) {
if (input instanceof StringValue) {
return Long.parseLong(((StringValue) input).getValue());
} else if (input instanceof IntValue) {
BigInteger value = ((IntValue) input).getValue();
// Check if out of bounds.
if (value.compareTo(LONG_MIN) < 0 || value.compareTo(LONG_MAX) > 0) {
throw new GraphQLException("Int literal is too big or too small for a long, would cause overflow");
}
return value.longValue();
}
return null;
}
});
public static GraphQLScalarType GraphQLShort = new GraphQLScalarType("Short", "Built-in Short as Int", new Coercing<Short, Short>() {
@Override
public Short serialize(Object input) {
if (input instanceof Short) {
return (Short) input;
} else if (isNumberIsh(input)) {
return toNumber(input).shortValue();
} else {
return null;
}
}
@Override
public Short parseValue(Object input) {
return serialize(input);
}
@Override
public Short parseLiteral(Object input) {
if (!(input instanceof IntValue)) return null;
BigInteger value = ((IntValue) input).getValue();
if (value.compareTo(SHORT_MIN) < 0 || value.compareTo(SHORT_MAX) > 0) {
throw new GraphQLException("Int literal is too big or too small for a short, would cause overflow");
}
return value.shortValue();
}
});
public static GraphQLScalarType GraphQLByte = new GraphQLScalarType("Byte", "Built-in Byte as Int", new Coercing<Byte, Byte>() {
@Override
public Byte serialize(Object input) {
if (input instanceof Byte) {
return (Byte) input;
} else if (isNumberIsh(input)) {
return toNumber(input).byteValue();
} else {
return null;
}
}
@Override
public Byte parseValue(Object input) {
return serialize(input);
}
@Override
public Byte parseLiteral(Object input) {
if (!(input instanceof IntValue)) return null;
BigInteger value = ((IntValue) input).getValue();
if (value.compareTo(BYTE_MIN) < 0 || value.compareTo(BYTE_MAX) > 0) {
throw new GraphQLException("Int literal is too big or too small for a byte, would cause overflow");
}
return value.byteValue();
}
});
public static GraphQLScalarType GraphQLFloat = new GraphQLScalarType("Float", "Built-in Float", new Coercing<Double, Double>() {
@Override
public Double serialize(Object input) {
if (input instanceof Double) {
return (Double) input;
} else if (isNumberIsh(input)) {
return toNumber(input).doubleValue();
} else {
return null;
}
}
@Override
public Double parseValue(Object input) {
return serialize(input);
}
@Override
public Double parseLiteral(Object input) {
if (input instanceof IntValue) {
return ((IntValue) input).getValue().doubleValue();
} else if (input instanceof FloatValue) {
return ((FloatValue) input).getValue().doubleValue();
} else {
return null;
}
}
});
public static GraphQLScalarType GraphQLBigInteger = new GraphQLScalarType("BigInteger", "Built-in java.math.BigInteger", new Coercing<BigInteger, BigInteger>() {
@Override
public BigInteger serialize(Object input) {
if (input instanceof BigInteger) {
return (BigInteger) input;
} else if (input instanceof String) {
return new BigInteger((String) input);
} else if (isNumberIsh(input)) {
return BigInteger.valueOf(toNumber(input).longValue());
} else {
return null;
}
}
@Override
public BigInteger parseValue(Object input) {
return serialize(input);
}
@Override
public BigInteger parseLiteral(Object input) {
if (input instanceof StringValue) {
return new BigInteger(((StringValue) input).getValue());
} else if (input instanceof IntValue) {
return ((IntValue) input).getValue();
}
return null;
}
});
public static GraphQLScalarType GraphQLBigDecimal = new GraphQLScalarType("BigDecimal", "Built-in java.math.BigDecimal", new Coercing<BigDecimal, BigDecimal>() {
@Override
public BigDecimal serialize(Object input) {
if (input instanceof BigDecimal) {
return (BigDecimal) input;
} else if (input instanceof String) {
return new BigDecimal((String) input);
} else if (isWholeNumber(input)) {
return BigDecimal.valueOf(toNumber(input).longValue());
} else if (input instanceof Number) {
return BigDecimal.valueOf(toNumber(input).doubleValue());
} else {
return null;
}
}
@Override
public BigDecimal parseValue(Object input) {
return serialize(input);
}
@Override
public BigDecimal parseLiteral(Object input) {
if (input instanceof StringValue) {
return new BigDecimal(((StringValue) input).getValue());
} else if (input instanceof IntValue) {
return new BigDecimal(((IntValue) input).getValue());
} else if (input instanceof FloatValue) {
return ((FloatValue) input).getValue();
}
return null;
}
});
public static GraphQLScalarType GraphQLString = new GraphQLScalarType("String", "Built-in String", new Coercing<String,String>() {
@Override
public String serialize(Object input) {
return input == null ? null : input.toString();
}
@Override
public String parseValue(Object input) {
return serialize(input);
}
@Override
public String parseLiteral(Object input) {
if (!(input instanceof StringValue)) return null;
return ((StringValue) input).getValue();
}
});
public static GraphQLScalarType GraphQLBoolean = new GraphQLScalarType("Boolean", "Built-in Boolean", new Coercing<Boolean,Boolean>() {
@Override
public Boolean serialize(Object input) {
if (input instanceof Boolean) {
return (Boolean) input;
} else if (input instanceof Integer) {
return (Integer) input > 0;
} else if (input instanceof String) {
return Boolean.parseBoolean((String) input);
} else {
return null;
}
}
@Override
public Boolean parseValue(Object input) {
return serialize(input);
}
@Override
public Boolean parseLiteral(Object input) {
if (!(input instanceof BooleanValue)) return null;
return ((BooleanValue) input).isValue();
}
});
public static GraphQLScalarType GraphQLID = new GraphQLScalarType("ID", "Built-in ID", new Coercing<Object,Object>() {
@Override
public Object serialize(Object input) {
if (input instanceof String) {
return input;
}
if (input instanceof Integer) {
return String.valueOf(input);
}
return null;
}
@Override
public Object parseValue(Object input) {
return serialize(input);
}
@Override
public Object parseLiteral(Object input) {
if (input instanceof StringValue) {
return ((StringValue) input).getValue();
}
if (input instanceof IntValue) {
return ((IntValue) input).getValue().toString();
}
return null;
}
});
public static GraphQLScalarType GraphQLChar = new GraphQLScalarType("Char", "Built-in Char as Character", new Coercing<Character,Character>() {
@Override
public Character serialize(Object input) {
if (input instanceof String) {
return ((String) input).length() != 1 ?
null : ((String) input).charAt(0);
} else if (input instanceof Character) {
return (Character) input;
} else {
return null;
}
}
@Override
public Character parseValue(Object input) {
return serialize(input);
}
@Override
public Character parseLiteral(Object input) {
if (!(input instanceof StringValue)) return null;
String value = ((StringValue) input).getValue();
if (value == null || value.length() != 1) return null;
return value.charAt(0);
}
});
}