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

Slice to slice is nil when the name is the same but the type is different #335

Open
ganxiangdong opened this issue Jul 20, 2023 · 0 comments

Comments

@ganxiangdong
Copy link

ganxiangdong commented Jul 20, 2023

Hi there,

When I try to convert an embedded slice, if the field name is the same in the slice, but the type is different, every element under the slice will be nil. I hope skip the mismatch field, continue to convert other matching fields.
What should I do?

My test code is as follows:

package main

import (
	"fmt"
	"github.com/mitchellh/mapstructure"
)

type G struct {
	Id   int
	Name string
}

type X struct {
	Id   int
	Name int
}

type GA struct {
	List []*G
}

type XA struct {
	List []*X
}

func main() {
	g2 := &GA{
		List: []*G{
			{
				Id:   11,
				Name: "g1",
			},
			{
				Id:   22,
				Name: "g2",
			},
		},
	}
	x2 := &XA{}
	err := mapstructure.Decode(g2, &x2)

	fmt.Printf("%+v \n", x2)
	//output: &{List:[<nil> <nil>]}

	fmt.Println(err)
	//output:
	//2 error(s) decoding:
	//* cannot parse 'List[0].Name' as int: strconv.ParseInt: parsing "g1": invalid syntax
	//* cannot parse 'List[1].Name' as int: strconv.ParseInt: parsing "g2": invalid syntax
}

my mapstructure version is v1.5.0. Looking forward to your reply.

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

1 participant