Chore: apimachinery types cleanup (#107988)

This commit is contained in:
Ryan McKinley
2025-07-10 12:45:16 -07:00
committed by GitHub
parent 13a89d4ae3
commit 06c00e4fa7
8 changed files with 179 additions and 129 deletions
@@ -13,9 +13,9 @@ import (
// Unstructured allows objects that do not have Golang structs registered to be manipulated
// generically.
type Unstructured struct {
// Object is a JSON compatible map with string, float, int, bool, []interface{},
// or map[string]interface{} children.
Object map[string]interface{}
// Object is a JSON compatible map with string, float, int, bool, []any,
// or map[string]any children.
Object map[string]any
}
// Produce an API definition that represents map[string]any
@@ -27,7 +27,7 @@ func (u Unstructured) OpenAPIDefinition() openapi.OpenAPIDefinition {
AdditionalProperties: &spec.SchemaOrBool{Allows: true},
},
VendorExtensible: spec.VendorExtensible{
Extensions: map[string]interface{}{
Extensions: map[string]any{
"x-kubernetes-preserve-unknown-fields": true,
},
},
@@ -35,14 +35,18 @@ func (u Unstructured) OpenAPIDefinition() openapi.OpenAPIDefinition {
}
}
func (u *Unstructured) UnstructuredContent() map[string]interface{} {
func (u *Unstructured) IsZero() bool {
return len(u.Object) == 0
}
func (u *Unstructured) UnstructuredContent() map[string]any {
if u.Object == nil {
return make(map[string]interface{})
return make(map[string]any)
}
return u.Object
}
func (u *Unstructured) SetUnstructuredContent(content map[string]interface{}) {
func (u *Unstructured) SetUnstructuredContent(content map[string]any) {
u.Object = content
}
@@ -64,7 +68,7 @@ func (u *Unstructured) DeepCopy() *Unstructured {
}
out := new(Unstructured)
*out = *u
out.Object = deepCopyJSONValue(u.Object).(map[string]interface{})
out.Object = deepCopyJSONValue(u.Object).(map[string]any)
return out
}
@@ -78,24 +82,24 @@ func (u *Unstructured) DeepCopyInto(out *Unstructured) {
// runtime.DeepCopyJSON(u.Object)
//
// BUT this avoids panic on int
func deepCopyJSONValue(x interface{}) interface{} {
func deepCopyJSONValue(x any) any {
switch x := x.(type) {
case map[string]interface{}:
case map[string]any:
if x == nil {
// Typed nil - an interface{} that contains a type map[string]interface{} with a value of nil
// Typed nil - an any that contains a type map[string]any with a value of nil
return x
}
clone := make(map[string]interface{}, len(x))
clone := make(map[string]any, len(x))
for k, v := range x {
clone[k] = deepCopyJSONValue(v)
}
return clone
case []interface{}:
case []any:
if x == nil {
// Typed nil - an interface{} that contains a type []interface{} with a value of nil
// Typed nil - an any that contains a type []any with a value of nil
return x
}
clone := make([]interface{}, len(x))
clone := make([]any, len(x))
for i, v := range x {
clone[i] = deepCopyJSONValue(v)
}
@@ -124,23 +128,23 @@ func deepCopyJSONValue(x interface{}) interface{} {
}
}
func (u *Unstructured) Set(field string, value interface{}) {
func (u *Unstructured) Set(field string, value any) {
if u.Object == nil {
u.Object = make(map[string]interface{})
u.Object = make(map[string]any)
}
_ = unstructured.SetNestedField(u.Object, value, field)
}
func (u *Unstructured) Remove(fields ...string) {
if u.Object == nil {
u.Object = make(map[string]interface{})
u.Object = make(map[string]any)
}
unstructured.RemoveNestedField(u.Object, fields...)
}
func (u *Unstructured) SetNestedField(value interface{}, fields ...string) {
func (u *Unstructured) SetNestedField(value any, fields ...string) {
if u.Object == nil {
u.Object = make(map[string]interface{})
u.Object = make(map[string]any)
}
_ = unstructured.SetNestedField(u.Object, value, fields...)
}