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

Merge with override using bool values gives unexpected behavior. #165

Closed
neppi opened this issue Sep 23, 2020 · 4 comments
Closed

Merge with override using bool values gives unexpected behavior. #165

neppi opened this issue Sep 23, 2020 · 4 comments

Comments

@neppi
Copy link

neppi commented Sep 23, 2020

type SomeBools struct {
	BoolA bool
	BoolB bool
	BoolC bool
	BoolD bool
}

func testBoolMerge() {
	A := SomeBools{
		BoolA: true,
		BoolB: true,
		BoolC: false,
		BoolD: false,
	}
	B := SomeBools{
		BoolA: true,
		BoolB: false,
		BoolC: true,
		BoolD: false,
	}

	log.Info("merging with Override option")
	log.Info(A)
	log.Info(B)
	mergo.Merge(&A, B, mergo.WithOverride)
	log.Info(A)
}

results in

  INFO ▶ testBoolMerge ▶  merging with Override option
  INFO ▶ testBoolMerge ▶  {true true false false}
  INFO ▶ testBoolMerge ▶  {true false true false}
  INFO ▶ testBoolMerge ▶  {true true true false}

BoolB is expected to be false too.

@xscode-auto-reply
Copy link

Thanks for opening a new issue. The team has been notified and will review it as soon as possible.
For urgent issues and priority support, visit https://xscode.com/imdario/mergo

@dgurns
Copy link

dgurns commented Oct 20, 2020

Having the same issue here. Merging will update false to true, but will not update true to false, since perhaps it sees the incoming false as a zero value. Seems like a pretty basic use case though. Am I missing something?

@dionysius
Copy link

Yes, this is definitely it what @dgurns wrote. false is the zero value for bool so it is IMO correct that mergo acts that way. You have the option WithOverwriteWithEmptyValue to include zero values also to ovewrite

@darccio
Copy link
Owner

darccio commented Mar 1, 2021

@dgurns Please refer to the official documentation about zero values: https://golang.org/ref/spec#The_zero_value

As you already wrote, and @dionysius confirmed, false is the zero value for bool.

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

4 participants