go-ini/ini

ReflectFrom does not work properly with AllowNonUniqueSections set to true

floriankramer opened this issue · 1 comments

Describe the bug
When enabling AllowNonUniqueSections and using SectionWithIndex(name, idx).ReflectFrom to create several sections with the same name from a struct the call to ReflectFrom deletes any previously created sections with the same name.

To Reproduce

package main

import (
	"fmt"

	"gopkg.in/ini.v1"
)

type T struct {
	value string
}

func main() {
	var opts ini.LoadOptions
	opts.AllowNonUniqueSections = true

	cfg := ini.Empty(opts)
	for i := 0; i < 10; i++ {
		data := T{
			value: "test",
		}

		err := cfg.SectionWithIndex("Test", i).ReflectFrom(&data)
		if err != nil {
			panic(err)
		}
	}

	sections, _ := cfg.SectionsByName("Test")
	fmt.Println("Number of test sections", len(sections))
}

Expected behavior
Several sections should exist after running ReflectFrom several times.

According to the test Test_ReflectFromStructNonUnique, this seems to be intended behavior. In particular,

		// note: using ReflectFrom from should overwrite the existing sections

I also found this behavior to be unexpected. In my context, I was looking to overwrite/update a single non-unique section with ReflectFrom but that will delete all the other matching sections in the file.