Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

policy: add a global boolean to enable tracing on policy matching #54

Draft
wants to merge 1 commit into
base: v1
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion pkg/policy/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -59,14 +62,19 @@ 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
} else {
return matchResultFalse, cur
}
}
if MatchTrace {
defer func() {
fmt.Printf("match %v --> %v\n", cur, matchResToStr(output))
}()
}

switch cur.Kind() {
case KindEqual:
Expand Down Expand Up @@ -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")
}
}
Loading