-
Notifications
You must be signed in to change notification settings - Fork 374
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
MapTo() doesn't seem to handle embedded struct #241
Comments
MapTo
seems not handling embedded struct
https://play.golang.org/p/7QrjK_AeYPY type Section struct {
- SubSection
+ SubSection `ini:"Section"` |
It works! however, is this documented somewhere? I didn't thought of doing so since "encoding/json" doesn't need the extra tag. |
@unknwon Thanks for the help so far, I tried adding the extra tag, but it doesn't work in a more complex situation. Here's an example of the structure I'm working with, please take a look. I learned the usage of Line 93 in ad8a106
Am I missing something? const INI = `
[Section]
FieldInSubSection = 1
FieldInSection = 2
[Section]
FieldInSubSection = 3
FieldInSection = 4
`
type File struct {
Sections []Section `ini:"Section,,,nonunique"`
}
type Section struct {
SubSection `ini:"Section"`
FieldInSection string `ini:"FieldInSection"`
}
type SubSection struct {
FieldInSubSection string `ini:"FieldInSubSection"`
}
func main() {
iniFile, err := ini.LoadSources(ini.LoadOptions{AllowNonUniqueSections: true}, strings.NewReader(INI))
if err != nil {
log.Fatalf("load error: %v", err)
}
f := new(File)
iniFile.MapTo(f)
fmt.Printf("%+v\n%+v", f.Sections[0], f.Sections[1]) // f.Sections[1].FieldInSubSection should be 3
} https://play.golang.org/p/c96iaAH_z00 diff --git a/b b/a
index f8d5ecf..5725b31 100644
const INI = `
[Section]
FieldInSubSection = 1
FieldInSection = 2
+
+[Section]
+FieldInSubSection = 3
+FieldInSection = 4
`
type File struct {
- Section `ini:"Section"`
+ Sections []Section `ini:"Section,,,nonunique"`
}
func main() {
- iniFile, err := ini.Load(strings.NewReader(INI))
+ iniFile, err := ini.LoadSources(ini.LoadOptions{AllowNonUniqueSections: true}, strings.NewReader(INI))
if err != nil {
log.Fatalf("load error: %v", err)
}
f := new(File)
iniFile.MapTo(f)
- fmt.Printf("%+v", f) // FieldInSubSection is not mapped
+ fmt.Printf("%+v\n%+v", f.Sections[0], f.Sections[1]) // f.Sections[1].FieldInSubSection should be 3
} |
Hmm, I can think this is a bug/unsupported but no idea how to make it work. Non-unique seems doesn't play well with struct mapping. |
@aligator Do you have any idea to make it work? |
I have to look into it. But I am not sure if it's easy to support. |
@tevino unknown merged it. It should work now as you expected. |
Thanks for your PR @aligator, it works like a charm so far, I'm still evaluating it, I'll let you know if anything goes wrong. Cheers! 🎉 |
Describe the bug
MapTo()
doesn't seem to handle embedded structTo Reproduce
https://play.golang.org/p/cymqihBewXM
Expected behavior
f.FieldInSubSection == "1"
The text was updated successfully, but these errors were encountered: