🏷️ Improved Resources usage by filter methods
This commit is contained in:
parent
4dc271dfc3
commit
962317d0c3
|
@ -1,17 +1,63 @@
|
||||||
package domain
|
package domain
|
||||||
|
|
||||||
import "path"
|
import (
|
||||||
|
"path"
|
||||||
|
)
|
||||||
|
|
||||||
// TODO(toby3d): search by type or name/id.
|
|
||||||
type Resources []*Resource
|
type Resources []*Resource
|
||||||
|
|
||||||
func (f Resources) GetMatch(pattern string) *Resource {
|
func (r Resources) GetType(targetType string) Resources {
|
||||||
for i := range f {
|
out := make(Resources, 0, len(r))
|
||||||
if matched, err := path.Match(pattern, f[i].Path); err != nil || !matched {
|
|
||||||
|
target, err := ParseResourceType(targetType)
|
||||||
|
if err != nil {
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range r {
|
||||||
|
if r[i].resourceType != target {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
return f[i]
|
out = append(out, r[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Resources) Get(path string) *Resource {
|
||||||
|
for i := range r {
|
||||||
|
if r[i].key != path {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return r[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Resources) Match(pattern string) Resources {
|
||||||
|
out := make(Resources, 0, len(r))
|
||||||
|
|
||||||
|
for i := range r {
|
||||||
|
if matched, err := path.Match(pattern, r[i].key); err != nil || !matched {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
out = append(out, r[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Resources) GetMatch(pattern string) *Resource {
|
||||||
|
for i := range r {
|
||||||
|
if matched, err := path.Match(pattern, r[i].key); err != nil || !matched {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return r[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue