diff --git a/engine/world.go b/engine/world.go index 6963d4cf..af02f8d6 100644 --- a/engine/world.go +++ b/engine/world.go @@ -181,6 +181,19 @@ func (obj *ResFilter) Match(kind, name, host string) error { return nil // match! } +// MatchFilters is a simple helper function to avoid duplicating this loop here. +// If any filter matches, it returns nil. Otherwise we error. +func MatchFilters(filters []*ResFilter, kind, name, host string) error { + // TODO: I'd love to avoid this O(N^2) matching if possible... + for _, filter := range filters { + if err := filter.Match(kind, name, host); err == nil { + return nil + } + } + + return fmt.Errorf("not matches found") // did not match +} + // ResOutput represents a record of exported resource data which we have read // out from the world storage system. The Data field contains an encoded version // of the resource, and even though decoding it will get you a Kind and Name, we diff --git a/etcd/client/resources/resources.go b/etcd/client/resources/resources.go index 64dd8e59..5873c87b 100644 --- a/etcd/client/resources/resources.go +++ b/etcd/client/resources/resources.go @@ -146,10 +146,8 @@ func GetResources(ctx context.Context, client interfaces.Client, hostname string } // TODO: I'd love to avoid this O(N^2) matching if possible... - for _, filter := range filters { - if err := filter.Match(kind, name, hostnameFrom); err != nil { - continue // did not match - } + if err := engine.MatchFilters(filters, kind, name, hostnameFrom); err != nil { + continue // did not match any of these } ro := &engine.ResOutput{ diff --git a/lang/core/collect.go b/lang/core/collect.go index 8533aff7..692661da 100644 --- a/lang/core/collect.go +++ b/lang/core/collect.go @@ -456,12 +456,21 @@ func (obj *CollectFunc) Call(ctx context.Context, args []types.Value) (types.Val if obj.init == nil { return nil, funcs.ErrCantSpeculate } + + list := types.NewList(obj.Info().Sig.Out) // collectFuncOutType + + if len(filters) == 0 { + // If we have no filters, it means we're matching on nothing, + // which happens if we've pre-filtered away all the resources + // that we'd want to collect, so here we return absolutely zero! + return list, nil + } + resOutput, err := obj.init.World.ResCollect(ctx, filters) if err != nil { return nil, err } - list := types.NewList(obj.Info().Sig.Out) // collectFuncOutType for _, x := range resOutput { // programming error if any of these error... if x.Kind != kind {