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

Issue with boolean type (bool) #24

Closed
strmaks opened this issue Mar 9, 2016 · 8 comments
Closed

Issue with boolean type (bool) #24

strmaks opened this issue Mar 9, 2016 · 8 comments

Comments

@strmaks
Copy link

strmaks commented Mar 9, 2016

If struct has a boolean fields, merge result is prefer true value, but this is not correct.
Example:

package main

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

type Foo struct {
    A string
    B int64
    C bool
}

func main() {
    src := Foo{
        A: "one",
        C: true,
    }

    dest := Foo{
        A: "two",
        B: 2,
        C: false,
    }

    mergo.Merge(&dest, src)

    fmt.Println(dest)
    // Will print
    // {two 2 true}
    // but expected result is {two 2 false}
}
@darccio
Copy link
Owner

darccio commented Mar 9, 2016

This is the expected behavior. As stated in README:

Mergo is intended to assign only zero value fields on destination with source value.

The zero value of a boolean field is false. It isn't possible to avoid it even with a pointer because Merge follows the pointers too. So, when it finds a pointer to bool with false value, it will overwrite with a true value if src matching field is true.

If you have any doubt, don't hesitate to ask me.

darccio added a commit that referenced this issue Mar 9, 2016
@strmaks
Copy link
Author

strmaks commented May 16, 2016

Thank you.

@TomOperator
Copy link

For those of you who need it, @Pothulapati built a fork that solves this here.

@PamelaKelly
Copy link

This is the expected behavior.

@darccio does this mean it is not possible to merge booleans at all using this tool then? If we cannot rely on it respecting a legitimate value of false? Or do you have a recommendation for how to get around this?

It would be unfortunate if so as it would make it unusable for anyone with boolean values in their yaml, and otherwise the tool is brilliant. :(

@vtolstov
Copy link

i think that for bool case we need to add additional logic

@PamelaKelly
Copy link

i think that for bool case we need to add additional logic

Do you mean in the mergo repo or where it is being called?

@vtolstov
Copy link

i think that for bool case we need to add additional logic

Do you mean in the mergo repo or where it is being called?

in mergo repo, but i dont know how to detect false vs not set =)

@emirot
Copy link

emirot commented Jul 23, 2024

Here is 3 ways to handle merge boolean the way we would like:

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

6 participants