From 8b8311bcca18bddb7cc9631f377a0188a7ba8326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Fri, 16 Jun 2023 09:29:49 +0200 Subject: [PATCH] fix: recursive lazy loading (#7552) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/engine/context/evaluate.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/pkg/engine/context/evaluate.go b/pkg/engine/context/evaluate.go index c0106371bf..e470fb0bde 100644 --- a/pkg/engine/context/evaluate.go +++ b/pkg/engine/context/evaluate.go @@ -40,29 +40,36 @@ func (ctx *context) Query(query string) (interface{}, error) { func (ctx *context) loadDeferred(query string) error { loaders := ctx.getMatchingLoaders(query) - for _, l := range loaders { - if err := l(); err != nil { + for _, loader := range loaders { + err := ctx.evaluateLoader(loader) + if err != nil { return err } } - return nil } -func (ctx *context) getMatchingLoaders(query string) []DeferredLoader { +func (ctx *context) getMatchingLoaders(query string) []string { ctx.deferred.mutex.Lock() defer ctx.deferred.mutex.Unlock() - var matchingLoaders []DeferredLoader - for name, deferredLoader := range ctx.deferred.loaders { + var matchingLoaders []string + for name := range ctx.deferred.loaders { if strings.Contains(query, name) { - matchingLoaders = append(matchingLoaders, deferredLoader) - delete(ctx.deferred.loaders, name) + matchingLoaders = append(matchingLoaders, name) } } - return matchingLoaders } +func (ctx *context) evaluateLoader(name string) error { + loader, ok := ctx.deferred.loaders[name] + if !ok { + return nil + } + delete(ctx.deferred.loaders, name) + return loader() +} + func (ctx *context) HasChanged(jmespath string) (bool, error) { objData, err := ctx.Query("request.object." + jmespath) if err != nil {