Skip to content
This repository has been archived by the owner on Oct 11, 2018. It is now read-only.

Commit

Permalink
Fix set value on nested field
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Jun 1, 2016
1 parent 3fe2fac commit ca506a7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion field.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,16 @@ func (f *Field) Field(name string) *Field {
// FieldOk returns the field from a nested struct. The boolean returns whether
// the field was found (true) or not (false).
func (f *Field) FieldOk(name string) (*Field, bool) {
v := strctVal(f.value.Interface())
value := &f.value
// value must be settable so we need to make sure it holds the address of the
// variable and not a copy, so we can pass the pointer to strctVal instead of a
// copy (which is not assigned to any variable, hence not settable).
// see "https://blog.golang.org/laws-of-reflection#TOC_8."
if f.value.Kind() != reflect.Ptr {
a := f.value.Addr()
value = &a
}
v := strctVal(value.Interface())
t := v.Type()

field, ok := t.FieldByName(name)
Expand Down
22 changes: 22 additions & 0 deletions structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,28 @@ func TestNestedNilPointer(t *testing.T) {
_ = Map(personWithDogWithCollar) // Doesn't panic
}

func TestSetValueOnNestedField(t *testing.T) {
type Base struct {
ID int
}

type User struct {
Base
Name string
}

u := User{}
s := New(&u)
f := s.Field("Base").Field("ID")
err := f.Set(10)
if err != nil {
t.Errorf("Error %v", err)
}
if f.Value().(int) != 10 {
t.Errorf("Value should be equal to 10, got %v", f.Value())
}
}

type Person struct {
Name string
Age int
Expand Down

0 comments on commit ca506a7

Please sign in to comment.