reprint icon indicating copy to clipboard operation
reprint copied to clipboard

cannot deep clone nested maps with interface{} types inside

Open tshi206 opened this issue 11 months ago • 0 comments

Hi just FYI, seems it doesn't deep clone nested maps with interface{} types inside:

package main

import (
	"fmt"

	"github.com/jinzhu/copier"
	"github.com/qdm12/reprint"
)

func main() {
	var fields map[string]interface{}
	msg := "body123"
	vals := []string{"fake1", "fake2", "fake3"}
	for _, regid := range vals {
		fields = map[string]interface{}{
			"message": map[string]interface{}{
				"data": msg,
				"token": regid,
			},
		}
		fmt.Println(fields)
	}
	fmt.Println("Hello World")
	fmt.Println("1", fields)
	tmp := make(map[string]interface{})
	err := reprint.FromTo(&fields, &tmp)
	if err != nil {
		panic(err)
	}
	fmt.Println("2", fields)
	fmt.Println("3", tmp)
	tmp["message"].(map[string]interface{})["token"] = "foobar123"
	fmt.Println("4", fields)
	fmt.Println("5", tmp)
	copier.CopyWithOption(&tmp, &fields, copier.Option{DeepCopy: true})
	tmp["message"].(map[string]interface{})["token"] = "foobar456"
	fmt.Println("6", fields)
	fmt.Println("7", tmp)
}

image notice println 4 and 5 where both map changed after the foobar123 value assignment

https://goplay.tools/snippet/_TIUbl-e2DT

tshi206 avatar Mar 15 '24 08:03 tshi206