1
// Copyright Istio Authors
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
17
import "maps" // nolint: depguard
19
// Equal reports whether two maps contain the same key/value pairs.
20
// Values are compared using ==.
21
func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool {
22
return maps.Equal(m1, m2)
25
// Clone returns a copy of the map.
26
// The elements are copied using assignment, so this is a shallow clone.
27
func Clone[M ~map[K]V, K comparable, V any](m M) M {
31
// Values returns the values of the map m.
32
// The values will be in an indeterminate order.
33
func Values[M ~map[K]V, K comparable, V any](m M) []V {
34
r := make([]V, 0, len(m))
41
// Keys returns the keys of the map m.
42
// The keys will be in an indeterminate order.
43
func Keys[M ~map[K]V, K comparable, V any](m M) []K {
44
r := make([]K, 0, len(m))
51
// MergeCopy creates a new map by merging all key/value pairs from base and override.
52
// When a key in override is already present in base,
53
// the value in base will be overwritten by the value associated
54
// with the key in override.
55
func MergeCopy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](base M1, override M2) M1 {
56
dst := make(M1, len(base)+len(override))
58
maps.Copy(dst, override)
62
// Contains checks if all key-value pairs in 'subset' are present in 'superset'.
63
// It returns true only if every key in 'subset' exists in 'superset' and their corresponding values are equal.
64
func Contains[M1, M2 ~map[K]V, K comparable, V comparable](superset M1, subset M2) bool {
65
for key, value := range subset {
66
if supersetValue, ok := superset[key]; !ok || supersetValue != value {
73
// EqualFunc is like Equal, but compares values using eq.
74
// Keys are still compared with ==.
75
func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool {
76
return maps.EqualFunc(m1, m2, eq)