Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions pkg/schemaconv/smd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}{}
Expand All @@ -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{}:
Expand Down