7
7
8
8
namespace Apple . AppStoreConnect . OpenApiDocument . Generator . Processors ;
9
9
10
- public static class AnonymousEnumProcessor
10
+ public static class AnonymousEnumParameterProcessor
11
11
{
12
12
public static bool TryProcessItem (
13
13
IReadOnlyCollection < PathItem > path ,
@@ -37,9 +37,8 @@ TransposeContext context
37
37
38
38
if (
39
39
path . ElementAt ( 0 ) . Properties . TryGetValue ( "name" , out var name )
40
- && path . ElementAt ( 2 ) . Properties . TryGetValue ( "tags" , out var tag )
41
40
&& 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 )
43
42
)
44
43
{
45
44
var innerTokenStartIndex = jsonReaderClone . TokenStartIndex ;
@@ -56,52 +55,9 @@ TransposeContext context
56
55
return false ;
57
56
}
58
57
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
-
102
58
private static bool TryReadInner (
103
59
IReadOnlyCollection < PathItem > parentPath ,
104
- string tag , string name , string operationId ,
60
+ string name , string operationId ,
105
61
ref Utf8JsonReader jsonReader , Utf8JsonWriter jsonWriter ,
106
62
TransposeContext context
107
63
)
@@ -232,10 +188,15 @@ TransposeContext context
232
188
return false ;
233
189
}
234
190
235
- var reference = context . GetEnumComponentReference (
191
+ var componentPrefix = GetComponentPrefix (
236
192
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 ,
239
200
enumValues
240
201
) ;
241
202
@@ -254,4 +215,131 @@ TransposeContext context
254
215
255
216
return true ;
256
217
}
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
+ }
257
345
}
0 commit comments