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