1
1
using System . Collections ;
2
2
using System . Collections . ObjectModel ;
3
- using System . Diagnostics . CodeAnalysis ;
4
3
using System . Reflection ;
5
4
using System . Text . Json . Serialization ;
6
5
@@ -79,7 +78,7 @@ public static V1CustomResourceDefinition Transpile(this MetadataLoadContext cont
79
78
type . GetCustomAttributeData < DescriptionAttribute > ( ) ? . GetCustomAttributeCtorArg < string > ( context , 0 ) ,
80
79
Properties = type . GetProperties ( )
81
80
. Where ( p => ! IgnoredToplevelProperties . Contains ( p . Name . ToLowerInvariant ( ) )
82
- && p . GetCustomAttributeData < IgnoreAttribute > ( ) == null )
81
+ && p . GetCustomAttributeData < IgnoreAttribute > ( ) == null )
83
82
. Select ( p => ( Name : p . GetPropertyName ( context ) , Schema : context . Map ( p ) ) )
84
83
. ToDictionary ( t => t . Name , t => t . Schema ) ,
85
84
} ) ;
@@ -107,8 +106,8 @@ public static IEnumerable<V1CustomResourceDefinition> Transpile(
107
106
=> types
108
107
. Select ( context . GetContextType )
109
108
. Where ( type => type . Assembly != context . GetContextType < KubernetesEntityAttribute > ( ) . Assembly
110
- && type . GetCustomAttributesData < KubernetesEntityAttribute > ( ) . Any ( )
111
- && ! type . GetCustomAttributesData < IgnoreAttribute > ( ) . Any ( ) )
109
+ && type . GetCustomAttributesData < KubernetesEntityAttribute > ( ) . Any ( )
110
+ && ! type . GetCustomAttributesData < IgnoreAttribute > ( ) . Any ( ) )
112
111
. Select ( type => ( Props : context . Transpile ( type ) ,
113
112
IsStorage : type . GetCustomAttributesData < StorageVersionAttribute > ( ) . Any ( ) ) )
114
113
. GroupBy ( grp => grp . Props . Metadata . Name )
@@ -291,56 +290,50 @@ private static V1JSONSchemaProps Map(this MetadataLoadContext context, Type type
291
290
return props ;
292
291
}
293
292
294
- var interfaces = type . IsInterface
293
+ var interfaces = ( type . IsInterface
295
294
? type . GetInterfaces ( ) . Append ( type )
296
- : type . GetInterfaces ( ) ;
295
+ : type . GetInterfaces ( ) ) . ToList ( ) ;
297
296
298
297
var interfaceNames = interfaces . Select ( i =>
299
298
i . IsGenericType
300
299
? i . GetGenericTypeDefinition ( ) . FullName
301
- : i . FullName ) ;
300
+ : i . FullName ) . ToList ( ) ;
302
301
303
302
if ( interfaceNames . Contains ( typeof ( IDictionary < , > ) . FullName ) )
304
303
{
305
304
var dictionaryImpl = interfaces
306
305
. First ( i => i . IsGenericType
307
- && i . GetGenericTypeDefinition ( ) . FullName == typeof ( IDictionary < , > ) . FullName ) ;
306
+ && i . GetGenericTypeDefinition ( ) . FullName == typeof ( IDictionary < , > ) . FullName ) ;
308
307
309
- var addlProps = context . Map ( dictionaryImpl . GenericTypeArguments [ 1 ] ) ;
308
+ var additionalProperties = context . Map ( dictionaryImpl . GenericTypeArguments [ 1 ] ) ;
310
309
return new V1JSONSchemaProps
311
310
{
312
311
Type = Object ,
313
- AdditionalProperties = addlProps ,
312
+ AdditionalProperties = additionalProperties ,
314
313
Nullable = false ,
315
314
} ;
316
315
}
317
316
318
317
if ( interfaceNames . Contains ( typeof ( IDictionary ) . FullName ) )
319
318
{
320
- return new V1JSONSchemaProps
321
- {
322
- Type = Object ,
323
- XKubernetesPreserveUnknownFields = true ,
324
- Nullable = false ,
325
- } ;
319
+ return new V1JSONSchemaProps { Type = Object , XKubernetesPreserveUnknownFields = true , Nullable = false , } ;
326
320
}
327
321
328
322
if ( interfaceNames . Contains ( typeof ( IEnumerable < > ) . FullName ) )
329
323
{
330
324
return context . MapEnumerationType ( type , interfaces ) ;
331
325
}
332
326
333
- switch ( type . BaseType ? . FullName )
327
+ return type . BaseType ? . FullName switch
334
328
{
335
- case "System.Object" :
336
- return context . MapObjectType ( type ) ;
337
- case "System.ValueType" :
338
- return context . MapValueType ( type ) ;
339
- case "System.Enum" :
340
- return new V1JSONSchemaProps { Type = String , EnumProperty = Enum . GetNames ( type ) . Cast < object > ( ) . ToList ( ) } ;
341
- default :
342
- throw InvalidType ( type ) ;
343
- }
329
+ "System.Object" => context . MapObjectType ( type ) ,
330
+ "System.ValueType" => context . MapValueType ( type ) ,
331
+ "System.Enum" => new V1JSONSchemaProps
332
+ {
333
+ Type = String , EnumProperty = Enum . GetNames ( type ) . Cast < object > ( ) . ToList ( ) ,
334
+ } ,
335
+ _ => throw InvalidType ( type ) ,
336
+ } ;
344
337
}
345
338
346
339
private static V1JSONSchemaProps MapObjectType ( this MetadataLoadContext context , Type type )
@@ -367,34 +360,38 @@ private static V1JSONSchemaProps MapObjectType(this MetadataLoadContext context,
367
360
}
368
361
369
362
return new V1JSONSchemaProps
370
- {
371
- Type = Object ,
372
- Description =
373
- type . GetCustomAttributeData < DescriptionAttribute > ( ) ? . GetCustomAttributeCtorArg < string > ( context , 0 ) ,
374
- Properties = type
375
- . GetProperties ( )
376
- . Where ( p => p . GetCustomAttributeData < IgnoreAttribute > ( ) == null )
377
- . Select ( p => ( Name : p . GetPropertyName ( context ) , Schema : context . Map ( p ) ) )
378
- . ToDictionary ( t => t . Name , t => t . Schema ) ,
379
- Required = type . GetProperties ( )
380
- . Where ( p => p . GetCustomAttributeData < RequiredAttribute > ( ) != null
363
+ {
364
+ Type = Object ,
365
+ Description =
366
+ type . GetCustomAttributeData < DescriptionAttribute > ( )
367
+ ? . GetCustomAttributeCtorArg < string > ( context , 0 ) ,
368
+ Properties = type
369
+ . GetProperties ( )
370
+ . Where ( p => p . GetCustomAttributeData < IgnoreAttribute > ( ) == null )
371
+ . Select ( p => ( Name : p . GetPropertyName ( context ) , Schema : context . Map ( p ) ) )
372
+ . ToDictionary ( t => t . Name , t => t . Schema ) ,
373
+ Required = type . GetProperties ( )
374
+ . Where ( p => p . GetCustomAttributeData < RequiredAttribute > ( ) != null
381
375
&& p . GetCustomAttributeData < IgnoreAttribute > ( ) == null )
382
- . Select ( p => p . GetPropertyName ( context ) )
383
- . ToList ( ) switch
384
- {
385
- { Count : > 0 } p => p ,
386
- _ => null ,
387
- } ,
388
- } ;
376
+ . Select ( p => p . GetPropertyName ( context ) )
377
+ . ToList ( ) switch
378
+ {
379
+ { Count : > 0 } p => p ,
380
+ _ => null ,
381
+ } ,
382
+ } ;
389
383
}
390
384
}
391
385
392
- private static V1JSONSchemaProps MapEnumerationType ( this MetadataLoadContext context , Type type , IEnumerable < Type > interfaces )
386
+ private static V1JSONSchemaProps MapEnumerationType (
387
+ this MetadataLoadContext context ,
388
+ Type type ,
389
+ IEnumerable < Type > interfaces )
393
390
{
394
391
Type ? enumerableType = interfaces
395
392
. FirstOrDefault ( i => i . IsGenericType
396
- && i . GetGenericTypeDefinition ( ) . FullName == typeof ( IEnumerable < > ) . FullName
397
- && i . GenericTypeArguments . Length == 1 ) ;
393
+ && i . GetGenericTypeDefinition ( ) . FullName == typeof ( IEnumerable < > ) . FullName
394
+ && i . GenericTypeArguments . Length == 1 ) ;
398
395
399
396
if ( enumerableType == null )
400
397
{
@@ -404,40 +401,33 @@ private static V1JSONSchemaProps MapEnumerationType(this MetadataLoadContext con
404
401
Type listType = enumerableType . GenericTypeArguments [ 0 ] ;
405
402
if ( listType . IsGenericType && listType . GetGenericTypeDefinition ( ) . FullName == typeof ( KeyValuePair < , > ) . FullName )
406
403
{
407
- var addlProps = context . Map ( listType . GenericTypeArguments [ 1 ] ) ;
408
- return new V1JSONSchemaProps { Type = Object , AdditionalProperties = addlProps , Nullable = false } ;
404
+ var additionalProperties = context . Map ( listType . GenericTypeArguments [ 1 ] ) ;
405
+ return new V1JSONSchemaProps
406
+ {
407
+ Type = Object ,
408
+ AdditionalProperties = additionalProperties ,
409
+ Nullable = false ,
410
+ } ;
409
411
}
410
412
411
413
var items = context . Map ( listType ) ;
412
414
return new V1JSONSchemaProps { Type = Array , Items = items , Nullable = false } ;
413
415
}
414
416
415
- private static V1JSONSchemaProps MapValueType ( this MetadataLoadContext context , Type type )
416
- {
417
- switch ( type . FullName )
417
+ private static V1JSONSchemaProps MapValueType ( this MetadataLoadContext _ , Type type ) =>
418
+ type . FullName switch
418
419
{
419
- case "System.Int32" :
420
- return new V1JSONSchemaProps { Type = Integer , Format = Int32 , Nullable = false } ;
421
- case "System.Int64" :
422
- return new V1JSONSchemaProps { Type = Integer , Format = Int64 , Nullable = false } ;
423
- case "System.Single" :
424
- return new V1JSONSchemaProps { Type = Number , Format = Float , Nullable = false } ;
425
- case "System.Double" :
426
- return new V1JSONSchemaProps { Type = Number , Format = Double , Nullable = false } ;
427
- case "System.Decimal" :
428
- return new V1JSONSchemaProps { Type = Number , Format = Decimal , Nullable = false } ;
429
- case "System.Boolean" :
430
- return new V1JSONSchemaProps { Type = Boolean , Nullable = false } ;
431
- case "System.DateTime" :
432
- case "System.DateTimeOffset" :
433
- return new V1JSONSchemaProps { Type = String , Format = DateTime , Nullable = false } ;
434
- default :
435
- throw InvalidType ( type ) ;
436
- }
437
- }
420
+ "System.Int32" => new V1JSONSchemaProps { Type = Integer , Format = Int32 , Nullable = false } ,
421
+ "System.Int64" => new V1JSONSchemaProps { Type = Integer , Format = Int64 , Nullable = false } ,
422
+ "System.Single" => new V1JSONSchemaProps { Type = Number , Format = Float , Nullable = false } ,
423
+ "System.Double" => new V1JSONSchemaProps { Type = Number , Format = Double , Nullable = false } ,
424
+ "System.Decimal" => new V1JSONSchemaProps { Type = Number , Format = Decimal , Nullable = false } ,
425
+ "System.Boolean" => new V1JSONSchemaProps { Type = Boolean , Nullable = false } ,
426
+ "System.DateTime" => new V1JSONSchemaProps { Type = String , Format = DateTime , Nullable = false } ,
427
+ "System.DateTimeOffset" => new V1JSONSchemaProps { Type = String , Format = DateTime , Nullable = false } ,
428
+ _ => throw InvalidType ( type ) ,
429
+ } ;
438
430
439
- private static ArgumentException InvalidType ( Type type )
440
- {
441
- return new ArgumentException ( $ "The given type { type . FullName } is not a valid Kubernetes entity.") ;
442
- }
431
+ private static ArgumentException InvalidType ( Type type ) =>
432
+ new ( $ "The given type { type . FullName } is not a valid Kubernetes entity.") ;
443
433
}
0 commit comments