From cbc58e0495f96eaee40c680543dceedf96582d30 Mon Sep 17 00:00:00 2001 From: Simeon Leatherland Date: Thu, 30 Jan 2020 16:13:29 +1100 Subject: [PATCH 1/4] Adding Java Scalars --- .../java/graphql/scalars/ExtendedScalars.java | 424 ++++++++++++++++++ 1 file changed, 424 insertions(+) diff --git a/src/main/java/graphql/scalars/ExtendedScalars.java b/src/main/java/graphql/scalars/ExtendedScalars.java index 2d15434..2ff0e02 100644 --- a/src/main/java/graphql/scalars/ExtendedScalars.java +++ b/src/main/java/graphql/scalars/ExtendedScalars.java @@ -1,6 +1,9 @@ package graphql.scalars; import graphql.PublicApi; +import graphql.language.FloatValue; +import graphql.language.IntValue; +import graphql.language.StringValue; import graphql.scalars.alias.AliasedScalar; import graphql.scalars.datetime.DateScalar; import graphql.scalars.datetime.DateTimeScalar; @@ -18,8 +21,15 @@ import graphql.scalars.regex.RegexScalar; import graphql.scalars.url.UrlScalar; import graphql.scalars.locale.LocaleScalar; +import graphql.schema.Coercing; +import graphql.schema.CoercingParseLiteralException; +import graphql.schema.CoercingParseValueException; +import graphql.schema.CoercingSerializeException; import graphql.schema.GraphQLScalarType; +import java.math.BigDecimal; +import java.math.BigInteger; + /** * This is the API entry point for all the extended scalars */ @@ -205,4 +215,418 @@ public static RegexScalar.Builder newRegexScalar(String name) { public static AliasedScalar.Builder newAliasedScalar(String name) { return new AliasedScalar.Builder().name(name); } + + + 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 isNumberIsh(Object input) { + return input instanceof Number || input instanceof String; + } + + private static String typeName(Object input) { + if (input == null) { + return "null"; + } + + return input.getClass().getSimpleName(); + } + + /** + * This represents the "Long" type which is a representation of java.lang.Long + */ + public static final GraphQLScalarType GraphQLLong = new GraphQLScalarType("Long", "Long type", new Coercing() { + + private Long convertImpl(Object input) { + if (input instanceof Long) { + return (Long) input; + } else if (isNumberIsh(input)) { + BigDecimal value; + try { + value = new BigDecimal(input.toString()); + } catch (NumberFormatException e) { + return null; + } + try { + return value.longValueExact(); + } catch (ArithmeticException e) { + return null; + } + } else { + return null; + } + + } + + @Override + public Long serialize(Object input) { + Long result = convertImpl(input); + if (result == null) { + throw new CoercingSerializeException( + "Expected type 'Long' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Long parseValue(Object input) { + Long result = convertImpl(input); + if (result == null) { + throw new CoercingParseValueException( + "Expected type 'Long' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Long parseLiteral(Object input) { + if (input instanceof StringValue) { + try { + return Long.parseLong(((StringValue) input).getValue()); + } catch (NumberFormatException e) { + throw new CoercingParseLiteralException( + "Expected value to be a Long but it was '" + String.valueOf(input) + "'" + ); + } + } else if (input instanceof IntValue) { + BigInteger value = ((IntValue) input).getValue(); + if (value.compareTo(LONG_MIN) < 0 || value.compareTo(LONG_MAX) > 0) { + throw new CoercingParseLiteralException( + "Expected value to be in the Long range but it was '" + value.toString() + "'" + ); + } + return value.longValue(); + } + throw new CoercingParseLiteralException( + "Expected AST type 'IntValue' or 'StringValue' but was '" + typeName(input) + "'." + ); + } + }); + + /** + * This represents the "Short" type which is a representation of java.lang.Short + */ + public static final GraphQLScalarType GraphQLShort = new GraphQLScalarType("Short", "Built-in Short as Int", new Coercing() { + + private Short convertImpl(Object input) { + if (input instanceof Short) { + return (Short) input; + } else if (isNumberIsh(input)) { + BigDecimal value; + try { + value = new BigDecimal(input.toString()); + } catch (NumberFormatException e) { + return null; + } + try { + return value.shortValueExact(); + } catch (ArithmeticException e) { + return null; + } + } else { + return null; + } + + } + + @Override + public Short serialize(Object input) { + Short result = convertImpl(input); + if (result == null) { + throw new CoercingSerializeException( + "Expected type 'Short' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Short parseValue(Object input) { + Short result = convertImpl(input); + if (result == null) { + throw new CoercingParseValueException( + "Expected type 'Short' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Short parseLiteral(Object input) { + if (!(input instanceof IntValue)) { + throw new CoercingParseLiteralException( + "Expected AST type 'IntValue' but was '" + typeName(input) + "'." + ); + } + BigInteger value = ((IntValue) input).getValue(); + if (value.compareTo(SHORT_MIN) < 0 || value.compareTo(SHORT_MAX) > 0) { + throw new CoercingParseLiteralException( + "Expected value to be in the Short range but it was '" + value.toString() + "'" + ); + } + return value.shortValue(); + } + }); + + /** + * This represents the "Byte" type which is a representation of java.lang.Byte + */ + public static final GraphQLScalarType GraphQLByte = new GraphQLScalarType("Byte", "Built-in Byte as Int", new Coercing() { + + private Byte convertImpl(Object input) { + if (input instanceof Byte) { + return (Byte) input; + } else if (isNumberIsh(input)) { + BigDecimal value; + try { + value = new BigDecimal(input.toString()); + } catch (NumberFormatException e) { + return null; + } + try { + return value.byteValueExact(); + } catch (ArithmeticException e) { + return null; + } + } else { + return null; + } + + } + + @Override + public Byte serialize(Object input) { + Byte result = convertImpl(input); + if (result == null) { + throw new CoercingSerializeException( + "Expected type 'Byte' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Byte parseValue(Object input) { + Byte result = convertImpl(input); + if (result == null) { + throw new CoercingParseValueException( + "Expected type 'Byte' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Byte parseLiteral(Object input) { + if (!(input instanceof IntValue)) { + throw new CoercingParseLiteralException( + "Expected AST type 'IntValue' but was '" + typeName(input) + "'." + ); + } + BigInteger value = ((IntValue) input).getValue(); + if (value.compareTo(BYTE_MIN) < 0 || value.compareTo(BYTE_MAX) > 0) { + throw new CoercingParseLiteralException( + "Expected value to be in the Byte range but it was '" + value.toString() + "'" + ); + } + return value.byteValue(); + } + }); + + + /** + * This represents the "BigInteger" type which is a representation of java.math.BigInteger + */ + public static final GraphQLScalarType GraphQLBigInteger = new GraphQLScalarType("BigInteger", "Built-in java.math.BigInteger", new Coercing() { + + private BigInteger convertImpl(Object input) { + if (isNumberIsh(input)) { + BigDecimal value; + try { + value = new BigDecimal(input.toString()); + } catch (NumberFormatException e) { + return null; + } + try { + return value.toBigIntegerExact(); + } catch (ArithmeticException e) { + return null; + } + } + return null; + + } + + @Override + public BigInteger serialize(Object input) { + BigInteger result = convertImpl(input); + if (result == null) { + throw new CoercingSerializeException( + "Expected type 'BigInteger' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public BigInteger parseValue(Object input) { + BigInteger result = convertImpl(input); + if (result == null) { + throw new CoercingParseValueException( + "Expected type 'BigInteger' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public BigInteger parseLiteral(Object input) { + if (input instanceof StringValue) { + try { + return new BigDecimal(((StringValue) input).getValue()).toBigIntegerExact(); + } catch (NumberFormatException | ArithmeticException e) { + throw new CoercingParseLiteralException( + "Unable to turn AST input into a 'BigInteger' : '" + String.valueOf(input) + "'" + ); + } + } else if (input instanceof IntValue) { + return ((IntValue) input).getValue(); + } else if (input instanceof FloatValue) { + try { + return ((FloatValue) input).getValue().toBigIntegerExact(); + } catch (ArithmeticException e) { + throw new CoercingParseLiteralException( + "Unable to turn AST input into a 'BigInteger' : '" + String.valueOf(input) + "'" + ); + } + } + throw new CoercingParseLiteralException( + "Expected AST type 'IntValue', 'StringValue' or 'FloatValue' but was '" + typeName(input) + "'." + ); + } + }); + + /** + * This represents the "BigDecimal" type which is a representation of java.math.BigDecimal + */ + public static final GraphQLScalarType GraphQLBigDecimal = new GraphQLScalarType("BigDecimal", "Built-in java.math.BigDecimal", new Coercing() { + + private BigDecimal convertImpl(Object input) { + if (isNumberIsh(input)) { + try { + return new BigDecimal(input.toString()); + } catch (NumberFormatException e) { + return null; + } + } + return null; + + } + + @Override + public BigDecimal serialize(Object input) { + BigDecimal result = convertImpl(input); + if (result == null) { + throw new CoercingSerializeException( + "Expected type 'BigDecimal' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public BigDecimal parseValue(Object input) { + BigDecimal result = convertImpl(input); + if (result == null) { + throw new CoercingParseValueException( + "Expected type 'BigDecimal' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public BigDecimal parseLiteral(Object input) { + if (input instanceof StringValue) { + try { + return new BigDecimal(((StringValue) input).getValue()); + } catch (NumberFormatException e) { + throw new CoercingParseLiteralException( + "Unable to turn AST input into a 'BigDecimal' : '" + String.valueOf(input) + "'" + ); + } + } else if (input instanceof IntValue) { + return new BigDecimal(((IntValue) input).getValue()); + } else if (input instanceof FloatValue) { + return ((FloatValue) input).getValue(); + } + throw new CoercingParseLiteralException( + "Expected AST type 'IntValue', 'StringValue' or 'FloatValue' but was '" + typeName(input) + "'." + ); + } + }); + + + /** + * This represents the "Char" type which is a representation of java.lang.Character + */ + public static final GraphQLScalarType GraphQLChar = new GraphQLScalarType("Char", "Built-in Char as Character", new Coercing() { + + private Character convertImpl(Object input) { + if (input instanceof String && ((String) input).length() == 1) { + return ((String) input).charAt(0); + } else if (input instanceof Character) { + return (Character) input; + } else { + return null; + } + + } + + @Override + public Character serialize(Object input) { + Character result = convertImpl(input); + if (result == null) { + throw new CoercingSerializeException( + "Expected type 'Char' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Character parseValue(Object input) { + Character result = convertImpl(input); + if (result == null) { + throw new CoercingParseValueException( + "Expected type 'Char' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Character parseLiteral(Object input) { + if (!(input instanceof StringValue)) { + throw new CoercingParseLiteralException( + "Expected AST type 'StringValue' but was '" + typeName(input) + "'." + ); + } + String value = ((StringValue) input).getValue(); + if (value.length() != 1) { + throw new CoercingParseLiteralException( + "Empty 'StringValue' provided." + ); + } + return value.charAt(0); + } + }); } From f2ceabc28c9bb7b4900c63d86bea823087e86bc7 Mon Sep 17 00:00:00 2001 From: Simeon Leatherland Date: Thu, 30 Jan 2020 16:25:24 +1100 Subject: [PATCH 2/4] Adding Tests for java scalar types --- .../scalars/ScalarsBigDecimalTest.groovy | 95 ++++++++++++++ .../scalars/ScalarsBigIntegerTest.groovy | 96 ++++++++++++++ .../graphql/scalars/ScalarsByteTest.groovy | 116 +++++++++++++++++ .../graphql/scalars/ScalarsCharTest.groovy | 81 ++++++++++++ .../graphql/scalars/ScalarsLongTest.groovy | 118 ++++++++++++++++++ .../graphql/scalars/ScalarsShortTest.groovy | 115 +++++++++++++++++ 6 files changed, 621 insertions(+) create mode 100644 src/test/groovy/graphql/scalars/ScalarsBigDecimalTest.groovy create mode 100644 src/test/groovy/graphql/scalars/ScalarsBigIntegerTest.groovy create mode 100644 src/test/groovy/graphql/scalars/ScalarsByteTest.groovy create mode 100644 src/test/groovy/graphql/scalars/ScalarsCharTest.groovy create mode 100644 src/test/groovy/graphql/scalars/ScalarsLongTest.groovy create mode 100644 src/test/groovy/graphql/scalars/ScalarsShortTest.groovy diff --git a/src/test/groovy/graphql/scalars/ScalarsBigDecimalTest.groovy b/src/test/groovy/graphql/scalars/ScalarsBigDecimalTest.groovy new file mode 100644 index 0000000..524e96f --- /dev/null +++ b/src/test/groovy/graphql/scalars/ScalarsBigDecimalTest.groovy @@ -0,0 +1,95 @@ +package graphql.scalars + +import graphql.Scalars +import graphql.language.BooleanValue +import graphql.language.FloatValue +import graphql.language.IntValue +import graphql.language.StringValue +import graphql.schema.CoercingParseLiteralException +import graphql.schema.CoercingParseValueException +import graphql.schema.CoercingSerializeException +import spock.lang.Specification +import spock.lang.Unroll + +import java.util.concurrent.atomic.AtomicInteger + +class ScalarsBigDecimalTest extends Specification { + + @Unroll + def "BigDecimal parse literal #literal.value as #result"() { + expect: + Scalars.GraphQLBigDecimal.getCoercing().parseLiteral(literal) == result + + where: + literal | result + new IntValue(12345678910 as BigInteger) | new BigDecimal("12345678910") + new StringValue("12345678911.12") | new BigDecimal("12345678911.12") + new FloatValue(new BigDecimal("42.42")) | new BigDecimal("42.42") + + } + + @Unroll + def "BigDecimal returns null for invalid #literal"() { + when: + Scalars.GraphQLBigDecimal.getCoercing().parseLiteral(literal) + then: + thrown(CoercingParseLiteralException) + + where: + literal | _ + new BooleanValue(true) | _ + new StringValue("not a number") | _ + } + + @Unroll + def "BigDecimal serialize #value into #result (#result.class)"() { + expect: + Scalars.GraphQLBigDecimal.getCoercing().serialize(value) == result + Scalars.GraphQLBigDecimal.getCoercing().parseValue(value) == result + + where: + value | result + "42" | new BigDecimal("42") + "42.123" | new BigDecimal("42.123") + 42.0000d | new BigDecimal("42.000") + new Integer(42) | new BigDecimal("42") + "-1" | new BigDecimal("-1") + new BigInteger(42) | new BigDecimal("42") + new BigDecimal("42") | new BigDecimal("42") + 42.3f | new BigDecimal("42.3") + 42.0d | new BigDecimal("42") + new Byte("42") | new BigDecimal("42") + new Short("42") | new BigDecimal("42") + 1234567l | new BigDecimal("1234567") + new AtomicInteger(42) | new BigDecimal("42") + } + + @Unroll + def "serialize throws exception for invalid input #value"() { + when: + Scalars.GraphQLBigDecimal.getCoercing().serialize(value) + then: + thrown(CoercingSerializeException) + + where: + value | _ + "" | _ + "not a number " | _ + new Object() | _ + } + + @Unroll + def "parseValue throws exception for invalid input #value"() { + when: + Scalars.GraphQLBigDecimal.getCoercing().parseValue(value) + then: + thrown(CoercingParseValueException) + + where: + value | _ + "" | _ + "not a number " | _ + new Object() | _ + } + +} diff --git a/src/test/groovy/graphql/scalars/ScalarsBigIntegerTest.groovy b/src/test/groovy/graphql/scalars/ScalarsBigIntegerTest.groovy new file mode 100644 index 0000000..5f61ee5 --- /dev/null +++ b/src/test/groovy/graphql/scalars/ScalarsBigIntegerTest.groovy @@ -0,0 +1,96 @@ +package graphql.scalars + +import graphql.Scalars +import graphql.language.BooleanValue +import graphql.language.FloatValue +import graphql.language.IntValue +import graphql.language.StringValue +import graphql.schema.CoercingParseLiteralException +import graphql.schema.CoercingParseValueException +import graphql.schema.CoercingSerializeException +import spock.lang.Specification +import spock.lang.Unroll + +import java.util.concurrent.atomic.AtomicInteger + +class ScalarsBigIntegerTest extends Specification { + + @Unroll + def "BigInteger parse literal #literal.value as #result"() { + expect: + Scalars.GraphQLBigInteger.getCoercing().parseLiteral(literal) == result + + where: + literal | result + new IntValue(12345678910 as BigInteger) | new BigInteger("12345678910") + new StringValue("12345678911") | new BigInteger("12345678911") + new FloatValue(new BigDecimal("42")) | new BigInteger("42") + } + + @Unroll + def "BigInteger returns null for invalid #literal"() { + when: + Scalars.GraphQLBigInteger.getCoercing().parseLiteral(literal) + then: + thrown(CoercingParseLiteralException) + + where: + literal | _ + new BooleanValue(true) | _ + new StringValue("42.3") | _ + new FloatValue(new BigDecimal("12.12")) | _ + new StringValue("not a number") | _ + } + + @Unroll + def "BigInteger serialize #value into #result (#result.class)"() { + expect: + Scalars.GraphQLBigInteger.getCoercing().serialize(value) == result + Scalars.GraphQLBigInteger.getCoercing().parseValue(value) == result + + where: + value | result + "42" | new BigInteger("42") + new Integer(42) | new BigInteger("42") + "-1" | new BigInteger("-1") + new BigInteger("42") | new BigInteger("42") + 42.0d | new BigInteger("42") + new Byte("42") | new BigInteger("42") + new Short("42") | new BigInteger("42") + 1234567l | new BigInteger("1234567") + new AtomicInteger(42) | new BigInteger("42") + } + + @Unroll + def "serialize throws exception for invalid input #value"() { + when: + Scalars.GraphQLBigInteger.getCoercing().serialize(value) + then: + thrown(CoercingSerializeException) + + where: + value | _ + "" | _ + "not a number " | _ + new BigDecimal("12.12") | _ + "12.12" | _ + new Object() | _ + } + + @Unroll + def "parseValue throws exception for invalid input #value"() { + when: + Scalars.GraphQLBigInteger.getCoercing().parseValue(value) + then: + thrown(CoercingParseValueException) + + where: + value | _ + "" | _ + "not a number " | _ + new BigDecimal("12.12") | _ + "12.12" | _ + new Object() | _ + } + +} diff --git a/src/test/groovy/graphql/scalars/ScalarsByteTest.groovy b/src/test/groovy/graphql/scalars/ScalarsByteTest.groovy new file mode 100644 index 0000000..5e89778 --- /dev/null +++ b/src/test/groovy/graphql/scalars/ScalarsByteTest.groovy @@ -0,0 +1,116 @@ +package graphql.scalars + +import graphql.Scalars +import graphql.language.FloatValue +import graphql.language.IntValue +import graphql.language.StringValue +import graphql.schema.CoercingParseLiteralException +import graphql.schema.CoercingParseValueException +import graphql.schema.CoercingSerializeException +import spock.lang.Specification +import spock.lang.Unroll + +import java.util.concurrent.atomic.AtomicInteger + +class ScalarsByteTest extends Specification { + + @Unroll + def "Byte parse literal #literal.value as #result"() { + expect: + Scalars.GraphQLByte.getCoercing().parseLiteral(literal) == result + + where: + literal | result + new IntValue(42 as BigInteger) | 42 + new IntValue(Byte.MAX_VALUE as BigInteger) | Byte.MAX_VALUE + new IntValue(Byte.MIN_VALUE as BigInteger) | Byte.MIN_VALUE + + } + + @Unroll + def "Byte returns null for invalid #literal"() { + when: + Scalars.GraphQLByte.getCoercing().parseLiteral(literal) + then: + thrown(CoercingParseLiteralException) + + where: + literal | _ + new IntValue(12345678910 as BigInteger) | _ + new StringValue("-1") | _ + new FloatValue(42.3) | _ + new IntValue(Byte.MAX_VALUE + 1l as BigInteger) | _ + new IntValue(Byte.MIN_VALUE - 1l as BigInteger) | _ + new StringValue("-1") | _ + new FloatValue(42.3) | _ + + } + + @Unroll + def "Byte serialize #value into #result (#result.class)"() { + expect: + Scalars.GraphQLByte.getCoercing().serialize(value) == result + Scalars.GraphQLByte.getCoercing().parseValue(value) == result + + where: + value | result + "42" | 42 + "42.0000" | 42 + 42.0000d | 42 + new Integer(42) | 42 + "-1" | -1 + new BigInteger(42) | 42 + new BigDecimal("42") | 42 + 42.0f | 42 + 42.0d | 42 + new Byte("42") | 42 + new Short("42") | 42 + 123l | 123 + new AtomicInteger(42) | 42 + Byte.MAX_VALUE | Byte.MAX_VALUE + Byte.MIN_VALUE | Byte.MIN_VALUE + } + + @Unroll + def "serialize throws exception for invalid input #value"() { + when: + Scalars.GraphQLByte.getCoercing().serialize(value) + then: + thrown(CoercingSerializeException) + + where: + value | _ + "" | _ + "not a number " | _ + "42.3" | _ + new Long(42345784398534785l) | _ + new Double(42.3) | _ + new Float(42.3) | _ + Byte.MAX_VALUE + 1l | _ + Byte.MIN_VALUE - 1l | _ + new Object() | _ + + } + + @Unroll + def "parseValue throws exception for invalid input #value"() { + when: + Scalars.GraphQLByte.getCoercing().parseValue(value) + then: + thrown(CoercingParseValueException) + + where: + value | _ + "" | _ + "not a number " | _ + "42.3" | _ + new Long(42345784398534785l) | _ + new Double(42.3) | _ + new Float(42.3) | _ + Byte.MAX_VALUE + 1l | _ + Byte.MIN_VALUE - 1l | _ + new Object() | _ + + } + +} diff --git a/src/test/groovy/graphql/scalars/ScalarsCharTest.groovy b/src/test/groovy/graphql/scalars/ScalarsCharTest.groovy new file mode 100644 index 0000000..0354b3b --- /dev/null +++ b/src/test/groovy/graphql/scalars/ScalarsCharTest.groovy @@ -0,0 +1,81 @@ +package graphql.scalars + +import graphql.Scalars +import graphql.language.IntValue +import graphql.language.StringValue +import graphql.schema.CoercingParseLiteralException +import graphql.schema.CoercingParseValueException +import graphql.schema.CoercingSerializeException +import spock.lang.Specification +import spock.lang.Unroll + +class ScalarsCharTest extends Specification { + + @Unroll + def "Char parse literal #literal.value as #result"() { + expect: + Scalars.GraphQLChar.getCoercing().parseLiteral(literal) == result + + where: + literal | result + new StringValue("a") | 'a' + new StringValue("b") | 'b' + + } + + @Unroll + def "Short returns null for invalid #literal"() { + when: + Scalars.GraphQLChar.getCoercing().parseLiteral(literal) + then: + thrown(CoercingParseLiteralException) + + where: + literal | _ + new StringValue("aa") | _ + new IntValue(12 as BigInteger) | _ + } + + @Unroll + def "Short serialize #value into #result (#result.class)"() { + expect: + Scalars.GraphQLChar.getCoercing().serialize(value) == result + Scalars.GraphQLChar.getCoercing().parseValue(value) == result + + where: + value | result + "a" | 'a' + 'z' | 'z' + } + + @Unroll + def "serialize throws exception for invalid input #value"() { + when: + Scalars.GraphQLChar.getCoercing().serialize(value) + then: + thrown(CoercingSerializeException) + + where: + value | _ + "" | _ + "aa" | _ + new Object() | _ + + } + + @Unroll + def "parseValue throws exception for invalid input #value"() { + when: + Scalars.GraphQLChar.getCoercing().parseValue(value) + then: + thrown(CoercingParseValueException) + + where: + value | _ + "" | _ + "aa" | _ + new Object() | _ + + } + +} diff --git a/src/test/groovy/graphql/scalars/ScalarsLongTest.groovy b/src/test/groovy/graphql/scalars/ScalarsLongTest.groovy new file mode 100644 index 0000000..2cf2eee --- /dev/null +++ b/src/test/groovy/graphql/scalars/ScalarsLongTest.groovy @@ -0,0 +1,118 @@ +package graphql.scalars + +import graphql.Scalars +import graphql.language.FloatValue +import graphql.language.IntValue +import graphql.language.StringValue +import graphql.schema.CoercingParseLiteralException +import graphql.schema.CoercingParseValueException +import graphql.schema.CoercingSerializeException +import spock.lang.Shared +import spock.lang.Specification +import spock.lang.Unroll + +import java.util.concurrent.atomic.AtomicInteger + +class ScalarsLongTest extends Specification { + + @Shared + def tooBig = new BigInteger(Long.toString(Long.MAX_VALUE)).add(new BigInteger("1")) + @Shared + def tooSmall = new BigInteger(Long.toString(Long.MIN_VALUE)).subtract(new BigInteger("1")) + + @Unroll + def "Long parse literal #literal.value as #result"() { + expect: + Scalars.GraphQLLong.getCoercing().parseLiteral(literal) == result + + where: + literal | result + new IntValue(42 as BigInteger) | 42 + new IntValue(Long.MAX_VALUE as BigInteger) | Long.MAX_VALUE + new IntValue(Long.MIN_VALUE as BigInteger) | Long.MIN_VALUE + new StringValue("12345678910") | 12345678910 + new StringValue("-1") | -1 + + } + + @Unroll + def "Long returns null for invalid #literal"() { + when: + Scalars.GraphQLLong.getCoercing().parseLiteral(literal) + then: + thrown(CoercingParseLiteralException) + + where: + literal | _ + new StringValue("not a number") | _ + new FloatValue(42.3) | _ + tooBig | null + tooSmall | null + new FloatValue(42.3) | null + } + + @Unroll + def "Long serialize #value into #result (#result.class)"() { + expect: + Scalars.GraphQLLong.getCoercing().serialize(value) == result + Scalars.GraphQLLong.getCoercing().parseValue(value) == result + + where: + value | result + "42" | 42 + "42.0000" | 42 + 42.0000d | 42 + new Integer(42) | 42 + "-1" | -1 + new BigInteger(42) | 42 + new BigDecimal("42") | 42 + 42.0f | 42 + 42.0d | 42 + new Byte("42") | 42 + new Short("42") | 42 + 12345678910l | 12345678910l + new AtomicInteger(42) | 42 + Long.MAX_VALUE | Long.MAX_VALUE + Long.MIN_VALUE | Long.MIN_VALUE + new Long(42345784398534785l) | 42345784398534785l + } + + @Unroll + def "serialize throws exception for invalid input #value"() { + when: + Scalars.GraphQLLong.getCoercing().serialize(value) + then: + thrown(CoercingSerializeException) + + where: + value | _ + "" | _ + "not a number " | _ + "42.3" | _ + new Double(42.3) | _ + new Float(42.3) | _ + tooBig | _ + tooSmall | _ + new Object() | _ + } + + @Unroll + def "parseValue throws exception for invalid input #value"() { + when: + Scalars.GraphQLLong.getCoercing().parseValue(value) + then: + thrown(CoercingParseValueException) + + where: + value | _ + "" | _ + "not a number " | _ + "42.3" | _ + new Double(42.3) | _ + new Float(42.3) | _ + tooBig | _ + tooSmall | _ + new Object() | _ + } + +} diff --git a/src/test/groovy/graphql/scalars/ScalarsShortTest.groovy b/src/test/groovy/graphql/scalars/ScalarsShortTest.groovy new file mode 100644 index 0000000..898db6e --- /dev/null +++ b/src/test/groovy/graphql/scalars/ScalarsShortTest.groovy @@ -0,0 +1,115 @@ +package graphql.scalars + +import graphql.Scalars +import graphql.language.FloatValue +import graphql.language.IntValue +import graphql.language.StringValue +import graphql.schema.CoercingParseLiteralException +import graphql.schema.CoercingParseValueException +import graphql.schema.CoercingSerializeException +import spock.lang.Specification +import spock.lang.Unroll + +import java.util.concurrent.atomic.AtomicInteger + +class ScalarsShortTest extends Specification { + + @Unroll + def "Short parse literal #literal.value as #result"() { + expect: + Scalars.GraphQLShort.getCoercing().parseLiteral(literal) == result + + where: + literal | result + new IntValue(42 as BigInteger) | 42 + new IntValue(Short.MAX_VALUE as BigInteger) | Short.MAX_VALUE + new IntValue(Short.MIN_VALUE as BigInteger) | Short.MIN_VALUE + + } + + @Unroll + def "Short returns null for invalid #literal"() { + when: + Scalars.GraphQLShort.getCoercing().parseLiteral(literal) + then: + thrown(CoercingParseLiteralException) + + where: + literal | _ + new IntValue(12345678910 as BigInteger) | _ + new StringValue("-1") | _ + new FloatValue(42.3) | _ + new IntValue(Short.MAX_VALUE + 1l as BigInteger) | null + new IntValue(Short.MIN_VALUE - 1l as BigInteger) | null + new StringValue("-1") | null + new FloatValue(42.3) | null + } + + @Unroll + def "Short serialize #value into #result (#result.class)"() { + expect: + Scalars.GraphQLShort.getCoercing().serialize(value) == result + Scalars.GraphQLShort.getCoercing().parseValue(value) == result + + where: + value | result + "42" | 42 + "42.0000" | 42 + 42.0000d | 42 + new Integer(42) | 42 + "-1" | -1 + new BigInteger(42) | 42 + new BigDecimal("42") | 42 + 42.0f | 42 + 42.0d | 42 + new Byte("42") | 42 + new Short("42") | 42 + 1234l | 1234 + new AtomicInteger(42) | 42 + Short.MAX_VALUE | Short.MAX_VALUE + Short.MIN_VALUE | Short.MIN_VALUE + } + + @Unroll + def "serialize throws exception for invalid input #value"() { + when: + Scalars.GraphQLShort.getCoercing().serialize(value) + then: + thrown(CoercingSerializeException) + + where: + value | _ + "" | _ + "not a number " | _ + "42.3" | _ + new Long(42345784398534785l) | _ + new Double(42.3) | _ + new Float(42.3) | _ + Short.MAX_VALUE + 1l | _ + Short.MIN_VALUE - 1l | _ + new Object() | _ + + } + + @Unroll + def "parseValue throws exception for invalid input #value"() { + when: + Scalars.GraphQLShort.getCoercing().parseValue(value) + then: + thrown(CoercingParseValueException) + + where: + value | _ + "" | _ + "not a number " | _ + "42.3" | _ + new Long(42345784398534785l) | _ + new Double(42.3) | _ + new Float(42.3) | _ + Short.MAX_VALUE + 1l | _ + Short.MIN_VALUE - 1l | _ + new Object() | _ + + } + +} From 9478eb74a206d6435f1231a1485960f25e667e94 Mon Sep 17 00:00:00 2001 From: Simeon Leatherland Date: Fri, 31 Jan 2020 09:38:19 +1100 Subject: [PATCH 3/4] Moving Scalars to an internal class & Updating tests to use the internal types --- .../java/graphql/scalars/ExtendedScalars.java | 412 +---------------- .../graphql/scalars/java/JavaPrimitives.java | 434 ++++++++++++++++++ .../{ => java}/ScalarsBigDecimalTest.groovy | 14 +- .../{ => java}/ScalarsBigIntegerTest.groovy | 15 +- .../scalars/{ => java}/ScalarsByteTest.groovy | 14 +- .../scalars/{ => java}/ScalarsCharTest.groovy | 14 +- .../scalars/{ => java}/ScalarsLongTest.groovy | 14 +- .../{ => java}/ScalarsShortTest.groovy | 14 +- 8 files changed, 487 insertions(+), 444 deletions(-) create mode 100644 src/main/java/graphql/scalars/java/JavaPrimitives.java rename src/test/groovy/graphql/scalars/{ => java}/ScalarsBigDecimalTest.groovy (83%) rename src/test/groovy/graphql/scalars/{ => java}/ScalarsBigIntegerTest.groovy (83%) rename src/test/groovy/graphql/scalars/{ => java}/ScalarsByteTest.groovy (87%) rename src/test/groovy/graphql/scalars/{ => java}/ScalarsCharTest.groovy (77%) rename src/test/groovy/graphql/scalars/{ => java}/ScalarsLongTest.groovy (87%) rename src/test/groovy/graphql/scalars/{ => java}/ScalarsShortTest.groovy (87%) diff --git a/src/main/java/graphql/scalars/ExtendedScalars.java b/src/main/java/graphql/scalars/ExtendedScalars.java index 2ff0e02..cb3325f 100644 --- a/src/main/java/graphql/scalars/ExtendedScalars.java +++ b/src/main/java/graphql/scalars/ExtendedScalars.java @@ -1,13 +1,12 @@ package graphql.scalars; import graphql.PublicApi; -import graphql.language.FloatValue; -import graphql.language.IntValue; -import graphql.language.StringValue; import graphql.scalars.alias.AliasedScalar; import graphql.scalars.datetime.DateScalar; import graphql.scalars.datetime.DateTimeScalar; import graphql.scalars.datetime.TimeScalar; +import graphql.scalars.java.JavaPrimitives; +import graphql.scalars.locale.LocaleScalar; import graphql.scalars.numeric.NegativeFloatScalar; import graphql.scalars.numeric.NegativeIntScalar; import graphql.scalars.numeric.NonNegativeFloatScalar; @@ -20,16 +19,8 @@ import graphql.scalars.object.ObjectScalar; import graphql.scalars.regex.RegexScalar; import graphql.scalars.url.UrlScalar; -import graphql.scalars.locale.LocaleScalar; -import graphql.schema.Coercing; -import graphql.schema.CoercingParseLiteralException; -import graphql.schema.CoercingParseValueException; -import graphql.schema.CoercingSerializeException; import graphql.schema.GraphQLScalarType; -import java.math.BigDecimal; -import java.math.BigInteger; - /** * This is the API entry point for all the extended scalars */ @@ -216,417 +207,34 @@ public static AliasedScalar.Builder newAliasedScalar(String name) { return new AliasedScalar.Builder().name(name); } - - 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 isNumberIsh(Object input) { - return input instanceof Number || input instanceof String; - } - - private static String typeName(Object input) { - if (input == null) { - return "null"; - } - - return input.getClass().getSimpleName(); - } - /** * This represents the "Long" type which is a representation of java.lang.Long */ - public static final GraphQLScalarType GraphQLLong = new GraphQLScalarType("Long", "Long type", new Coercing() { - - private Long convertImpl(Object input) { - if (input instanceof Long) { - return (Long) input; - } else if (isNumberIsh(input)) { - BigDecimal value; - try { - value = new BigDecimal(input.toString()); - } catch (NumberFormatException e) { - return null; - } - try { - return value.longValueExact(); - } catch (ArithmeticException e) { - return null; - } - } else { - return null; - } - - } - - @Override - public Long serialize(Object input) { - Long result = convertImpl(input); - if (result == null) { - throw new CoercingSerializeException( - "Expected type 'Long' but was '" + typeName(input) + "'." - ); - } - return result; - } - - @Override - public Long parseValue(Object input) { - Long result = convertImpl(input); - if (result == null) { - throw new CoercingParseValueException( - "Expected type 'Long' but was '" + typeName(input) + "'." - ); - } - return result; - } - - @Override - public Long parseLiteral(Object input) { - if (input instanceof StringValue) { - try { - return Long.parseLong(((StringValue) input).getValue()); - } catch (NumberFormatException e) { - throw new CoercingParseLiteralException( - "Expected value to be a Long but it was '" + String.valueOf(input) + "'" - ); - } - } else if (input instanceof IntValue) { - BigInteger value = ((IntValue) input).getValue(); - if (value.compareTo(LONG_MIN) < 0 || value.compareTo(LONG_MAX) > 0) { - throw new CoercingParseLiteralException( - "Expected value to be in the Long range but it was '" + value.toString() + "'" - ); - } - return value.longValue(); - } - throw new CoercingParseLiteralException( - "Expected AST type 'IntValue' or 'StringValue' but was '" + typeName(input) + "'." - ); - } - }); + public static final GraphQLScalarType GraphQLLong = JavaPrimitives.GraphQLLong; /** * This represents the "Short" type which is a representation of java.lang.Short */ - public static final GraphQLScalarType GraphQLShort = new GraphQLScalarType("Short", "Built-in Short as Int", new Coercing() { - - private Short convertImpl(Object input) { - if (input instanceof Short) { - return (Short) input; - } else if (isNumberIsh(input)) { - BigDecimal value; - try { - value = new BigDecimal(input.toString()); - } catch (NumberFormatException e) { - return null; - } - try { - return value.shortValueExact(); - } catch (ArithmeticException e) { - return null; - } - } else { - return null; - } - - } - - @Override - public Short serialize(Object input) { - Short result = convertImpl(input); - if (result == null) { - throw new CoercingSerializeException( - "Expected type 'Short' but was '" + typeName(input) + "'." - ); - } - return result; - } - - @Override - public Short parseValue(Object input) { - Short result = convertImpl(input); - if (result == null) { - throw new CoercingParseValueException( - "Expected type 'Short' but was '" + typeName(input) + "'." - ); - } - return result; - } - - @Override - public Short parseLiteral(Object input) { - if (!(input instanceof IntValue)) { - throw new CoercingParseLiteralException( - "Expected AST type 'IntValue' but was '" + typeName(input) + "'." - ); - } - BigInteger value = ((IntValue) input).getValue(); - if (value.compareTo(SHORT_MIN) < 0 || value.compareTo(SHORT_MAX) > 0) { - throw new CoercingParseLiteralException( - "Expected value to be in the Short range but it was '" + value.toString() + "'" - ); - } - return value.shortValue(); - } - }); + public static final GraphQLScalarType GraphQLShort = JavaPrimitives.GraphQLShort; /** * This represents the "Byte" type which is a representation of java.lang.Byte */ - public static final GraphQLScalarType GraphQLByte = new GraphQLScalarType("Byte", "Built-in Byte as Int", new Coercing() { - - private Byte convertImpl(Object input) { - if (input instanceof Byte) { - return (Byte) input; - } else if (isNumberIsh(input)) { - BigDecimal value; - try { - value = new BigDecimal(input.toString()); - } catch (NumberFormatException e) { - return null; - } - try { - return value.byteValueExact(); - } catch (ArithmeticException e) { - return null; - } - } else { - return null; - } - - } - - @Override - public Byte serialize(Object input) { - Byte result = convertImpl(input); - if (result == null) { - throw new CoercingSerializeException( - "Expected type 'Byte' but was '" + typeName(input) + "'." - ); - } - return result; - } - - @Override - public Byte parseValue(Object input) { - Byte result = convertImpl(input); - if (result == null) { - throw new CoercingParseValueException( - "Expected type 'Byte' but was '" + typeName(input) + "'." - ); - } - return result; - } - - @Override - public Byte parseLiteral(Object input) { - if (!(input instanceof IntValue)) { - throw new CoercingParseLiteralException( - "Expected AST type 'IntValue' but was '" + typeName(input) + "'." - ); - } - BigInteger value = ((IntValue) input).getValue(); - if (value.compareTo(BYTE_MIN) < 0 || value.compareTo(BYTE_MAX) > 0) { - throw new CoercingParseLiteralException( - "Expected value to be in the Byte range but it was '" + value.toString() + "'" - ); - } - return value.byteValue(); - } - }); - + public static final GraphQLScalarType GraphQLByte = JavaPrimitives.GraphQLByte; /** - * This represents the "BigInteger" type which is a representation of java.math.BigInteger + * This represents the "BigDecimal" type which is a representation of java.math.BigDecimal */ - public static final GraphQLScalarType GraphQLBigInteger = new GraphQLScalarType("BigInteger", "Built-in java.math.BigInteger", new Coercing() { - - private BigInteger convertImpl(Object input) { - if (isNumberIsh(input)) { - BigDecimal value; - try { - value = new BigDecimal(input.toString()); - } catch (NumberFormatException e) { - return null; - } - try { - return value.toBigIntegerExact(); - } catch (ArithmeticException e) { - return null; - } - } - return null; - - } - - @Override - public BigInteger serialize(Object input) { - BigInteger result = convertImpl(input); - if (result == null) { - throw new CoercingSerializeException( - "Expected type 'BigInteger' but was '" + typeName(input) + "'." - ); - } - return result; - } - - @Override - public BigInteger parseValue(Object input) { - BigInteger result = convertImpl(input); - if (result == null) { - throw new CoercingParseValueException( - "Expected type 'BigInteger' but was '" + typeName(input) + "'." - ); - } - return result; - } - - @Override - public BigInteger parseLiteral(Object input) { - if (input instanceof StringValue) { - try { - return new BigDecimal(((StringValue) input).getValue()).toBigIntegerExact(); - } catch (NumberFormatException | ArithmeticException e) { - throw new CoercingParseLiteralException( - "Unable to turn AST input into a 'BigInteger' : '" + String.valueOf(input) + "'" - ); - } - } else if (input instanceof IntValue) { - return ((IntValue) input).getValue(); - } else if (input instanceof FloatValue) { - try { - return ((FloatValue) input).getValue().toBigIntegerExact(); - } catch (ArithmeticException e) { - throw new CoercingParseLiteralException( - "Unable to turn AST input into a 'BigInteger' : '" + String.valueOf(input) + "'" - ); - } - } - throw new CoercingParseLiteralException( - "Expected AST type 'IntValue', 'StringValue' or 'FloatValue' but was '" + typeName(input) + "'." - ); - } - }); + public static final GraphQLScalarType GraphQLBigDecimal = JavaPrimitives.GraphQLBigDecimal; /** - * This represents the "BigDecimal" type which is a representation of java.math.BigDecimal + * This represents the "BigInteger" type which is a representation of java.math.BigInteger */ - public static final GraphQLScalarType GraphQLBigDecimal = new GraphQLScalarType("BigDecimal", "Built-in java.math.BigDecimal", new Coercing() { - - private BigDecimal convertImpl(Object input) { - if (isNumberIsh(input)) { - try { - return new BigDecimal(input.toString()); - } catch (NumberFormatException e) { - return null; - } - } - return null; - - } - - @Override - public BigDecimal serialize(Object input) { - BigDecimal result = convertImpl(input); - if (result == null) { - throw new CoercingSerializeException( - "Expected type 'BigDecimal' but was '" + typeName(input) + "'." - ); - } - return result; - } - - @Override - public BigDecimal parseValue(Object input) { - BigDecimal result = convertImpl(input); - if (result == null) { - throw new CoercingParseValueException( - "Expected type 'BigDecimal' but was '" + typeName(input) + "'." - ); - } - return result; - } - - @Override - public BigDecimal parseLiteral(Object input) { - if (input instanceof StringValue) { - try { - return new BigDecimal(((StringValue) input).getValue()); - } catch (NumberFormatException e) { - throw new CoercingParseLiteralException( - "Unable to turn AST input into a 'BigDecimal' : '" + String.valueOf(input) + "'" - ); - } - } else if (input instanceof IntValue) { - return new BigDecimal(((IntValue) input).getValue()); - } else if (input instanceof FloatValue) { - return ((FloatValue) input).getValue(); - } - throw new CoercingParseLiteralException( - "Expected AST type 'IntValue', 'StringValue' or 'FloatValue' but was '" + typeName(input) + "'." - ); - } - }); - + public static final GraphQLScalarType GraphQLBigInteger = JavaPrimitives.GraphQLBigInteger; /** * This represents the "Char" type which is a representation of java.lang.Character */ - public static final GraphQLScalarType GraphQLChar = new GraphQLScalarType("Char", "Built-in Char as Character", new Coercing() { - - private Character convertImpl(Object input) { - if (input instanceof String && ((String) input).length() == 1) { - return ((String) input).charAt(0); - } else if (input instanceof Character) { - return (Character) input; - } else { - return null; - } - - } - - @Override - public Character serialize(Object input) { - Character result = convertImpl(input); - if (result == null) { - throw new CoercingSerializeException( - "Expected type 'Char' but was '" + typeName(input) + "'." - ); - } - return result; - } - - @Override - public Character parseValue(Object input) { - Character result = convertImpl(input); - if (result == null) { - throw new CoercingParseValueException( - "Expected type 'Char' but was '" + typeName(input) + "'." - ); - } - return result; - } + public static final GraphQLScalarType GraphQLChar = JavaPrimitives.GraphQLChar; - @Override - public Character parseLiteral(Object input) { - if (!(input instanceof StringValue)) { - throw new CoercingParseLiteralException( - "Expected AST type 'StringValue' but was '" + typeName(input) + "'." - ); - } - String value = ((StringValue) input).getValue(); - if (value.length() != 1) { - throw new CoercingParseLiteralException( - "Empty 'StringValue' provided." - ); - } - return value.charAt(0); - } - }); } diff --git a/src/main/java/graphql/scalars/java/JavaPrimitives.java b/src/main/java/graphql/scalars/java/JavaPrimitives.java new file mode 100644 index 0000000..e6d9fff --- /dev/null +++ b/src/main/java/graphql/scalars/java/JavaPrimitives.java @@ -0,0 +1,434 @@ +package graphql.scalars.java; + +import graphql.Internal; +import graphql.language.FloatValue; +import graphql.language.IntValue; +import graphql.language.StringValue; +import graphql.schema.Coercing; +import graphql.schema.CoercingParseLiteralException; +import graphql.schema.CoercingParseValueException; +import graphql.schema.CoercingSerializeException; +import graphql.schema.GraphQLScalarType; + +import java.math.BigDecimal; +import java.math.BigInteger; + +/** + * Access these via {@link graphql.scalars.ExtendedScalars} + */ +@Internal +public class JavaPrimitives { + + 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 isNumberIsh(Object input) { + return input instanceof Number || input instanceof String; + } + + private static String typeName(Object input) { + if (input == null) { + return "null"; + } + + return input.getClass().getSimpleName(); + } + + /** + * This represents the "Long" type which is a representation of java.lang.Long + */ + public static final GraphQLScalarType GraphQLLong = new GraphQLScalarType("Long", "Long type", new Coercing() { + + private Long convertImpl(Object input) { + if (input instanceof Long) { + return (Long) input; + } else if (isNumberIsh(input)) { + BigDecimal value; + try { + value = new BigDecimal(input.toString()); + } catch (NumberFormatException e) { + return null; + } + try { + return value.longValueExact(); + } catch (ArithmeticException e) { + return null; + } + } else { + return null; + } + + } + + @Override + public Long serialize(Object input) { + Long result = convertImpl(input); + if (result == null) { + throw new CoercingSerializeException( + "Expected type 'Long' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Long parseValue(Object input) { + Long result = convertImpl(input); + if (result == null) { + throw new CoercingParseValueException( + "Expected type 'Long' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Long parseLiteral(Object input) { + if (input instanceof StringValue) { + try { + return Long.parseLong(((StringValue) input).getValue()); + } catch (NumberFormatException e) { + throw new CoercingParseLiteralException( + "Expected value to be a Long but it was '" + String.valueOf(input) + "'" + ); + } + } else if (input instanceof IntValue) { + BigInteger value = ((IntValue) input).getValue(); + if (value.compareTo(LONG_MIN) < 0 || value.compareTo(LONG_MAX) > 0) { + throw new CoercingParseLiteralException( + "Expected value to be in the Long range but it was '" + value.toString() + "'" + ); + } + return value.longValue(); + } + throw new CoercingParseLiteralException( + "Expected AST type 'IntValue' or 'StringValue' but was '" + typeName(input) + "'." + ); + } + }); + + /** + * This represents the "Short" type which is a representation of java.lang.Short + */ + public static final GraphQLScalarType GraphQLShort = new GraphQLScalarType("Short", "Built-in Short as Int", new Coercing() { + + private Short convertImpl(Object input) { + if (input instanceof Short) { + return (Short) input; + } else if (isNumberIsh(input)) { + BigDecimal value; + try { + value = new BigDecimal(input.toString()); + } catch (NumberFormatException e) { + return null; + } + try { + return value.shortValueExact(); + } catch (ArithmeticException e) { + return null; + } + } else { + return null; + } + + } + + @Override + public Short serialize(Object input) { + Short result = convertImpl(input); + if (result == null) { + throw new CoercingSerializeException( + "Expected type 'Short' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Short parseValue(Object input) { + Short result = convertImpl(input); + if (result == null) { + throw new CoercingParseValueException( + "Expected type 'Short' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Short parseLiteral(Object input) { + if (!(input instanceof IntValue)) { + throw new CoercingParseLiteralException( + "Expected AST type 'IntValue' but was '" + typeName(input) + "'." + ); + } + BigInteger value = ((IntValue) input).getValue(); + if (value.compareTo(SHORT_MIN) < 0 || value.compareTo(SHORT_MAX) > 0) { + throw new CoercingParseLiteralException( + "Expected value to be in the Short range but it was '" + value.toString() + "'" + ); + } + return value.shortValue(); + } + }); + + /** + * This represents the "Byte" type which is a representation of java.lang.Byte + */ + public static final GraphQLScalarType GraphQLByte = new GraphQLScalarType("Byte", "Built-in Byte as Int", new Coercing() { + + private Byte convertImpl(Object input) { + if (input instanceof Byte) { + return (Byte) input; + } else if (isNumberIsh(input)) { + BigDecimal value; + try { + value = new BigDecimal(input.toString()); + } catch (NumberFormatException e) { + return null; + } + try { + return value.byteValueExact(); + } catch (ArithmeticException e) { + return null; + } + } else { + return null; + } + + } + + @Override + public Byte serialize(Object input) { + Byte result = convertImpl(input); + if (result == null) { + throw new CoercingSerializeException( + "Expected type 'Byte' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Byte parseValue(Object input) { + Byte result = convertImpl(input); + if (result == null) { + throw new CoercingParseValueException( + "Expected type 'Byte' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Byte parseLiteral(Object input) { + if (!(input instanceof IntValue)) { + throw new CoercingParseLiteralException( + "Expected AST type 'IntValue' but was '" + typeName(input) + "'." + ); + } + BigInteger value = ((IntValue) input).getValue(); + if (value.compareTo(BYTE_MIN) < 0 || value.compareTo(BYTE_MAX) > 0) { + throw new CoercingParseLiteralException( + "Expected value to be in the Byte range but it was '" + value.toString() + "'" + ); + } + return value.byteValue(); + } + }); + + + /** + * This represents the "BigInteger" type which is a representation of java.math.BigInteger + */ + public static final GraphQLScalarType GraphQLBigInteger = new GraphQLScalarType("BigInteger", "Built-in java.math.BigInteger", new Coercing() { + + private BigInteger convertImpl(Object input) { + if (isNumberIsh(input)) { + BigDecimal value; + try { + value = new BigDecimal(input.toString()); + } catch (NumberFormatException e) { + return null; + } + try { + return value.toBigIntegerExact(); + } catch (ArithmeticException e) { + return null; + } + } + return null; + + } + + @Override + public BigInteger serialize(Object input) { + BigInteger result = convertImpl(input); + if (result == null) { + throw new CoercingSerializeException( + "Expected type 'BigInteger' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public BigInteger parseValue(Object input) { + BigInteger result = convertImpl(input); + if (result == null) { + throw new CoercingParseValueException( + "Expected type 'BigInteger' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public BigInteger parseLiteral(Object input) { + if (input instanceof StringValue) { + try { + return new BigDecimal(((StringValue) input).getValue()).toBigIntegerExact(); + } catch (NumberFormatException | ArithmeticException e) { + throw new CoercingParseLiteralException( + "Unable to turn AST input into a 'BigInteger' : '" + String.valueOf(input) + "'" + ); + } + } else if (input instanceof IntValue) { + return ((IntValue) input).getValue(); + } else if (input instanceof FloatValue) { + try { + return ((FloatValue) input).getValue().toBigIntegerExact(); + } catch (ArithmeticException e) { + throw new CoercingParseLiteralException( + "Unable to turn AST input into a 'BigInteger' : '" + String.valueOf(input) + "'" + ); + } + } + throw new CoercingParseLiteralException( + "Expected AST type 'IntValue', 'StringValue' or 'FloatValue' but was '" + typeName(input) + "'." + ); + } + }); + + /** + * This represents the "BigDecimal" type which is a representation of java.math.BigDecimal + */ + public static final GraphQLScalarType GraphQLBigDecimal = new GraphQLScalarType("BigDecimal", "Built-in java.math.BigDecimal", new Coercing() { + + private BigDecimal convertImpl(Object input) { + if (isNumberIsh(input)) { + try { + return new BigDecimal(input.toString()); + } catch (NumberFormatException e) { + return null; + } + } + return null; + + } + + @Override + public BigDecimal serialize(Object input) { + BigDecimal result = convertImpl(input); + if (result == null) { + throw new CoercingSerializeException( + "Expected type 'BigDecimal' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public BigDecimal parseValue(Object input) { + BigDecimal result = convertImpl(input); + if (result == null) { + throw new CoercingParseValueException( + "Expected type 'BigDecimal' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public BigDecimal parseLiteral(Object input) { + if (input instanceof StringValue) { + try { + return new BigDecimal(((StringValue) input).getValue()); + } catch (NumberFormatException e) { + throw new CoercingParseLiteralException( + "Unable to turn AST input into a 'BigDecimal' : '" + String.valueOf(input) + "'" + ); + } + } else if (input instanceof IntValue) { + return new BigDecimal(((IntValue) input).getValue()); + } else if (input instanceof FloatValue) { + return ((FloatValue) input).getValue(); + } + throw new CoercingParseLiteralException( + "Expected AST type 'IntValue', 'StringValue' or 'FloatValue' but was '" + typeName(input) + "'." + ); + } + }); + + + /** + * This represents the "Char" type which is a representation of java.lang.Character + */ + public static final GraphQLScalarType GraphQLChar = new GraphQLScalarType("Char", "Built-in Char as Character", new Coercing() { + + private Character convertImpl(Object input) { + if (input instanceof String && ((String) input).length() == 1) { + return ((String) input).charAt(0); + } else if (input instanceof Character) { + return (Character) input; + } else { + return null; + } + + } + + @Override + public Character serialize(Object input) { + Character result = convertImpl(input); + if (result == null) { + throw new CoercingSerializeException( + "Expected type 'Char' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Character parseValue(Object input) { + Character result = convertImpl(input); + if (result == null) { + throw new CoercingParseValueException( + "Expected type 'Char' but was '" + typeName(input) + "'." + ); + } + return result; + } + + @Override + public Character parseLiteral(Object input) { + if (!(input instanceof StringValue)) { + throw new CoercingParseLiteralException( + "Expected AST type 'StringValue' but was '" + typeName(input) + "'." + ); + } + String value = ((StringValue) input).getValue(); + if (value.length() != 1) { + throw new CoercingParseLiteralException( + "Empty 'StringValue' provided." + ); + } + return value.charAt(0); + } + }); +} diff --git a/src/test/groovy/graphql/scalars/ScalarsBigDecimalTest.groovy b/src/test/groovy/graphql/scalars/java/ScalarsBigDecimalTest.groovy similarity index 83% rename from src/test/groovy/graphql/scalars/ScalarsBigDecimalTest.groovy rename to src/test/groovy/graphql/scalars/java/ScalarsBigDecimalTest.groovy index 524e96f..ac26a60 100644 --- a/src/test/groovy/graphql/scalars/ScalarsBigDecimalTest.groovy +++ b/src/test/groovy/graphql/scalars/java/ScalarsBigDecimalTest.groovy @@ -1,4 +1,4 @@ -package graphql.scalars +package graphql.scalars.java import graphql.Scalars import graphql.language.BooleanValue @@ -18,7 +18,7 @@ class ScalarsBigDecimalTest extends Specification { @Unroll def "BigDecimal parse literal #literal.value as #result"() { expect: - Scalars.GraphQLBigDecimal.getCoercing().parseLiteral(literal) == result + JavaPrimitives.GraphQLBigDecimal.getCoercing().parseLiteral(literal) == result where: literal | result @@ -31,7 +31,7 @@ class ScalarsBigDecimalTest extends Specification { @Unroll def "BigDecimal returns null for invalid #literal"() { when: - Scalars.GraphQLBigDecimal.getCoercing().parseLiteral(literal) + JavaPrimitives.GraphQLBigDecimal.getCoercing().parseLiteral(literal) then: thrown(CoercingParseLiteralException) @@ -44,8 +44,8 @@ class ScalarsBigDecimalTest extends Specification { @Unroll def "BigDecimal serialize #value into #result (#result.class)"() { expect: - Scalars.GraphQLBigDecimal.getCoercing().serialize(value) == result - Scalars.GraphQLBigDecimal.getCoercing().parseValue(value) == result + JavaPrimitives.GraphQLBigDecimal.getCoercing().serialize(value) == result + JavaPrimitives.GraphQLBigDecimal.getCoercing().parseValue(value) == result where: value | result @@ -67,7 +67,7 @@ class ScalarsBigDecimalTest extends Specification { @Unroll def "serialize throws exception for invalid input #value"() { when: - Scalars.GraphQLBigDecimal.getCoercing().serialize(value) + JavaPrimitives.GraphQLBigDecimal.getCoercing().serialize(value) then: thrown(CoercingSerializeException) @@ -81,7 +81,7 @@ class ScalarsBigDecimalTest extends Specification { @Unroll def "parseValue throws exception for invalid input #value"() { when: - Scalars.GraphQLBigDecimal.getCoercing().parseValue(value) + JavaPrimitives.GraphQLBigDecimal.getCoercing().parseValue(value) then: thrown(CoercingParseValueException) diff --git a/src/test/groovy/graphql/scalars/ScalarsBigIntegerTest.groovy b/src/test/groovy/graphql/scalars/java/ScalarsBigIntegerTest.groovy similarity index 83% rename from src/test/groovy/graphql/scalars/ScalarsBigIntegerTest.groovy rename to src/test/groovy/graphql/scalars/java/ScalarsBigIntegerTest.groovy index 5f61ee5..32f9719 100644 --- a/src/test/groovy/graphql/scalars/ScalarsBigIntegerTest.groovy +++ b/src/test/groovy/graphql/scalars/java/ScalarsBigIntegerTest.groovy @@ -1,10 +1,11 @@ -package graphql.scalars +package graphql.scalars.java import graphql.Scalars import graphql.language.BooleanValue import graphql.language.FloatValue import graphql.language.IntValue import graphql.language.StringValue +import graphql.scalars.ExtendedScalars import graphql.schema.CoercingParseLiteralException import graphql.schema.CoercingParseValueException import graphql.schema.CoercingSerializeException @@ -18,7 +19,7 @@ class ScalarsBigIntegerTest extends Specification { @Unroll def "BigInteger parse literal #literal.value as #result"() { expect: - Scalars.GraphQLBigInteger.getCoercing().parseLiteral(literal) == result + ExtendedScalars.GraphQLBigInteger.getCoercing().parseLiteral(literal) == result where: literal | result @@ -30,7 +31,7 @@ class ScalarsBigIntegerTest extends Specification { @Unroll def "BigInteger returns null for invalid #literal"() { when: - Scalars.GraphQLBigInteger.getCoercing().parseLiteral(literal) + ExtendedScalars.GraphQLBigInteger.getCoercing().parseLiteral(literal) then: thrown(CoercingParseLiteralException) @@ -45,8 +46,8 @@ class ScalarsBigIntegerTest extends Specification { @Unroll def "BigInteger serialize #value into #result (#result.class)"() { expect: - Scalars.GraphQLBigInteger.getCoercing().serialize(value) == result - Scalars.GraphQLBigInteger.getCoercing().parseValue(value) == result + ExtendedScalars.GraphQLBigInteger.getCoercing().serialize(value) == result + ExtendedScalars.GraphQLBigInteger.getCoercing().parseValue(value) == result where: value | result @@ -64,7 +65,7 @@ class ScalarsBigIntegerTest extends Specification { @Unroll def "serialize throws exception for invalid input #value"() { when: - Scalars.GraphQLBigInteger.getCoercing().serialize(value) + ExtendedScalars.GraphQLBigInteger.getCoercing().serialize(value) then: thrown(CoercingSerializeException) @@ -80,7 +81,7 @@ class ScalarsBigIntegerTest extends Specification { @Unroll def "parseValue throws exception for invalid input #value"() { when: - Scalars.GraphQLBigInteger.getCoercing().parseValue(value) + ExtendedScalars.GraphQLBigInteger.getCoercing().parseValue(value) then: thrown(CoercingParseValueException) diff --git a/src/test/groovy/graphql/scalars/ScalarsByteTest.groovy b/src/test/groovy/graphql/scalars/java/ScalarsByteTest.groovy similarity index 87% rename from src/test/groovy/graphql/scalars/ScalarsByteTest.groovy rename to src/test/groovy/graphql/scalars/java/ScalarsByteTest.groovy index 5e89778..6d680b5 100644 --- a/src/test/groovy/graphql/scalars/ScalarsByteTest.groovy +++ b/src/test/groovy/graphql/scalars/java/ScalarsByteTest.groovy @@ -1,4 +1,4 @@ -package graphql.scalars +package graphql.scalars.java import graphql.Scalars import graphql.language.FloatValue @@ -17,7 +17,7 @@ class ScalarsByteTest extends Specification { @Unroll def "Byte parse literal #literal.value as #result"() { expect: - Scalars.GraphQLByte.getCoercing().parseLiteral(literal) == result + JavaPrimitives.GraphQLByte.getCoercing().parseLiteral(literal) == result where: literal | result @@ -30,7 +30,7 @@ class ScalarsByteTest extends Specification { @Unroll def "Byte returns null for invalid #literal"() { when: - Scalars.GraphQLByte.getCoercing().parseLiteral(literal) + JavaPrimitives.GraphQLByte.getCoercing().parseLiteral(literal) then: thrown(CoercingParseLiteralException) @@ -49,8 +49,8 @@ class ScalarsByteTest extends Specification { @Unroll def "Byte serialize #value into #result (#result.class)"() { expect: - Scalars.GraphQLByte.getCoercing().serialize(value) == result - Scalars.GraphQLByte.getCoercing().parseValue(value) == result + JavaPrimitives.GraphQLByte.getCoercing().serialize(value) == result + JavaPrimitives.GraphQLByte.getCoercing().parseValue(value) == result where: value | result @@ -74,7 +74,7 @@ class ScalarsByteTest extends Specification { @Unroll def "serialize throws exception for invalid input #value"() { when: - Scalars.GraphQLByte.getCoercing().serialize(value) + JavaPrimitives.GraphQLByte.getCoercing().serialize(value) then: thrown(CoercingSerializeException) @@ -95,7 +95,7 @@ class ScalarsByteTest extends Specification { @Unroll def "parseValue throws exception for invalid input #value"() { when: - Scalars.GraphQLByte.getCoercing().parseValue(value) + JavaPrimitives.GraphQLByte.getCoercing().parseValue(value) then: thrown(CoercingParseValueException) diff --git a/src/test/groovy/graphql/scalars/ScalarsCharTest.groovy b/src/test/groovy/graphql/scalars/java/ScalarsCharTest.groovy similarity index 77% rename from src/test/groovy/graphql/scalars/ScalarsCharTest.groovy rename to src/test/groovy/graphql/scalars/java/ScalarsCharTest.groovy index 0354b3b..9444aa4 100644 --- a/src/test/groovy/graphql/scalars/ScalarsCharTest.groovy +++ b/src/test/groovy/graphql/scalars/java/ScalarsCharTest.groovy @@ -1,4 +1,4 @@ -package graphql.scalars +package graphql.scalars.java import graphql.Scalars import graphql.language.IntValue @@ -14,7 +14,7 @@ class ScalarsCharTest extends Specification { @Unroll def "Char parse literal #literal.value as #result"() { expect: - Scalars.GraphQLChar.getCoercing().parseLiteral(literal) == result + JavaPrimitives.GraphQLChar.getCoercing().parseLiteral(literal) == result where: literal | result @@ -26,7 +26,7 @@ class ScalarsCharTest extends Specification { @Unroll def "Short returns null for invalid #literal"() { when: - Scalars.GraphQLChar.getCoercing().parseLiteral(literal) + JavaPrimitives.GraphQLChar.getCoercing().parseLiteral(literal) then: thrown(CoercingParseLiteralException) @@ -39,8 +39,8 @@ class ScalarsCharTest extends Specification { @Unroll def "Short serialize #value into #result (#result.class)"() { expect: - Scalars.GraphQLChar.getCoercing().serialize(value) == result - Scalars.GraphQLChar.getCoercing().parseValue(value) == result + JavaPrimitives.GraphQLChar.getCoercing().serialize(value) == result + JavaPrimitives.GraphQLChar.getCoercing().parseValue(value) == result where: value | result @@ -51,7 +51,7 @@ class ScalarsCharTest extends Specification { @Unroll def "serialize throws exception for invalid input #value"() { when: - Scalars.GraphQLChar.getCoercing().serialize(value) + JavaPrimitives.GraphQLChar.getCoercing().serialize(value) then: thrown(CoercingSerializeException) @@ -66,7 +66,7 @@ class ScalarsCharTest extends Specification { @Unroll def "parseValue throws exception for invalid input #value"() { when: - Scalars.GraphQLChar.getCoercing().parseValue(value) + JavaPrimitives.GraphQLChar.getCoercing().parseValue(value) then: thrown(CoercingParseValueException) diff --git a/src/test/groovy/graphql/scalars/ScalarsLongTest.groovy b/src/test/groovy/graphql/scalars/java/ScalarsLongTest.groovy similarity index 87% rename from src/test/groovy/graphql/scalars/ScalarsLongTest.groovy rename to src/test/groovy/graphql/scalars/java/ScalarsLongTest.groovy index 2cf2eee..03b754f 100644 --- a/src/test/groovy/graphql/scalars/ScalarsLongTest.groovy +++ b/src/test/groovy/graphql/scalars/java/ScalarsLongTest.groovy @@ -1,4 +1,4 @@ -package graphql.scalars +package graphql.scalars.java import graphql.Scalars import graphql.language.FloatValue @@ -23,7 +23,7 @@ class ScalarsLongTest extends Specification { @Unroll def "Long parse literal #literal.value as #result"() { expect: - Scalars.GraphQLLong.getCoercing().parseLiteral(literal) == result + JavaPrimitives.GraphQLLong.getCoercing().parseLiteral(literal) == result where: literal | result @@ -38,7 +38,7 @@ class ScalarsLongTest extends Specification { @Unroll def "Long returns null for invalid #literal"() { when: - Scalars.GraphQLLong.getCoercing().parseLiteral(literal) + JavaPrimitives.GraphQLLong.getCoercing().parseLiteral(literal) then: thrown(CoercingParseLiteralException) @@ -54,8 +54,8 @@ class ScalarsLongTest extends Specification { @Unroll def "Long serialize #value into #result (#result.class)"() { expect: - Scalars.GraphQLLong.getCoercing().serialize(value) == result - Scalars.GraphQLLong.getCoercing().parseValue(value) == result + JavaPrimitives.GraphQLLong.getCoercing().serialize(value) == result + JavaPrimitives.GraphQLLong.getCoercing().parseValue(value) == result where: value | result @@ -80,7 +80,7 @@ class ScalarsLongTest extends Specification { @Unroll def "serialize throws exception for invalid input #value"() { when: - Scalars.GraphQLLong.getCoercing().serialize(value) + JavaPrimitives.GraphQLLong.getCoercing().serialize(value) then: thrown(CoercingSerializeException) @@ -99,7 +99,7 @@ class ScalarsLongTest extends Specification { @Unroll def "parseValue throws exception for invalid input #value"() { when: - Scalars.GraphQLLong.getCoercing().parseValue(value) + JavaPrimitives.GraphQLLong.getCoercing().parseValue(value) then: thrown(CoercingParseValueException) diff --git a/src/test/groovy/graphql/scalars/ScalarsShortTest.groovy b/src/test/groovy/graphql/scalars/java/ScalarsShortTest.groovy similarity index 87% rename from src/test/groovy/graphql/scalars/ScalarsShortTest.groovy rename to src/test/groovy/graphql/scalars/java/ScalarsShortTest.groovy index 898db6e..d619c0a 100644 --- a/src/test/groovy/graphql/scalars/ScalarsShortTest.groovy +++ b/src/test/groovy/graphql/scalars/java/ScalarsShortTest.groovy @@ -1,4 +1,4 @@ -package graphql.scalars +package graphql.scalars.java import graphql.Scalars import graphql.language.FloatValue @@ -17,7 +17,7 @@ class ScalarsShortTest extends Specification { @Unroll def "Short parse literal #literal.value as #result"() { expect: - Scalars.GraphQLShort.getCoercing().parseLiteral(literal) == result + JavaPrimitives.GraphQLShort.getCoercing().parseLiteral(literal) == result where: literal | result @@ -30,7 +30,7 @@ class ScalarsShortTest extends Specification { @Unroll def "Short returns null for invalid #literal"() { when: - Scalars.GraphQLShort.getCoercing().parseLiteral(literal) + JavaPrimitives.GraphQLShort.getCoercing().parseLiteral(literal) then: thrown(CoercingParseLiteralException) @@ -48,8 +48,8 @@ class ScalarsShortTest extends Specification { @Unroll def "Short serialize #value into #result (#result.class)"() { expect: - Scalars.GraphQLShort.getCoercing().serialize(value) == result - Scalars.GraphQLShort.getCoercing().parseValue(value) == result + JavaPrimitives.GraphQLShort.getCoercing().serialize(value) == result + JavaPrimitives.GraphQLShort.getCoercing().parseValue(value) == result where: value | result @@ -73,7 +73,7 @@ class ScalarsShortTest extends Specification { @Unroll def "serialize throws exception for invalid input #value"() { when: - Scalars.GraphQLShort.getCoercing().serialize(value) + JavaPrimitives.GraphQLShort.getCoercing().serialize(value) then: thrown(CoercingSerializeException) @@ -94,7 +94,7 @@ class ScalarsShortTest extends Specification { @Unroll def "parseValue throws exception for invalid input #value"() { when: - Scalars.GraphQLShort.getCoercing().parseValue(value) + JavaPrimitives.GraphQLShort.getCoercing().parseValue(value) then: thrown(CoercingParseValueException) From 4f85f91f62b61afe75556d05ca4ba2eb267e5f02 Mon Sep 17 00:00:00 2001 From: Simeon Leatherland Date: Wed, 5 Feb 2020 16:28:08 +1100 Subject: [PATCH 4/4] Using the ExtendedScalars reference in tests --- .../java/graphql/scalars/java/JavaPrimitives.java | 10 +++++----- .../scalars/java/ScalarsBigDecimalTest.groovy | 14 +++++++------- .../graphql/scalars/java/ScalarsByteTest.groovy | 14 +++++++------- .../graphql/scalars/java/ScalarsCharTest.groovy | 14 +++++++------- .../graphql/scalars/java/ScalarsLongTest.groovy | 14 +++++++------- .../graphql/scalars/java/ScalarsShortTest.groovy | 14 +++++++------- 6 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/main/java/graphql/scalars/java/JavaPrimitives.java b/src/main/java/graphql/scalars/java/JavaPrimitives.java index e6d9fff..9046ae4 100644 --- a/src/main/java/graphql/scalars/java/JavaPrimitives.java +++ b/src/main/java/graphql/scalars/java/JavaPrimitives.java @@ -116,7 +116,7 @@ public Long parseLiteral(Object input) { /** * This represents the "Short" type which is a representation of java.lang.Short */ - public static final GraphQLScalarType GraphQLShort = new GraphQLScalarType("Short", "Built-in Short as Int", new Coercing() { + public static final GraphQLScalarType GraphQLShort = new GraphQLScalarType("Short", "Short as Int", new Coercing() { private Short convertImpl(Object input) { if (input instanceof Short) { @@ -181,7 +181,7 @@ public Short parseLiteral(Object input) { /** * This represents the "Byte" type which is a representation of java.lang.Byte */ - public static final GraphQLScalarType GraphQLByte = new GraphQLScalarType("Byte", "Built-in Byte as Int", new Coercing() { + public static final GraphQLScalarType GraphQLByte = new GraphQLScalarType("Byte", "Byte as Int", new Coercing() { private Byte convertImpl(Object input) { if (input instanceof Byte) { @@ -247,7 +247,7 @@ public Byte parseLiteral(Object input) { /** * This represents the "BigInteger" type which is a representation of java.math.BigInteger */ - public static final GraphQLScalarType GraphQLBigInteger = new GraphQLScalarType("BigInteger", "Built-in java.math.BigInteger", new Coercing() { + public static final GraphQLScalarType GraphQLBigInteger = new GraphQLScalarType("BigInteger", "java.math.BigInteger", new Coercing() { private BigInteger convertImpl(Object input) { if (isNumberIsh(input)) { @@ -319,7 +319,7 @@ public BigInteger parseLiteral(Object input) { /** * This represents the "BigDecimal" type which is a representation of java.math.BigDecimal */ - public static final GraphQLScalarType GraphQLBigDecimal = new GraphQLScalarType("BigDecimal", "Built-in java.math.BigDecimal", new Coercing() { + public static final GraphQLScalarType GraphQLBigDecimal = new GraphQLScalarType("BigDecimal", "java.math.BigDecimal", new Coercing() { private BigDecimal convertImpl(Object input) { if (isNumberIsh(input)) { @@ -380,7 +380,7 @@ public BigDecimal parseLiteral(Object input) { /** * This represents the "Char" type which is a representation of java.lang.Character */ - public static final GraphQLScalarType GraphQLChar = new GraphQLScalarType("Char", "Built-in Char as Character", new Coercing() { + public static final GraphQLScalarType GraphQLChar = new GraphQLScalarType("Char", "Char as Character", new Coercing() { private Character convertImpl(Object input) { if (input instanceof String && ((String) input).length() == 1) { diff --git a/src/test/groovy/graphql/scalars/java/ScalarsBigDecimalTest.groovy b/src/test/groovy/graphql/scalars/java/ScalarsBigDecimalTest.groovy index ac26a60..bbd6c57 100644 --- a/src/test/groovy/graphql/scalars/java/ScalarsBigDecimalTest.groovy +++ b/src/test/groovy/graphql/scalars/java/ScalarsBigDecimalTest.groovy @@ -1,10 +1,10 @@ package graphql.scalars.java -import graphql.Scalars import graphql.language.BooleanValue import graphql.language.FloatValue import graphql.language.IntValue import graphql.language.StringValue +import graphql.scalars.ExtendedScalars import graphql.schema.CoercingParseLiteralException import graphql.schema.CoercingParseValueException import graphql.schema.CoercingSerializeException @@ -18,7 +18,7 @@ class ScalarsBigDecimalTest extends Specification { @Unroll def "BigDecimal parse literal #literal.value as #result"() { expect: - JavaPrimitives.GraphQLBigDecimal.getCoercing().parseLiteral(literal) == result + ExtendedScalars.GraphQLBigDecimal.getCoercing().parseLiteral(literal) == result where: literal | result @@ -31,7 +31,7 @@ class ScalarsBigDecimalTest extends Specification { @Unroll def "BigDecimal returns null for invalid #literal"() { when: - JavaPrimitives.GraphQLBigDecimal.getCoercing().parseLiteral(literal) + ExtendedScalars.GraphQLBigDecimal.getCoercing().parseLiteral(literal) then: thrown(CoercingParseLiteralException) @@ -44,8 +44,8 @@ class ScalarsBigDecimalTest extends Specification { @Unroll def "BigDecimal serialize #value into #result (#result.class)"() { expect: - JavaPrimitives.GraphQLBigDecimal.getCoercing().serialize(value) == result - JavaPrimitives.GraphQLBigDecimal.getCoercing().parseValue(value) == result + ExtendedScalars.GraphQLBigDecimal.getCoercing().serialize(value) == result + ExtendedScalars.GraphQLBigDecimal.getCoercing().parseValue(value) == result where: value | result @@ -67,7 +67,7 @@ class ScalarsBigDecimalTest extends Specification { @Unroll def "serialize throws exception for invalid input #value"() { when: - JavaPrimitives.GraphQLBigDecimal.getCoercing().serialize(value) + ExtendedScalars.GraphQLBigDecimal.getCoercing().serialize(value) then: thrown(CoercingSerializeException) @@ -81,7 +81,7 @@ class ScalarsBigDecimalTest extends Specification { @Unroll def "parseValue throws exception for invalid input #value"() { when: - JavaPrimitives.GraphQLBigDecimal.getCoercing().parseValue(value) + ExtendedScalars.GraphQLBigDecimal.getCoercing().parseValue(value) then: thrown(CoercingParseValueException) diff --git a/src/test/groovy/graphql/scalars/java/ScalarsByteTest.groovy b/src/test/groovy/graphql/scalars/java/ScalarsByteTest.groovy index 6d680b5..9d2dcea 100644 --- a/src/test/groovy/graphql/scalars/java/ScalarsByteTest.groovy +++ b/src/test/groovy/graphql/scalars/java/ScalarsByteTest.groovy @@ -1,6 +1,6 @@ package graphql.scalars.java -import graphql.Scalars +import graphql.scalars.ExtendedScalars import graphql.language.FloatValue import graphql.language.IntValue import graphql.language.StringValue @@ -17,7 +17,7 @@ class ScalarsByteTest extends Specification { @Unroll def "Byte parse literal #literal.value as #result"() { expect: - JavaPrimitives.GraphQLByte.getCoercing().parseLiteral(literal) == result + ExtendedScalars.GraphQLByte.getCoercing().parseLiteral(literal) == result where: literal | result @@ -30,7 +30,7 @@ class ScalarsByteTest extends Specification { @Unroll def "Byte returns null for invalid #literal"() { when: - JavaPrimitives.GraphQLByte.getCoercing().parseLiteral(literal) + ExtendedScalars.GraphQLByte.getCoercing().parseLiteral(literal) then: thrown(CoercingParseLiteralException) @@ -49,8 +49,8 @@ class ScalarsByteTest extends Specification { @Unroll def "Byte serialize #value into #result (#result.class)"() { expect: - JavaPrimitives.GraphQLByte.getCoercing().serialize(value) == result - JavaPrimitives.GraphQLByte.getCoercing().parseValue(value) == result + ExtendedScalars.GraphQLByte.getCoercing().serialize(value) == result + ExtendedScalars.GraphQLByte.getCoercing().parseValue(value) == result where: value | result @@ -74,7 +74,7 @@ class ScalarsByteTest extends Specification { @Unroll def "serialize throws exception for invalid input #value"() { when: - JavaPrimitives.GraphQLByte.getCoercing().serialize(value) + ExtendedScalars.GraphQLByte.getCoercing().serialize(value) then: thrown(CoercingSerializeException) @@ -95,7 +95,7 @@ class ScalarsByteTest extends Specification { @Unroll def "parseValue throws exception for invalid input #value"() { when: - JavaPrimitives.GraphQLByte.getCoercing().parseValue(value) + ExtendedScalars.GraphQLByte.getCoercing().parseValue(value) then: thrown(CoercingParseValueException) diff --git a/src/test/groovy/graphql/scalars/java/ScalarsCharTest.groovy b/src/test/groovy/graphql/scalars/java/ScalarsCharTest.groovy index 9444aa4..7808f28 100644 --- a/src/test/groovy/graphql/scalars/java/ScalarsCharTest.groovy +++ b/src/test/groovy/graphql/scalars/java/ScalarsCharTest.groovy @@ -1,6 +1,6 @@ package graphql.scalars.java -import graphql.Scalars +import graphql.scalars.ExtendedScalars import graphql.language.IntValue import graphql.language.StringValue import graphql.schema.CoercingParseLiteralException @@ -14,7 +14,7 @@ class ScalarsCharTest extends Specification { @Unroll def "Char parse literal #literal.value as #result"() { expect: - JavaPrimitives.GraphQLChar.getCoercing().parseLiteral(literal) == result + ExtendedScalars.GraphQLChar.getCoercing().parseLiteral(literal) == result where: literal | result @@ -26,7 +26,7 @@ class ScalarsCharTest extends Specification { @Unroll def "Short returns null for invalid #literal"() { when: - JavaPrimitives.GraphQLChar.getCoercing().parseLiteral(literal) + ExtendedScalars.GraphQLChar.getCoercing().parseLiteral(literal) then: thrown(CoercingParseLiteralException) @@ -39,8 +39,8 @@ class ScalarsCharTest extends Specification { @Unroll def "Short serialize #value into #result (#result.class)"() { expect: - JavaPrimitives.GraphQLChar.getCoercing().serialize(value) == result - JavaPrimitives.GraphQLChar.getCoercing().parseValue(value) == result + ExtendedScalars.GraphQLChar.getCoercing().serialize(value) == result + ExtendedScalars.GraphQLChar.getCoercing().parseValue(value) == result where: value | result @@ -51,7 +51,7 @@ class ScalarsCharTest extends Specification { @Unroll def "serialize throws exception for invalid input #value"() { when: - JavaPrimitives.GraphQLChar.getCoercing().serialize(value) + ExtendedScalars.GraphQLChar.getCoercing().serialize(value) then: thrown(CoercingSerializeException) @@ -66,7 +66,7 @@ class ScalarsCharTest extends Specification { @Unroll def "parseValue throws exception for invalid input #value"() { when: - JavaPrimitives.GraphQLChar.getCoercing().parseValue(value) + ExtendedScalars.GraphQLChar.getCoercing().parseValue(value) then: thrown(CoercingParseValueException) diff --git a/src/test/groovy/graphql/scalars/java/ScalarsLongTest.groovy b/src/test/groovy/graphql/scalars/java/ScalarsLongTest.groovy index 03b754f..39793f4 100644 --- a/src/test/groovy/graphql/scalars/java/ScalarsLongTest.groovy +++ b/src/test/groovy/graphql/scalars/java/ScalarsLongTest.groovy @@ -1,6 +1,6 @@ package graphql.scalars.java -import graphql.Scalars +import graphql.scalars.ExtendedScalars import graphql.language.FloatValue import graphql.language.IntValue import graphql.language.StringValue @@ -23,7 +23,7 @@ class ScalarsLongTest extends Specification { @Unroll def "Long parse literal #literal.value as #result"() { expect: - JavaPrimitives.GraphQLLong.getCoercing().parseLiteral(literal) == result + ExtendedScalars.GraphQLLong.getCoercing().parseLiteral(literal) == result where: literal | result @@ -38,7 +38,7 @@ class ScalarsLongTest extends Specification { @Unroll def "Long returns null for invalid #literal"() { when: - JavaPrimitives.GraphQLLong.getCoercing().parseLiteral(literal) + ExtendedScalars.GraphQLLong.getCoercing().parseLiteral(literal) then: thrown(CoercingParseLiteralException) @@ -54,8 +54,8 @@ class ScalarsLongTest extends Specification { @Unroll def "Long serialize #value into #result (#result.class)"() { expect: - JavaPrimitives.GraphQLLong.getCoercing().serialize(value) == result - JavaPrimitives.GraphQLLong.getCoercing().parseValue(value) == result + ExtendedScalars.GraphQLLong.getCoercing().serialize(value) == result + ExtendedScalars.GraphQLLong.getCoercing().parseValue(value) == result where: value | result @@ -80,7 +80,7 @@ class ScalarsLongTest extends Specification { @Unroll def "serialize throws exception for invalid input #value"() { when: - JavaPrimitives.GraphQLLong.getCoercing().serialize(value) + ExtendedScalars.GraphQLLong.getCoercing().serialize(value) then: thrown(CoercingSerializeException) @@ -99,7 +99,7 @@ class ScalarsLongTest extends Specification { @Unroll def "parseValue throws exception for invalid input #value"() { when: - JavaPrimitives.GraphQLLong.getCoercing().parseValue(value) + ExtendedScalars.GraphQLLong.getCoercing().parseValue(value) then: thrown(CoercingParseValueException) diff --git a/src/test/groovy/graphql/scalars/java/ScalarsShortTest.groovy b/src/test/groovy/graphql/scalars/java/ScalarsShortTest.groovy index d619c0a..e8ff506 100644 --- a/src/test/groovy/graphql/scalars/java/ScalarsShortTest.groovy +++ b/src/test/groovy/graphql/scalars/java/ScalarsShortTest.groovy @@ -1,9 +1,9 @@ package graphql.scalars.java -import graphql.Scalars import graphql.language.FloatValue import graphql.language.IntValue import graphql.language.StringValue +import graphql.scalars.ExtendedScalars import graphql.schema.CoercingParseLiteralException import graphql.schema.CoercingParseValueException import graphql.schema.CoercingSerializeException @@ -17,7 +17,7 @@ class ScalarsShortTest extends Specification { @Unroll def "Short parse literal #literal.value as #result"() { expect: - JavaPrimitives.GraphQLShort.getCoercing().parseLiteral(literal) == result + ExtendedScalars.GraphQLShort.getCoercing().parseLiteral(literal) == result where: literal | result @@ -30,7 +30,7 @@ class ScalarsShortTest extends Specification { @Unroll def "Short returns null for invalid #literal"() { when: - JavaPrimitives.GraphQLShort.getCoercing().parseLiteral(literal) + ExtendedScalars.GraphQLShort.getCoercing().parseLiteral(literal) then: thrown(CoercingParseLiteralException) @@ -48,8 +48,8 @@ class ScalarsShortTest extends Specification { @Unroll def "Short serialize #value into #result (#result.class)"() { expect: - JavaPrimitives.GraphQLShort.getCoercing().serialize(value) == result - JavaPrimitives.GraphQLShort.getCoercing().parseValue(value) == result + ExtendedScalars.GraphQLShort.getCoercing().serialize(value) == result + ExtendedScalars.GraphQLShort.getCoercing().parseValue(value) == result where: value | result @@ -73,7 +73,7 @@ class ScalarsShortTest extends Specification { @Unroll def "serialize throws exception for invalid input #value"() { when: - JavaPrimitives.GraphQLShort.getCoercing().serialize(value) + ExtendedScalars.GraphQLShort.getCoercing().serialize(value) then: thrown(CoercingSerializeException) @@ -94,7 +94,7 @@ class ScalarsShortTest extends Specification { @Unroll def "parseValue throws exception for invalid input #value"() { when: - JavaPrimitives.GraphQLShort.getCoercing().parseValue(value) + ExtendedScalars.GraphQLShort.getCoercing().parseValue(value) then: thrown(CoercingParseValueException)