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

How to reset field to zero value? #43

Closed
wanghq opened this issue Oct 10, 2017 · 2 comments
Closed

How to reset field to zero value? #43

wanghq opened this issue Oct 10, 2017 · 2 comments

Comments

@wanghq
Copy link

wanghq commented Oct 10, 2017

Seems there is no way to reset a field to zero value, e.g. reset true with false, reset 1 with 0. Probably the lib shouldn't follow a pointer if it points to primitive types.

type ValueResetExample struct {
	A *bool
	B bool
	C *int
	D int
}

func valueReset() {
	bt := true
	bf := false
	i1 := 1
	i0 := 0
	dst := ValueResetExample{
		A: &bt,
		B: bt,
		C: &i1,
		D: i1,
	}
	src := ValueResetExample{
		A: &bf,
		B: bf,
		C: &i0,
		D: i0,
	}

	err := mergo.MergeWithOverwrite(&dst, src)
	if err != nil {
		panic(err)
	}
	// expected result: A: false, B: false, C: 0, D: 0
	fmt.Printf("A: %v, B: %v, C: %v, D: %v\n", *dst.A, dst.B, *dst.C, dst.D)

	err = mergo.MergeWithOverwrite(&src, dst)
	if err != nil {
		panic(err)
	}
	fmt.Printf("A: %v, B: %v, C: %v, D: %v\n", *src.A, src.B, *src.C, src.D)
}

Output

A: true, B: true, C: 1, D: 1
A: true, B: true, C: 1, D: 1
@darccio
Copy link
Owner

darccio commented Oct 19, 2017

I'll get around this ASAP but I think it's kind of no retro compatible change in behavior.

@darccio
Copy link
Owner

darccio commented Apr 2, 2018

I took longer than expected but I looked at this at last. As there is no way to know when a field has a zero-value by default or on purpose, we cannot change it in the core.

You could use a Transformer, introduced in late 2017, to work around this.

@darccio darccio closed this as completed Apr 2, 2018
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