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

Breaked merge of bool pointers in 0.3.8 #131

Closed
xt99 opened this issue Jan 13, 2020 · 11 comments
Closed

Breaked merge of bool pointers in 0.3.8 #131

xt99 opened this issue Jan 13, 2020 · 11 comments

Comments

@xt99
Copy link

xt99 commented Jan 13, 2020

Hello,
First of all, I would like to thanks contributors for this great library!

We use mergo a lot in our project for merging structs. We have bools there and I'm aware about the issues with merging false values (which can be empty or can be set by user) to the true values. This is why we're using pointers to booleans. Following code worked well with 0.3.7 version and was giving us correct values when overriding *true with *false:

package main

import (
	"fmt"
	"github.com/imdario/mergo"
)

type Foo struct {
	A *bool
	B string
}

func main() {
	src := Foo{
		A: func(v bool) *bool { return &v }(false),
		B: "src",
	}
	dest := Foo{
		A: func(v bool) *bool { return &v }(true),
		B: "dest",
	}
	mergo.Merge(&dest, src, mergo.WithOverride)
	fmt.Println(*dest.A, dest.B)
	// Should print "false src" because of override
}

But 0.3.8 version breaks this correct result of false src for this code and gives us wrong true src value now.

Can you please take a look on this issue? Thank you very much!

@tzachshabtay
Copy link

Workaround:

package main

import (
        "reflect"

	"github.com/imdario/mergo"
	"github.com/sanity-io/litter"
)

type nullTransformer struct {
}

func (t *nullTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
	if typ.Kind() == reflect.Ptr {
		return func(dst, src reflect.Value) error {
			if dst.CanSet() && !src.IsNil() {
				dst.Set(src)
			}
			return nil
		}
	}
	return nil
}

type Container struct {
    Test *bool
}

func main() {
        bTrue, bFalse := true, false
	from, to := &Container{&bFalse}, &Container{&bTrue}
	if err := mergo.Merge(to, from, mergo.WithOverride, mergo.WithTransformers(&nullTransformer{})); err != nil {
	   panic(err)
	}
	litter.Dump(to)
}

@darccio
Copy link
Owner

darccio commented May 17, 2020

@xt99 The previous behavior caused a bug fixed by #125. Please use mergo.WithOverwriteWithEmptyValue to override with empty values in the next release 0.3.10.

@darccio darccio closed this as completed May 17, 2020
darccio added a commit that referenced this issue Jul 17, 2020
commit 64219f269280df8ad7c8abf48c3a33697154e70a
Author: Dario Castañé <[email protected]>
Date:   Sat Jul 18 00:11:30 2020 +0200

    Issue #123 reverted

commit 1c0f4c5e64026215964a7365cc2da1750bf44f31
Author: János Pásztor <[email protected]>
Date:   Mon Jun 8 08:42:20 2020 +0200

    Added janoszen/containerssh as  a mergo user

commit 36e7eb818ccb22432f4c0824f820c5c832b70538
Author: Dario Castañé <[email protected]>
Date:   Sat Jul 18 00:09:18 2020 +0200

    Fatal* replaced by Error* functions

commit a3badb1749efbc887cf881cf78eba881c0864773
Author: Dario Castañé <[email protected]>
Date:   Sun May 17 16:59:56 2020 +0200

    Improved semantics of tests for issue #129

commit e7166a5d1ccf985ad9077a4ce23edc0fae0d7378
Author: Dario Castañé <[email protected]>
Date:   Sun May 17 16:52:27 2020 +0200

    Issue #129 closed

commit 4c6b27f7c7eacc5575f1eb454419258e8c92c2d1
Author: Dario Castañé <[email protected]>
Date:   Sun May 17 16:40:14 2020 +0200

    Issue #89 closed

commit 293f485af2dce978b4fd27b65789b5e4a92df4fd
Author: Dario Castañé <[email protected]>
Date:   Sun May 17 16:36:55 2020 +0200

    Improved name for the final condition to set

commit a4db553223c3a1ba8de4035e1110133ec3c5ae48
Author: Dario Castañé <[email protected]>
Date:   Sun May 17 16:06:36 2020 +0200

    Issue #138 fixed

commit 8583b70567e0f59382ca76f0bbe999470e8982df
Author: Dario Castañé <[email protected]>
Date:   Sun May 17 14:58:37 2020 +0200

    Issue #136 resolved

commit f34173f37dbe977477feb95cf3cba1cfb3919630
Author: Dario Castañé <[email protected]>
Date:   Fri Jul 17 23:50:34 2020 +0200

    Issue #131 fixed again

commit 1fdd4e8dbae7c1b8c88d550aab142f32bc3d0c86
Author: Dario Castañé <[email protected]>
Date:   Sun May 17 14:47:55 2020 +0200

    Issue 131 fixed

commit 2fbb87d2f9fbdb47fdf6d2d4274ec8a05ca698c2
Author: Tariq Ibrahim <[email protected]>
Date:   Thu Apr 23 12:49:33 2020 -0700

    add release badge to track latest release of mergo

