struct: ignore unexported fields when reflect (#239)

This commit is contained in:
ᴜɴᴋɴᴡᴏɴ
2020-05-09 22:43:35 +08:00
committed by GitHub
parent 0b997d758f
commit ad8a10643d
2 changed files with 6 additions and 2 deletions
+4
View File
@@ -564,6 +564,10 @@ func (s *Section) reflectFrom(val reflect.Value) error {
typ := val.Type() typ := val.Type()
for i := 0; i < typ.NumField(); i++ { for i := 0; i < typ.NumField(); i++ {
if !val.Field(i).CanInterface() {
continue
}
field := val.Field(i) field := val.Field(i)
tpField := typ.Field(i) tpField := typ.Field(i)
+2 -2
View File
@@ -260,7 +260,6 @@ func Test_MapToStruct(t *testing.T) {
So(ts.TimePtrNil, ShouldEqual, nil) So(ts.TimePtrNil, ShouldEqual, nil)
So(*ts.DurationPtr, ShouldEqual, 0) So(*ts.DurationPtr, ShouldEqual, 0)
So(ts.DurationPtrNil, ShouldEqual, nil) So(ts.DurationPtrNil, ShouldEqual, nil)
}) })
Convey("Map section to struct", func() { Convey("Map section to struct", func() {
@@ -440,12 +439,13 @@ func Test_ReflectFromStruct(t *testing.T) {
GPA float64 GPA float64
Date time.Time Date time.Time
NeverMind string `ini:"-"` NeverMind string `ini:"-"`
ignored string
*Embeded `ini:"infos" comment:"Embeded section"` *Embeded `ini:"infos" comment:"Embeded section"`
} }
t, err := time.Parse(time.RFC3339, "1993-10-07T20:17:05Z") t, err := time.Parse(time.RFC3339, "1993-10-07T20:17:05Z")
So(err, ShouldBeNil) So(err, ShouldBeNil)
a := &Author{"Unknwon", true, nil, 21, 100, 2.8, t, "", a := &Author{"Unknwon", true, nil, 21, 100, 2.8, t, "", "ignored",
&Embeded{ &Embeded{
[]time.Time{t, t}, []time.Time{t, t},
[]string{"HangZhou", "Boston"}, []string{"HangZhou", "Boston"},