Open sunsingerus opened 1 year ago
PR #234
@sunsingerus I'm marking this as a feature for v2.
@darccio any chances of merging my PR #234 into v2 ? What steps should be performed in order to get PR #234 accepted?
@sunsingerus v2 must be created, it's work in progress.
@sunsingerus v2 must be created, it's work in progress.
Any change the #234 being merged? Anything we can do to help making this feature possible?
@sunsingerus in your PR having the following structs:
type Index struct {
Key string `json:"key,omitempty"`
Values []string `json:"values,omitempty"`
Value string `json:"value,omitempty"`
Required string `json:"required,omitempty"`
}
type Config struct {
Index []*Index `json:"index,omitempty"`
}
Should this code work? Because it is panicking when trying to merge two maps with the desired configs...
This only represents my use case, when I need to merge two maps containing arrays...
dst := &Config{
Index: []*Index{
{
Key: "type",
Values: []string{"value1", "value2"},
Required: "true",
},
{
Key: "channel",
Values: []string{"value3", "value4"},
Required: "true",
},
},
}
src := &Config{
Index: []*Index{
{
Key: "*",
},
{
Key: "*",
},
},
}
expected := &Config{
Index: []*Index{
{
Key: "*",
Values: []string{"value1", "value2"},
Required: "true",
},
{
Key: "*",
Values: []string{"value3", "value4"},
Required: "true",
},
},
}
// convert to map[string]any using json marshal + unmarshal
mapDst, _ := map.ToMap(dst)
// convert to map[string]any using json marshal + unmarshal
mapSrc, _ := map.ToMap(src)
err := mergo.Merge(mapDst, mapSrc, mergo.WithSliceDeepMerge, mergo.WithOverride)
if err != nil {
t.Errorf("Error while merging %s", err)
}
// convert back to config using json marshal + unmarshal
resultDst, _ := map.FromMap(mapDst)
if !reflect.DeepEqual(resultDst, expected) {
t.Errorf("expected: %#v\ngot: %#v", expected, dst)
}
Right now mergo provides two options to handle slices:
WithAppendSlice
- which appends all items fromsrc
todst
WithSliceDeepCopy
- which merges with override (override is mandatory and not configurable)min(len(src), len(dst))
itemsThere is also
WithOverrideEmptySlice
option, but it is little irrelevant to the discussion.In case one needs to deep merge (not copy part of the src with override) elements of slices and add to
dst
items that are insrc
on places that are beyondlen(dst)
, looks like there is no such an option available at the moment. Better to show piece of code withsrc
,dst
andexpected
values. Example:Let's add new option, say
mergo.WithSliceDeepMerge
, (ispired bymergo.WithSliceDeepCopy
) but with the functionality described. Also it would be nice to have existing options such asmergo.WithOverride
andmergo.WithOverwriteWithEmptyValue
being able to tune merge process. Appending extra elements fromdst
tosrc
may be done configurable as well.I am ready to make PR with described functionality as well.
Upvote & Fund