commit 815b47be4da2a8dcb87ab36544466b89c1c0e40d
Author: Eyal <[email protected]>
Date:   Tue Apr 14 08:57:57 2020 -0400

    Fix typo transfomer -> transformer

commit 6ed75f818a997495ab9eb9d474f4bc13bad3e5bc
Author: Dario Castañé <[email protected]>
Date:   Fri Mar 27 08:34:25 2020 +0100

    README updated to last release v0.3..9

commit 9b0f1c7e5c418675c7f9d1ab588b8bf6b29ac1ff
Author: Dario Castañé <[email protected]>
Date:   Fri Jul 17 23:28:50 2020 +0200

    Improved semantics for mergeable fields

commit cf80c4cbd6102982dd91eb0ded30558692a3f43d
Author: Dario Castañé <[email protected]>
Date:   Fri Jul 17 23:18:59 2020 +0200

    Faulty test removed

commit 5a49eb5583bd3ebdae0afe011a66ae5365196918
Author: Dario Castañé <[email protected]>
Date:   Fri Jul 17 23:10:01 2020 +0200

    Working on CI fixes and v0.3.9 bugs

commit b75bbeef3b546d3fb916271538adc25bbde20f8d
Author: Dario Castañé <[email protected]>
Date:   Thu Mar 26 22:43:59 2020 +0100

    Dead code removed

commit c08f60a65adf317d234f1f5eb51572c30c9b87d0
Author: Dario Castañé <[email protected]>
Date:   Thu Mar 26 22:43:50 2020 +0100

    Destination pointer type check added to map

commit e5e7507289bba3e635e6a9df7671d58915ac0487
Author: Stefan Bourlon <[email protected]>
Date:   Mon Feb 3 11:51:09 2020 -0800

    merge: test dst is a pointer

    Return an error instead of panicking if merge receives dst != pointer

commit b8a2f9bd2feb2110ee3cb550372cc4105ebcaa99
Author: Umair Idris <[email protected]>
Date:   Mon Jan 20 05:21:09 2020 -0500

    Fix lint (#135)

commit 3cebbeca61cb9226249551e9c8afc24bf64fe559
Author: komalsinha-g <[email protected]>
Date:   Fri Jan 17 14:36:55 2020 +0530

    Make "OverwriteWithEmptyValue" in config struct to be set by any method. (#133)

    Make "OverwriteWithEmptyValue" in config struct to be set by any method.

    this make the merge more customizable. In cases, where request toggles a boolean value and the the request message can change a "true" value to false we can pass the flag to merge accordingly, which is not possible otherwise.

commit f444b9dcd2f5e66f8572b474b465564ba4f54240
Author: Dario Castañé <[email protected]>
Date:   Wed Jan 1 22:27:24 2020 +0100

    DeepSource support

commit a6318969156f71a80c41afd42104c53f239ff23a
Author: Dario Castañé <[email protected]>
Date:   Wed Jan 1 22:25:43 2020 +0100

    Assert removed

commit 6c2f66f933a1741f448e437dea3cb491a7c979d8
Author: Peer Xu <[email protected]>
Date:   Sat Oct 12 10:35:47 2019 +0800

    Fixed typo

commit 8993363a0c7c25aec36e0da04e82d4988d6cc96b
Author: Peer Xu <[email protected]>
Date:   Sat Oct 12 10:26:25 2019 +0800

    added WithSliceDeepCopy config flag
@pckhoi
Copy link

pckhoi commented May 23, 2021

I think mergo.WithOverwriteWithEmptyValue does not fix this. A pointer to false isn't an empty value.

@normanjaeckel
Copy link

Can we please reopen this issue? mergo.WithOverwriteWithEmptyValue does not fix this.

@darccio
Copy link
Owner

darccio commented Oct 19, 2021

@pckhoi @normanjaeckel Reopened, but I'm not able to dedicate time to fix it.

@darccio darccio reopened this Oct 19, 2021
@tbnguyen1407
Copy link

Hello, any update on this?

@darccio
Copy link
Owner

darccio commented Mar 31, 2022

@tbnguyen1407 No, sorry. My current job is time-consuming and I'm not able to deal with issues without PRs.

octobered added a commit to octobered/mergo that referenced this issue Jul 23, 2022
    In some case, nil pointer merging should be skipped. This field could work with `WithOverwriteWithEmptyValue` to solve situation in darccio#131, a false bool pointer will be merged and a nil bool pointer will be ignored.
@tbnguyen1407
Copy link

Hello, can you help look at the PR? @imdario

@darccio
Copy link
Owner

darccio commented Aug 13, 2022

@tbnguyen1407 Still the same situation. Would this code from @octobered help? octobered@9e3abad

@eh-steve
Copy link

eh-steve commented Oct 28, 2022

Does this address the issue (I've added more complete tests)?

@darccio
Copy link
Owner

darccio commented Oct 28, 2022

@eh-steve I'll check it. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants