go-ini/ini

Reflect From Struct - section name configuration for root level properties

rpieczon opened this issue · 3 comments

Describe the feature

Let me configure dedicated section for each structure's root level property.
Currently all root level properties goes into 'DEFAULT' section.

Describe the solution you'd like

type SampleStruct struct {
  Property1      string `ini:"section1#prop1"`
  Property2      string `ini:"section2#prop2"`
}
input := SampleStruct{
  Property1: "value1",
  Property2: "value2",
}

file := ini.Empty()
ini.ReflectFrom(file, &input)
cfg.WriteTo(os.Stdout)

Would generate:

[section1]
prop1: value1

[section2]
prop2: value2

Describe alternatives you've considered

type SampleStruct struct {
  Property1      string `ini:"prop1" section:"section1"`
  Property2      string `ini:"prop2" section:"section2"`
}

Additional context

No response

Code of Conduct

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

Hey, if you just want to change the section for the entire struct, you can do:

file := ini.Empty()
-ini.ReflectFrom(file, &input)
+file.Section("section1").ReflectFrom(&input)

@unknwon somehow section mapping via tags, as described by @rpieczon isn't working anymore.
Could you please give an advice what I'm missing.

Example code:

type Teststruct struct {
    Prop1 `ini:"prop1" section:"section1"`
    Prop2 `ini:"prop2" section:"section2"`
}

func (tst *Teststruct) MarshalINI() ([]byte, error) {
file := ini.Empty()
	err := file.ReflectFrom(tst)
	if err != nil {
		return nil, err
	}

	buf := bytes.NewBuffer(nil)
	_, err = file.WriteTo(buf)
	if err != nil {
		return nil, err
	}

	return buf.Bytes(), nil
}

Expected output:
[section1]
prop1 = val1

[section2]
prop2 = val2

Given output:
prop1 = val1
prop2 = val2

@unknwon somehow section mapping via tags, as described by @rpieczon isn't working anymore.
Could you please give an advice what I'm missing.

This issue is a feature request, which means what's been descried is something missing :)