mitchellh / mapstructure

Go library for decoding generic map values into native Go structures and vice versa.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Source data must be an array or slice, got string

zhangchiyue opened this issue · comments

When i try decode a map[string]interface {} into stuct the error return "Source data must be an array or slice, got string".
This is the code :

func main() {
	id, _ := primitive.ObjectIDFromHex("62295b0650935306baee588c")
	source := &Person{
		Name: "Lucy",
		Data: &User{
			Username: "Luck",
			Uid:      id,
		},
	}
	bytes, _ := json.Marshal(source)
	target := &Person{}
	json.Unmarshal(bytes, target)
	fmt.Printf("%T\n", target.Data)
	result := &User{}
	config := mapstructure.DecoderConfig{
		Result:  result,
		TagName: "json",
	}
	decoder, _ := mapstructure.NewDecoder(&config)
	err := decoder.Decode(target.Data)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%+v", result)
}

type User struct {
	Username string             `json:"username"`
	Uid      primitive.ObjectID `json:"uid"`
}

type Person struct {
	Name string `json:"name"`
	Data any    `json:"data"`
}

The error message is "* 'uid': source data must be an array or slice, got string"

commented

I think that's because the primitive.ObjectID type is a an array of byte but when it decode from map the given type was string.

commented

You could add DecodeHook with given syntax, it would check the type both of destination type and origin type since ObjectID is an array we set the destination type to array if it match then convert the data to string and generate new ObjectID with it. Hopes it help!

DecodeHook: func(from, to reflect.Type, d interface{}) (interface{}, error) {
	if from.Kind() == reflect.String && to.Kind() == reflect.Array {
		sid := d.(string)
		id, err := primitive.ObjectIDFromHex(sid)
		if err != nil {
			fmt.Println(err)
		}

		d = id
	}

	return d, nil
},