private static void CompleteValue( ICompleteValueContext context, IType type, object result) { if (type.IsNonNullType()) { CompleteValue(context, type.InnerType(), result); HandleNonNullViolation(context); } else if (result is null) { context.Value = null; } else if (IsError(result)) { HandleErrors(context, result); } else if (type.IsListType()) { CompleteList(context, type.ElementType(), result); } else if (type is ILeafType leafType) { CompleteLeafType(context, leafType, result); } else if (type.IsCompositeType()) { CompleteCompositeType(context, type, result); } else { throw new NotSupportedException(); } }
private static void CompleteCompositeType( ICompleteValueContext context, IType type, object result) { ObjectType objectType = context.LocalContextData.Count != 0 && context.LocalContextData.TryGetValue( WellKnownContextData.Type, out object o) && o is NameString typeName ? context.ResolveObjectType(typeName) : context.ResolveObjectType(type, result); if (objectType == null) { context.AddError(b => b.SetMessage(string.Format( CultureInfo.InvariantCulture, CoreResources.CompleteCompositeType_UnknownSchemaType, result.GetType().GetTypeName(), type.NamedType().Name.Value))); context.Value = null; } else { context.Value = context.EnqueueForProcessing(objectType, result); } }
private static void HandleNonNullViolation( ICompleteValueContext context) { if (context.Value is null) { if (!context.HasErrors) { context.AddError(b => b.SetMessage( CoreResources.HandleNonNullViolation_Message)); } context.IsViolatingNonNullType = true; } }
private static void CompleteList( ICompleteValueContext context, IType elementType, object result) { if (result is IEnumerable collection) { var i = 0; var list = new List <object>(); Path path = context.Path; foreach (var element in collection) { list.Add(null); var local = i; context.Value = null; context.Path = path.Append(i); context.SetElementNull = () => list[local] = null; CompleteValue(context, elementType, element); if (context.IsViolatingNonNullType) { context.IsViolatingNonNullType = false; context.Value = null; context.Path = path; return; } list[i] = context.Value; i++; } context.IsViolatingNonNullType = false; context.Path = path; context.Value = list; } else { context.AddError(b => b.SetMessage(string.Format( CultureInfo.InvariantCulture, CoreResources.CompleteList_ListTypeInvalid, typeof(IEnumerable).FullName))); context.Value = null; } }
private static void HandleErrors( ICompleteValueContext context, object result) { if (result is IError error) { context.AddError(error); context.Value = null; } else if (result is IEnumerable <IError> errors) { foreach (IError err in errors) { context.AddError(err); } context.Value = null; } }
private static void CompleteCompositeType( ICompleteValueContext context, IType type, object result) { ObjectType objectType = context.ResolveObjectType(type, result); if (objectType == null) { context.AddError(b => b.SetMessage(string.Format( CultureInfo.InvariantCulture, CoreResources.CompleteCompositeType_UnknownSchemaType, context.Value.GetType().GetTypeName()))); context.Value = null; } else { var objectResult = new OrderedDictionary(); context.Value = objectResult; context.EnqueueForProcessing(objectType, objectResult, result); } }
private static void CompleteLeafType( ICompleteValueContext context, ILeafType leafType, object result) { try { if (TryConvertLeafValue( context.Converter, leafType, result, out object converted)) { context.Value = leafType.Serialize(converted); } else { context.AddError(b => b.SetMessage(string.Format( CultureInfo.InvariantCulture, CoreResources.CompleteLeafType_CannotConvertValue, leafType.TypeName()))); } } catch (ScalarSerializationException ex) { context.AddError(b => b.SetMessage(ex.Message) .SetException(ex)); } catch (Exception ex) { context.AddError(b => b.SetMessage(CoreResources .CompleteLeadType_UndefinedError) .SetException(ex)); } }