fix convertField and its comments.

Kubernetes-commit: d0c323fb8fbfa5c1b91ae445cbda60a416e85e65
This commit is contained in:
Jiahui Feng 2024-01-23 16:47:33 -08:00 committed by Kubernetes Publisher
parent 3a5a43790e
commit eb407cc3dd
1 changed files with 6 additions and 10 deletions

View File

@ -44,8 +44,8 @@ var _ ref.Val = (*ObjectVal)(nil)
var _ traits.Zeroer = (*ObjectVal)(nil) var _ traits.Zeroer = (*ObjectVal)(nil)
// ConvertToNative converts the object to map[string]any. // ConvertToNative converts the object to map[string]any.
// Any lists that the object refers to must be list of ObjectVal, // All nested lists are converted into []any native type.
// and will be converted into []map[string]any native type. //
// It returns an error if the target type is not map[string]any, // It returns an error if the target type is not map[string]any,
// or any recursive conversion fails. // or any recursive conversion fails.
func (v *ObjectVal) ConvertToNative(typeDesc reflect.Type) (any, error) { func (v *ObjectVal) ConvertToNative(typeDesc reflect.Type) (any, error) {
@ -55,11 +55,7 @@ func (v *ObjectVal) ConvertToNative(typeDesc reflect.Type) (any, error) {
} }
result = make(map[string]any, len(v.fields)) result = make(map[string]any, len(v.fields))
for k, v := range v.fields { for k, v := range v.fields {
converted, err := convertField(v) result[k] = convertField(v)
if err != nil {
return nil, err
}
result[k] = converted
} }
return result, nil return result, nil
} }
@ -109,7 +105,7 @@ func (v *ObjectVal) IsZeroValue() bool {
// For objects, the expected type is map[string]any // For objects, the expected type is map[string]any
// For lists, the expected type is []any // For lists, the expected type is []any
// For anything else, it is converted via value.Value() // For anything else, it is converted via value.Value()
func convertField(value ref.Val) (any, error) { func convertField(value ref.Val) any {
// special handling for lists, where the elements are converted with Value() instead of ConvertToNative // special handling for lists, where the elements are converted with Value() instead of ConvertToNative
// to allow them to become native value of any type. // to allow them to become native value of any type.
if listOfVal, ok := value.Value().([]ref.Val); ok { if listOfVal, ok := value.Value().([]ref.Val); ok {
@ -117,7 +113,7 @@ func convertField(value ref.Val) (any, error) {
for _, v := range listOfVal { for _, v := range listOfVal {
result = append(result, v.Value()) result = append(result, v.Value())
} }
return result, nil return result
} }
return value.Value(), nil return value.Value()
} }