Skip to content

Commit

Permalink
struct: ignore unexported fields when reflect (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed May 9, 2020
1 parent 0b997d7 commit ad8a106
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ func (s *Section) reflectFrom(val reflect.Value) error {
typ := val.Type()

for i := 0; i < typ.NumField(); i++ {
if !val.Field(i).CanInterface() {
continue
}

field := val.Field(i)
tpField := typ.Field(i)

Expand Down
4 changes: 2 additions & 2 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ func Test_MapToStruct(t *testing.T) {
So(ts.TimePtrNil, ShouldEqual, nil)
So(*ts.DurationPtr, ShouldEqual, 0)
So(ts.DurationPtrNil, ShouldEqual, nil)

})

Convey("Map section to struct", func() {
Expand Down Expand Up @@ -440,12 +439,13 @@ func Test_ReflectFromStruct(t *testing.T) {
GPA float64
Date time.Time
NeverMind string `ini:"-"`
ignored string
*Embeded `ini:"infos" comment:"Embeded section"`
}

t, err := time.Parse(time.RFC3339, "1993-10-07T20:17:05Z")
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{
[]time.Time{t, t},
[]string{"HangZhou", "Boston"},
Expand Down

0 comments on commit ad8a106

Please sign in to comment.