all: Use new reflect based CLI (#5487)
This commit is contained in:
committed by
Jakob Borg
parent
7bac927ac8
commit
dc929946fe
@@ -15,8 +15,12 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type defaultParser interface {
|
||||
ParseDefault(string) error
|
||||
}
|
||||
|
||||
// SetDefaults sets default values on a struct, based on the default annotation.
|
||||
func SetDefaults(data interface{}) error {
|
||||
func SetDefaults(data interface{}) {
|
||||
s := reflect.ValueOf(data).Elem()
|
||||
t := s.Type()
|
||||
|
||||
@@ -26,15 +30,22 @@ func SetDefaults(data interface{}) error {
|
||||
|
||||
v := tag.Get("default")
|
||||
if len(v) > 0 {
|
||||
if parser, ok := f.Interface().(interface {
|
||||
ParseDefault(string) (interface{}, error)
|
||||
}); ok {
|
||||
val, err := parser.ParseDefault(v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
if f.CanInterface() {
|
||||
if parser, ok := f.Interface().(defaultParser); ok {
|
||||
if err := parser.ParseDefault(v); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if f.CanAddr() && f.Addr().CanInterface() {
|
||||
if parser, ok := f.Addr().Interface().(defaultParser); ok {
|
||||
if err := parser.ParseDefault(v); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
f.Set(reflect.ValueOf(val))
|
||||
continue
|
||||
}
|
||||
|
||||
switch f.Interface().(type) {
|
||||
@@ -44,14 +55,14 @@ func SetDefaults(data interface{}) error {
|
||||
case int:
|
||||
i, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
panic(err)
|
||||
}
|
||||
f.SetInt(i)
|
||||
|
||||
case float64:
|
||||
i, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
panic(err)
|
||||
}
|
||||
f.SetFloat(i)
|
||||
|
||||
@@ -68,7 +79,6 @@ func SetDefaults(data interface{}) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CopyMatchingTag copies fields tagged tag:"value" from "from" struct onto "to" struct.
|
||||
|
||||
Reference in New Issue
Block a user