|
| 1 | +package graphql.scalar; |
| 2 | + |
| 3 | +import graphql.Internal; |
| 4 | +import graphql.language.FloatValue; |
| 5 | +import graphql.language.IntValue; |
| 6 | +import graphql.language.StringValue; |
| 7 | +import graphql.schema.Coercing; |
| 8 | +import graphql.schema.CoercingParseLiteralException; |
| 9 | +import graphql.schema.CoercingParseValueException; |
| 10 | +import graphql.schema.CoercingSerializeException; |
| 11 | + |
| 12 | +import java.math.BigDecimal; |
| 13 | +import java.math.BigInteger; |
| 14 | + |
| 15 | +import static graphql.scalar.CoercingUtil.isNumberIsh; |
| 16 | +import static graphql.scalar.CoercingUtil.typeName; |
| 17 | + |
| 18 | +@Internal |
| 19 | +public class GraphqlBigIntegerCoercing implements Coercing<BigInteger, BigInteger> { |
| 20 | + |
| 21 | + private BigInteger convertImpl(Object input) { |
| 22 | + if (isNumberIsh(input)) { |
| 23 | + BigDecimal value; |
| 24 | + try { |
| 25 | + value = new BigDecimal(input.toString()); |
| 26 | + } catch (NumberFormatException e) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + try { |
| 30 | + return value.toBigIntegerExact(); |
| 31 | + } catch (ArithmeticException e) { |
| 32 | + return null; |
| 33 | + } |
| 34 | + } |
| 35 | + return null; |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public BigInteger serialize(Object input) { |
| 41 | + BigInteger result = convertImpl(input); |
| 42 | + if (result == null) { |
| 43 | + throw new CoercingSerializeException( |
| 44 | + "Expected type 'BigInteger' but was '" + typeName(input) + "'." |
| 45 | + ); |
| 46 | + } |
| 47 | + return result; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public BigInteger parseValue(Object input) { |
| 52 | + BigInteger result = convertImpl(input); |
| 53 | + if (result == null) { |
| 54 | + throw new CoercingParseValueException( |
| 55 | + "Expected type 'BigInteger' but was '" + typeName(input) + "'." |
| 56 | + ); |
| 57 | + } |
| 58 | + return result; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public BigInteger parseLiteral(Object input) { |
| 63 | + if (input instanceof StringValue) { |
| 64 | + try { |
| 65 | + return new BigDecimal(((StringValue) input).getValue()).toBigIntegerExact(); |
| 66 | + } catch (NumberFormatException | ArithmeticException e) { |
| 67 | + throw new CoercingParseLiteralException( |
| 68 | + "Unable to turn AST input into a 'BigInteger' : '" + input + "'" |
| 69 | + ); |
| 70 | + } |
| 71 | + } else if (input instanceof IntValue) { |
| 72 | + return ((IntValue) input).getValue(); |
| 73 | + } else if (input instanceof FloatValue) { |
| 74 | + try { |
| 75 | + return ((FloatValue) input).getValue().toBigIntegerExact(); |
| 76 | + } catch (ArithmeticException e) { |
| 77 | + throw new CoercingParseLiteralException( |
| 78 | + "Unable to turn AST input into a 'BigInteger' : '" + input + "'" |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | + throw new CoercingParseLiteralException( |
| 83 | + "Expected AST type 'IntValue', 'StringValue' or 'FloatValue' but was '" + typeName(input) + "'." |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
0 commit comments