-
-
Notifications
You must be signed in to change notification settings - Fork 285
Description
Hi all, we are heavily depends on terraform and we using provider named cloudposse/terraform-provider-utils their code based on mergo to provide merge capabilities of complex structures. We noticed a issue while structures which can have slices and as for us this behavior far from correct ( maybe this is a feature :) ).
Use case:
We have 2 inputs as yaml which are decoded into terraform structures.
NOTE: in some cases it may happen that we can have dups inside arrays
Config A
list:
- value-1
- value-2
- value-3Config B
list:
- value-1
- value-5
- value-4Results if we using WithAppendSlice
list:
- value-1
- value-2
- value-3
- value-1
- value-5
- value-4Results if we using WithSliceDeepCopy
list:
- value-1
- value-2
- value-3Result with WithAppendSlice and WithSliceDeduplication
list:
- value-1
- value-2
- value-3
- value-5
- value-4NOTE: order preserved, sort not used to ensure merged lists preserving the same or close to original order.
WithSliceDeepCopy from my understanding supposed to copy items from both slices and merge them together, but this not happening and only Config A is seems copied. I'm not fully sure is that a goal for WithSliceDeepCopy or not?
In a same time WithAppendSlice almost meet our expectations, but as you can see above from result for it, we have duplicates, because both Configs include value-1.
It can be easily fixed by de-duplication of final slice, but current way of WithAppendSlice probably desired result for some cases.
Before to do any PRs i would like to ask if we can add to version 1 of mergo a modifier like WithSliceDeduplication which then can call extra logic within WithAppendSlice scope ?
All we need add extra helper function and condition to enable this function to end of this block: https://github.com/darccio/mergo/blob/master/merge.go#L171-L175
I did locally such functionality and its behavior looks good, plus using options approach we can preserve existing behavior, while with WithSliceDeduplication we can extend it without breaking anyones experience who already using `mergo.