From 25dccddb75bc5f5cdbca9eedcccdbc4c0e722182 Mon Sep 17 00:00:00 2001
From: yuangongji
Date: Wed, 19 Aug 2020 11:52:30 +0800
Subject: [PATCH] Update struct_test.go
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: 盒慄エ盒極エ盒。盒硝エ
---
struct.go | 2 +-
struct_test.go | 33 +++++++++++++++++++++++++++++++++
2 files changed, 34 insertions(+), 1 deletion(-)
diff --git a/struct.go b/struct.go
index ae5ef4a..ad90300 100644
--- a/struct.go
+++ b/struct.go
@@ -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)
diff --git a/struct_test.go b/struct_test.go
index 9d61b23..4a5df16 100644
--- a/struct_test.go
+++ b/struct_test.go
@@ -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
+
`)
})
})