public override object WrapValue(GraphEntityInfo entityInfo, GraphTypeInfo typeInfo, object value, bool isSpecified) { var input = value as Dictionary <string, object>; if (typeInfo.IsInputType && input != null) { var obj = Activator.CreateInstance(typeInfo.GetTypeRepresentation().AsType()); foreach (var field in typeInfo.Fields.Where(field => !field.IsIgnored)) { object fieldValue; isSpecified = true; if (!input.TryGetValue(field.Name, out fieldValue)) { if (!field.Type.IsNullable && field.DefaultValue == null) { throw new Exception($"Value for non-nullable field '{field.Name}' not provided."); } isSpecified = false; fieldValue = field.DefaultValue; } var propertyInfo = field.AttributeProvider as PropertyInfo; if (propertyInfo == null) { throw new Exception($"Invalid field '{field.Name}' on input object; must be a property."); } propertyInfo.SetValue(obj, _parent.Wrap(field, field.Type, fieldValue, isSpecified)); } return(obj); } return(value); }
public object Wrap(GraphEntityInfo entity, GraphTypeInfo type, object value, bool isSpecified) { value = WrapValue(entity, type, value, isSpecified); return(NextWrapper != null ? NextWrapper.Wrap(entity, type, value, isSpecified) : value); }
public override object WrapValue(GraphEntityInfo entityInfo, GraphTypeInfo typeInfo, object value, bool isSpecified) { if (value == null || !typeInfo.IsPrimitive || typeInfo.IsEnumerationType) { return(value); } try { var targetType = typeInfo.GetTypeRepresentation().AsType(); var converter = TypeDescriptor.GetConverter(targetType); if (converter.CanConvertFrom(value.GetType())) { return(converter.ConvertFrom(value)); } if (value is IConvertible) { return(Convert.ChangeType(value, targetType)); } } catch (Exception ex) { throw new ArgumentException($"Unable to cast {GetEntityDescription(entityInfo)} '{entityInfo.Name}' to '{typeInfo.Name}'.", ex); } return(value); }
private static string DeriveTypeName(GraphEntityInfo entityInfo, TypeInfo typeInfo) { var entity = entityInfo.TypeResolver.DeriveType(typeInfo); entityInfo.TypeResolver.ApplyAttributes(entity); return(entity.Name); }
public override object WrapValue(GraphEntityInfo entityInfo, GraphTypeInfo typeInfo, object value, bool isSpecified) { if (typeInfo.TypeRepresentation.IsGenericType(typeof(Optional <>))) { return(Optional.Construct(typeInfo, value, isSpecified)); } return(value); }
public GraphEntityInfo ApplyAttributes(GraphEntityInfo entityInfo) { var attributeProvider = entityInfo.AttributeProvider is TypeInfo ? GetTypeInfo(entityInfo.AttributeProvider) : entityInfo.AttributeProvider; _metaDataHandler.DeriveMetaData(entityInfo, attributeProvider); return(entityInfo); }
public override void DeriveMetaData(GraphEntityInfo entity) { if (!string.IsNullOrWhiteSpace(_name)) { entity.Name = _name; } else { MapEntity(entity); } }
public override object WrapValue(GraphEntityInfo entityInfo, GraphTypeInfo typeInfo, object value, bool isSpecified) { if (!typeInfo.IsNullable) { if (value == null) { throw new ArgumentException($"Null value provided for non-nullable {GetEntityDescription(entityInfo)} '{entityInfo.Name}'."); } return(NonNull.Construct(typeInfo, value)); } return(value); }
public void MapEntity(GraphEntityInfo entity) { var argumentEntity = entity as GraphArgumentInfo; var parameterInfo = entity.AttributeProvider as ParameterInfo; if (argumentEntity != null && parameterInfo != null) { _mappableTarget.MapArgument(argumentEntity, parameterInfo); return; } var fieldEntity = entity as GraphFieldInfo; if (fieldEntity != null) { var memberInfo = entity.AttributeProvider as MemberInfo; if (memberInfo is PropertyInfo || memberInfo is MethodInfo) { _mappableTarget.MapField(fieldEntity, memberInfo); return; } var fieldInfo = entity.AttributeProvider as FieldInfo; if (fieldInfo != null) { if (fieldInfo.IsLiteral) { _mappableTarget.MapEnumMember(fieldEntity, fieldInfo); } else { _mappableTarget.MapField(fieldEntity, fieldInfo); } return; } } var typeEntity = entity as GraphTypeInfo; var typeInfo = entity.AttributeProvider as TypeInfo; if (typeEntity != null && typeInfo != null) { _mappableTarget.MapType(typeEntity, typeInfo); return; } throw new ArgumentException("Unable to map provided object."); }
protected string GetEntityDescription(GraphEntityInfo entity) { if (entity is GraphTypeInfo) { return("type"); } if (entity is GraphFieldInfo) { return("field"); } if (entity is GraphArgumentInfo) { return("argument"); } return("entity"); }
public override object WrapValue(GraphEntityInfo entityInfo, GraphTypeInfo typeInfo, object value, bool isSpecified) { if (typeInfo.IsPrimitive && !typeInfo.IsEnumerationType && value is IConvertible) { try { return(Convert.ChangeType(value, typeInfo.GetTypeRepresentation().AsType())); } catch (Exception ex) { throw new ArgumentException($"Unable to cast {GetEntityDescription(entityInfo)} '{entityInfo.Name}' to '{typeInfo.Name}'.", ex); } } return(value); }
public override object WrapValue(GraphEntityInfo entityInfo, GraphTypeInfo typeInfo, object value, bool isSpecified) { if (typeInfo.IsListType) { if (!(value is IEnumerable input)) { return(null); } var elementType = typeInfo.TypeParameter.TypeRepresentation.AsType(); var listType = typeof(List <>).MakeGenericType(elementType); var list = Activator.CreateInstance(listType) as IList; foreach (var obj in input) { list.Add(_parent.Wrap(entityInfo, typeInfo.TypeParameter, obj, true)); } return(typeInfo.IsArrayType ? list.ConvertToArrayRuntime(elementType) : list); } return(value); }
public static void ShouldNotBeDescribed(this GraphEntityInfo entity) { entity.Description.ShouldBeEmpty(); }
public override object WrapValue(GraphEntityInfo entityInfo, GraphTypeInfo typeInfo, object value, bool _) { var isNull = value == null; var shouldReturnValue = isNull; if (shouldReturnValue) { return(null); } var valueType = value.GetType(); var isPrimitive = typeInfo.IsPrimitive; isPrimitive |= valueType.GetTypeInfo().IsPrimitive; isPrimitive |= value is string; shouldReturnValue |= isPrimitive; if (shouldReturnValue) { return(value); } var isNonNull = typeof(NonNull <string>).Name == typeInfo.TypeRepresentation.Name; shouldReturnValue |= isNonNull ? typeInfo.TypeRepresentation.GenericTypeArguments.First() == valueType : typeInfo.TypeRepresentation.AsType() == valueType; if (shouldReturnValue) { return(value); } if (typeInfo.IsScalarType) { return(Activator.CreateInstance(typeInfo.GetTypeRepresentation().AsType(), value)); } if (!typeInfo.IsInputType || !(value is Dictionary <string, object> input)) { return(value); } var obj = Activator.CreateInstance(typeInfo.GetTypeRepresentation().AsType()); foreach (var field in typeInfo.Fields.Where(field => !field.IsIgnored)) { var isSpecified = true; if (!input.TryGetValue(field.Name, out var fieldValue)) { if (!field.Type.IsNullable && field.DefaultValue == null) { throw new Exception($"Value for non-nullable field '{field.Name}' not provided."); } isSpecified = false; fieldValue = field.DefaultValue; } if (!(field.AttributeProvider is PropertyInfo propertyInfo)) { throw new Exception($"Invalid field '{field.Name}' on input object; must be a property."); } propertyInfo.SetValue(obj, _parent.Wrap(field, field.Type, fieldValue, isSpecified)); } return(obj); }
public override void DeriveMetaData(GraphEntityInfo entity) { entity.Description = _description; }
public static void ShouldBeNamed(this GraphEntityInfo entity, string name) { entity.Name.ShouldEqual(name); }
public override void DeriveMetaData(GraphEntityInfo entity) { entity.IsIgnored = true; }
public static void ShouldBeDeprecatedWithReason(this GraphEntityInfo entity, string reason) { entity.IsDeprecated.ShouldEqual(true); entity.DeprecationReason.ShouldEqual(reason); }
public static void AndWithDeprecationReason(this GraphEntityInfo entity, string reason) { entity.ShouldBeDeprecatedWithReason(reason); }
public static void ShouldNotBeDeprecated(this GraphEntityInfo entity) { entity.IsDeprecated.ShouldEqual(false); }
public static void AndWithoutDeprecationReason(this GraphEntityInfo entity) { entity.ShouldNotBeDeprecated(); }
public static void AndWithDescription(this GraphEntityInfo entity, string description) { entity.ShouldHaveDescription(description); }
public abstract object WrapValue(GraphEntityInfo entityInfo, GraphTypeInfo typeInfo, object value, bool isSpecified);
public virtual bool ShouldBeApplied(GraphEntityInfo entity) { return(true); }
public virtual void DeriveMetaData(GraphEntityInfo entity) { MapEntity(entity); }
protected void MapEntity(GraphEntityInfo entity) { _entityMapper.MapEntity(entity); }
public override object WrapValue(GraphEntityInfo entityInfo, GraphTypeInfo typeInfo, object value) => value;
public static void AndWithoutDescription(this GraphEntityInfo entity) { entity.ShouldNotBeDescribed(); }
public GraphEntityInfo ApplyAttributes(GraphEntityInfo entityInfo) => _reflector.ApplyAttributes(entityInfo);
public static void ShouldHaveDescription(this GraphEntityInfo entity, string description) { entity.Description.ShouldEqual(description); }