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

MapTo() doesn't seem to handle embedded struct #241

Closed
tevino opened this issue May 20, 2020 · 9 comments
Closed

MapTo() doesn't seem to handle embedded struct #241

tevino opened this issue May 20, 2020 · 9 comments

Comments

@tevino
Copy link

tevino commented May 20, 2020

Describe the bug
MapTo() doesn't seem to handle embedded struct

To Reproduce

const INI = `
[Section]
FieldInSubSection = 1
FieldInSection = 2
`

type File struct {
	Section `ini:"Section"`
}

type Section struct {
	SubSection
	FieldInSection string `ini:"FieldInSection"`
}
type SubSection struct {
	FieldInSubSection string `ini:"FieldInSubSection"`
}

func main() {
	iniFile, err := ini.Load(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
}

https://play.golang.org/p/cymqihBewXM

Expected behavior
f.FieldInSubSection == "1"

@tevino tevino changed the title MapTo seems not handling embedded struct MapTo() seems not handling embedded struct May 20, 2020
@tevino tevino changed the title MapTo() seems not handling embedded struct MapTo() seems not to handle embedded struct May 20, 2020
@tevino tevino changed the title MapTo() seems not to handle embedded struct MapTo() doesn't seem to handle embedded struct May 20, 2020
@unknwon
Copy link
Member

unknwon commented May 21, 2020

https://play.golang.org/p/7QrjK_AeYPY

type Section struct {
-	SubSection
+	SubSection     `ini:"Section"`

@unknwon unknwon closed this as completed May 21, 2020
@tevino
Copy link
Author

tevino commented May 21, 2020

It works!

however, is this documented somewhere? I didn't thought of doing so since "encoding/json" doesn't need the extra tag.

@unknwon
Copy link
Member

unknwon commented May 21, 2020

@tevino
Copy link
Author

tevino commented May 22, 2020

@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 nonunique from here:

Peer []testPeer `ini:",,,nonunique"`

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
 }

@unknwon
Copy link
Member

unknwon commented May 22, 2020

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.

@tevino
Copy link
Author

tevino commented May 25, 2020

@aligator Do you have any idea to make it work?

@aligator
Copy link
Contributor

I have to look into it. But I am not sure if it's easy to support.

@aligator
Copy link
Contributor

@tevino unknown merged it. It should work now as you expected.

@tevino
Copy link
Author

tevino commented May 28, 2020

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! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants