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

Nested map of struct doesn't ignore empty values #101

Open
co-in opened this issue May 22, 2021 · 0 comments
Open

Nested map of struct doesn't ignore empty values #101

co-in opened this issue May 22, 2021 · 0 comments
Assignees

Comments

@co-in
Copy link

co-in commented May 22, 2021

https://play.golang.org/p/1Wv3nOtX1CC

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/jinzhu/copier"
)

type mySubStruct struct {
	Val6 *string
	Val7 *string
}

type myStruct struct {
	Val1 *string
	Val2 *string
	Val3 *string
	Val4 map[string]mySubStruct
	Val5 map[string]*string
	Val8 *string
}

func main() {
	var err error
	m1 := new(myStruct)
	v1 := []byte(`{
		"val1":"####",
		"val2":null,
		"val3":"Tres",
		"val4":{
			"test":{
				"val6":"####",
				"val7":"Cinco"
			}
		},
		"val8":"Siete"
	}`)
	err = json.NewDecoder(bytes.NewReader(v1)).Decode(m1)
	if err != nil {
		panic(err)
	}

	m2 := new(myStruct)
	v2 := []byte(`{
		"val1":"Uno",
		"val2":"Dos",
		"val4":{
			"test":{
				"val6":"Cuatro"
			}
		},
		"val5":{
			"left":"Seis"
		},
		"val8":null
	}`)
	err = json.NewDecoder(bytes.NewReader(v2)).Decode(m2)
	if err != nil {
		panic(err)
	}

	err = copier.CopyWithOption(m1, m2, copier.Option{IgnoreEmpty: true, DeepCopy: true})

	b, _ := json.Marshal(m1)
	fmt.Println("Result\t", string(b))
	fmt.Println("Expected", `{"val1":"Uno","val2":"Dos","val3":"Tres","val4":{"test":{"val6":"Cuatro","val7":"Cinco"}},"val5":{"left":"Seis"}},"val8":"Siete"`)
}
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

2 participants