@@ -13,13 +13,15 @@ namespace KubeOps.Transpiler;
13
13
public static class Entities
14
14
{
15
15
/// <summary>
16
- /// Create a metadata / scope tuple out of a given entity type.
16
+ /// Create a metadata / scope tuple out of a given entity type via externally loaded assembly .
17
17
/// </summary>
18
18
/// <param name="context">The context that loaded the types.</param>
19
19
/// <param name="entityType">The type to convert.</param>
20
20
/// <returns>A tuple that contains <see cref="EntityMetadata"/> and a scope.</returns>
21
21
/// <exception cref="ArgumentException">Thrown when the type contains no <see cref="KubernetesEntityAttribute"/>.</exception>
22
- public static ( EntityMetadata Metadata , string Scope ) ToEntityMetadata ( this MetadataLoadContext context , Type entityType )
22
+ public static ( EntityMetadata Metadata , string Scope ) ToEntityMetadata (
23
+ this MetadataLoadContext context ,
24
+ Type entityType )
23
25
=> ( context . GetContextType ( entityType ) . GetCustomAttributeData < KubernetesEntityAttribute > ( ) ,
24
26
context . GetContextType ( entityType ) . GetCustomAttributeData < EntityScopeAttribute > ( ) ) switch
25
27
{
@@ -29,15 +31,45 @@ public static (EntityMetadata Metadata, string Scope) ToEntityMetadata(this Meta
29
31
attr . GetCustomAttributeNamedArg < string > ( context , nameof ( KubernetesEntityAttribute . Kind ) ) ,
30
32
entityType . Name ) ,
31
33
Defaulted (
32
- attr . GetCustomAttributeNamedArg < string > ( context , nameof ( KubernetesEntityAttribute . ApiVersion ) ) ,
34
+ attr . GetCustomAttributeNamedArg < string > (
35
+ context ,
36
+ nameof ( KubernetesEntityAttribute . ApiVersion ) ) ,
33
37
"v1" ) ,
34
38
attr . GetCustomAttributeNamedArg < string > ( context , nameof ( KubernetesEntityAttribute . Group ) ) ,
35
39
attr . GetCustomAttributeNamedArg < string > ( context , nameof ( KubernetesEntityAttribute . PluralName ) ) ) ,
36
40
scope switch
37
41
{
38
- null => Enum . GetName ( EntityScope . Namespaced ) ?? "namespaced " ,
42
+ null => Enum . GetName ( EntityScope . Namespaced ) ?? "Namespaced " ,
39
43
_ => Enum . GetName (
40
- scope . GetCustomAttributeCtorArg < EntityScope > ( context , 0 ) ) ?? "namespaced" ,
44
+ scope . GetCustomAttributeCtorArg < EntityScope > ( context , 0 ) ) ?? "Namespaced" ,
45
+ } ) ,
46
+ } ;
47
+
48
+ /// <summary>
49
+ /// Create a metadata / scope tuple out of a given entity type via reflection in the same loaded assembly.
50
+ /// </summary>
51
+ /// <param name="entityType">The type to convert.</param>
52
+ /// <returns>A tuple that contains <see cref="EntityMetadata"/> and a scope.</returns>
53
+ /// <exception cref="ArgumentException">Thrown when the type contains no <see cref="KubernetesEntityAttribute"/>.</exception>
54
+ public static ( EntityMetadata Metadata , string Scope ) ToEntityMetadata ( Type entityType )
55
+ => ( entityType . GetCustomAttributeData < KubernetesEntityAttribute > ( ) ,
56
+ entityType . GetCustomAttributeData < EntityScopeAttribute > ( ) ) switch
57
+ {
58
+ ( null , _ ) => throw new ArgumentException ( "The given type is not a valid Kubernetes entity." ) ,
59
+ ( { } attr , var scope ) => ( new (
60
+ Defaulted (
61
+ attr . GetCustomAttributeNamedArg < string > ( nameof ( KubernetesEntityAttribute . Kind ) ) ,
62
+ entityType . Name ) ,
63
+ Defaulted (
64
+ attr . GetCustomAttributeNamedArg < string > ( nameof ( KubernetesEntityAttribute . ApiVersion ) ) ,
65
+ "v1" ) ,
66
+ attr . GetCustomAttributeNamedArg < string > ( nameof ( KubernetesEntityAttribute . Group ) ) ,
67
+ attr . GetCustomAttributeNamedArg < string > ( nameof ( KubernetesEntityAttribute . PluralName ) ) ) ,
68
+ scope switch
69
+ {
70
+ null => Enum . GetName ( EntityScope . Namespaced ) ?? "Namespaced" ,
71
+ _ => Enum . GetName (
72
+ scope . GetCustomAttributeCtorArg < EntityScope > ( 0 ) ) ?? "Namespaced" ,
41
73
} ) ,
42
74
} ;
43
75
0 commit comments