From 64c078563bf94abdcfd7c6bdc8fd64cd78e0d688 Mon Sep 17 00:00:00 2001 From: liubog2008 Date: Wed, 5 Feb 2025 17:09:03 +0800 Subject: [PATCH] fix(proto): fix GvkParser from proto.NewOpenAPIV3Data Signed-off-by: liubog2008 --- pkg/schemaconv/smd.go | 48 +++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/pkg/schemaconv/smd.go b/pkg/schemaconv/smd.go index 9887d185b..9fbbe2a76 100644 --- a/pkg/schemaconv/smd.go +++ b/pkg/schemaconv/smd.go @@ -125,17 +125,9 @@ func makeUnions(extensions map[string]interface{}) ([]schema.Union, error) { return nil, fmt.Errorf(`"x-kubernetes-unions" should be a list, got %#v`, unions) } for _, iunion := range unions { - union, ok := iunion.(map[interface{}]interface{}) - if !ok { - return nil, fmt.Errorf(`"x-kubernetes-unions" items should be a map of string to unions, got %#v`, iunion) - } - unionMap := map[string]interface{}{} - for k, v := range union { - key, ok := k.(string) - if !ok { - return nil, fmt.Errorf(`"x-kubernetes-unions" has non-string key: %#v`, k) - } - unionMap[key] = v + unionMap, err := toMap(iunion) + if err != nil { + return nil, fmt.Errorf(`"x-kubernetes-unions" has unexpected items: %w`, err) } schemaUnion, err := makeUnion(unionMap) if err != nil { @@ -179,19 +171,14 @@ func makeUnion(extensions map[string]interface{}) (schema.Union, error) { } if ifields, ok := extensions["fields-to-discriminateBy"]; ok { - fields, ok := ifields.(map[interface{}]interface{}) - if !ok { - return schema.Union{}, fmt.Errorf(`"fields-to-discriminateBy" must be a map[string]string, got: %#v`, ifields) + fields, err := toMap(ifields) + if err != nil { + return schema.Union{}, fmt.Errorf(`"fields-to-discriminateBy" must be a map[string]string: %w`, err) } // Needs sorted keys by field. keys := []string{} - for ifield := range fields { - field, ok := ifield.(string) - if !ok { - return schema.Union{}, fmt.Errorf(`"fields-to-discriminateBy": field must be a string, got: %#v`, ifield) - } + for field := range fields { keys = append(keys, field) - } sort.Strings(keys) reverseMap := map[string]struct{}{} @@ -217,6 +204,27 @@ func makeUnion(extensions map[string]interface{}) (schema.Union, error) { return union, nil } +func toMap(o interface{}) (map[string]interface{}, error) { + switch m := o.(type) { + // "gopkg.in/yaml.v3" will encode data into map[string]interface{} + case map[string]interface{}: + return m, nil + // "sigs.k8s.io/yaml/goyaml.v2" will encode data into map[interface{}]interface{} + case map[interface{}]interface{}: + ret := map[string]interface{}{} + for k, v := range m { + key, ok := k.(string) + if !ok { + return nil, fmt.Errorf(`has non-string key: %#v`, k) + } + ret[key] = v + } + return ret, nil + default: + return nil, fmt.Errorf(`items should be a map of string to interface, got %#v`, o) + } +} + func toStringSlice(o interface{}) (out []string, ok bool) { switch t := o.(type) { case []interface{}: