Skip to content

Implementing StructReflector prevents processing all fields #364

@maxenglander

Description

@maxenglander

Version

go1.24.2

Describe the bug

When a struct has a field implementing StructReflector, fields after that field are not processed.

To reproduce

Address is not present in the output.

type Address struct {
	Line1, Line2 string
}

type Employer struct {
	Name  string
	Title string
}

type Employers []*Employer

func (es Employers) ReflectINIStruct(f *File) error {
	for _, e := range es {
		f.Section(e.Name).Key("Title").SetValue(e.Title)
	}
	return nil
}

// Inspired by https://github.com/go-ini/ini/issues/199
func Test_StructReflector(t *testing.T) {
	t.Run("reflect with StructReflector interface", func(t *testing.T) {
		p := &struct {
			FirstName string
			Employer  Employers
			Address   Address
		}{
			FirstName: "Andrew",
			Employer: []*Employer{
				{
					Name:  `Employer "VMware"`,
					Title: "Staff II Engineer",
				},
				{
					Name:  `Employer "EMC"`,
					Title: "Consultant Engineer",
				},
			},
			Address: Address{
				Line1: "123 Ini Drive",
				Line2: "Section 1",
			},
		}

		f := Empty()
		assert.NoError(t, f.ReflectFrom(p))

		var buf bytes.Buffer
		_, err := f.WriteTo(&buf)
		require.NoError(t, err)

		assert.Equal(t, `FirstName = Andrew

[Employer "VMware"]
Title = Staff II Engineer

[Employer "EMC"]
Title = Consultant Engineer

[Address]
Line1 = 123 Ini Drive
Line2 = Section 1
`,
			buf.String(),
		)
	})
}

Expected behavior

Address should be present in the output.

Additional context

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions