private static object?DeserializeScalarResult(
            IInputType inputType,
            object?value,
            InputParser parser,
            Path path)
        {
            switch (value)
            {
            case IReadOnlyList <object> list:
            {
                IInputType elementType = inputType;
                Type       runtimeType = typeof(List <object>);
                if (inputType.IsListType())
                {
                    elementType = (IInputType)inputType.ElementType();
                    runtimeType = inputType.RuntimeType;
                }

                var deserializedList =
                    (IList)Activator.CreateInstance(runtimeType) !;

                var i = 0;
                foreach (var item in list)
                {
                    deserializedList.Add(
                        DeserializeScalarResult(elementType, item, parser, path.Append(i++)));
                }

                return(deserializedList);
            }

            case ListValueNode listLiteral:
            {
                var elementType = (IInputType)inputType.ElementType();
                var list        = new List <object?>();

                var i = 0;
                foreach (IValueNode item in listLiteral.Items)
                {
                    list.Add(
                        DeserializeScalarResult(elementType, item, parser, path.Append(i++)));
                }

                return(list);
            }

            case IValueNode literal:
                return(parser.ParseLiteral(literal, inputType, path));

            default:
                return(parser.ParseResult(value, inputType, path));
            }
        }
示例#2
0
        private static object ParseListType(
            IInputType sourceType,
            Type targetType,
            IValueNode literal)
        {
            var           sourceElementType = (IInputType)sourceType.ElementType();
            Type          targetElementType = GetListElementType(targetType);
            List <object> items             = new List <object>();

            if (literal is ListValueNode lv)
            {
                foreach (IValueNode element in lv.Items)
                {
                    items.Add(ParseLiteral(
                                  sourceElementType, targetElementType,
                                  element));
                }
            }
            else
            {
                items.Add(ParseLiteral(
                              sourceElementType, targetElementType,
                              literal));
            }

            return(CreateListObject(targetType, targetElementType, items));
        }
示例#3
0
        public bool TryHandle(
            FilterOperation operation,
            IInputType type,
            IValueNode value,
            IQueryableFilterVisitorContext context,
            [NotNullWhen(true)] out Expression?expression)
        {
            if (operation.Type == typeof(IComparable) &&
                type.IsInstanceOfType(value))
            {
                Expression property = context.GetInstance();

                if (!operation.IsSimpleArrayType())
                {
                    property = Expression.Property(context.GetInstance(), operation.Property);
                }

                if (operation.Kind == FilterOperationKind.In)
                {
                    expression = FilterExpressionBuilder.In(
                        property,
                        operation.Property.PropertyType,
                        ParseValue());
                    return(true);
                }

                if (operation.Kind == FilterOperationKind.NotIn)
                {
                    expression = FilterExpressionBuilder.Not(
                        FilterExpressionBuilder.In(
                            property,
                            operation.Property.PropertyType,
                            ParseValue()));
                    return(true);
                }
            }

            expression = null;
            return(false);

            object ParseValue()
            {
                var  parsedValue = type.ParseLiteral(value);
                Type elementType = type.ElementType().ToRuntimeType();

                if (operation.Property.PropertyType != elementType)
                {
                    Type listType = typeof(List <>).MakeGenericType(
                        operation.Property.PropertyType);

                    parsedValue = context.TypeConverter.Convert(
                        typeof(object),
                        listType,
                        parsedValue);
                }

                return(parsedValue);
            }
        }
        public bool TryHandle(
            FilterOperation operation,
            IInputType type,
            IValueNode value,
            Expression instance,
            ITypeConversion converter,
            out Expression expression)
        {
            if (operation.Type == typeof(IComparable) &&
                type.IsInstanceOfType(value))
            {
                MemberExpression property =
                    Expression.Property(instance, operation.Property);

                switch (operation.Kind)
                {
                case FilterOperationKind.In:
                    expression = FilterExpressionBuilder.In(
                        property,
                        operation.Property.PropertyType,
                        ParseValue());
                    return(true);

                case FilterOperationKind.NotIn:
                    expression = FilterExpressionBuilder.Not(
                        FilterExpressionBuilder.In(
                            property,
                            operation.Property.PropertyType,
                            ParseValue())
                        );
                    return(true);
                }
            }

            expression = null;
            return(false);

            object ParseValue()
            {
                var  parsedValue = type.ParseLiteral(value);
                Type elementType = type.ElementType().ToClrType();

                if (operation.Property.PropertyType != elementType)
                {
                    Type listType = typeof(List <>).MakeGenericType(
                        operation.Property.PropertyType);

                    parsedValue = converter.Convert(
                        typeof(object),
                        listType,
                        parsedValue);
                }

                return(parsedValue);
            }
        }
示例#5
0
        private static object?DeserializeEnumResult(IInputType inputType, object?value)
        {
            switch (value)
            {
            case IReadOnlyList <object> list:
            {
                var elementType      = (IInputType)inputType.ElementType();
                var deserializedList = (IList)Activator.CreateInstance(inputType.RuntimeType) !;

                foreach (object?item in list)
                {
                    deserializedList.Add(DeserializeEnumResult(elementType, item));
                }

                return(deserializedList);
            }

            case ListValueNode listLiteral:
            {
                var elementType = (IInputType)inputType.ElementType();
                var list        = new List <object?>();

                foreach (IValueNode item in listLiteral.Items)
                {
                    list.Add(DeserializeEnumResult(elementType, item));
                }

                return(list);
            }

            case StringValueNode stringLiteral:
                return(inputType.Deserialize(stringLiteral.Value));

            case IValueNode literal:
                return(inputType.ParseLiteral(literal));

            default:
                return(inputType.Deserialize(value));
            }
        }
 private static IValueNode ParseList(
     HashSet <object> processed,
     IInputType type,
     object fieldValue)
 {
     if (fieldValue is IEnumerable enumerable)
     {
         IType elementType = type.ElementType().InnerType();
         if (elementType is InputObjectType iot)
         {
             return(ParseObjectList(processed, iot, enumerable));
         }
         else
         {
             return(ParseScalarList((IInputType)elementType, enumerable));
         }
     }
     return(NullValueNode.Default);
 }
示例#7
0
        private static object ParseValue(
            object parsedValue,
            FilterOperation operation,
            IInputType type,
            IQueryableFilterVisitorContext context)
        {
            if (type.IsListType())
            {
                Type elementType = type.ElementType().ToClrType();

                if (operation.Property.PropertyType != elementType)
                {
                    Type listType = typeof(List <>).MakeGenericType(
                        operation.Property.PropertyType);

                    parsedValue = context.TypeConverter.Convert(
                        typeof(object),
                        listType,
                        parsedValue);
                }

                return(parsedValue);
            }
            else
            {
                if (!operation.Property.PropertyType.IsInstanceOfType(parsedValue))
                {
                    parsedValue = context.TypeConverter.Convert(
                        typeof(object),
                        operation.Property.PropertyType,
                        parsedValue);
                }

                return(parsedValue);
            }
        }