From 6717a3a89c68541fcd616776d01a66f72d66a511 Mon Sep 17 00:00:00 2001 From: Fabio Bozzo Date: Mon, 4 Nov 2024 10:56:06 +0100 Subject: [PATCH] refactor: simplify optional selector handling Let Select() handle optional selectors by checking its nil return value, rather than explicitly checking IsOptional() Applied this pattern consistently across all statement kinds (Equal, Like, All, Any, etc) --- pkg/policy/match.go | 55 ++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/pkg/policy/match.go b/pkg/policy/match.go index bdf66e4..480cedb 100644 --- a/pkg/policy/match.go +++ b/pkg/policy/match.go @@ -71,56 +71,56 @@ func matchStatement(cur Statement, node ipld.Node) (_ matchResult, leafMost Stat case KindEqual: if s, ok := cur.(equality); ok { res, err := s.selector.Select(node) - if err != nil || res == nil { - if s.selector.IsOptional() { - return matchResultTrue, nil - } + if err != nil { return matchResultNoData, cur } + if res == nil { // Optional selector that didn't match + return matchResultTrue, nil + } return boolToRes(datamodel.DeepEqual(s.value, res)) } case KindGreaterThan: if s, ok := cur.(equality); ok { res, err := s.selector.Select(node) - if err != nil || res == nil { - if s.selector.IsOptional() { - return matchResultTrue, nil - } + if err != nil { return matchResultNoData, cur } + if res == nil { + return matchResultTrue, nil + } return boolToRes(isOrdered(s.value, res, gt)) } case KindGreaterThanOrEqual: if s, ok := cur.(equality); ok { res, err := s.selector.Select(node) - if err != nil || res == nil { - if s.selector.IsOptional() { - return matchResultTrue, nil - } + if err != nil { return matchResultNoData, cur } + if res == nil { + return matchResultTrue, nil + } return boolToRes(isOrdered(s.value, res, gte)) } case KindLessThan: if s, ok := cur.(equality); ok { res, err := s.selector.Select(node) - if err != nil || res == nil { - if s.selector.IsOptional() { - return matchResultTrue, nil - } + if err != nil { return matchResultNoData, cur } + if res == nil { + return matchResultTrue, nil + } return boolToRes(isOrdered(s.value, res, lt)) } case KindLessThanOrEqual: if s, ok := cur.(equality); ok { res, err := s.selector.Select(node) - if err != nil || res == nil { - if s.selector.IsOptional() { - return matchResultTrue, nil - } + if err != nil { return matchResultNoData, cur } + if res == nil { + return matchResultTrue, nil + } return boolToRes(isOrdered(s.value, res, lte)) } case KindNot: @@ -171,9 +171,12 @@ func matchStatement(cur Statement, node ipld.Node) (_ matchResult, leafMost Stat case KindLike: if s, ok := cur.(wildcard); ok { res, err := s.selector.Select(node) - if err != nil || res == nil { + if err != nil { return matchResultNoData, cur } + if res == nil { + return matchResultTrue, nil + } v, err := res.AsString() if err != nil { return matchResultFalse, cur // not a string @@ -183,9 +186,12 @@ func matchStatement(cur Statement, node ipld.Node) (_ matchResult, leafMost Stat case KindAll: if s, ok := cur.(quantifier); ok { res, err := s.selector.Select(node) - if err != nil || res == nil { + if err != nil { return matchResultNoData, cur } + if res == nil { + return matchResultTrue, nil + } it := res.ListIterator() if it == nil { return matchResultFalse, cur // not a list @@ -210,9 +216,12 @@ func matchStatement(cur Statement, node ipld.Node) (_ matchResult, leafMost Stat case KindAny: if s, ok := cur.(quantifier); ok { res, err := s.selector.Select(node) - if err != nil || res == nil { + if err != nil { return matchResultNoData, cur } + if res == nil { + return matchResultTrue, nil + } it := res.ListIterator() if it == nil { return matchResultFalse, cur // not a list