Skip to content

Commit be806d1

Browse files
authored
Refactor anonymous enum processor (#20)
* Reimplement AnonymousEnumProcessor as AnonymousEnumParameterProcessor * Allow empty TypeNameExtensions.CreateTypeName(lastPropertySpan)
1 parent fc1cbd7 commit be806d1

File tree

7 files changed

+207
-208
lines changed

7 files changed

+207
-208
lines changed

src/Apple.AppStoreConnect.OpenApiDocument.Generator/Extensions/TypeNameExtensions.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ ReadOnlySpan<char> lastPropertySpan
1717
].AsSpan();
1818

1919
typePrefix.CopyTo(titleSpan);
20-
lastPropertySpan.CopyTo(titleSpan[componentNameLength..]);
2120

22-
if (char.IsLower(titleSpan[componentNameLength]))
21+
if (titleSpan.Length > typePrefix.Length)
2322
{
24-
titleSpan[componentNameLength] = char.ToUpperInvariant(titleSpan[componentNameLength]);
23+
lastPropertySpan.CopyTo(titleSpan[componentNameLength..]);
24+
25+
if (char.IsLower(titleSpan[componentNameLength]))
26+
{
27+
titleSpan[componentNameLength] = char.ToUpperInvariant(titleSpan[componentNameLength]);
28+
}
2529
}
2630

2731
return titleSpan;

