-
Notifications
You must be signed in to change notification settings - Fork 18
/
properties.go
81 lines (69 loc) · 1.56 KB
/
properties.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package common
import (
"encoding/json"
"fmt"
"github.com/open-component-model/ocm/pkg/generics"
)
// Properties describes a set of name/value pairs.
type Properties map[string]string
// Digest returns the object digest of a Property set.
func (p Properties) Digest() ([]byte, error) {
data, err := json.Marshal(p)
if err != nil {
return nil, fmt.Errorf("failed to produce digest: %w", err)
}
return data, nil
}
func (p Properties) SetNonEmptyValue(name, value string) {
if value != "" {
p[name] = value
}
}
// Equals compares two identities.
func (p Properties) Equals(o Properties) bool {
if len(p) != len(o) {
return false
}
for k, v := range p {
if v2, ok := o[k]; !ok || v != v2 {
return false
}
}
return true
}
// Match implements the selector interface.
func (p Properties) Match(obj map[string]string) (bool, error) {
for k, v := range p {
if obj[k] != v {
return false, nil
}
}
return true, nil
}
// Names returns the set of property names.
func (c Properties) Names() generics.Set[string] {
return generics.KeySet(c)
}
// String returns a string representation.
func (c Properties) String() string {
if c == nil {
return "<none>"
}
//nolint: errchkjson // just a string map
d, _ := json.Marshal(c)
return string(d)
}
// Copy copies identity.
func (p Properties) Copy() Properties {
if p == nil {
return nil
}
n := Properties{}
for k, v := range p {
n[k] = v
}
return n
}