From a11e80e2c7d3eb75e32a4ca5c166d87e9f496153 Mon Sep 17 00:00:00 2001
From: Evgenii Stratonikov <e.stratonikov@yadro.com>
Date: Mon, 19 Aug 2024 09:32:39 +0300
Subject: [PATCH] [#86] engine: Simplify multiple chains processing

So, it's sunday evening and I am sitting on-call trying to debug strange
node behaviour. It took me 3 whole minutes to understand the code being
changed: it accumulates bools in slices, even though no slice is needed;
it uses subtle condition from the first loop to make decision in the
second one, and finally it uses named return values.

In this commit we remove the slice and the second loop, because why not.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
---
 pkg/engine/chain_router.go | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)

diff --git a/pkg/engine/chain_router.go b/pkg/engine/chain_router.go
index 830919f..fe6c470 100644
--- a/pkg/engine/chain_router.go
+++ b/pkg/engine/chain_router.go
@@ -42,41 +42,36 @@ func (dr *defaultChainRouter) checkLocal(name chain.Name, rt RequestTarget, r re
 	if dr.local == nil {
 		return
 	}
-	var ruleFounds []bool
+
+	var hasAllow bool
 	for _, target := range rt.Targets() {
 		status, ruleFound, err = dr.matchLocalOverrides(name, target, r)
 		if err != nil || ruleFound && status != chain.Allow {
 			return
 		}
-		ruleFounds = append(ruleFounds, ruleFound)
+		hasAllow = hasAllow || ruleFound
 	}
 
 	status = chain.NoRuleFound
-	for _, ruleFound = range ruleFounds {
-		if ruleFound {
-			status = chain.Allow
-			break
-		}
+	if hasAllow {
+		status = chain.Allow
 	}
 	return
 }
 
 func (dr *defaultChainRouter) checkMorph(name chain.Name, rt RequestTarget, r resource.Request) (status chain.Status, ruleFound bool, err error) {
-	var ruleFounds []bool
+	var hasAllow bool
 	for _, target := range rt.Targets() {
 		status, ruleFound, err = dr.matchMorphRuleChains(name, target, r)
 		if err != nil || ruleFound && status != chain.Allow {
 			return
 		}
-		ruleFounds = append(ruleFounds, ruleFound)
+		hasAllow = hasAllow || ruleFound
 	}
 
 	status = chain.NoRuleFound
-	for _, ruleFound = range ruleFounds {
-		if ruleFound {
-			status = chain.Allow
-			break
-		}
+	if hasAllow {
+		status = chain.Allow
 	}
 	return
 }