This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ArgumentParser.cs
149 lines (130 loc) · 4.34 KB
/
ArgumentParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using HotChocolate.Language;
using HotChocolate.Utilities;
namespace ApolloGraphQL.HotChocolate.Federation.Helpers;
/// <summary>
/// A helper for getting field values from a representation object.
/// </summary>
internal static class ArgumentParser
{
public static T? GetValue<T>(
IValueNode valueNode,
IType type,
string[] path)
=> TryGetValue<T>(valueNode, type, path, 0, out var value) ? value : default;
public static bool TryGetValue<T>(
IValueNode valueNode,
IType type,
string[] path,
out T? value)
=> TryGetValue<T>(valueNode, type, path, 0, out value);
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static bool TryGetValue<T>(
IValueNode valueNode,
IType type,
string[] path,
int i,
out T? value)
{
// TODO does not support list
switch (valueNode.Kind)
{
case SyntaxKind.ObjectValue:
var current = path[i];
if (type.IsNonNullType())
{
return TryGetValue(valueNode, type.InnerType(), path, i, out value);
}
if (type is not IComplexOutputType complexType ||
!complexType.Fields.TryGetField(current, out var field))
{
break;
}
foreach (var fieldValue in ((ObjectValueNode)valueNode).Fields)
{
if (fieldValue.Name.Value.EqualsOrdinal(current))
{
if (path.Length < ++i && field.Type.IsCompositeType())
{
break;
}
return TryGetValue(fieldValue.Value, field.Type, path, i, out value);
}
}
break;
case SyntaxKind.NullValue:
value = default(T);
return true;
case SyntaxKind.StringValue:
case SyntaxKind.IntValue:
case SyntaxKind.FloatValue:
case SyntaxKind.BooleanValue:
if (type.NamedType() is not ScalarType scalarType)
{
break;
}
value = (T)scalarType.ParseLiteral(valueNode)!;
return true;
case SyntaxKind.EnumValue:
if (type.NamedType() is not EnumType enumType)
{
break;
}
value = (T)enumType.ParseLiteral(valueNode)!;
return true;
}
value = default;
return false;
}
public static bool Matches(IValueNode valueNode, IReadOnlyList<string[]> required)
{
if (required.Count == 1)
{
return Matches(valueNode, required[0], 0);
}
if (required.Count == 2)
{
return Matches(valueNode, required[0], 0) &&
Matches(valueNode, required[1], 0);
}
for (var i = 0; i < required.Count; i++)
{
if (!Matches(valueNode, required[i], 0))
{
return false;
}
}
return true;
}
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static bool Matches(IValueNode valueNode, string[] path, int i)
{
switch (valueNode.Kind)
{
// TODO does not handle list
case SyntaxKind.ObjectValue:
var current = path[i];
foreach (var fieldValue in ((ObjectValueNode)valueNode).Fields)
{
if (fieldValue.Name.Value.EqualsOrdinal(current))
{
if (path.Length >= ++i)
{
return Matches(fieldValue.Value, path, i);
}
break;
}
}
break;
case SyntaxKind.NullValue:
case SyntaxKind.StringValue:
case SyntaxKind.IntValue:
case SyntaxKind.FloatValue:
case SyntaxKind.BooleanValue:
case SyntaxKind.EnumValue:
return true;
}
return false;
}
}