From 79278b8eba74a64ee02c73d3e1cb9081b082d9fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=9C=C9=B4=E1=B4=8B=C9=B4=E1=B4=A1=E1=B4=8F=C9=B4?= Date: Sat, 9 May 2020 22:41:13 +0800 Subject: [PATCH] struct: ignore unexported fields --- struct.go | 4 ++++ struct_test.go | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/struct.go b/struct.go index 7f241bf..1df5471 100644 --- a/struct.go +++ b/struct.go @@ -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) diff --git a/struct_test.go b/struct_test.go index 2b8fc29..75a7ebb 100644 --- a/struct_test.go +++ b/struct_test.go @@ -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() { @@ -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"},