Skip to content

Commit

Permalink
fix: skips unconvertable types
Browse files Browse the repository at this point in the history
and emmits WARN to stdout
  • Loading branch information
bartoszmajsak committed Sep 1, 2023
1 parent f25c848 commit 561d0e6
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pkg/kfconfig/ossmplugin/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ossmplugin

import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"reflect"
)
Expand Down Expand Up @@ -84,14 +85,21 @@ func setDefaults(obj interface{}) {
field := value.Field(i)
tag := typ.Field(i).Tag.Get("default")

if tag != "" && field.CanSet() && isEmptyValue(field) {
defaultValue := reflect.ValueOf(tag)
field.Set(defaultValue)
}

if field.Kind() == reflect.Struct {
setDefaults(field.Addr().Interface())
}

if tag != "" && field.IsValid() && field.CanSet() && isEmptyValue(field) {
defaultValue := reflect.ValueOf(tag)
targetType := field.Type()
if defaultValue.Type().ConvertibleTo(targetType) {
convertedValue := defaultValue.Convert(targetType)
field.Set(convertedValue)
} else {
panic(fmt.Sprintf("WARN: unable to convert \"%s\" to %s\n", defaultValue, targetType.Name()))
}
}

}
}

Expand Down

0 comments on commit 561d0e6

Please sign in to comment.