-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ merge
asset.Labels
and asset.Platform.Labels
We will merge `asset.Labels` and `asset.Platform.Labels` for backwards compatibility. Should be removed in v12 when we remove `asset.Platform.Labels`. Signed-off-by: Salim Afiune Maya <afiune@mondoo.com>
- Loading branch information
Showing
3 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package mapx | ||
|
||
// Merge merges two maps of type `map[K]T` giving preference first map. | ||
func Merge[K comparable, V any](m1, m2 map[K]V) map[K]V { | ||
merged := make(map[K]V) | ||
|
||
// store all key:value's from the second map | ||
for key, value := range m2 { | ||
merged[key] = value | ||
} | ||
|
||
// iterate over the first map to give it preference | ||
for key, value := range m1 { | ||
merged[key] = value | ||
} | ||
|
||
return merged | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package mapx_test | ||
|
||
import ( | ||
"testing" | ||
|
||
subject "go.mondoo.com/cnquery/v11/utils/mapx" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMerge(t *testing.T) { | ||
t.Run("map[string]int", func(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
m1, m2 map[string]int | ||
expected map[string]int | ||
}{ | ||
{ | ||
name: "Merge with nil maps", | ||
m1: map[string]int{"a": 1}, | ||
m2: nil, | ||
expected: map[string]int{"a": 1}, | ||
}, | ||
{ | ||
name: "Merge with no conflicts", | ||
m1: map[string]int{"a": 1, "b": 2}, | ||
m2: map[string]int{"c": 3, "d": 4}, | ||
expected: map[string]int{"a": 1, "b": 2, "c": 3, "d": 4}, | ||
}, | ||
{ | ||
name: "Merge with conflicts, prefer first map", | ||
m1: map[string]int{"a": 10, "b": 20}, | ||
m2: map[string]int{"a": 1, "b": 2, "c": 3}, | ||
expected: map[string]int{"a": 10, "b": 20, "c": 3}, | ||
}, | ||
{ | ||
name: "First map empty", | ||
m1: map[string]int{}, | ||
m2: map[string]int{"x": 100, "y": 200}, | ||
expected: map[string]int{"x": 100, "y": 200}, | ||
}, | ||
{ | ||
name: "Second map empty", | ||
m1: map[string]int{"x": 100, "y": 200}, | ||
m2: map[string]int{}, | ||
expected: map[string]int{"x": 100, "y": 200}, | ||
}, | ||
{ | ||
name: "Both maps empty", | ||
m1: map[string]int{}, | ||
m2: map[string]int{}, | ||
expected: map[string]int{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := subject.Merge(tt.m1, tt.m2) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("map[string]string", func(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
m1, m2 map[string]string | ||
expected map[string]string | ||
}{ | ||
{ | ||
name: "Merge with nil maps", | ||
m1: nil, | ||
m2: map[string]string{"a": "coco"}, | ||
expected: map[string]string{"a": "coco"}, | ||
}, | ||
{ | ||
name: "Merge two non-empty maps with string keys", | ||
m1: map[string]string{"a": "apple", "b": "banana"}, | ||
m2: map[string]string{"b": "BLUEBERRY", "c": "cherry"}, | ||
expected: map[string]string{"a": "apple", "b": "banana", "c": "cherry"}, | ||
}, | ||
{ | ||
name: "Merge with an empty first map", | ||
m1: map[string]string{}, | ||
m2: map[string]string{"a": "apple", "b": "banana"}, | ||
expected: map[string]string{"a": "apple", "b": "banana"}, | ||
}, | ||
{ | ||
name: "Merge with an empty second map", | ||
m1: map[string]string{"a": "apple", "b": "banana"}, | ||
m2: map[string]string{}, | ||
expected: map[string]string{"a": "apple", "b": "banana"}, | ||
}, | ||
{ | ||
name: "Merge two empty maps", | ||
m1: map[string]string{}, | ||
m2: map[string]string{}, | ||
expected: map[string]string{}, | ||
}, | ||
{ | ||
name: "Merge two nil maps", | ||
m1: nil, | ||
m2: nil, | ||
expected: map[string]string{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.expected, subject.Merge(tt.m1, tt.m2)) | ||
}) | ||
} | ||
}) | ||
} |