Skip to content

Commit

Permalink
struct: Support reflect from struct with non-anonymous structure poin…
Browse files Browse the repository at this point in the history
…ter (#259)

Co-authored-by: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>

Co-authored-by: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>
  • Loading branch information
ygj6 and unknwon committed Aug 27, 2020
1 parent 31bf7ce commit abeaefd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func (s *Section) reflectFrom(val reflect.Value) error {
continue
}

if (tpField.Type.Kind() == reflect.Ptr && tpField.Anonymous) ||
if (tpField.Type.Kind() == reflect.Ptr && tpField.Type.Elem().Kind() == reflect.Struct) ||
(tpField.Type.Kind() == reflect.Struct && tpField.Type.Name() != "Time") {
// Note: The only error here is section doesn't exist.
sec, err := s.f.GetSection(fieldName)
Expand Down
33 changes: 33 additions & 0 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,39 @@ None =
last_name = Doe
omitempty = 9
`)
})

Convey("Reflect from struct with non-anonymous structure pointer", func() {
cfg := ini.Empty()
type Rpc struct {
Enable bool `ini:"enable"`
Type string `ini:"type"`
Address string `ini:"addr"`
Name string `ini:"name"`
}
type Cfg struct {
Rpc *Rpc `ini:"rpc"`
}

config := &Cfg{
Rpc: &Rpc{
Enable: true,
Type: "type",
Address: "address",
Name: "name",
},
}
So(cfg.ReflectFrom(config), ShouldBeNil)

var buf bytes.Buffer
_, err = cfg.WriteTo(&buf)
So(buf.String(), ShouldEqual, `[rpc]
enable = true
type = type
addr = address
name = name
`)
})
})
Expand Down

0 comments on commit abeaefd

Please sign in to comment.