From b05a136b81d484fd9299ae5824eb4dd86e72be4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Thu, 7 Nov 2024 10:43:01 +0100 Subject: [PATCH] policy: add a global boolean to enable tracing on policy matching --- pkg/policy/match.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/pkg/policy/match.go b/pkg/policy/match.go index c3862d5..b1b522e 100644 --- a/pkg/policy/match.go +++ b/pkg/policy/match.go @@ -9,6 +9,9 @@ import ( "github.com/ipld/go-ipld-prime/must" ) +// MatchTrace, if set, will print tracing statements to stdout of the policy matching resolution. +var MatchTrace = false + // Match determines if the IPLD node satisfies the policy. func (p Policy) Match(node datamodel.Node) bool { for _, stmt := range p { @@ -59,7 +62,7 @@ const ( // - matchResultNoData: if the selector didn't match the expected data. // For matchResultTrue and matchResultNoData, the leaf-most (innermost) statement failing to be true is returned, // as well as the corresponding root-most encompassing statement. -func matchStatement(cur Statement, node ipld.Node) (_ matchResult, leafMost Statement) { +func matchStatement(cur Statement, node ipld.Node) (output matchResult, leafMost Statement) { var boolToRes = func(v bool) (matchResult, Statement) { if v { return matchResultTrue, nil @@ -67,6 +70,11 @@ func matchStatement(cur Statement, node ipld.Node) (_ matchResult, leafMost Stat return matchResultFalse, cur } } + if MatchTrace { + defer func() { + fmt.Printf("match %v --> %v\n", cur, matchResToStr(output)) + }() + } switch cur.Kind() { case KindEqual: @@ -274,3 +282,18 @@ func gt(order int) bool { return order == 1 } func gte(order int) bool { return order == 0 || order == 1 } func lt(order int) bool { return order == -1 } func lte(order int) bool { return order == 0 || order == -1 } + +func matchResToStr(res matchResult) string { + switch res { + case matchResultTrue: + return "True" + case matchResultFalse: + return "False" + case matchResultNoData: + return "NoData" + case matchResultOptionalNoData: + return "OptionalNoData" + default: + panic("invalid matchResult") + } +}