🏷️ Improved Resources usage by filter methods

This commit is contained in:
Maxim Lebedev 2023-11-10 09:08:36 +06:00
parent 4dc271dfc3
commit 962317d0c3
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 52 additions and 6 deletions

View File

@ -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