-
Notifications
You must be signed in to change notification settings - Fork 3
/
resources_of_type.go
46 lines (36 loc) · 1.27 KB
/
resources_of_type.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
package workspace
import (
"github.com/turbot/pipe-fittings/modconfig"
)
// GetWorkspaceResourcesOfType returns all resources of type T from a workspace
func GetWorkspaceResourcesOfType[T modconfig.HclResource](w *Workspace) map[string]T {
var res = map[string]T{}
resourceFunc := func(item modconfig.HclResource) (bool, error) {
if item, ok := item.(T); ok {
res[item.Name()] = item
}
return true, nil
}
// resource func does not return error
_ = w.GetResourceMaps().WalkResources(resourceFunc)
return res
}
// FilterWorkspaceResourcesOfType returns all resources of type T from a workspace which satisf y the filter,
// which is specified as a SQL syntax where clause
func FilterWorkspaceResourcesOfType[T modconfig.HclResource](w *Workspace, filter ResourceFilter) (map[string]T, error) {
var res = map[string]T{}
filterPredicate, err := filter.getPredicate()
if err != nil {
return nil, err
}
resourceFunc := func(item modconfig.HclResource) (bool, error) {
// if item is correct type and matches the predicate, add it to the result
if item, ok := item.(T); ok && filterPredicate(item) {
res[item.Name()] = item
}
return true, nil
}
// resource func does not return error
_ = w.GetResourceMaps().WalkResources(resourceFunc)
return res, nil
}