@@ -178,16 +178,32 @@ impl Struct {
178
178
179
179
#convert_method
180
180
181
- fn from_json_value( value: #serde_json_path:: Value ) -> :: std:: result:: Result <Self , #parse_object_error> {
182
- let api_version = value
181
+ fn from_json_value( object: #serde_json_path:: Value ) -> :: std:: result:: Result <Self , #parse_object_error> {
182
+ let object_kind = object
183
+ . get( "kind" )
184
+ . ok_or_else( || #parse_object_error:: FieldMissing { field: "kind" . to_string( ) } ) ?
185
+ . as_str( )
186
+ . ok_or_else( || #parse_object_error:: FieldNotStr { field: "kind" . to_string( ) } ) ?;
187
+
188
+ // Note(@sbernauer): One could argue we don't need to check the kind, but this
189
+ // is a nice sanity check. If for *some* reason a unexpected kind ends up at a
190
+ // conversion, the problem might be very hard to spot without this.
191
+ if object_kind != stringify!( #enum_ident) {
192
+ return Err ( #parse_object_error:: UnexpectedObjectKind {
193
+ kind: object_kind. to_string( ) ,
194
+ supported_kind: stringify!( #enum_ident) . to_string( ) ,
195
+ } ) ;
196
+ }
197
+
198
+ let api_version = object
183
199
. get( "apiVersion" )
184
200
. ok_or_else( || #parse_object_error:: FieldMissing { field: "apiVersion" . to_string( ) } ) ?
185
201
. as_str( )
186
202
. ok_or_else( || #parse_object_error:: FieldNotStr { field: "apiVersion" . to_string( ) } ) ?;
187
203
188
204
let object = match api_version {
189
205
#( #api_versions => {
190
- let object = #serde_json_path:: from_value( value )
206
+ let object = #serde_json_path:: from_value( object )
191
207
. map_err( |source| #parse_object_error:: Deserialize { source } ) ?;
192
208
193
209
Self :: #variant_idents( object)
@@ -321,7 +337,6 @@ impl Struct {
321
337
let versioned_path = & * kubernetes_arguments. crates . versioned ;
322
338
let kube_core_path = & * kubernetes_arguments. crates . kube_core ;
323
339
324
- let parse_object_error = quote ! { #versioned_path:: ParseObjectError } ;
325
340
let convert_object_error = quote ! { #versioned_path:: ConvertObjectError } ;
326
341
327
342
// Generate conversion paths and the match arms for these paths
@@ -432,27 +447,6 @@ impl Struct {
432
447
let mut converted_objects = :: std:: vec:: Vec :: with_capacity( objects. len( ) ) ;
433
448
434
449
for object in objects {
435
- let object_kind = object
436
- . get( "kind" )
437
- . ok_or_else( || #convert_object_error:: Parse {
438
- source: #parse_object_error:: FieldMissing { field: "kind" . to_string( ) }
439
- } ) ?
440
- . as_str( )
441
- . ok_or_else( || #convert_object_error:: Parse {
442
- source: #parse_object_error:: FieldNotStr { field: "kind" . to_string( ) }
443
- } ) ?;
444
- // Note(@sbernauer): One could argue we don't need to check the send kind, but
445
- // IMHO this is a nice sanity check. If for *some* reason a wrong kind ends up
446
- // at a conversion, the problem might be very hard to spot without this.
447
- if object_kind != stringify!( #struct_ident) {
448
- return Err ( #convert_object_error:: Parse {
449
- source: #parse_object_error:: WrongObjectKind {
450
- kind: object_kind. to_string( ) ,
451
- supported_kind: stringify!( #struct_ident) . to_string( ) ,
452
- }
453
- } )
454
- }
455
-
456
450
// This clone is required because in the noop case we move the object into
457
451
// the converted objects vec.
458
452
let current_object = Self :: from_json_value( object. clone( ) )
0 commit comments