public void MultiLine_IsCoordinateValid_Should_Fail_When_Point(string typeName) { // arrange 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>(() => type.ParseLiteral(valueNode)); }
public void ParseLiteral_Should_Pass_When_NoCrs(string typeName) { // arrange INamedInputType type = CreateInputType(typeName); var typeField = new ObjectFieldNode(WellKnownFields.TypeFieldName, _geometryType); var coordField = new ObjectFieldNode( WellKnownFields.CoordinatesFieldName, _coordinatesSyntaxNode); var valueNode = new ObjectValueNode(typeField, coordField); // act object?parsedResult = type.ParseLiteral(valueNode); // assert AssertGeometry(parsedResult); }
protected override void VisitObjectField( ObjectFieldNode node, ConverterContext context) { if (context.InputFields.TryGetField( node.Name.Value, out InputField inputField)) { var valueContext = new ConverterContext(); valueContext.InputType = inputField.Type; valueContext.ClrType = GetClrType(inputField, context.Object); VisitValue(node.Value, valueContext); inputField.SetValue(context.Object, valueContext.Object); } }
private static IValueNode ResolveObjectField( ObjectValueNode obj, Queue <string> path) { if (path.Any()) { string name = path.Dequeue(); ObjectFieldNode fieldValue = obj.Fields .FirstOrDefault(t => t.Name.Value == name); return(fieldValue.Value); } else { return(null); } }
protected override ISyntaxVisitorAction OnFieldLeave( TContext context, IFilterField field, ObjectFieldNode node) { if (field?.Handler is IFilterFieldHandler <TContext, T> handler && handler.TryHandleLeave( context, field, node, out ISyntaxVisitorAction? action)) { return(action); } return(SyntaxVisitor.Skip); }
protected override ISyntaxVisitorAction Leave( ObjectFieldNode node, QueryableFilterVisitorContext context) { if (context.Operations.Peek() is FilterOperationField field) { for (var i = context.FieldHandlers.Count - 1; i >= 0; i--) { context.FieldHandlers[i].Leave( field, node, context); } } return(base.Leave(node, context)); }
protected override ObjectFieldNode RewriteObjectField( ObjectFieldNode node, object context) { if (_type.Peek().NamedType() is InputObjectType inputObject && inputObject.Fields.TryGetField( node.Name.Value, out InputField field)) { IValueNode rewritten = null; _type.Push(field.Type); switch (node.Value) { case ListValueNode value: rewritten = RewriteListValue(value, context); break; case ObjectValueNode value: rewritten = RewriteObjectValue(value, context); break; case VariableNode value: rewritten = ReplaceVariable(value, field.Type); break; default: rewritten = node.Value; break; } _type.Pop(); if (rewritten == node.Value) { return(node); } return(node.WithValue(rewritten)); } // TODO : resource throw new QueryException( ErrorBuilder.New() .SetMessage("Unknown field.") .Build()); }
protected override ISyntaxVisitorAction OnFieldEnter( TContext context, ISortField field, ObjectFieldNode node) { if (field.Handler is ISortFieldHandler <TContext, T> handler && handler.TryHandleEnter( context, field, node, out ISyntaxVisitorAction? action)) { return(action); } return(SyntaxVisitor.SkipAndLeave); }
protected sealed override ISyntaxVisitorAction Enter( ISyntaxNode node, ISyntaxVisitorContext context) { return(node switch { DocumentNode n => Enter(n, context), OperationDefinitionNode n => Enter(n, context), VariableDefinitionNode n => Enter(n, context), VariableNode n => Enter(n, context), SelectionSetNode n => Enter(n, context), FieldNode n => Enter(n, context), ArgumentNode n => Enter(n, context), FragmentSpreadNode n => Enter(n, context), InlineFragmentNode n => Enter(n, context), FragmentDefinitionNode n => Enter(n, context), DirectiveNode n => Enter(n, context), NamedTypeNode n => Enter(n, context), ListTypeNode n => Enter(n, context), NonNullTypeNode n => Enter(n, context), ListValueNode n => Enter(n, context), ObjectValueNode n => Enter(n, context), ObjectFieldNode n => Enter(n, context), IValueNode n => Enter(n, context), SchemaDefinitionNode n => Enter(n, context), OperationTypeDefinitionNode n => Enter(n, context), ScalarTypeDefinitionNode n => Enter(n, context), ObjectTypeDefinitionNode n => Enter(n, context), FieldDefinitionNode n => Enter(n, context), InputValueDefinitionNode n => Enter(n, context), InterfaceTypeDefinitionNode n => Enter(n, context), UnionTypeDefinitionNode n => Enter(n, context), EnumTypeDefinitionNode n => Enter(n, context), EnumValueDefinitionNode n => Enter(n, context), InputObjectTypeDefinitionNode n => Enter(n, context), DirectiveDefinitionNode n => Enter(n, context), SchemaExtensionNode n => Enter(n, context), ScalarTypeExtensionNode n => Enter(n, context), ObjectTypeExtensionNode n => Enter(n, context), InterfaceTypeExtensionNode n => Enter(n, context), UnionTypeExtensionNode n => Enter(n, context), EnumTypeExtensionNode n => Enter(n, context), InputObjectTypeExtensionNode n => Enter(n, context), _ => throw new NotSupportedException() });
public bool Enter( FilterOperationField field, ObjectFieldNode node, IQueryableFilterVisitorContext context, out ISyntaxVisitorAction action) { if (field.Operation.Kind == FilterOperationKind.Object) { MemberExpression nestedProperty = Expression.Property( context.GetInstance(), field.Operation.Property); context.PushInstance(nestedProperty); action = SyntaxVisitor.Continue; return(true); } action = SyntaxVisitor.SkipAndLeave; return(false); }
public void ParseLiteral_Should_Pass_When_CorrectGeometry(string typeName) { // arrange var inputParser = new InputParser(new DefaultTypeConverter()); INamedInputType type = CreateInputType(typeName); var typeField = new ObjectFieldNode(WellKnownFields.TypeFieldName, _geometryType); var coordField = new ObjectFieldNode( WellKnownFields.CoordinatesFieldName, _coordinatesSyntaxNode); var crsField = new ObjectFieldNode(WellKnownFields.CrsFieldName, 26912); var valueNode = new ObjectValueNode(typeField, coordField, crsField); // act var parsedResult = inputParser.ParseLiteral(valueNode, type); // assert AssertGeometry(parsedResult, 26912); }
public void LineString_IsCoordinateValid_Should_Fail_When_LessThanTwoPoint(string typeName) { // arrange var inputParser = new InputParser(new DefaultTypeConverter()); INamedInputType type = CreateInputType(typeName); var coords = new ListValueNode( 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)); }
public override VisitorAction Enter( ObjectFieldNode node, ISyntaxNode parent, IReadOnlyList <object> path, IReadOnlyList <ISyntaxNode> ancestors) { base.Enter(node, parent, path, ancestors); if (Operations.Peek() is FilterOperationField field) { for (var i = _fieldHandlers.Count - 1; i >= 0; i--) { if (_fieldHandlers[i].Enter( field, node, parent, path, ancestors, Closures, _inMemory, out VisitorAction action)) { return(action); } } for (var i = _opHandlers.Count - 1; i >= 0; i--) { if (_opHandlers[i].TryHandle( field.Operation, field.Type, node.Value, Closures.Peek().Instance.Peek(), _converter, _inMemory, out Expression expression)) { Closures.Peek().Level.Peek().Enqueue(expression); break; } } return(VisitorAction.Skip); } return(VisitorAction.Continue); }
public VisitorAction Enter( ObjectFieldNode node, ISyntaxNode parent, IReadOnlyList <object> path, IReadOnlyList <ISyntaxNode> ancestors) { if (_type.Peek().NamedType() is InputObjectType inputObject && inputObject.Fields.TryGetField( node.Name.Value, out IInputField? field)) { _type.Push(field.Type); _action.Push(VisitorAction.Continue); return(VisitorAction.Continue); } _action.Push(VisitorAction.Skip); return(VisitorAction.Skip); }
protected override ISyntaxVisitorAction Enter( ObjectFieldNode node, IDocumentValidatorContext context) { if (context.Types.TryPeek(out IType type) && type.NamedType() is InputObjectType it && it.Fields.TryGetField(node.Name.Value, out InputField? field)) { if (field.Type.IsNonNullType() && node.Value.IsNull()) { context.Errors.Add( context.FieldIsRequiredButNull(node, field.Name)); } context.InputFields.Push(field); context.Types.Push(field.Type); return(Continue); }
public static (int typeIndex, int coordinateIndex, int crsIndex) GetFieldIndices( ObjectValueNode obj, string _typeFieldName, string _coordinatesFieldName, string _crsFieldName) { var coordinateIndex = -1; var typeIndex = -1; var crsIndex = -1; for (var i = 0; i < obj.Fields.Count; i++) { if (coordinateIndex > -1 && typeIndex > -1 && crsIndex > -1) { return(typeIndex, coordinateIndex, crsIndex); } ObjectFieldNode field = obj.Fields[i]; if (typeIndex < 0 && string.Equals(field.Name.Value, _typeFieldName, StringComparison.InvariantCultureIgnoreCase)) { typeIndex = i; continue; } if (coordinateIndex < 0 && string.Equals(field.Name.Value, _coordinatesFieldName, StringComparison.InvariantCultureIgnoreCase)) { coordinateIndex = i; continue; } if (crsIndex < 0 && string.Equals(field.Name.Value, _crsFieldName, StringComparison.InvariantCultureIgnoreCase)) { crsIndex = i; continue; } } return(typeIndex, coordinateIndex, crsIndex); }
public override bool TryHandleLeave( QueryableSortContext context, ISortField field, ObjectFieldNode node, [NotNullWhen(true)] out ISyntaxVisitorAction?action) { if (field.RuntimeType is null) { action = null; return(false); } // Deque last context.PopInstance(); context.RuntimeTypes.Pop(); action = SyntaxVisitor.Continue; return(true); }
protected override ISyntaxVisitorAction Enter( ObjectFieldNode node, QueryableSortVisitorContext context) { base.Enter(node, context); if (context.Operations.Peek() is SortOperationField sortField && sortField.Operation?.Property != null) { context.Closure.EnqueueProperty(sortField.Operation.Property); if (!sortField.Operation.IsObject) { var kind = (SortOperationKind)sortField.Type.Deserialize(node.Value.Value) !; context.SortOperations.Enqueue(context.CreateSortOperation(kind)); } } return(Continue); }
/// <inheritdoc /> public override bool TryHandleEnter( Neo4JSortVisitorContext context, ISortField field, ObjectFieldNode node, [NotNullWhen(true)] out ISyntaxVisitorAction?action) { if (node.Value.IsNull()) { context.ReportError( ErrorHelper.CreateNonNullError(field, node.Value, context)); action = SyntaxVisitor.Skip; return(true); } context.Path.Push(field.GetName()); action = SyntaxVisitor.Continue; return(true); }
public override VisitorAction Leave( ObjectFieldNode node, ISyntaxNode parent, IReadOnlyList <object> path, IReadOnlyList <ISyntaxNode> ancestors) { if (Operations.Peek() is FilterOperationField field && field.Operation.Kind == FilterOperationKind.Object) { // Deque last expression to prefix with nullcheck var condition = Level.Peek().Dequeue(); var nullCheck = Expression.NotEqual( Instance.Peek(), Expression.Constant(null, typeof(object))); Level.Peek().Enqueue(Expression.AndAlso(nullCheck, condition)); Instance.Pop(); } return(base.Leave(node, parent, path, ancestors)); }
public static bool Enter( FilterOperationField field, ObjectFieldNode node, IQueryableFilterVisitorContext context, [NotNullWhen(true)] out ISyntaxVisitorAction?action) { if (node.Value.IsNull()) { if (field.Operation.IsNullable) { MemberExpression nestedProperty = Expression.Property( context.GetInstance(), field.Operation.Property); Expression expression = FilterExpressionBuilder.Equals(nestedProperty, null !); context.GetLevel().Enqueue(expression); } else { context.ReportError( ErrorHelper.CreateNonNullError(field, node, context)); } action = SyntaxVisitor.Skip; return(true); } if (field.Operation.Kind == FilterOperationKind.Object) { MemberExpression nestedProperty = Expression.Property( context.GetInstance(), field.Operation.Property); context.PushInstance(nestedProperty); action = SyntaxVisitor.Continue; return(true); } action = null; return(false); }
public static bool Enter( FilterOperationField field, ObjectFieldNode node, IQueryableFilterVisitorContext context, [NotNullWhen(true)] out ISyntaxVisitorAction?action) { if (field.Operation.Kind == FilterOperationKind.ArraySome || field.Operation.Kind == FilterOperationKind.ArrayNone || field.Operation.Kind == FilterOperationKind.ArrayAll) { if (!field.Operation.IsNullable && node.Value.IsNull()) { context.ReportError( ErrorHelper.CreateNonNullError(field, node, context)); action = SyntaxVisitor.Skip; return(true); } MemberExpression nestedProperty = Expression.Property( context.GetInstance(), field.Operation.Property); context.PushInstance(nestedProperty); Type closureType = GetTypeFor(field.Operation); if (node.Value.IsNull()) { context.AddIsNullClosure(closureType); action = SyntaxVisitor.SkipAndLeave; } else { context.AddClosure(closureType); action = SyntaxVisitor.Continue; } return(true); } action = null; return(false); }
private void VisitInputField( ISet <string> visitedFields, FieldBase <IInputType> field, ObjectFieldNode fieldValue) { if (visitedFields.Add(field.Name)) { if (fieldValue.Value is ObjectValueNode ov && field.Type.NamedType() is InputObjectType it) { VisitObjectValue(it, ov); } } else { Errors.Add(new ValidationError( $"Field `{field.Name}` is ambiguous.", fieldValue)); } }
private static bool TryRewriteField( ObjectFieldNode original, InputField field, IVariableValueCollection variableValues, [NotNullWhen(true)] out ObjectFieldNode?rewritten) { if (TryRewriteValue( original.Value, field.Type, field.DefaultValue ?? NullValueNode.Default, variableValues, out IValueNode? rewrittenValue)) { rewritten = original.WithValue(rewrittenValue); return(true); } rewritten = null; return(false); }
public override VisitorAction Enter( ObjectFieldNode node, ISyntaxNode parent, IReadOnlyList <object> path, IReadOnlyList <ISyntaxNode> ancestors) { base.Enter(node, parent, path, ancestors); if (Operations.Peek() is SortOperationField sortField) { Closure.EnqueueProperty(sortField.Operation.Property); if (!sortField.Operation.IsObject) { var kind = (SortOperationKind)sortField.Type.Deserialize(node.Value.Value); SortOperations.Enqueue(CreateSortOperation(kind)); } } return(VisitorAction.Continue); }
protected override ISyntaxVisitorAction Enter( ObjectFieldNode node, QueryableSortVisitorContext context) { base.Enter(node, context); if (context.Operations.Peek() is SortOperationField sortField && sortField.Operation?.Property != null) { context.Closure.EnqueueProperty(sortField.Operation.Property); if (!sortField.Operation.IsObject) { InputParser parser = context.InputParser; var kind = (SortOperationKind)parser.ParseLiteral(node.Value, sortField) !; context.SortOperations.Enqueue(context.CreateSortOperation(kind)); } } return(Continue); }
public override bool TryHandleEnter( QueryableFilterContext context, IFilterField field, ObjectFieldNode node, [NotNullWhen(true)] out ISyntaxVisitorAction?action) { if (node.Value.IsNull()) { context.ReportError( ErrorHelper.CreateNonNullError(field, node.Value, context)); action = SyntaxVisitor.Skip; return(true); } if (field.RuntimeType is null) { action = null; return(false); } Expression nestedProperty; if (field.Member is PropertyInfo propertyInfo) { nestedProperty = Expression.Property(context.GetInstance(), propertyInfo); } else if (field.Member is MethodInfo methodInfo) { nestedProperty = Expression.Call(context.GetInstance(), methodInfo); } else { throw new InvalidOperationException(); } context.PushInstance(nestedProperty); context.RuntimeTypes.Push(field.RuntimeType); action = SyntaxVisitor.Continue; return(true); }
public override bool TryHandleEnter( QueryableFilterContext context, IFilterField field, ObjectFieldNode node, [NotNullWhen(true)] out ISyntaxVisitorAction?action) { if (field is IFilterOperationField filterOperationField) { if (node.Value.IsNull()) { context.ReportError( ErrorHelper.CreateNonNullError( field, node.Value, context)); action = SyntaxVisitor.Skip; return(true); } if (!TryHandleOperation( context, filterOperationField, node, out Expression? nestedProperty)) { context.ReportError( ErrorHelper.CouldNotCreateFilterForOperation( field, node.Value, context)); action = SyntaxVisitor.Skip; return(true); } context.RuntimeTypes.Push(_runtimeType); context.PushInstance(nestedProperty); action = SyntaxVisitor.Continue; } else { action = SyntaxVisitor.Break; } return(true); }
protected override void VisitObject( IDictionary <string, object> dictionary, ConverterContext context) { if (!context.InputType.IsInputObjectType()) { throw new QueryException(ErrorBuilder.New() .SetMessage($"The value of {context.Name} has a wrong structure.") .SetCode(ErrorCodes.Execution.InvalidType) .AddLocation(context.Node) .Build()); } IInputType originalType = context.InputType; InputObjectType type = (InputObjectType)context.InputType.NamedType(); var fields = new ObjectFieldNode[dictionary.Count]; int i = 0; foreach (KeyValuePair <string, object> field in dictionary) { if (!type.Fields.TryGetField(field.Key, out InputField f)) { throw new QueryException(ErrorBuilder.New() .SetMessage($"The value of {context.Name} has a wrong structure.") .SetCode(ErrorCodes.Execution.InvalidType) .AddLocation(context.Node) .Build()); } context.InputType = f.Type; context.Object = null; VisitField(field, context); fields[i++] = new ObjectFieldNode(field.Key, (IValueNode)context.Object); } context.InputType = originalType; context.Object = new ObjectValueNode(fields); }
public override bool TryHandleEnter( QueryableFilterContext context, IFilterField field, ObjectFieldNode node, [NotNullWhen(true)] out ISyntaxVisitorAction?action) { if (node.Value.IsNull()) { context.ReportError( ErrorHelper.CreateNonNullError(field, node.Value, context)); action = SyntaxVisitor.Skip; return(true); } if (field.RuntimeType is null) { action = null; return(false); } Expression nestedProperty = field.Member switch { PropertyInfo propertyInfo => Expression.Property(context.GetInstance(), propertyInfo), MethodInfo methodInfo => Expression.Call(context.GetInstance(), methodInfo), null => throw ThrowHelper.QueryableFiltering_NoMemberDeclared(field), _ => throw ThrowHelper.QueryableFiltering_MemberInvalid(field.Member, field) }; context.PushInstance(nestedProperty); context.RuntimeTypes.Push(field.RuntimeType); action = SyntaxVisitor.Continue; return(true); }