public T GetArgument <T>(string argumentName) { if (string.IsNullOrEmpty(argumentName)) { throw new ArgumentNullException(nameof(argumentName)); } Dictionary <string, ArgumentNode> arguments = GetArguments(); if (arguments.TryGetValue(argumentName, out ArgumentNode argValue) && Type.Arguments.TryGetField(argumentName, out InputField arg)) { if (typeof(T).IsAssignableFrom(arg.Type.ClrType)) { return((T)arg.Type.ParseLiteral(argValue.Value)); } else { return(ValueDeserializer .ParseLiteral <T>(arg.Type, argValue.Value)); } } throw new ArgumentException( "The argument name is invalid.", nameof(argumentName)); }
public void ParseScalarNullValue() { // arrange var sourceType = new IntType(); var targetType = typeof(int); NullValueNode literal = NullValueNode.Default; // act object result = ValueDeserializer .ParseLiteral(sourceType, targetType, literal); // assert Assert.Null(result); }
private void SetProperty(InputField argument, object obj, PropertyInfo property) { Dictionary <string, ArgumentNode> arguments = GetArguments(); if (arguments.TryGetValue(argument.Name, out ArgumentNode argumentValue)) { object parsedValue = ValueDeserializer.ParseLiteral( argument.Type, property.PropertyType, argumentValue.Value); ValueDeserializer.SetProperty( property, argument.Type.IsListType(), obj, parsedValue); } }
public void ParseScalarListToArray() { // arrange var sourceType = new ListType(new IntType()); var targetType = typeof(int[]); var literal = new ListValueNode(new IntValueNode("1")); // act object result = ValueDeserializer .ParseLiteral(sourceType, targetType, literal); // assert Assert.IsType <int[]>(result); Assert.Collection((int[])result, t => Assert.Equal(1, t)); }
public void ParseNonNullScalar() { // arrange var sourceType = new NonNullType(new IntType()); var targetType = typeof(int); IntValueNode literal = new IntValueNode("1"); // act object result = ValueDeserializer .ParseLiteral(sourceType, targetType, literal); // assert Assert.IsType <int>(result); Assert.Equal(1, result); }
public void ParseScalarAndConvert() { // arrange var sourceType = new IntType(); var targetType = typeof(string); var literal = new IntValueNode("1"); // act object result = ValueDeserializer .ParseLiteral(sourceType, targetType, literal); // assert Assert.IsType <string>(result); Assert.Equal("1", result); }
public void ParseAndMapObject() { // arrange ISchema schema = CreateSchema(); var sourceType = schema.GetType <INamedInputType>("FooInput"); var targetType = typeof(FooOnlyBar1); var literal = CreateFoo(); // act FooOnlyBar1 result = ValueDeserializer .ParseLiteral(sourceType, targetType, literal) as FooOnlyBar1; // assert Assert.NotNull(result); Assert.Equal("123", result.Bar1); }
public void ParseObjectGraph() { // arrange ISchema schema = CreateSchema(); var sourceType = schema.GetType <INamedInputType>("BarInput"); var targetType = typeof(Bar); var literal = CreateBar(); // act Bar result = ValueDeserializer .ParseLiteral(sourceType, targetType, literal) as Bar; // assert Assert.NotNull(result); Assert.NotNull(result.Foo); Assert.Equal("123", result.Foo.Bar1); Assert.Equal("456", result.Foo.Bar2); }
public void ParseAndMapObjectGraphWithList() { // arrange ISchema schema = CreateSchema(); var sourceType = schema.GetType <INamedInputType>( "BarWithArrayInput"); var targetType = typeof(BarWithListOnlyGet); var literal = CreateBarWithArray(); // act BarWithListOnlyGet result = ValueDeserializer .ParseLiteral(sourceType, targetType, literal) as BarWithListOnlyGet; // assert Assert.NotNull(result); Assert.NotNull(result.Foos); Assert.Collection(result.Foos, t => { Assert.Equal("123", t.Bar1); Assert.Equal("456", t.Bar2); }); }