policy: fix "all" and "any" to apply a single statement on a collection

Matching the spec
This commit is contained in:
Michael Muré
2024-09-04 15:58:08 +02:00
parent d51bbc303c
commit d042b5cdfd
4 changed files with 23 additions and 24 deletions

View File

@@ -104,9 +104,9 @@ func statementFromIPLD(path string, node datamodel.Node) (Statement, error) {
if err != nil {
return nil, err
}
statementsNodes, _ := node.LookupByIndex(2)
statements, err := statementsFromIPLD(path+"1/", statementsNodes)
return quantifier{kind: op, selector: sel, statements: statements}, nil
statementsNode, _ := node.LookupByIndex(2)
statement, err := statementFromIPLD(path+"1/", statementsNode)
return quantifier{kind: op, selector: sel, statement: statement}, nil
default:
return nil, ErrUnrecognizedOperator(path, op)
@@ -243,7 +243,7 @@ func statementToIPLD(statement Statement) (datamodel.Node, error) {
if err != nil {
return nil, err
}
args, err := statementsToIPLD(statement.statements)
args, err := statementToIPLD(statement.statement)
if err != nil {
return nil, err
}

View File

@@ -110,7 +110,7 @@ func matchStatement(statement Statement, node ipld.Node) bool {
return false
}
for _, n := range many {
ok := Match(s.statements, n)
ok := matchStatement(s.statement, n)
if !ok {
return false
}
@@ -124,7 +124,7 @@ func matchStatement(statement Statement, node ipld.Node) bool {
return false
}
for _, n := range many {
ok := Match(s.statements, n)
ok := matchStatement(s.statement, n)
if ok {
return true
}

View File

@@ -400,8 +400,7 @@ func TestMatch(t *testing.T) {
pol := Policy{
Any(
selector.MustParse(".[]"),
GreaterThan(selector.MustParse(".value"), literal.Int(10)),
LessThan(selector.MustParse(".value"), literal.Int(50)),
GreaterThan(selector.MustParse(".value"), literal.Int(60)),
),
}
ok := Match(pol, nd)

View File

@@ -40,23 +40,23 @@ func (e equality) Kind() string {
}
func Equal(selector selector.Selector, value ipld.Node) Statement {
return equality{KindEqual, selector, value}
return equality{kind: KindEqual, selector: selector, value: value}
}
func GreaterThan(selector selector.Selector, value ipld.Node) Statement {
return equality{KindGreaterThan, selector, value}
return equality{kind: KindGreaterThan, selector: selector, value: value}
}
func GreaterThanOrEqual(selector selector.Selector, value ipld.Node) Statement {
return equality{KindGreaterThanOrEqual, selector, value}
return equality{kind: KindGreaterThanOrEqual, selector: selector, value: value}
}
func LessThan(selector selector.Selector, value ipld.Node) Statement {
return equality{KindLessThan, selector, value}
return equality{kind: KindLessThan, selector: selector, value: value}
}
func LessThanOrEqual(selector selector.Selector, value ipld.Node) Statement {
return equality{KindLessThanOrEqual, selector, value}
return equality{kind: KindLessThanOrEqual, selector: selector, value: value}
}
type negation struct {
@@ -68,7 +68,7 @@ func (n negation) Kind() string {
}
func Not(stmt Statement) Statement {
return negation{stmt}
return negation{statement: stmt}
}
type connective struct {
@@ -81,11 +81,11 @@ func (c connective) Kind() string {
}
func And(stmts ...Statement) Statement {
return connective{KindAnd, stmts}
return connective{kind: KindAnd, statements: stmts}
}
func Or(stmts ...Statement) Statement {
return connective{KindOr, stmts}
return connective{kind: KindOr, statements: stmts}
}
type wildcard struct {
@@ -103,23 +103,23 @@ func Like(selector selector.Selector, pattern string) (Statement, error) {
if err != nil {
return nil, err
}
return wildcard{selector, pattern, g}, nil
return wildcard{selector: selector, pattern: pattern, glob: g}, nil
}
type quantifier struct {
kind string
selector selector.Selector
statements []Statement
kind string
selector selector.Selector
statement Statement
}
func (n quantifier) Kind() string {
return n.kind
}
func All(selector selector.Selector, policy ...Statement) Statement {
return quantifier{KindAll, selector, policy}
func All(selector selector.Selector, statement Statement) Statement {
return quantifier{kind: KindAll, selector: selector, statement: statement}
}
func Any(selector selector.Selector, policy ...Statement) Statement {
return quantifier{KindAny, selector, policy}
func Any(selector selector.Selector, statement Statement) Statement {
return quantifier{kind: KindAny, selector: selector, statement: statement}
}