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

Unmarshaling doesn't work for composed types #9

Closed
BarakHirsch opened this issue Jun 7, 2022 · 1 comment · Fixed by #10
Closed

Unmarshaling doesn't work for composed types #9

BarakHirsch opened this issue Jun 7, 2022 · 1 comment · Fixed by #10
Assignees
Labels
bug Something isn't working

Comments

@BarakHirsch
Copy link

Hey,
I was trying to use the lib to unmarshal a composed struct, and expected it to work like the json package and to fill the nested struct as well.
It looks like the issue is in mapStructFields that doesn't handle composed structs so it skips the nested values.

package main

import (
	"fmt"
	"github.com/perimeterx/marshmallow"
)

type A struct {
	Foo string `json:"foo"`
	Boo []int  `json:"boo"`
}

type B struct {
	A
}

func main() {
	marshmallow.EnableCache() // this is used to boost performance, read more below
	a := A{}
	result, err := marshmallow.Unmarshal([]byte(`{"foo":"bar","boo":[1,2,3],"goo":12.6}`), &a)
	fmt.Printf("a=%+v, result=%+v, err=%v", a, result, err)
	// Output: a={Foo:bar Boo:[1 2 3]}, result=map[boo:[1 2 3] foo:bar goo:12.6], err=<nil>

	b := B{}
	result, err = marshmallow.Unmarshal([]byte(`{"foo":"bar","boo":[1,2,3],"goo":12.6}`), &b)
	fmt.Printf("b=%+v, result=%+v, err=%v", b, result, err)
	// Output: b={A:{Foo: Boo:[]}}, result=map[boo:[1 2 3] foo:bar goo:12.6], err=<nil>
}

@avivpxi avivpxi added the bug Something isn't working label Jul 5, 2022
@avivpxi avivpxi self-assigned this Jul 5, 2022
@avivpxi
Copy link
Collaborator

avivpxi commented Jul 5, 2022

@BarakHirsch Thanks for submitting!
Sorry for the late reply, the library is still under heavy internal use but we plan to announce it next week.
We're hosting a meetup on the matter, you're more than welcome to drop by ❤️

Regarding the issue - I see you've zeroed in, care to push a fix? I'd love to get you on board on that.
If not, I'll update on the progress in the upcoming weeks to solve it. 🙏

@avivpxi avivpxi linked a pull request Jul 10, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants