-
Notifications
You must be signed in to change notification settings - Fork 110
/
resource_utils.go
66 lines (58 loc) · 1.9 KB
/
resource_utils.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
package testutils
import (
"go.viam.com/rdk/resource"
)
// NewResourceNameSet returns a flattened set of name strings from
// a collection of resource.Name objects for the purposes of comparison
// in automated tests.
func NewResourceNameSet(resourceNames ...resource.Name) map[resource.Name]struct{} {
set := make(map[resource.Name]struct{}, len(resourceNames))
for _, val := range resourceNames {
set[val] = struct{}{}
}
return set
}
// ExtractNames takes a slice of resource.Name objects
// and returns a slice of name strings for the purposes of comparison
// in automated tests.
func ExtractNames(values ...resource.Name) []string {
var names []string
for _, n := range values {
names = append(names, n.ShortName())
}
return names
}
// ConcatResourceNames takes a slice of slices of resource.Name objects
// and returns a concatenated slice of resource.Name for the purposes of comparison
// in automated tests.
func ConcatResourceNames(values ...[]resource.Name) []resource.Name {
var rNames []resource.Name
for _, v := range values {
rNames = append(rNames, v...)
}
return rNames
}
// AddSuffixes takes a slice of resource.Name objects and for each suffix,
// adds the suffix to every object, then returns the entire list.
func AddSuffixes(values []resource.Name, suffixes ...string) []resource.Name {
var rNames []resource.Name
for _, s := range suffixes {
for _, v := range values {
newName := resource.NameFromSubtype(v.Subtype, v.Name+s)
rNames = append(rNames, newName)
}
}
return rNames
}
// AddRemotes takes a slice of resource.Name objects and for each remote,
// adds the remote to every object, then returns the entire list.
func AddRemotes(values []resource.Name, remotes ...string) []resource.Name {
var rNames []resource.Name
for _, s := range remotes {
for _, v := range values {
v = v.PrependRemote(resource.RemoteName(s))
rNames = append(rNames, v)
}
}
return rNames
}