Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant