Created
January 31, 2017 03:26
-
-
Save bbakerman/9bdb1cf835735113b83e16d64547bf49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package graphql; | |
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; | |
/** | |
* The standard numerical scalars in {@link Scalars} are strict in implementation and hence a Double | |
* passed into a Integer type will not be coerced into that type, whereas these implementations are more lenient | |
* and will coerce {@link Number}s into the corresponding scalar values. | |
*/ | |
@SuppressWarnings("Duplicates") | |
public class LenientNumericalScalars { | |
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); | |
public static GraphQLScalarType GraphQLInt = new GraphQLScalarType("Int", "Built-in Int", new Coercing<Integer>() { | |
@Override | |
public Integer serialize(Object input) { | |
if (input instanceof String) { | |
return Integer.parseInt((String) input); | |
} else if (input instanceof Integer) { | |
return (Integer) input; | |
} else if (input instanceof Number) { | |
return ((Number) 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>() { | |
@Override | |
public Long serialize(Object input) { | |
if (input instanceof String) { | |
return Long.parseLong((String) input); | |
} else if (input instanceof Long) { | |
return (Long) input; | |
} else if (input instanceof Integer) { | |
return ((Integer) input).longValue(); | |
} else if (input instanceof Number) { | |
return ((Number) 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 GraphQLFloat = new GraphQLScalarType("Float", "Built-in Float", new Coercing<Double>() { | |
@Override | |
public Double serialize(Object input) { | |
if (input instanceof String) { | |
return Double.parseDouble((String) input); | |
} else if (input instanceof Double) { | |
return (Double) input; | |
} else if (input instanceof Float) { | |
return (double) (Float) input; | |
} else if (input instanceof Integer) { | |
return (double) (Integer) input; | |
} else if (input instanceof Number) { | |
return ((Number) 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>() { | |
@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 (input instanceof Integer) { | |
return BigInteger.valueOf((Integer) input); | |
} else if (input instanceof Long) { | |
return BigInteger.valueOf((Long) input); | |
} else if (input instanceof Number) { | |
return BigInteger.valueOf(((Number) 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>() { | |
@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 (input instanceof Float) { | |
return BigDecimal.valueOf((Float) input); | |
} else if (input instanceof Double) { | |
return BigDecimal.valueOf((Double) input); | |
} else if (input instanceof Integer) { | |
return BigDecimal.valueOf((Integer) input); | |
} else if (input instanceof Long) { | |
return BigDecimal.valueOf((Long) input); | |
} else if (input instanceof Number) { | |
return BigDecimal.valueOf(((Number) input).longValue()); | |
} 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 GraphQLByte = new GraphQLScalarType("Byte", "Built-in Byte as Int", new Coercing<Byte>() { | |
@Override | |
public Byte serialize(Object input) { | |
if (input instanceof String) { | |
return Byte.parseByte((String) input); | |
} else if (input instanceof Byte) { | |
return (Byte) input; | |
} else if (input instanceof Number) { | |
return ((Number) 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 GraphQLShort = new GraphQLScalarType("Short", "Built-in Short as Int", new Coercing<Short>() { | |
@Override | |
public Short serialize(Object input) { | |
if (input instanceof String) { | |
return Short.parseShort((String) input); | |
} else if (input instanceof Short) { | |
return (Short) input; | |
} else if (input instanceof Number) { | |
return ((Number) 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(); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment