mergo icon indicating copy to clipboard operation
mergo copied to clipboard

Merge Slices

Open sunsingerus opened this issue 1 year ago • 6 comments

Right now mergo provides two options to handle slices:

  1. WithAppendSlice - which appends all items from src to dst
  2. WithSliceDeepCopy - which merges with override (override is mandatory and not configurable) min(len(src), len(dst)) items

There 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.

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

sunsingerus avatar May 07 '23 17:05 sunsingerus