예제 #1
0
 protected override ISyntaxVisitorAction Enter(
     ListValueNode node,
     QueryableFilterVisitorContext context)
 {
     context.PushLevel(new Queue <Expression>());
     return(Continue);
 }
예제 #2
0
        /// <summary>
        /// Processes the queue as far as it needs to to generate a fully qualiffied
        /// <see cref="SyntaxNode" /> based on its ruleset.
        /// </summary>
        /// <param name="tokenStream">The token stream.</param>
        /// <returns>LexicalToken.</returns>
        public SyntaxNode MakeNode(TokenStream tokenStream)
        {
            var children      = new List <SyntaxNode>();
            var startLocation = tokenStream.Location;

            tokenStream.MatchOrThrow(TokenType.BracketLeft);
            tokenStream.Next();

            while (!tokenStream.EndOfStream && !tokenStream.Match(TokenType.BracketRight))
            {
                var childMaker = ValueMakerFactory.CreateMaker(tokenStream.ActiveToken);
                if (childMaker == null)
                {
                    throw new GraphQLSyntaxException(
                              tokenStream.Location,
                              $"Unexpected token in list, no value could be parsed. Expected '{TokenType.BracketRight.Description().ToString()}' or an " +
                              $"input object value but receieved '{tokenStream.ActiveToken.Text.ToString()}'");
                }

                var childNode = childMaker.MakeNode(tokenStream);
                children.Add(childNode);
            }

            // ensure we are pointing at an end of list
            tokenStream.MatchOrThrow(TokenType.BracketRight);
            tokenStream.Next();

            var listNode = new ListValueNode(startLocation);

            listNode.Children.AddRange(children);

            return(listNode);
        }
예제 #3
0
        protected override void VisitListValue(
            ListValueNode node,
            ConverterContext context)
        {
            if (context.InputType.IsListType())
            {
                ListType listType = context.InputType.ListType();
                Type     tempType = listType.ToRuntimeType();
                var      temp     = (IList)Activator.CreateInstance(tempType);

                for (int i = 0; i < node.Items.Count; i++)
                {
                    var valueContext = new ConverterContext();
                    valueContext.InputType = (IInputType)listType.ElementType;
                    valueContext.ClrType   = listType.ElementType.ToRuntimeType();

                    VisitValue(node.Items[i], valueContext);

                    temp.Add(valueContext.Object);
                }

                Type expectedType = context.ClrType == typeof(object)
                    ? typeof(List <object>)
                    : context.ClrType;

                context.Object = expectedType.IsAssignableFrom(tempType)
                    ? temp
                    : _converter.Convert(tempType, expectedType, temp);
            }
        }
예제 #4
0
 protected override ISyntaxVisitorAction Enter(
     ListValueNode node,
     TContext context)
 {
     context.PushLevel(new Queue <T>());
     return(Continue);
 }
