-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathutil.go
190 lines (169 loc) · 4.15 KB
/
util.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package in_toto
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
)
var ErrUnknownMetadataType = errors.New("unknown metadata type encountered: not link or layout")
/*
Set represents a data structure for set operations. See `NewSet` for how to
create a Set, and available Set receivers for useful set operations.
Under the hood Set aliases map[string]struct{}, where the map keys are the set
elements and the map values are a memory-efficient way of storing the keys.
*/
type Set map[string]struct{}
/*
NewSet creates a new Set, assigns it the optionally passed variadic string
elements, and returns it.
*/
func NewSet(elems ...string) Set {
var s Set = make(map[string]struct{})
for _, elem := range elems {
s.Add(elem)
}
return s
}
/*
Has returns True if the passed string is member of the set on which it was
called and False otherwise.
*/
func (s Set) Has(elem string) bool {
_, ok := s[elem]
return ok
}
/*
Add adds the passed string to the set on which it was called, if the string is
not a member of the set.
*/
func (s Set) Add(elem string) {
s[elem] = struct{}{}
}
/*
Remove removes the passed string from the set on which was is called, if the
string is a member of the set.
*/
func (s Set) Remove(elem string) {
delete(s, elem)
}
/*
Intersection creates and returns a new Set with the elements of the set on
which it was called that are also in the passed set.
*/
func (s Set) Intersection(s2 Set) Set {
res := NewSet()
for elem := range s {
if !s2.Has(elem) {
continue
}
res.Add(elem)
}
return res
}
/*
Difference creates and returns a new Set with the elements of the set on
which it was called that are not in the passed set.
*/
func (s Set) Difference(s2 Set) Set {
res := NewSet()
for elem := range s {
if s2.Has(elem) {
continue
}
res.Add(elem)
}
return res
}
/*
Filter creates and returns a new Set with the elements of the set on which it
was called that match the passed pattern. A matching error is treated like a
non-match plus a warning is printed.
*/
func (s Set) Filter(pattern string) Set {
res := NewSet()
for elem := range s {
matched, err := match(pattern, elem)
if err != nil {
fmt.Printf("WARNING: %s, pattern was '%s'\n", err, pattern)
continue
}
if !matched {
continue
}
res.Add(elem)
}
return res
}
/*
Slice creates and returns an unordered string slice with the elements of the
set on which it was called.
*/
func (s Set) Slice() []string {
var res []string
res = make([]string, 0, len(s))
for elem := range s {
res = append(res, elem)
}
return res
}
/*
artifactsDictKeyStrings returns string keys of passed HashObj map in an
unordered string slice.
*/
func artifactsDictKeyStrings(m map[string]HashObj) []string {
res := make([]string, len(m))
i := 0
for k := range m {
res[i] = k
i++
}
return res
}
/*
IsSubSet checks if the parameter subset is a
subset of the superset s.
*/
func (s Set) IsSubSet(subset Set) bool {
if len(subset) > len(s) {
return false
}
for key := range subset {
if s.Has(key) {
continue
} else {
return false
}
}
return true
}
func loadPayload(payloadBytes []byte) (any, error) {
var payload map[string]any
if err := json.Unmarshal(payloadBytes, &payload); err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
if payload["_type"] == "link" {
var link Link
if err := checkRequiredJSONFields(payload, reflect.TypeOf(link)); err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
decoder := json.NewDecoder(strings.NewReader(string(payloadBytes)))
decoder.DisallowUnknownFields()
if err := decoder.Decode(&link); err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
return link, nil
} else if payload["_type"] == "layout" {
var layout Layout
if err := checkRequiredJSONFields(payload, reflect.TypeOf(layout)); err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
decoder := json.NewDecoder(strings.NewReader(string(payloadBytes)))
decoder.DisallowUnknownFields()
if err := decoder.Decode(&layout); err != nil {
return nil, fmt.Errorf("error decoding payload: %w", err)
}
return layout, nil
}
return nil, ErrUnknownMetadataType
}