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)
This commit is contained in:
Fabio Bozzo
2024-11-04 10:56:06 +01:00
parent 9e9c632ded
commit 6717a3a89c

View File

@@ -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