chore: refactor (#2085)

master
Kevin Wan 2 years ago committed by GitHub
parent f3b8fef34f
commit 18035bd4d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -60,20 +60,6 @@ func Deref(t reflect.Type) reflect.Type {
return t return t
} }
// DerefVal dereferences a value, if pointer value nil set new a value, returns is not a ptr element value.
func DerefVal(v reflect.Value) reflect.Value {
for {
if v.Kind() != reflect.Ptr {
break
}
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
return v
}
// Repr returns the string representation of v. // Repr returns the string representation of v.
func Repr(v interface{}) string { func Repr(v interface{}) string {
if v == nil { if v == nil {
@ -157,6 +143,23 @@ func doParseKeyAndOptions(field reflect.StructField, value string) (string, *fie
return key, &fieldOpts, nil return key, &fieldOpts, nil
} }
// ensureValue ensures nested members not to be nil.
// If pointer value is nil, set to a new value.
func ensureValue(v reflect.Value) reflect.Value {
for {
if v.Kind() != reflect.Ptr {
break
}
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
return v
}
func implicitValueRequiredStruct(tag string, tp reflect.Type) (bool, error) { func implicitValueRequiredStruct(tag string, tp reflect.Type) (bool, error) {
numFields := tp.NumField() numFields := tp.NumField()
for i := 0; i < numFields; i++ { for i := 0; i < numFields; i++ {
@ -491,8 +494,8 @@ func setValue(kind reflect.Kind, value reflect.Value, str string) error {
if !value.CanSet() { if !value.CanSet() {
return errValueNotSettable return errValueNotSettable
} }
value = DerefVal(value)
value = ensureValue(value)
v, err := convertType(kind, str) v, err := convertType(kind, str)
if err != nil { if err != nil {
return err return err

@ -100,7 +100,7 @@ func TestDerefValInt(t *testing.T) {
for _, each := range cases { for _, each := range cases {
t.Run(each.t.String(), func(t *testing.T) { t.Run(each.t.String(), func(t *testing.T) {
assert.Equal(t, each.expect, DerefVal(each.t).Kind()) assert.Equal(t, each.expect, ensureValue(each.t).Kind())
}) })
} }
} }

Loading…
Cancel
Save