-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
DirectiveCollection.cs
223 lines (187 loc) · 6.99 KB
/
DirectiveCollection.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using HotChocolate.Configuration;
using HotChocolate.Language;
using HotChocolate.Types.Descriptors.Definitions;
using HotChocolate.Types.Helpers;
using HotChocolate.Utilities;
#nullable enable
namespace HotChocolate.Types;
/// <summary>
/// Represents a collection of directives of a <see cref="ITypeSystemMember"/>.
/// </summary>
public sealed class DirectiveCollection : IDirectiveCollection
{
private readonly Directive[] _directives;
private DirectiveCollection(Directive[] directives)
{
_directives = directives ?? throw new ArgumentNullException(nameof(directives));
}
/// <inheritdoc />
public int Count => _directives.Length;
/// <inheritdoc />
public IEnumerable<Directive> this[string directiveName]
{
get
{
var directives = _directives;
return directives.Length == 0
? Enumerable.Empty<Directive>()
: FindDirectives(directives, directiveName);
}
}
private static IEnumerable<Directive> FindDirectives(Directive[] directives, string name)
{
for (var i = 0; i < directives.Length; i++)
{
var directive = directives[i];
if (directive.Type.Name.EqualsOrdinal(name))
{
yield return directive;
}
}
}
/// <inheritdoc />
public Directive this[int index] => _directives[index];
/// <inheritdoc />
public Directive? FirstOrDefault(string directiveName)
{
directiveName.EnsureGraphQLName();
var span = _directives.AsSpan();
ref var start = ref MemoryMarshal.GetReference(span);
ref var end = ref Unsafe.Add(ref start, span.Length);
while (Unsafe.IsAddressLessThan(ref start, ref end))
{
if (start.Type.Name.EqualsOrdinal(directiveName))
{
return start;
}
// move pointer
start = ref Unsafe.Add(ref start, 1);
}
return null;
}
/// <inheritdoc />
public bool ContainsDirective(string directiveName)
=> FirstOrDefault(directiveName) is not null;
internal static DirectiveCollection CreateAndComplete(
ITypeCompletionContext context,
object source,
IReadOnlyList<DirectiveDefinition> definitions)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (definitions is null)
{
throw new ArgumentNullException(nameof(definitions));
}
var location = DirectiveHelper.InferDirectiveLocation(source);
var directives = new Directive[definitions.Count];
var directiveNames = TypeMemHelper.RentNameSet();
var hasErrors = false;
for (var i = 0; i < directives.Length; i++)
{
var definition = definitions[i];
var value = definition.Value;
if (context.TryGetDirectiveType(definition.Type, out var directiveType))
{
if ((directiveType.Locations & location) != location)
{
hasErrors = true;
var directiveNode = definition.Value as DirectiveNode;
context.ReportError(
ErrorHelper.DirectiveCollection_LocationNotAllowed(
directiveType,
location,
context.Type,
directiveNode,
source));
continue;
}
if (!directiveNames.Add(directiveType.Name) && !directiveType.IsRepeatable)
{
hasErrors = true;
var directiveNode = definition.Value as DirectiveNode;
context.ReportError(
ErrorHelper.DirectiveCollection_DirectiveIsUnique(
directiveType,
context.Type,
directiveNode,
source));
continue;
}
DirectiveNode? syntaxNode = null;
object? runtimeValue = null;
try
{
// We will parse or format the directive.
// This will also validate the directive instance values which could
// cause and error.
if (value is DirectiveNode node)
{
syntaxNode = node;
runtimeValue = directiveType.Parse(node);
}
else
{
syntaxNode = directiveType.Format(value);
runtimeValue = value;
}
}
catch (SerializationException ex)
{
hasErrors = true;
Debug.Assert(
ex.Path is not null,
"The path is always passed in with directives!");
context.ReportError(
ErrorHelper.DirectiveCollection_ArgumentError(
directiveType,
syntaxNode,
source,
ex.Path,
ex));
}
if (syntaxNode is not null && runtimeValue is not null)
{
directives[i] = new Directive(directiveType, syntaxNode, runtimeValue);
}
}
}
// If we had any errors while building the directives list we will
// clean the null entries out so that the list is consistent.
// We only do that so we can collect other schema errors as well and do
// not have to fully fail here but have one SchemaException at the end of
// the schema creation that contains a list of errors.
if (hasErrors)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
directives = directives.Where(t => t is not null).ToArray();
}
return new DirectiveCollection(directives);
}
internal ReadOnlySpan<Directive> AsSpan()
=> _directives;
internal ref Directive GetReference()
#if NET6_0_OR_GREATER
=> ref MemoryMarshal.GetArrayDataReference(_directives);
#else
=> ref MemoryMarshal.GetReference(_directives.AsSpan());
#endif
/// <inheritdoc />
public IEnumerator<Directive> GetEnumerator()
=> ((IEnumerable<Directive>)_directives).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
}