Skip to content

Commit abeaefd

Browse files
ygj6unknwon
andauthored
struct: Support reflect from struct with non-anonymous structure pointer (#259)
Co-authored-by: ᴜɴᴋɴᴡᴏɴ <[email protected]> Co-authored-by: ᴜɴᴋɴᴡᴏɴ <[email protected]>
1 parent 31bf7ce commit abeaefd

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

struct.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ func (s *Section) reflectFrom(val reflect.Value) error {
595595
continue
596596
}
597597

598-
if (tpField.Type.Kind() == reflect.Ptr && tpField.Anonymous) ||
598+
if (tpField.Type.Kind() == reflect.Ptr && tpField.Type.Elem().Kind() == reflect.Struct) ||
599599
(tpField.Type.Kind() == reflect.Struct && tpField.Type.Name() != "Time") {
600600
// Note: The only error here is section doesn't exist.
601601
sec, err := s.f.GetSection(fieldName)

struct_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,39 @@ None =
554554
last_name = Doe
555555
omitempty = 9
556556
557+
`)
558+
})
559+
560+
Convey("Reflect from struct with non-anonymous structure pointer", func() {
561+
cfg := ini.Empty()
562+
type Rpc struct {
563+
Enable bool `ini:"enable"`
564+
Type string `ini:"type"`
565+
Address string `ini:"addr"`
566+
Name string `ini:"name"`
567+
}
568+
type Cfg struct {
569+
Rpc *Rpc `ini:"rpc"`
570+
}
571+
572+
config := &Cfg{
573+
Rpc: &Rpc{
574+
Enable: true,
575+
Type: "type",
576+
Address: "address",
577+
Name: "name",
578+
},
579+
}
580+
So(cfg.ReflectFrom(config), ShouldBeNil)
581+
582+
var buf bytes.Buffer
583+
_, err = cfg.WriteTo(&buf)
584+
So(buf.String(), ShouldEqual, `[rpc]
585+
enable = true
586+
type = type
587+
addr = address
588+
name = name
589+
557590
`)
558591
})
559592
})

0 commit comments

Comments
 (0)