src/Apple.AppStoreConnect.OpenApiDocument.Generator/JsonIterator.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static void ProcessJson(
1515
{
1616
var path = new Stack<PathItem>();
1717
ReadOnlySpan<byte> lastProperty = null;
18-
var context = new TransposeContext();
18+
using var context = new TransposeContext();
1919

2020
while (jsonReader.Read())
2121
{
@@ -119,7 +119,7 @@ TransposeContext context
119119
path,
120120
lastProperty,
121121
ref jsonReader, jsonWriter
122-
) || AnonymousEnumProcessor.TryProcessItem(
122+
) || AnonymousEnumParameterProcessor.TryProcessItem(
123123
path,
124124
lastProperty,
125125
ref jsonReader, jsonWriter,
@@ -139,12 +139,7 @@ private static bool TryWriteAdditional(
139139
TransposeContext context
140140
)
141141
{
142-
var response = AnonymousEnumProcessor.TryWriteAdditional(
143-
pathItem,
144-
path,
145-
jsonWriter,
146-
context
147-
);
142+
var response = false;
148143

149144
response = AnonymousComplexPropertyProcessor.TryWriteAdditional(
150145
pathItem,

src/Apple.AppStoreConnect.OpenApiDocument.Generator/Processors/AnonymousComplexPropertyProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ TransposeContext context
201201
})
202202
)
203203
{
204-
foreach (var renamedComponentValue in context.RenamedComponentValues)
204+
foreach (var renamedComponentValue in context.NewComponents)
205205
{
206206
jsonWriter.WritePropertyName(renamedComponentValue.Key);
207207
renamedComponentValue.Value.WriteTo(jsonWriter);

src/Apple.AppStoreConnect.OpenApiDocument.Generator/Processors/AnonymousEnumProcessor.cs renamed to src/Apple.AppStoreConnect.OpenApiDocument.Generator/Processors/AnonymousEnumParameterProcessor.cs

Lines changed: 138 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Apple.AppStoreConnect.OpenApiDocument.Generator.Processors;
99

10-
public static class AnonymousEnumProcessor
10+
public static class AnonymousEnumParameterProcessor
1111
{
1212
public static bool TryProcessItem(
1313
IReadOnlyCollection<PathItem> path,
@@ -37,9 +37,8 @@ TransposeContext context
3737

3838
if (
3939
path.ElementAt(0).Properties.TryGetValue("name", out var name)
40-
&& path.ElementAt(2).Properties.TryGetValue("tags", out var tag)
4140
&& path.ElementAt(2).Properties.TryGetValue("operationId", out var operationId)
42-
&& TryReadInner(path, tag, name, operationId, ref jsonReaderClone, jsonWriter, context)
41+
&& TryReadInner(path, name, operationId, ref jsonReaderClone, jsonWriter, context)
4342
)
4443
{
4544
var innerTokenStartIndex = jsonReaderClone.TokenStartIndex;
@@ -56,52 +55,9 @@ TransposeContext context
5655
return false;
5756
}
5857

59-
public static bool TryWriteAdditional(
60-
PathItem? pathItem,
61-
IReadOnlyCollection<PathItem> path,
62-
Utf8JsonWriter jsonWriter,
63-
TransposeContext context
64-
)
65-
{
66-
if (
67-
pathItem == new PathItem(JsonTokenType.StartObject, "schemas")
68-
&& path.SequenceEqual(new PathItem[]
69-
{
70-
new(JsonTokenType.StartObject, "components"),
71-
new(JsonTokenType.StartObject, null),
72-
})
73-
)
74-
{
75-
foreach (var enumComponentValue in context.EnumComponentValues)
76-
{
77-
jsonWriter.WritePropertyName(enumComponentValue.Key);
78-
jsonWriter.WriteStartObject();
79-
80-
jsonWriter.WritePropertyName("type"u8);
81-
jsonWriter.WriteStringValue("string"u8);
82-
83-
jsonWriter.WritePropertyName("enum"u8);
84-
jsonWriter.WriteStartArray();
85-
86-
foreach (var enumValue in enumComponentValue.Value)
87-
{
88-
jsonWriter.WriteStringValue(enumValue);
89-
}
90-
91-
jsonWriter.WriteEndArray();
92-
93-
jsonWriter.WriteEndObject();
94-
}
95-
96-
return true;
97-
}
98-
99-
return false;
100-
}
101-
10258
private static bool TryReadInner(
10359
IReadOnlyCollection<PathItem> parentPath,
104-
string tag, string name, string operationId,
60+
string name, string operationId,
10561
ref Utf8JsonReader jsonReader, Utf8JsonWriter jsonWriter,
10662
TransposeContext context
10763
)
@@ -232,10 +188,15 @@ TransposeContext context
232188
return false;
233189
}
234190

235-
var reference = context.GetEnumComponentReference(
191+
var componentPrefix = GetComponentPrefix(
236192
parentPath,
237-
tag, name.AsSpan(),
238-
operationId.AsSpan(),
193+
name.AsSpan(),
194+
operationId.AsSpan()
195+
);
196+
197+
var reference = context.GetEnumComponentReference(
198+
componentPrefix,
199+
ReadOnlySpan<char>.Empty,
239200
enumValues
240201
);
241202

@@ -254,4 +215,131 @@ TransposeContext context
254215

255216
return true;
256217
}
218+
219+
private static ReadOnlySpan<char> GetComponentPrefix(
220+
IReadOnlyCollection<PathItem> parentPath,
221+
ReadOnlySpan<char> parameterName,
222+
ReadOnlySpan<char> operationId
223+
)
224+
{
225+
var componentName = GetComponentName(parameterName);
226+
227+
var urlPath = parentPath.ElementAt(3).PropertyName!.ToArray().AsSpan();
228+
if (urlPath[0] == '/')
229+
{
230+
urlPath = urlPath[1..];
231+
}
232+
233+
var slashIndex = urlPath.IndexOf('/');
234+
var urlVersion = urlPath[..slashIndex];
235+
236+
if (char.IsLower(urlVersion[0]))
237+
{
238+
urlVersion[0] = char.ToUpperInvariant(urlVersion[0]);
239+
}
240+
241+
if (operationId.IndexOf('-') is var dashIndex and > 0)
242+
{
243+
var operationGroupName = operationId[..dashIndex];
244+
dashIndex++;
245+
operationId = operationId[dashIndex..];
246+
dashIndex = operationId.IndexOf('-');
247+
248+
ReadOnlySpan<char> operationSuffix;
249+
if (dashIndex > 0)
250+
{
251+
operationSuffix = operationId[(dashIndex + 1)..];
252+
operationId = operationId[..dashIndex];
253+
}
254+
else
255+
{
256+
operationSuffix = operationId;
257+
operationId = default;
258+
}
259+
260+
if (
261+
operationGroupName.Length > 0
262+
&& char.IsLower(operationGroupName[0])
263+
)
264+
{
265+
var operationGroupNameSpan = operationGroupName.ToArray().AsSpan();
266+
operationGroupNameSpan[0] = char.ToUpperInvariant(operationGroupNameSpan[0]);
267+
268+
operationGroupName = operationGroupNameSpan;
269+
}
270+
271+
if (
272+
operationId.Length > 0
273+
&& char.IsLower(operationId[0])
274+
)
275+
{
276+
var operationIdSpan = operationId.ToArray().AsSpan();
277+
operationIdSpan[0] = char.ToUpperInvariant(operationIdSpan[0]);
278+
279+
operationId = operationIdSpan;
280+
}
281+
282+
var i = 0;
283+
Span<char> operationName = new char[operationSuffix.Length];
284+
while (operationSuffix.IndexOf('_') is var underscoreIndex and > 0)
285+
{
286+
var token = operationSuffix[..underscoreIndex];
287+
token.CopyTo(operationName[i..]);
288+
289+
if (char.IsLower(operationName[i]))
290+
{
291+
operationName[i] = char.ToUpperInvariant(operationName[i]);
292+
}
293+
294+
i += token.Length;
295+
operationSuffix = operationSuffix[(underscoreIndex + 1)..];
296+
}
297+
298+
operationSuffix.CopyTo(operationName[i..]);
299+
if (char.IsLower(operationName[i]))
300+
{
301+
operationName[i] = char.ToUpperInvariant(operationName[i]);
302+
}
303+
304+
i += operationSuffix.Length;
305+
operationName = operationName[..i];
306+
307+
return
308+
$"{operationGroupName.ToString()}{urlVersion.ToString()}Operation{operationId.ToString()}{operationName.ToString()}{componentName}"
309+
.AsSpan();
310+
}
311+
312+
return $"{urlVersion.ToString()}Operation{operationId.ToString()}{componentName}".AsSpan();
313+
}
314+
315+
private static string GetComponentName(
316+
ReadOnlySpan<char> parameterName
317+
)
318+
{
319+
var openingBracket = parameterName.IndexOf('[');
320+
321+
if (openingBracket > 0)
322+
{
323+
openingBracket++;
324+
parameterName = parameterName[openingBracket..];
325+
326+
var closingBracket = parameterName.IndexOf(']');
327+
328+
if (closingBracket > 0)
329+
{
330+
parameterName = parameterName[..closingBracket];
331+
}
332+
}
333+
334+
if (char.IsLower(parameterName[0]))
335+
{
336+
var upper = new char[parameterName.Length].AsSpan();
337+
338+
parameterName.CopyTo(upper);
339+
upper[0] = char.ToUpperInvariant(parameterName[0]);
340+
parameterName = upper;
341+
}
342+
343+
return $"Parameter{parameterName.ToString()}";
344+
}
257345
}

0 commit comments

Comments
 (0)