Java源码示例:graphql.language.IntValue
示例1
@Override
public Object parseLiteral(final Object input) throws CoercingParseLiteralException {
try {
if (input instanceof IntValue) {
return ((IntValue) input).getValue().longValue();
} else if (input instanceof StringValue) {
final String value = ((StringValue) input).getValue().toLowerCase();
if (value.startsWith("0x")) {
return Bytes.fromHexStringLenient(value).toLong();
} else {
return Long.parseLong(value);
}
}
} catch (final NumberFormatException e) {
// fall through
}
throw new CoercingParseLiteralException("Value is not any Long : '" + input + "'");
}
示例2
@Override
public UInt256 parseLiteral(final Object input) throws CoercingParseLiteralException {
try {
if (input instanceof StringValue) {
return UInt256.fromHexString(((StringValue) input).getValue());
} else if (input instanceof IntValue) {
return UInt256.valueOf(((IntValue) input).getValue());
}
} catch (final IllegalArgumentException e) {
// fall through
}
throw new CoercingParseLiteralException("Value is not any BigInt : '" + input + "'");
}
示例3
@Nullable
@Override
public Object parseLiteral(Object input) throws CoercingParseLiteralException {
if (input instanceof NullValue) {
return null;
} else if (input instanceof FloatValue) {
return ((FloatValue) input).getValue();
} else if (input instanceof StringValue) {
return ((StringValue) input).getValue();
} else if (input instanceof IntValue) {
return ((IntValue) input).getValue();
} else if (input instanceof BooleanValue) {
return ((BooleanValue) input).isValue();
} else if (input instanceof EnumValue) {
return ((EnumValue) input).getName();
} else if (input instanceof ArrayValue) {
return ((ArrayValue) input).getValues()
.stream()
.map(this::parseLiteral)
.collect(Collectors.toList());
} else if (input instanceof ObjectValue) {
return ((ObjectValue) input).getObjectFields()
.stream()
.collect(Collectors.toMap(ObjectField::getName, f -> parseLiteral(f.getValue())));
}
return Assert.assertShouldNeverHappen();
}
示例4
public static Object parseLiteral(Object input, Map<String, Object> variables) throws CoercingParseLiteralException {
if (!(input instanceof Value)) {
log.error("Expected 'Value', got: {}", input);
throw new CoercingParseLiteralException("Expected 'Value', got: " + input);
}
Object result = null;
if (input instanceof StringValue) {
result = ((StringValue) input).getValue();
} else if (input instanceof IntValue) {
result = ((IntValue) input).getValue();
} else if (input instanceof FloatValue) {
result = ((FloatValue) input).getValue();
} else if (input instanceof BooleanValue) {
result = ((BooleanValue) input).isValue();
} else if (input instanceof EnumValue) {
result = ((EnumValue) input).getName();
} else if (input instanceof VariableReference) {
result = variables.get(((VariableReference) input).getName());
} else if (input instanceof ArrayValue) {
result = ((ArrayValue) input).getValues().stream()
.map(v -> parseLiteral(v, variables))
.collect(toList());
} else if (input instanceof ObjectValue) {
result = ((ObjectValue) input).getObjectFields().stream()
.collect(toMap(ObjectField::getName, f -> parseLiteral(f.getValue(), variables)));
}
return result;
}
示例5
@Override
public JsonElement parseLiteral(Object input) {
if (input instanceof ObjectValue || input instanceof ArrayValue) {
throw literalParsingException(input, StringValue.class, BooleanValue.class, EnumValue.class,
FloatValue.class, IntValue.class, NullValue.class);
}
return parseJsonValue(((Value) input));
}
示例6
private static JsonElement parseJsonValue(Value value) {
if (value instanceof BooleanValue) {
return new JsonPrimitive(((BooleanValue) value).isValue());
}
if (value instanceof EnumValue) {
return new JsonPrimitive(((EnumValue) value).getName());
}
if (value instanceof FloatValue) {
return new JsonPrimitive(((FloatValue) value).getValue());
}
if (value instanceof IntValue) {
return new JsonPrimitive(((IntValue) value).getValue());
}
if (value instanceof NullValue) {
return JsonNull.INSTANCE;
}
if (value instanceof StringValue) {
return new JsonPrimitive(((StringValue) value).getValue());
}
if (value instanceof ArrayValue) {
List<Value> values = ((ArrayValue) value).getValues();
JsonArray jsonArray = new JsonArray(values.size());
values.forEach(v -> jsonArray.add(parseJsonValue(v)));
return jsonArray;
}
if (value instanceof ObjectValue) {
final JsonObject result = new JsonObject();
((ObjectValue) value).getObjectFields().forEach(objectField ->
result.add(objectField.getName(), parseJsonValue(objectField.getValue())));
return result;
}
//Should never happen, as it would mean the variable was not replaced by the parser
throw new CoercingParseLiteralException("Unknown scalar AST type: " + value.getClass().getName());
}
示例7
private static JsonNode parseJsonValue(Value value, Map<String, Object> variables) {
if (value instanceof BooleanValue) {
return JsonNodeFactory.instance.booleanNode(((BooleanValue) value).isValue());
}
if (value instanceof EnumValue) {
return JsonNodeFactory.instance.textNode(((EnumValue) value).getName());
}
if (value instanceof FloatValue) {
return JsonNodeFactory.instance.numberNode(((FloatValue) value).getValue());
}
if (value instanceof IntValue) {
return JsonNodeFactory.instance.numberNode(((IntValue) value).getValue());
}
if (value instanceof NullValue) {
return JsonNodeFactory.instance.nullNode();
}
if (value instanceof StringValue) {
return JsonNodeFactory.instance.textNode(((StringValue) value).getValue());
}
if (value instanceof ArrayValue) {
List<Value> values = ((ArrayValue) value).getValues();
ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode(values.size());
values.forEach(v -> jsonArray.add(parseJsonValue(v, variables)));
return jsonArray;
}
if (value instanceof VariableReference) {
return OBJECT_MAPPER.convertValue(variables.get(((VariableReference) value).getName()), JsonNode.class);
}
if (value instanceof ObjectValue) {
final ObjectNode result = JsonNodeFactory.instance.objectNode();
((ObjectValue) value).getObjectFields().forEach(objectField ->
result.set(objectField.getName(), parseJsonValue(objectField.getValue(), variables)));
return result;
}
//Should never happen
throw new CoercingParseLiteralException("Unknown scalar AST type: " + value.getClass().getName());
}
示例8
@Override
public DecimalNode parseLiteral(Object input) {
if (input instanceof IntValue) {
return (DecimalNode) JsonNodeFactory.instance.numberNode(((IntValue) input).getValue());
} else if (input instanceof FloatValue) {
return (DecimalNode) JsonNodeFactory.instance.numberNode(((FloatValue) input).getValue());
} else {
throw new CoercingParseLiteralException(errorMessage(input, IntValue.class, FloatValue.class));
}
}
示例9
@Override
public IntNode parseLiteral(Object input) {
if (input instanceof IntValue) {
try {
return IntNode.valueOf(((IntValue) input).getValue().intValueExact());
} catch (ArithmeticException e) {
throw new CoercingParseLiteralException(input + " does not fit into an int without a loss of precision");
}
} else {
throw new CoercingParseLiteralException(errorMessage(input, IntValue.class));
}
}
示例10
@Override
public BigIntegerNode parseLiteral(Object input) {
if (input instanceof IntValue) {
return BigIntegerNode.valueOf(((IntValue) input).getValue());
} else {
throw new CoercingParseLiteralException(errorMessage(input, IntValue.class));
}
}
示例11
@Override
public ShortNode parseLiteral(Object input) {
if (input instanceof IntValue) {
try {
return ShortNode.valueOf(((IntValue) input).getValue().shortValueExact());
} catch (ArithmeticException e) {
throw new CoercingParseLiteralException(input + " does not fit into a short without a loss of precision");
}
} else {
throw new CoercingParseLiteralException(errorMessage(input, IntValue.class));
}
}
示例12
@Override
public FloatNode parseLiteral(Object input) {
if (input instanceof IntValue) {
return FloatNode.valueOf(((IntValue) input).getValue().floatValue());
} if (input instanceof FloatValue) {
return FloatNode.valueOf(((FloatValue) input).getValue().floatValue());
} else {
throw new CoercingParseLiteralException(errorMessage(input, IntValue.class, FloatValue.class));
}
}
示例13
@Override
public DoubleNode parseLiteral(Object input) {
if (input instanceof IntValue) {
return DoubleNode.valueOf(((IntValue) input).getValue().doubleValue());
} if (input instanceof FloatValue) {
return DoubleNode.valueOf(((FloatValue) input).getValue().doubleValue());
} else {
throw new CoercingParseLiteralException(errorMessage(input, IntValue.class, FloatValue.class));
}
}
示例14
private void testEpochMilliTemporal(Class type, Coercing coercing, String expected, long literal) {
Object parsed = coercing.parseLiteral(new IntValue(new BigInteger(Long.toString(literal))));
assertTrue(type.isInstance(parsed));
assertEquals(expected, coercing.serialize(parsed));
parsed = coercing.parseValue(literal);
assertTrue(type.isInstance(parsed));
assertEquals(expected, coercing.serialize(parsed));
}
示例15
private PageInformation extractPageInformation(DataFetchingEnvironment environment, Field field) {
Optional<Argument> paginationRequest = field.getArguments().stream().filter(it -> GraphQLSchemaBuilder.PAGINATION_REQUEST_PARAM_NAME.equals(it.getName())).findFirst();
if (paginationRequest.isPresent()) {
field.getArguments().remove(paginationRequest.get());
ObjectValue paginationValues = (ObjectValue) paginationRequest.get().getValue();
IntValue page = (IntValue) paginationValues.getObjectFields().stream().filter(it -> "page".equals(it.getName())).findFirst().get().getValue();
IntValue size = (IntValue) paginationValues.getObjectFields().stream().filter(it -> "size".equals(it.getName())).findFirst().get().getValue();
return new PageInformation(page.getValue().intValue(), size.getValue().intValue());
}
return new PageInformation(1, Integer.MAX_VALUE);
}
示例16
@Override
public Object parseLiteral(Object input) {
if (input instanceof StringValue) {
return parseStringToLocalDateTime(((StringValue) input).getValue());
} else if (input instanceof IntValue) {
BigInteger value = ((IntValue) input).getValue();
return parseLongToLocalDateTime(value.longValue());
}
return null;
}
示例17
@Override
public Instant parseLiteral(Object input) {
if (input instanceof IntValue) {
return Instant.ofEpochSecond(((IntValue) input).getValue().longValue());
}
return null;
}
示例18
@Override
public Object parseLiteral(Object input) {
if (input instanceof StringValue) {
return parseStringToLocalDate(((StringValue) input).getValue());
} else if (input instanceof IntValue) {
BigInteger value = ((IntValue) input).getValue();
return parseLongToLocalDate(value.longValue());
}
return null;
}
示例19
@Override
public Object parseLiteral(Object input) {
if (input instanceof StringValue) {
return parseStringToDate(((StringValue) input).getValue());
} else if (input instanceof IntValue) {
BigInteger value = ((IntValue) input).getValue();
return new Date(value.longValue());
}
return null;
}
示例20
private int getArgumentsValue(List<Argument> argumentList) {
int argumentValue = 0;
if (argumentList.size() > 0) {
for (Argument object : argumentList) {
BigInteger value = ((IntValue) object.getValue()).getValue();
int val = value.intValue();
argumentValue = argumentValue + val;
}
} else {
argumentValue = 1;
}
return argumentValue;
}
示例21
/**
* Extracts a scalar value, this is needed because of GraphQL strict types
*/
protected Object getRealValue(Value value, DataFetchingEnvironment env) {
if (value instanceof BooleanValue) {
return ((BooleanValue)value).isValue();
} else if (value instanceof FloatValue) {
return ((FloatValue) value).getValue();
} else if (value instanceof IntValue) {
return ((IntValue) value).getValue();
} else if (value instanceof StringValue) {
return ((StringValue) value).getValue();
} else if (value instanceof VariableReference) {
return env.getVariables().get(((VariableReference) value).getName());
}
return null;
}
示例22
@Test
public void intValue() {
Value value = IntValue.newIntValue(new BigInteger("1")).build();
Object result = JsonCoercingUtil.parseLiteral(value, emptyMap());
assertThat(result, is(new BigInteger("1")));
}