예제 #5
0
        private bool ListNeedsCleanUp(
            ListType type,
            ListValueNode listValue)
        {
            if (type.ElementType.IsEnumType() &&
                listValue.Items.Count > 0 &&
                listValue.Items[0] is StringValueNode)
            {
                return(true);
            }

            if (type.ElementType.IsInputObjectType() &&
                type.ElementType.NamedType() is InputObjectType iot)
            {
                for (int i = 0; i < listValue.Items.Count; i++)
                {
                    if (listValue.Items[i] is ObjectValueNode ov &&
                        ObjectNeedsCleanUp(iot, ov))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
예제 #6
0
        public void Parse_InputObject_NullableEnumList()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddInputObjectType <FooInput>()
                             .ModifyOptions(o => o.StrictValidation = false)
                             .Create();

            var type = new NonNullType(schema.GetType <InputObjectType>("FooInput"));

            var listData = new ListValueNode(
                NullValueNode.Default,
                new EnumValueNode("BAZ"));

            var fieldData = new ObjectValueNode(
                new ObjectFieldNode("bars", listData));

            // act
            var parser      = new InputParser();
            var runtimeData = parser.ParseLiteral(fieldData, type, Path.Root.Append("root"));

            // assert
            Assert.Collection(
                Assert.IsType <FooInput>(runtimeData).Bars,
                t => Assert.Null(t),
                t => Assert.Equal(Bar.Baz, t));
        }
예제 #7
0
 protected override ISyntaxVisitorAction Leave(
     ListValueNode node,
     IDocumentValidatorContext context)
 {
     context.Types.Pop();
     return(Continue);
 }
예제 #8
0
 public override VisitorAction Enter(
     ListValueNode node,
     ISyntaxNode parent,
     IReadOnlyList <object> path,
     IReadOnlyList <ISyntaxNode> ancestors)
 {
     return(VisitorAction.Continue);
 }
 public override VisitorAction Enter(
     ListValueNode node,
     ISyntaxNode parent,
     IReadOnlyList <object> path,
     IReadOnlyList <ISyntaxNode> ancestors)
 {
     Level.Push(new Queue <Expression>());
     return(VisitorAction.Continue);
 }
예제 #10
0
 public static IError SortingVisitor_ListValues(ISortField field, ListValueNode node) =>
 ErrorBuilder.New()
 .SetMessage(
     DataResources.SortingVisitor_ListInput_AreNotSuported,
     field.DeclaringType.Name,
     field.Name)
 .AddLocation(node)
 .SetExtension(nameof(field), field)
 .Build();
예제 #11
0
        private static ListValueNode Rewrite(
            ListValueNode node,
            ListType type,
            IVariableValueCollection variableValues)
        {
            if (node.Items.Count == 0)
            {
                return(node);
            }

            if (node.Items.Count == 1)
            {
                return(TryRewriteValue(
                           node.Items[0],
                           type.ElementType,
                           NullValueNode.Default,
                           variableValues,
                           out IValueNode? rewritten)
                    ? node.WithItems(new[] { rewritten })
                    : node);
            }

            IValueNode[]? rewrittenItems = null;

            for (var i = 0; i < node.Items.Count; i++)
            {
                if (TryRewriteValue(
                        node.Items[i],
                        type.ElementType,
                        NullValueNode.Default,
                        variableValues,
                        out IValueNode? rewritten))
                {
                    if (rewrittenItems is null)
                    {
                        rewrittenItems = new IValueNode[node.Items.Count];
                        for (var j = 0; j < i; j++)
                        {
                            rewrittenItems[j] = node.Items[j];
                        }
                    }
                    rewrittenItems[i] = rewritten;
                }
                else if (rewrittenItems is not null)
                {
                    rewrittenItems[i] = node.Items[i];
                }
            }

            if (rewrittenItems is not null)
            {
                return(node.WithItems(rewrittenItems));
            }

            return(node);
        }
예제 #12
0
        public void ParseLiteral_With_2Invalid_Coordinates_Throws()
        {
            var type       = new GeoJsonPositionType();
            var coordinate = new ListValueNode(
                new FloatValueNode(1.0),
                new StringValueNode("2.2")
                );

            Assert.Throws <SerializationException>(() => type.ParseLiteral(coordinate));
        }
예제 #13
0
        protected override ListValueNode RewriteListValue(
            ListValueNode node,
            object context)
        {
            ListValueNode current = node;

            if (_type.Peek().ListType().ElementType is IInputType elementType)
            {
                _type.Push(_type.Peek().ListType().ElementType);

                if (current.Items.Count > 0)
                {
                    IValueNode[] rented = ArrayPool <IValueNode> .Shared.Rent(
                        current.Items.Count);

                    Span <IValueNode> copy = rented;
                    copy = copy.Slice(0, current.Items.Count);
                    bool rewrite = false;

                    for (int i = 0; i < current.Items.Count; i++)
                    {
                        IValueNode value = current.Items[i];

                        if (value is VariableNode variable)
                        {
                            rewrite = true;
                            copy[i] = ReplaceVariable(variable, elementType);
                        }
                        else
                        {
                            copy[i] = value;
                        }
                    }

                    if (rewrite)
                    {
                        var rewritten = new IValueNode[current.Items.Count];

                        for (int i = 0; i < current.Items.Count; i++)
                        {
                            rewritten[i] = copy[i];
                        }

                        current = current.WithItems(rewritten);
                    }

                    copy.Clear();
                    ArrayPool <IValueNode> .Shared.Return(rented);
                }

                _type.Pop();
            }

            return(current);
        }
예제 #14
0
 protected override ISyntaxVisitorAction Enter(
     ListValueNode node,
     IDocumentValidatorContext context)
 {
     if (context.Types.TryPeek(out IType? type) && type.IsListType())
     {
         context.Types.Push(type.ElementType());
         return(Continue);
     }
     return(Break);
 }
예제 #15
0
        public void Matrix_Cannot_Be_Coerced_From_List()
        {
            // arrange
            var type  = (IInputType) new ListType(new ListType(new BooleanType()));
            var value = new ListValueNode(new BooleanValueNode(true));

            // act
            Action action = () => type.ParseLiteral(value);

            // assert
            Assert.Throws <SerializationException>(action);
        }
예제 #16
0
        public void IsInstanceOfType_Invalid2ElementCoordinate_False()
        {
            // arrange
            var type       = new GeoJsonPositionType();
            var coordinate = new ListValueNode(new StringValueNode("1"), new FloatValueNode(1.2));

            // act
            bool?result = type.IsInstanceOfType(coordinate);

            // assert
            Assert.False(result);
        }
예제 #17
0
        public void IsInstanceOfType_Valid2ElementCoordinate_True()
        {
            // arrange
            var type       = new GeoJsonPositionType();
            var coordinate = new ListValueNode(new IntValueNode(1), new FloatValueNode(1.2));

            // act
            bool?result = type.IsInstanceOfType(coordinate);

            // assert
            Assert.True(result);
        }
예제 #18
0
        public void ListCannotBeCoercedFromMixedList()
        {
            // arrange
            var inputParser = new InputParser(new DefaultTypeConverter());
            var type        = (IInputType) new ListType(new BooleanType());
            var list        = new ListValueNode(new BooleanValueNode(true), new StringValueNode("foo"));

            // act
            void Action() => inputParser.ParseLiteral(list, type, Path.New("root"));

            // assert
            Assert.Throws <SerializationException>(Action);
        }
예제 #19
0
        public void Matrix_Cannot_Be_Coerced_From_List()
        {
            // arrange
            var inputParser = new InputParser(new DefaultTypeConverter());
            var type        = (IInputType) new ListType(new ListType(new BooleanType()));
            var value       = new ListValueNode(new BooleanValueNode(true));

            // act
            void Action() => inputParser.ParseLiteral(value, type, Path.New("root"));

            // assert
            Assert.Throws <SerializationException>(Action);
        }
예제 #20
0
        public void ParseLiteral_With_2Valid_Coordinates()
        {
            var type       = new GeoJsonPositionType();
            var coordinate = new ListValueNode(
                new FloatValueNode(1.0),
                new IntValueNode(2)
                );

            object?result = type.ParseLiteral(coordinate);

            Assert.Equal(1.0, Assert.IsType <Coordinate>(result).X);
            Assert.Equal(2, Assert.IsType <Coordinate>(result).Y);
        }
예제 #21
0
        public void ListCanBeCoercedFromListValue()
        {
            // arrange
            var inputParser = new InputParser(new DefaultTypeConverter());
            var type        = (IInputType) new ListType(new BooleanType());
            var list        = new ListValueNode(new BooleanValueNode(true), new BooleanValueNode(false));

            // act
            var coercedValue = inputParser.ParseLiteral(list, type, Path.New("root"));

            // assert
            Assert.Collection(Assert.IsType <List <bool?> >(coercedValue), Assert.True, Assert.False);
        }
        public void IsInstanceOfType_Invalid3ElementCoordinate_False()
        {
            // arrange
            var type       = new GeoJSONPositionScalar();
            var coordinate = new ListValueNode(new IntValueNode(1), new FloatValueNode(1.2),
                                               new StringValueNode("2"));

            // act
            bool result = type.IsInstanceOfType(coordinate);

            // assert
            Assert.False(result);
        }
예제 #23
0
        public void IsInstanceOfType_Invalid4ElementCoordinate_False()
        {
            var type       = new GeoJsonPositionType();
            var coordinate = new ListValueNode(
                new IntValueNode(1),
                new IntValueNode(2),
                new IntValueNode(3),
                new IntValueNode(4));

            bool?result = type.IsInstanceOfType(coordinate);

            Assert.False(result);
        }
예제 #24
0
        private object CreateList(ListValueNode listLiteral)
        {
            var list = (IList)Activator.CreateInstance(ClrType);

            for (var i = 0; i < listLiteral.Items.Count; i++)
            {
                object element = InnerInputType.ParseLiteral(
                    listLiteral.Items[i]);
                list.Add(element);
            }

            return(list);
        }
예제 #25
0
        public void IsInstanceOfType_List2ElementCoordinate_False()
        {
            // arrange
            var type       = new GeoJsonPositionType();
            var coordinate = new ListValueNode(
                new ListValueNode(new FloatValueNode(1.1), new FloatValueNode(1.2)));

            // act
            var result = type.IsInstanceOfType(coordinate);

            // assert
            Assert.False(result);
        }
예제 #26
0
        public void ParseLiteral_ListValue_To_ListString()
        {
            // arrange
            var listType = (IInputType) new ListType(new StringType());
            var list     = new ListValueNode(new StringValueNode("abc"));

            // act
            var serializedList = listType.ParseLiteral(list);

            // assert
            Assert.Collection(
                Assert.IsType <List <string> >(serializedList),
                t => Assert.Equal("abc", t));
        }
예제 #27
0
        public void ListCannotBeCoercedFromMixedList()
        {
            // arrange
            var type = (IInputType) new ListType(new BooleanType());
            var list = new ListValueNode(
                new BooleanValueNode(true),
                new StringValueNode("foo"));

            // act
            Action action = () => type.ParseLiteral(list);

            // assert
            Assert.Throws <SerializationException>(action);
        }
예제 #28
0
        public List <object> Convert(ListValueNode listValue)
        {
            if (listValue == null)
            {
                throw new ArgumentNullException(nameof(listValue));
            }

            List <object>   list     = null;
            Action <object> setValue =
                value => list = (List <object>)value;

            VisitListValue(listValue, setValue);
            return(list);
        }
예제 #29
0
        public void Matrix_Can_Be_Coerced_From_Matrix()
        {
            // arrange
            var type  = (IInputType) new ListType(new ListType(new BooleanType()));
            var value = new ListValueNode(
                new ListValueNode(new BooleanValueNode(true)),
                new ListValueNode(new BooleanValueNode(true), new BooleanValueNode(false)));

            // act
            object coercedValue = type.ParseLiteral(value);

            // assert
            coercedValue.MatchSnapshot();
        }
        public void MultiPolygon_IsCoordinateValid_Should_Fail_When_Point(string typeName)
        {
            // arrange
            var             inputParser = new InputParser(new DefaultTypeConverter());
            INamedInputType type        = CreateInputType(typeName);
            var             coords      = new ListValueNode(new IntValueNode(30), new IntValueNode(10));
            var             typeField   = new ObjectFieldNode(WellKnownFields.TypeFieldName, _geometryType);
            var             coordField  = new ObjectFieldNode(WellKnownFields.CoordinatesFieldName, coords);
            var             valueNode   = new ObjectValueNode(typeField, coordField);

            // act
            // assert
            Assert.Throws <SerializationException>(() => inputParser.ParseLiteral(valueNode, type));
        }