policy: fix "all" and "any" to apply a single statement on a collection
Matching the spec
This commit is contained in:
@@ -104,9 +104,9 @@ func statementFromIPLD(path string, node datamodel.Node) (Statement, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
statementsNodes, _ := node.LookupByIndex(2)
|
statementsNode, _ := node.LookupByIndex(2)
|
||||||
statements, err := statementsFromIPLD(path+"1/", statementsNodes)
|
statement, err := statementFromIPLD(path+"1/", statementsNode)
|
||||||
return quantifier{kind: op, selector: sel, statements: statements}, nil
|
return quantifier{kind: op, selector: sel, statement: statement}, nil
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return nil, ErrUnrecognizedOperator(path, op)
|
return nil, ErrUnrecognizedOperator(path, op)
|
||||||
@@ -243,7 +243,7 @@ func statementToIPLD(statement Statement) (datamodel.Node, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
args, err := statementsToIPLD(statement.statements)
|
args, err := statementToIPLD(statement.statement)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ func matchStatement(statement Statement, node ipld.Node) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, n := range many {
|
for _, n := range many {
|
||||||
ok := Match(s.statements, n)
|
ok := matchStatement(s.statement, n)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -124,7 +124,7 @@ func matchStatement(statement Statement, node ipld.Node) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, n := range many {
|
for _, n := range many {
|
||||||
ok := Match(s.statements, n)
|
ok := matchStatement(s.statement, n)
|
||||||
if ok {
|
if ok {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -400,8 +400,7 @@ func TestMatch(t *testing.T) {
|
|||||||
pol := Policy{
|
pol := Policy{
|
||||||
Any(
|
Any(
|
||||||
selector.MustParse(".[]"),
|
selector.MustParse(".[]"),
|
||||||
GreaterThan(selector.MustParse(".value"), literal.Int(10)),
|
GreaterThan(selector.MustParse(".value"), literal.Int(60)),
|
||||||
LessThan(selector.MustParse(".value"), literal.Int(50)),
|
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
ok := Match(pol, nd)
|
ok := Match(pol, nd)
|
||||||
|
|||||||
@@ -40,23 +40,23 @@ func (e equality) Kind() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Equal(selector selector.Selector, value ipld.Node) Statement {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
type negation struct {
|
||||||
@@ -68,7 +68,7 @@ func (n negation) Kind() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Not(stmt Statement) Statement {
|
func Not(stmt Statement) Statement {
|
||||||
return negation{stmt}
|
return negation{statement: stmt}
|
||||||
}
|
}
|
||||||
|
|
||||||
type connective struct {
|
type connective struct {
|
||||||
@@ -81,11 +81,11 @@ func (c connective) Kind() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func And(stmts ...Statement) Statement {
|
func And(stmts ...Statement) Statement {
|
||||||
return connective{KindAnd, stmts}
|
return connective{kind: KindAnd, statements: stmts}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Or(stmts ...Statement) Statement {
|
func Or(stmts ...Statement) Statement {
|
||||||
return connective{KindOr, stmts}
|
return connective{kind: KindOr, statements: stmts}
|
||||||
}
|
}
|
||||||
|
|
||||||
type wildcard struct {
|
type wildcard struct {
|
||||||
@@ -103,23 +103,23 @@ func Like(selector selector.Selector, pattern string) (Statement, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return wildcard{selector, pattern, g}, nil
|
return wildcard{selector: selector, pattern: pattern, glob: g}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type quantifier struct {
|
type quantifier struct {
|
||||||
kind string
|
kind string
|
||||||
selector selector.Selector
|
selector selector.Selector
|
||||||
statements []Statement
|
statement Statement
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n quantifier) Kind() string {
|
func (n quantifier) Kind() string {
|
||||||
return n.kind
|
return n.kind
|
||||||
}
|
}
|
||||||
|
|
||||||
func All(selector selector.Selector, policy ...Statement) Statement {
|
func All(selector selector.Selector, statement Statement) Statement {
|
||||||
return quantifier{KindAll, selector, policy}
|
return quantifier{kind: KindAll, selector: selector, statement: statement}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Any(selector selector.Selector, policy ...Statement) Statement {
|
func Any(selector selector.Selector, statement Statement) Statement {
|
||||||
return quantifier{KindAny, selector, policy}
|
return quantifier{kind: KindAny, selector: selector, statement: statement}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user