Skip to content

Commit 307e828

Browse files
committed
chore: use assert/assert_eq with message instead of panic
1 parent e311361 commit 307e828

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

kube-core/src/schema/transform_any_of.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,6 @@ pub(crate) fn hoist_any_of_subschema_with_a_nullable_variant(kube_schema: &mut S
159159
*kube_schema = to_hoist.clone();
160160

161161
// Set the schema to nullable (as we know we matched the null variant earlier)
162+
// TODO (@NickLarsenNZ): Do we need to insert `nullable` into `other` too?
162163
kube_schema.extensions.insert("nullable".to_owned(), true.into());
163164
}

kube-core/src/schema/transform_properties.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ pub(crate) fn hoist_properties_for_any_of_subschemas(kube_schema: &mut SchemaObj
377377
}
378378

379379
// Ensure we aren't looking at the one with a null
380-
// TODO (@NickLarsenNZ): Combine the logic with the function that covers the nullable anyOf
381380
if subschemas.len() == 2 {
382381
// This is the signature for the null variant, indicating the "other"
383382
// variant is the subschema that needs hoisting
@@ -422,28 +421,31 @@ pub(crate) fn hoist_properties_for_any_of_subschemas(kube_schema: &mut SchemaObj
422421

423422
// For a tagged enum, we need to preserve the variant description
424423
if preserve_description {
425-
if object.properties.len() != 1 {
426-
// TODO (@NickLarsenNZ): Use assert_eq with error message (and in the other place)
427-
panic!("Expecting a subschema for a tagged enum variant, so there should only be one property");
428-
}
424+
assert_eq!(
425+
object.properties.len(),
426+
1,
427+
"Expecting only a single property defined for the tagged enum variant schema"
428+
);
429429

430-
if let Schema::Object(x) = object
430+
if let Schema::Object(subschema) = object
431431
.properties
432432
.values_mut()
433433
.next()
434-
.expect("it's there, trust sbernauer")
434+
.expect("asserted that one and only one property exists")
435435
{
436-
// I hope the new metadata doesn't have default set
437-
// I also hope that we didn't destroy some existing metadata.
438-
// Surely it would only exist in one or the other
439-
// See: https://github.com/kube-rs/kube/blob/98bfbe3d7923321a16ccde9e690fd2ce8c7efc32/kube-core/src/schema.rs#L409-L426
440-
x.metadata = metadata
436+
assert!(
437+
// While possible, it is unexpected if the subschema metadata is already set for the property
438+
subschema.metadata.is_none(),
439+
"subschema metadata for property should be empty"
440+
);
441+
// Move the variant description down to the properties (before they get hoisted)
442+
subschema.metadata = metadata
441443
};
442444
}
443445

444446
// If properties are set, hoist them to the schema properties.
445447
// This will panic if duplicate properties are encountered that do not have the same shape.
446-
// That can happen when the untagged enum variants each refer to structs which contain the same field name.
448+
// That can happen when the untagged enum variants each refer to structs which contain the same field name but with different types or doc-comments.
447449
// The developer needs to make them the same.
448450
// TODO (@NickLarsenNZ): Add a case for a structural variant, and a tuple variant containing a structure where the same field name is used.
449451
while let Some((property_name, Schema::Object(property_schema_object))) =
@@ -456,16 +458,13 @@ pub(crate) fn hoist_properties_for_any_of_subschemas(kube_schema: &mut SchemaObj
456458
.properties
457459
.get(&property_name)
458460
{
459-
if existing_property != &Schema::Object(property_schema_object.clone()) {
460-
// TODO (@NickLarsenNZ): Here we could do another check to see if it only differs by description.
461-
// If the schema property description is not set, then we could overwrite it and not panic.
462-
dbg!(
463-
&property_name,
464-
existing_property,
465-
&Schema::Object(property_schema_object.clone()),
466-
);
467-
panic!("Properties for {property_name:?} are defined multiple times with different shapes")
468-
}
461+
// TODO (@NickLarsenNZ): Here we could do another check to see if it only differs by description.
462+
// If the schema property description is not set, then we could overwrite it and not panic.
463+
assert_eq!(
464+
existing_property,
465+
&Schema::Object(property_schema_object.clone()),
466+
"Properties for {property_name:?} are defined multiple times with different shapes"
467+
);
469468
} else {
470469
// Otherwise, insert the subschema properties into the schema properties
471470
parent_object

0 commit comments

Comments
 (0)