ini icon indicating copy to clipboard operation
ini copied to clipboard

Allow NonUniqueSections not working for Embedded Structs

Open olapiv opened this issue 2 years ago • 0 comments

Version

v1.67.0

Describe the bug

When attempting to create an ini file from a struct containing a slice field, whereby

  1. the type of the slice contains an embedded struct
  2. the elements of the slice are supposed to be displayed as non-unique sections in the ini file

the elements of the slice are correctly shown as separate sections. However, the embedded struct is only shown once; the embedded struct of the last element is written to the first section.

To reproduce

type BaseSlot struct {
	Id int `ini:",nonunique"`
}

type BigSlot struct {
	*BaseSlot `ini:"big_slot,nonunique"`
	Color     string
}

type IniFile struct {
	BigSlots []BigSlot `ini:"big_slot,nonunique"`
}

func main() {
	iniFile := &IniFile{
		BigSlots: []BigSlot{
			{
				BaseSlot: &BaseSlot{
					Id: 3,
				},
				Color: "blue",
			},
			{
				BaseSlot: &BaseSlot{
					Id: 4,
				},
				Color: "red",
			},
		},
	}

	cfg := ini.Empty(ini.LoadOptions{
		AllowNonUniqueSections: true,
	})
	err := ini.ReflectFrom(cfg, iniFile)
	if err != nil {
		panic(err)
	}
	cfg.SaveTo("./some_file.ini")
}

the resulting file looks like this:

[big_slot]
Id    = 4
Color = blue

[big_slot]
Color = red

Expected behavior

The file to look as follows:

[big_slot]
Id    = 3
Color = blue

[big_slot]
Id    = 4
Color = red

Additional context

No response

Code of Conduct

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

olapiv avatar Sep 05 '22 12:09 olapiv