From 6550a380e74606315d05a9b00b39ccc7bbdfeb02 Mon Sep 17 00:00:00 2001 From: Max Englander Date: Thu, 8 May 2025 14:14:30 -0400 Subject: [PATCH] struct: support additional fields after StructReflector encountered Currently when a field implements `StructReflector`, `reflectFrom` will call that and stop processing any other fields. Change it to use a `continue` instead of a `return`, so that additional fields may be processed. Signed-off-by: Max Englander --- struct.go | 5 ++++- struct_test.go | 25 +++++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/struct.go b/struct.go index a486b2f..15eca58 100644 --- a/struct.go +++ b/struct.go @@ -598,7 +598,10 @@ func (s *Section) reflectFrom(val reflect.Value) error { } if r, ok := field.Interface().(StructReflector); ok { - return r.ReflectINIStruct(s.f) + if err := r.ReflectINIStruct(s.f); err != nil { + return err + } + continue } fieldName := s.parseFieldName(tpField.Name, rawName) diff --git a/struct_test.go b/struct_test.go index b51243a..d1470e3 100644 --- a/struct_test.go +++ b/struct_test.go @@ -510,7 +510,8 @@ func Test_ReflectFromStruct(t *testing.T) { ti, err := time.Parse(time.RFC3339, "1993-10-07T20:17:05Z") require.NoError(t, err) - a := &Author{"Unknwon", true, nil, 21, 100, 2.8, ti, "", "ignored", + a := &Author{ + "Unknwon", true, nil, 21, 100, 2.8, ti, "", "ignored", &Embeded{ []time.Time{ti, ti}, []string{"HangZhou", "Boston"}, @@ -521,7 +522,8 @@ func Test_ReflectFromStruct(t *testing.T) { []float64{192.168, 10.11}, []bool{true, false}, []int{}, - }} + }, + } cfg := Empty() assert.NoError(t, ReflectFrom(cfg, a)) @@ -779,8 +781,10 @@ path = /tmp/gpm-profiles/test1.profile shadow := &ShadowStruct{ StringArray: []string{"s1", "s2"}, Allowshadow: []string{"s3", "s4"}, - Dates: []time.Time{time.Date(2020, 9, 12, 00, 00, 00, 651387237, time.UTC), - time.Date(2020, 9, 12, 00, 00, 00, 651387237, time.UTC)}, + Dates: []time.Time{ + time.Date(2020, 9, 12, 00, 00, 00, 651387237, time.UTC), + time.Date(2020, 9, 12, 00, 00, 00, 651387237, time.UTC), + }, Places: []string{"HangZhou", "Boston"}, Years: []int{1993, 1994}, Numbers: []int64{10010, 10086}, @@ -857,6 +861,10 @@ func Test_Duration(t *testing.T) { }) } +type Address struct { + Line1, Line2 string +} + type Employer struct { Name string Title string @@ -877,6 +885,7 @@ func Test_StructReflector(t *testing.T) { p := &struct { FirstName string Employer Employers + Address Address }{ FirstName: "Andrew", Employer: []*Employer{ @@ -889,6 +898,10 @@ func Test_StructReflector(t *testing.T) { Title: "Consultant Engineer", }, }, + Address: Address{ + Line1: "123 Ini Drive", + Line2: "Section 1", + }, } f := Empty() @@ -905,6 +918,10 @@ Title = Staff II Engineer [Employer "EMC"] Title = Consultant Engineer + +[Address] +Line1 = 123 Ini Drive +Line2 = Section 1 `, buf.String(), )