Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflect From Struct - section name configuration for root level properties #325

Open
1 task done
rpieczon opened this issue Jun 6, 2022 · 3 comments
Open
1 task done
Labels
feature Categorizes as related to a new feature

Comments

@rpieczon
Copy link

rpieczon commented Jun 6, 2022

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
@rpieczon rpieczon added the feature Categorizes as related to a new feature label Jun 6, 2022
@unknwon
Copy link
Member

unknwon commented Jun 25, 2022

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)

@iFrozenPhoenix
Copy link

@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
Copy link
Member

unknwon commented Aug 11, 2022

@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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Categorizes as related to a new feature
Projects
None yet
Development

No branches or pull requests

3 participants