Skip to content

Commit 4801358

Browse files
committed
Apply feedback
1 parent ad0f61a commit 4801358

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

pkg/ottl/ottlfuncs/README.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ List of available Functions:
1616
- [delete_matching_keys](#delete_matching_keys)
1717
- [keep_keys](#keep_keys)
1818
- [limit](#limit)
19-
- [merge](#merge)
19+
- [merge_maps](#merge_maps)
2020
- [replace_all_matches](#replace_all_matches)
2121
- [replace_all_patterns](#replace_all_patterns)
2222
- [replace_match](#replace_match)
@@ -239,12 +239,11 @@ Examples:
239239

240240
- `limit(resource.attributes, 50, ["http.host", "http.method"])`
241241

242-
### merge
242+
### merge_maps
243243

244-
`merge(target, source, strategy)`
244+
`merge_maps(target, source, strategy)`
245245

246-
The `merge` function merges the source type into the target type using the supplied strategy to handle conflicts.
247-
Currently only merging of maps is supported
246+
The `merge_maps` function merges the source map into the target map using the supplied strategy to handle conflicts.
248247

249248
`target` is a `pdata.Map` type field. `source` is a `pdata.Map` type field. `strategy` is a string that must be one of `insert`, `update`, or `upsert`.
250249

@@ -253,17 +252,17 @@ If strategy is:
253252
- `update`: Update the entry in `target` with the value from `source` where the key does exist.
254253
- `upsert`: Performs insert or update. Insert the value from `source` into `target` where the key does not already exist and update the entry in `target` with the value from `source` where the key does exist.
255254

256-
`merge` is a special case of the [`set` function](#set). If you need to completely override `target`, use `set` instead.
255+
`merge_maps` is a special case of the [`set` function](#set). If you need to completely override `target`, use `set` instead.
257256

258257
Examples:
259258

260-
- `merge(attributes, ParseJSON(body), "upsert")`
259+
- `merge_maps(attributes, ParseJSON(body), "upsert")`
261260

262261

263-
- `merge(attributes, ParseJSON(attributes["kubernetes"]), "update")`
262+
- `merge_maps(attributes, ParseJSON(attributes["kubernetes"]), "update")`
264263

265264

266-
- `merge(attributes, resource.attributes, "insert")`
265+
- `merge_maps(attributes, resource.attributes, "insert")`
267266

268267
### replace_all_matches
269268

pkg/ottl/ottlfuncs/func_merge.go renamed to pkg/ottl/ottlfuncs/func_merge_maps.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ const (
2929
UPSERT = "upsert"
3030
)
3131

32-
func Merge[K any](target ottl.Getter[K], source ottl.Getter[K], strategy string) (ottl.ExprFunc[K], error) {
32+
// MergeMaps function merges the source map into the target map using the supplied strategy to handle conflicts.
33+
// Strategy definitions:
34+
//
35+
// insert: Insert the value from `source` into `target` where the key does not already exist.
36+
// update: Update the entry in `target` with the value from `source` where the key does exist
37+
// upsert: Performs insert or update. Insert the value from `source` into `target` where the key does not already exist and update the entry in `target` with the value from `source` where the key does exist.
38+
func MergeMaps[K any](target ottl.Getter[K], source ottl.Getter[K], strategy string) (ottl.ExprFunc[K], error) {
3339
if strategy != INSERT && strategy != UPDATE && strategy != UPSERT {
3440
return nil, fmt.Errorf("invalid value for strategy, %v, must be 'insert', 'update' or 'upsert'", strategy)
3541
}
@@ -64,12 +70,8 @@ func Merge[K any](target ottl.Getter[K], source ottl.Getter[K], strategy string)
6470
}
6571
case UPSERT:
6672
mergeFunc = func(k string, v pcommon.Value) {
67-
if tv, ok := targetMap.Get(k); ok {
68-
v.CopyTo(tv)
69-
} else {
70-
tv := targetMap.PutEmpty(k)
71-
v.CopyTo(tv)
72-
}
73+
tv := targetMap.PutEmpty(k)
74+
v.CopyTo(tv)
7375
}
7476
default:
7577
return nil, fmt.Errorf("unknown strategy, %v", strategy)

pkg/ottl/ottlfuncs/func_merge_test.go renamed to pkg/ottl/ottlfuncs/func_merge_maps_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func Test_MergeMaps(t *testing.T) {
149149
scenarioMap := pcommon.NewMap()
150150
input.CopyTo(scenarioMap)
151151

152-
exprFunc, err := Merge[pcommon.Map](targetGetter, tt.source, tt.strategy)
152+
exprFunc, err := MergeMaps[pcommon.Map](targetGetter, tt.source, tt.strategy)
153153
assert.NoError(t, err)
154154

155155
result, err := exprFunc(context.Background(), scenarioMap)

0 commit comments

Comments
 (0)