Skip to content

Commit

Permalink
Fixed dictionary middleware. (#2492)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Oct 27, 2020
1 parent ab0edc3 commit f491d2f
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 349 deletions.
153 changes: 0 additions & 153 deletions src/HotChocolate/Core/src/Abstractions/Execution/FieldData.cs

This file was deleted.

62 changes: 0 additions & 62 deletions src/HotChocolate/Core/src/Abstractions/Execution/FieldValue.cs

This file was deleted.

Expand Up @@ -24,9 +24,9 @@ public ResultValue(string name, object? value, bool isNullable)

public override bool Equals(object? obj)
{
return obj is FieldValue value &&
return obj is ResultValue value &&
HasValue == value.HasValue &&
Name == value.Key &&
Name == value.Name &&
Value == value.Value;
}

Expand Down Expand Up @@ -55,8 +55,8 @@ public override int GetHashCode()
{
unchecked
{
int hash = (Name?.GetHashCode() ?? 0) * 3;
hash = hash ^ ((Value?.GetHashCode() ?? 0) * 7);
var hash = (Name?.GetHashCode() ?? 0) * 3;
hash ^= (Value?.GetHashCode() ?? 0) * 7;
return hash;
}
}
Expand Down
@@ -0,0 +1,112 @@
using System;
using System.Collections;
using System.Collections.Generic;
using HotChocolate.Language;
using HotChocolate.Types;

namespace HotChocolate.Stitching.Delegation
{
internal static class DictionaryDeserializer
{
public static object? DeserializeResult(
IType fieldType,
object? obj)
{
INamedType namedType = fieldType.NamedType();

if (namedType is IInputType && fieldType is IInputType inputType)
{
if (namedType.Kind == TypeKind.Enum)
{
return DeserializeEnumResult(inputType, obj);
}

if (namedType.Kind == TypeKind.Scalar)
{
return DeserializeScalarResult(inputType, obj);
}
}

return obj is NullValueNode ? null : obj;
}

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 object? DeserializeScalarResult(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 IValueNode literal:
return inputType.ParseLiteral(literal);

default:
return inputType.Deserialize(value);
}
}
}
}

0 comments on commit f491d2f

Please sign in to comment.