private void VisitValue( IInputType type, object obj, Action <object> setValue, ISet <object> processed) { if (obj is null) { setValue(null); } else if (type.IsListType()) { VisitList( (ListType)type.ListType(), obj, setValue, processed); } else if (type.IsLeafType()) { VisitLeaf( (INamedInputType)type.NamedType(), obj, setValue, processed); } else if (type.IsInputObjectType()) { VisitInputObject( (InputObjectType)type.NamedType(), obj, setValue, processed); } else { throw new NotSupportedException(); } }
private static object ConvertValue( ITypeConversion converter, IInputType type, object value) { Type sourceType = typeof(object); if (type.IsListType() && value is IEnumerable <object> e) { if (e.Any()) { Type elementType = e.FirstOrDefault()?.GetType(); if (elementType != null) { sourceType = typeof(IEnumerable <>).MakeGenericType(elementType); } } else { return(Activator.CreateInstance(type.ClrType)); } } return(converter.Convert(sourceType, type.ClrType, value)); }
private static IValueNode ParseValue( HashSet <object> processed, IInputType inputType, object obj) { if (inputType.IsNonNullType()) { return(ParseValue(processed, (IInputType)inputType.InnerType(), obj)); } else if (inputType.IsListType()) { return(ParseList(processed, inputType, obj)); } else if (inputType.IsScalarType() || inputType.IsEnumType()) { return(ParseScalar(inputType, obj)); } else if (inputType.IsInputObjectType() && !processed.Contains(obj)) { return(ParseObject(processed, (InputObjectType)inputType, obj)); } else { return(NullValueNode.Default); } }
private static void HandleFieldValue( HashSet <object> processed, Dictionary <string, IValueNode> fieldValues, InputField field, IInputType fieldType, object fieldValue) { if (fieldValue == null) { fieldValues[field.Name] = NullValueNode.Default; } else if (fieldType.IsNonNullType()) { HandleFieldValue(processed, fieldValues, field, (IInputType)fieldType.InnerType(), fieldValue); } else if (fieldType.IsListType()) { fieldValues[field.Name] = ParseList( processed, fieldType, fieldValue); } else if (fieldType.IsScalarType() || fieldType.IsEnumType()) { fieldValues[field.Name] = ParseScalar( fieldType, fieldValue); } else if (fieldType.IsInputObjectType() && !processed.Contains(fieldValue)) { fieldValues[field.Name] = ParseObject( processed, (InputObjectType)fieldType, fieldValue); } }
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)); } }
private bool ValueNeedsCleanUp(IInputType type, IValueNode value) { if (type.IsEnumType() && value is StringValueNode) { return(true); } if (type.IsInputObjectType() && type.NamedType() is InputObjectType iot && value is ObjectValueNode ov) { return(ObjectNeedsCleanUp(iot, ov)); } if (type.IsListType() && value is ListValueNode listValue) { return(ListNeedsCleanUp(type.ListType(), listValue)); } return(false); }
private object DeserializeValue(IInputType type, object value) { if (type.IsLeafType() && type.NamedType() is ISerializableType serializable && serializable.TryDeserialize(value, out object deserialized)) { return(deserialized); } if (type.IsListType() && value is IList <object> ) { return(_inputTypeConverter.Convert(value, type)); } if (type.IsInputObjectType() && value is IDictionary <string, object> ) { return(_inputTypeConverter.Convert(value, type)); } return(value); }
public static object ParseLiteral( IInputType sourceType, Type targetType, IValueNode literal) { if (sourceType.IsScalarType() || sourceType.IsEnumType()) { return(ParseScalarType(sourceType, targetType, literal)); } else if (sourceType.IsListType()) { return(ParseListType(sourceType, targetType, literal)); } else if (sourceType.IsInputObjectType() && sourceType.NamedType() is InputObjectType iot) { return(ParseObjectType(iot, targetType, literal)); } throw new NotSupportedException( "The serializer does not support the specified source type."); }
private IValueNode RebuildValue(IInputType type, IValueNode value) { if (type.IsEnumType() || type.IsScalarType()) { return(RebuildScalarValue(type, value)); } if (type.IsListType() && type.ListType() is ListType lt && value is ListValueNode lv) { return(RebuildListValue(lt, lv)); } if (type.IsInputObjectType() && type.NamedType() is InputObjectType iot && value is ObjectValueNode ov) { return(RebuildObjectValue(iot, ov)); } return(value); }
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); } }