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

feat: use isReflectNil instead of isEmptyValue to gate Transformers #1

Merged
merged 1 commit into from
Feb 2, 2022

Conversation

GGabriele
Copy link
Collaborator

Original upstream PR with reasoning behind this patch: darccio#203

Couple of additional comments:

  • this change doesn't affect the normal behaviour of mergo (see below), but it only affects how transformers are gated
  • since custom transformers need to match data types in order to execute, this shouldn't affect any existing transformers either
$ go test -race ./... -count=1
ok  	github.com/imdario/mergo	0.418s

@GGabriele GGabriele requested a review from hbagdi January 26, 2022 10:11
@CLAassistant
Copy link

CLAassistant commented Jan 26, 2022

CLA assistant check
All committers have signed the CLA.

@GGabriele
Copy link
Collaborator Author

Not sure what's going on with travis, as same tests are passing locally :\

@hbagdi
Copy link
Member

hbagdi commented Jan 27, 2022

Can you dig deeper to see if there are versions that are not matching up?

@GGabriele
Copy link
Collaborator Author

I added a build matrix for various releases of go and it seems this works with >= 1.13

I see 1.13 introduced some changes to the reflect package, maybe that's related

@hbagdi
Copy link
Member

hbagdi commented Feb 1, 2022

I'm not familiar with this codebase so can't review. I'll trust you with this one for now.
Some changes:

  • Let's test on Go 1.13 and above and get the CI green again.
  • Squash into a single commit and capture all the context in the commit body, with links to Github issues where needed. A 100-line commit message body is not a problem ever.

Mergo handles merges with zero-value fields as follows:

	1. dst: true       src: false      ->     dst: true
	2. dst: nil        src: false      ->     dst: false
	3. dst: nil	   src: true	   ->	  dst: true
	4. dst: false	   src: true 	   ->	  dst: true

In some cases (darccio#166) it may be desirable
to change this default behaviour using a custom Transformer, for example
to not overwrite 'dst' when it's already set to 'false' (or '0' etc).
Unfortunately, in such cases (when dst holds a zero-value) no Transformers can
be applied due to the way these are gated (darccio#166).

Here we are changing how transformers are gated by using the isReflectNil function
insted of isEmptyValue.

Note that:
- this change doesn't affect the default behaviour of mergo, as it only affects
  how transformers are gated
- since custom transformers need to match data types in order to execute,
  this shouldn't affect any existing transformers either
@GGabriele
Copy link
Collaborator Author

I'm not familiar with this codebase so can't review. I'll trust you with this one for now. Some changes:

  • Let's test on Go 1.13 and above and get the CI green again.
  • Squash into a single commit and capture all the context in the commit body, with links to Github issues where needed. A 100-line commit message body is not a problem ever.

Done!

@hbagdi
Copy link
Member

hbagdi commented Feb 1, 2022

Go ahead and merge and do a release.

@GGabriele GGabriele merged commit a7ccdf3 into master Feb 2, 2022
@GGabriele GGabriele deleted the gating_transformers branch February 2, 2022 08:24
GGabriele added a commit to Kong/go-kong that referenced this pull request Feb 2, 2022
We recently forked and patched mergo (Kong/mergo#1)
to improve the way we can handle zero-value fields with defaults values.
Currently, if an entity field is set with a zero-value (e.g. a target
with weight=0, a route with strip_path=false etc.), we will overwrite it
when merging it with its default if this is not a zero-value itself.

This update the mergo dependence and add a custom transformer to support
such cases as well. Also, add few test cases with zero-value fields.
rainest pushed a commit to Kong/go-kong that referenced this pull request Feb 2, 2022
We recently forked and patched mergo (Kong/mergo#1)
to improve the way we can handle zero-value fields with defaults values.
Currently, if an entity field is set with a zero-value (e.g. a target
with weight=0, a route with strip_path=false etc.), we will overwrite it
when merging it with its default if this is not a zero-value itself.

This update the mergo dependence and add a custom transformer to support
such cases as well. Also, add few test cases with zero-value fields.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants