Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set default values of fields of a pointer type field #18

Merged
merged 5 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
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
18 changes: 8 additions & 10 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Set(ptr interface{}) error {
}
}
}

callSetter(ptr)
return nil
}

Expand Down Expand Up @@ -133,13 +133,11 @@ func setField(field reflect.Value, defaultVal string) error {
}
field.Set(ref.Elem().Convert(field.Type()))
case reflect.Struct:
ref := reflect.New(field.Type())
if defaultVal != "" && defaultVal != "{}" {
if err := json.Unmarshal([]byte(defaultVal), ref.Interface()); err != nil {
if err := json.Unmarshal([]byte(defaultVal), field.Addr().Interface()); err != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat 👍

return err
}
}
field.Set(ref.Elem())
case reflect.Ptr:
field.Set(reflect.New(field.Type().Elem()))
}
Expand All @@ -150,13 +148,9 @@ func setField(field reflect.Value, defaultVal string) error {
setField(field.Elem(), defaultVal)
callSetter(field.Interface())
case reflect.Struct:
ref := reflect.New(field.Type())
ref.Elem().Set(field)
if err := Set(ref.Interface()); err != nil {
if err := Set(field.Addr().Interface()); err != nil {
return err
}
callSetter(ref.Interface())
field.Set(ref.Elem())
case reflect.Slice:
for j := 0; j < field.Len(); j++ {
if err := setField(field.Index(j), defaultVal); err != nil {
Expand All @@ -176,11 +170,15 @@ func shouldInitializeField(field reflect.Value, tag string) bool {
switch field.Kind() {
case reflect.Struct:
return true
case reflect.Ptr:
if !field.IsNil() && field.Elem().Kind() == reflect.Struct {
return true
}
case reflect.Slice:
return field.Len() > 0 || tag != ""
}

return (tag != "")
return tag != ""
}

// CanUpdate returns true when the given value is an initial value of its type
Expand Down
49 changes: 49 additions & 0 deletions defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,52 @@ func TestCanUpdate(t *testing.T) {
}
}
}

type Child struct {
Name string `default:"Tom"`
Age int `default:"20"`
}

type Parent struct {
Child *Child
}

func TestPointerStructMember(t *testing.T) {
m := Parent{Child: &Child{Name: "Jim"}}
Set(&m)
if m.Child.Age != 20 {
t.Errorf("20 is expected")
}
}

type Main struct {
MainInt int `default:"-"`
*Other `default:"{}"`
}

type Other struct {
OtherInt int `default:"-"`
}

func (s *Main) SetDefaults() {
if CanUpdate(s.MainInt) {
s.MainInt = 1
}
}

func (s *Other) SetDefaults() {
if CanUpdate(s.OtherInt) {
s.OtherInt = 1
}
}

func TestDefaultsSetter(t *testing.T) {
main := &Main{}
Set(main)
if main.OtherInt != 1 {
t.Errorf("expected 1 for OtherInt, got %d", main.OtherInt)
}
if main.MainInt != 1 {
t.Errorf("expected 1 for MainInt, got %d", main.MainInt)
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/creasty/defaults

go 1.11