-
-
Notifications
You must be signed in to change notification settings - Fork 274
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 Slices #233
Comments
PR #234 |
@sunsingerus I'm marking this as a feature for v2. |
@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 with
src
,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 as
mergo.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
The text was updated successfully, but these errors were encountered: