@@ -45,6 +45,19 @@ static async Task Main(string[] args)
45
45
// Read the spec
46
46
var swagger = await SwaggerDocument . FromFileAsync ( specPath ) ;
47
47
48
+
49
+ // Register helpers used in the templating.
50
+ Helpers . Register ( nameof ( ToXmlDoc ) , ToXmlDoc ) ;
51
+ Helpers . Register ( nameof ( GetClassName ) , GetClassName ) ;
52
+ Helpers . Register ( nameof ( GetMethodName ) , GetMethodName ) ;
53
+ Helpers . Register ( nameof ( GetDotNetName ) , GetDotNetName ) ;
54
+ Helpers . Register ( nameof ( GetDotNetType ) , GetDotNetType ) ;
55
+ Helpers . Register ( nameof ( GetPathExpression ) , GetPathExpression ) ;
56
+ Helpers . Register ( nameof ( GetGroup ) , GetGroup ) ;
57
+ Helpers . Register ( nameof ( GetApiVersion ) , GetApiVersion ) ;
58
+ Helpers . Register ( nameof ( GetKind ) , GetKind ) ;
59
+
60
+ // Generate the Watcher operations
48
61
// We skip operations where the name of the class in the C# client could not be determined correctly.
49
62
// That's usually because there are different version of the same object (e.g. for deployments).
50
63
Collection < string > blacklistedOperations = new Collection < string > ( )
@@ -62,17 +75,31 @@ static async Task Main(string[] args)
62
75
&& o . Operation . ActualParameters . Any ( p => p . Name == "name" )
63
76
&& ! blacklistedOperations . Contains ( o . Operation . OperationId ) ) . ToArray ( ) ;
64
77
65
- // Register helpers used in the templating.
66
- Helpers . Register ( nameof ( ToXmlDoc ) , ToXmlDoc ) ;
67
- Helpers . Register ( nameof ( ClassName ) , ClassName ) ;
68
- Helpers . Register ( nameof ( MethodName ) , MethodName ) ;
69
- Helpers . Register ( nameof ( GetDotNetName ) , GetDotNetName ) ;
70
- Helpers . Register ( nameof ( GetDotNetType ) , GetDotNetType ) ;
71
- Helpers . Register ( nameof ( GetPathExpression ) , GetPathExpression ) ;
72
-
73
78
// Render.
74
79
Render . FileToFile ( "IKubernetes.Watch.cs.template" , watchOperations , $ "{ outputDirectory } IKubernetes.Watch.cs") ;
75
80
Render . FileToFile ( "Kubernetes.Watch.cs.template" , watchOperations , $ "{ outputDirectory } Kubernetes.Watch.cs") ;
81
+
82
+ // Generate the interface declarations
83
+ var skippedTypes = new Collection < string > ( )
84
+ {
85
+ "V1beta1Deployment" ,
86
+ "V1beta1DeploymentList" ,
87
+ "V1beta1DeploymentRollback" ,
88
+ "V1beta1DeploymentRollback" ,
89
+ "V1beta1Scale" ,
90
+ "V1beta1PodSecurityPolicy" ,
91
+ "V1beta1PodSecurityPolicyList" ,
92
+ "V1WatchEvent" ,
93
+ } ;
94
+
95
+ var definitions = swagger . Definitions . Values
96
+ . Where (
97
+ d => d . ExtensionData != null
98
+ && d . ExtensionData . ContainsKey ( "x-kubernetes-group-version-kind" )
99
+ && ! skippedTypes . Contains ( GetClassName ( d ) ) ) ;
100
+
101
+ // Render.
102
+ Render . FileToFile ( "ModelExtensions.cs.template" , definitions , $ "{ outputDirectory } ModelExtensions.cs") ;
76
103
}
77
104
78
105
static void ToXmlDoc ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
@@ -101,44 +128,92 @@ static void ToXmlDoc(RenderContext context, IList<object> arguments, IDictionary
101
128
}
102
129
}
103
130
104
- static void MethodName ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
131
+ static void GetClassName ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
105
132
{
106
133
if ( arguments != null && arguments . Count > 0 && arguments [ 0 ] != null && arguments [ 0 ] is SwaggerOperation )
107
134
{
108
- context . Write ( MethodName ( arguments [ 0 ] as SwaggerOperation ) ) ;
135
+ context . Write ( GetClassName ( arguments [ 0 ] as SwaggerOperation ) ) ;
136
+ }
137
+ else if ( arguments != null && arguments . Count > 0 && arguments [ 0 ] != null && arguments [ 0 ] is JsonSchema4 )
138
+ {
139
+ context . Write ( GetClassName ( arguments [ 0 ] as JsonSchema4 ) ) ;
109
140
}
110
141
}
111
142
112
- static string MethodName ( SwaggerOperation watchOperation )
143
+ static string GetClassName ( SwaggerOperation watchOperation )
113
144
{
114
- var tag = watchOperation . Tags [ 0 ] ;
115
- tag = tag . Replace ( "_" , string . Empty ) ;
145
+ var groupVersionKind = ( Dictionary < string , object > ) watchOperation . ExtensionData [ "x-kubernetes-group-version-kind" ] ;
146
+ var group = ( string ) groupVersionKind [ "group" ] ;
147
+ var kind = ( string ) groupVersionKind [ "kind" ] ;
148
+ var version = ( string ) groupVersionKind [ "version" ] ;
116
149
117
- var methodName = ToPascalCase ( watchOperation . OperationId ) ;
150
+ var className = $ "{ ToPascalCase ( version ) } { kind } ";
151
+ return className ;
152
+ }
118
153
119
- // This tries to remove the version from the method name, e.g. watchCoreV1NamespacedPod => WatchNamespacedPod
120
- methodName = methodName . Replace ( tag , string . Empty , StringComparison . OrdinalIgnoreCase ) ;
121
- methodName += "Async" ;
122
- return methodName ;
154
+ private static string GetClassName ( JsonSchema4 definition )
155
+ {
156
+ var groupVersionKindElements = ( object [ ] ) definition . ExtensionData [ "x-kubernetes-group-version-kind" ] ;
157
+ var groupVersionKind = ( Dictionary < string , object > ) groupVersionKindElements [ 0 ] ;
158
+
159
+ var group = groupVersionKind [ "group" ] as string ;
160
+ var version = groupVersionKind [ "version" ] as string ;
161
+ var kind = groupVersionKind [ "kind" ] as string ;
162
+
163
+ return $ "{ ToPascalCase ( version ) } { ToPascalCase ( kind ) } ";
164
+ }
165
+
166
+ static void GetKind ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
167
+ {
168
+ if ( arguments != null && arguments . Count > 0 && arguments [ 0 ] != null && arguments [ 0 ] is JsonSchema4 )
169
+ {
170
+ context . Write ( GetKind ( arguments [ 0 ] as JsonSchema4 ) ) ;
171
+ }
172
+ }
173
+
174
+ private static string GetKind ( JsonSchema4 definition )
175
+ {
176
+ var groupVersionKindElements = ( object [ ] ) definition . ExtensionData [ "x-kubernetes-group-version-kind" ] ;
177
+ var groupVersionKind = ( Dictionary < string , object > ) groupVersionKindElements [ 0 ] ;
178
+
179
+ return groupVersionKind [ "kind" ] as string ;
180
+ }
181
+
182
+ static void GetGroup ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
183
+ {
184
+ if ( arguments != null && arguments . Count > 0 && arguments [ 0 ] != null && arguments [ 0 ] is JsonSchema4 )
185
+ {
186
+ context . Write ( GetGroup ( arguments [ 0 ] as JsonSchema4 ) ) ;
187
+ }
123
188
}
124
189
125
- static void ClassName ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
190
+ private static string GetGroup ( JsonSchema4 definition )
191
+ {
192
+ var groupVersionKindElements = ( object [ ] ) definition . ExtensionData [ "x-kubernetes-group-version-kind" ] ;
193
+ var groupVersionKind = ( Dictionary < string , object > ) groupVersionKindElements [ 0 ] ;
194
+
195
+ return groupVersionKind [ "group" ] as string ;
196
+ }
197
+
198
+ static void GetMethodName ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
126
199
{
127
200
if ( arguments != null && arguments . Count > 0 && arguments [ 0 ] != null && arguments [ 0 ] is SwaggerOperation )
128
201
{
129
- context . Write ( ClassName ( arguments [ 0 ] as SwaggerOperation ) ) ;
202
+ context . Write ( GetMethodName ( arguments [ 0 ] as SwaggerOperation ) ) ;
130
203
}
131
204
}
132
205
133
- static string ClassName ( SwaggerOperation watchOperation )
206
+ static string GetMethodName ( SwaggerOperation watchOperation )
134
207
{
135
- var groupVersionKind = ( Dictionary < string , object > ) watchOperation . ExtensionData [ "x-kubernetes-group-version-kind" ] ;
136
- var group = ( string ) groupVersionKind [ "group" ] ;
137
- var kind = ( string ) groupVersionKind [ "kind" ] ;
138
- var version = ( string ) groupVersionKind [ "version" ] ;
208
+ var tag = watchOperation . Tags [ 0 ] ;
209
+ tag = tag . Replace ( "_" , string . Empty ) ;
139
210
140
- var className = $ "{ ToPascalCase ( version ) } { kind } ";
141
- return className ;
211
+ var methodName = ToPascalCase ( watchOperation . OperationId ) ;
212
+
213
+ // This tries to remove the version from the method name, e.g. watchCoreV1NamespacedPod => WatchNamespacedPod
214
+ methodName = methodName . Replace ( tag , string . Empty , StringComparison . OrdinalIgnoreCase ) ;
215
+ methodName += "Async" ;
216
+ return methodName ;
142
217
}
143
218
144
219
static void GetDotNetType ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
@@ -148,11 +223,11 @@ static void GetDotNetType(RenderContext context, IList<object> arguments, IDicti
148
223
var parameter = arguments [ 0 ] as SwaggerParameter ;
149
224
context . Write ( GetDotNetType ( parameter . Type , parameter . Name , parameter . IsRequired ) ) ;
150
225
}
151
- else if ( arguments != null && arguments . Count > 2 && arguments [ 0 ] != null && arguments [ 1 ] != null && arguments [ 2 ] != null && arguments [ 0 ] is JsonObjectType && arguments [ 1 ] is string && arguments [ 2 ] is bool )
226
+ else if ( arguments != null && arguments . Count > 2 && arguments [ 0 ] != null && arguments [ 1 ] != null && arguments [ 2 ] != null && arguments [ 0 ] is JsonObjectType && arguments [ 1 ] is string && arguments [ 2 ] is bool )
152
227
{
153
228
context . Write ( GetDotNetType ( ( JsonObjectType ) arguments [ 0 ] , ( string ) arguments [ 1 ] , ( bool ) arguments [ 2 ] ) ) ;
154
229
}
155
- else if ( arguments != null && arguments . Count > 0 && arguments [ 0 ] != null )
230
+ else if ( arguments != null && arguments . Count > 0 && arguments [ 0 ] != null )
156
231
{
157
232
context . Write ( $ "ERROR: Expected SwaggerParameter but got { arguments [ 0 ] . GetType ( ) . FullName } ") ;
158
233
}
@@ -243,8 +318,29 @@ private static string GetPathExpression(SwaggerOperationDescription operation)
243
318
return pathExpression ;
244
319
}
245
320
321
+ static void GetApiVersion ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
322
+ {
323
+ if ( arguments != null && arguments . Count > 0 && arguments [ 0 ] != null && arguments [ 0 ] is JsonSchema4 )
324
+ {
325
+ context . Write ( GetApiVersion ( arguments [ 0 ] as JsonSchema4 ) ) ;
326
+ }
327
+ }
328
+
329
+ private static string GetApiVersion ( JsonSchema4 definition )
330
+ {
331
+ var groupVersionKindElements = ( object [ ] ) definition . ExtensionData [ "x-kubernetes-group-version-kind" ] ;
332
+ var groupVersionKind = ( Dictionary < string , object > ) groupVersionKindElements [ 0 ] ;
333
+
334
+ return groupVersionKind [ "version" ] as string ;
335
+ }
336
+
246
337
private static string ToPascalCase ( string name )
247
338
{
339
+ if ( string . IsNullOrWhiteSpace ( name ) )
340
+ {
341
+ return name ;
342
+ }
343
+
248
344
return char . ToUpper ( name [ 0 ] ) + name . Substring ( 1 ) ;
249
345
}
250
346
}
0 commit comments