Right now mergo provides two options to handle slices:
WithAppendSlice
- which appends all items from src
to dst
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 in src
on places that are beyond len(dst)
, looks like there is no such an option available at the moment.
Better to show piece of code with src
, dst
and expected
values.
Example:
type SimpleStruct struct {
Field1 string
Field2 string
Field3 string
}
type StructWithSliceOfSimpleStructs struct {
SliceOfSimpleStructs []SimpleStruct
}
src = StructWithSliceOfSimpleStructs{
SliceOfSimpleStructs: []SimpleStruct{
{
Field1: "src:Slice[0].Field1",
Field2: "src:Slice[0].Field2",
Field3: "",
},
{
Field1: "src:Slice[1].Field1",
Field2: "src:Slice[1].Field2",
Field3: "",
},
},
}
dst = StructWithSliceOfSimpleStructs{
SliceOfSimpleStructs: []SimpleStruct{
{
Field1: "dst:Slice[0].Field1",
Field2: "",
Field3: "dst:Slice[0].Field3",
},
},
}
expected := StructWithSliceOfSimpleStructs{
SliceOfSimpleStructs: []SimpleStruct{
{
// Original dst field is expected not to be overwritten by value
Field1: "dst:Slice[0].Field1",
// Empty dst field is expected to be filled with src value
Field2: "src:Slice[0].Field2",
// Original dst field is expected not to be overwritten by empty value
Field3: "dst:Slice[0].Field3",
},
// Expected dst being appended
{
Field1: "src:Slice[1].Field1",
Field2: "src:Slice[1].Field2",
Field3: "",
},
},
}
Let's add new option, say mergo.WithSliceDeepMerge
, (ispired by mergo.WithSliceDeepCopy
) but with the functionality described.
Also it would be nice to have existing options such as mergo.WithOverride
and mergo.WithOverwriteWithEmptyValue
being able to tune merge process. Appending extra elements from dst
to src
may be done configurable as well.
I am ready to make PR with described functionality as well.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too