interval: the IN operator can now check if an integer value is inlcuded in a range
This commit is contained in:
@@ -26,7 +26,7 @@ const (
|
||||
ParamIterator = "iterator"
|
||||
)
|
||||
|
||||
// to be moved in its own source file
|
||||
// to be moved into its own source file
|
||||
const (
|
||||
ConstLastIndex = 0xFFFF_FFFF
|
||||
)
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"git.portale-stac.it/go-pkg/expr/sym"
|
||||
"git.portale-stac.it/go-pkg/expr/types/array"
|
||||
"git.portale-stac.it/go-pkg/expr/types/dict"
|
||||
"git.portale-stac.it/go-pkg/expr/types/interval"
|
||||
"git.portale-stac.it/go-pkg/expr/types/list"
|
||||
)
|
||||
|
||||
@@ -46,6 +47,9 @@ func evalIn(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
} else if list.IsLinkedList(rightValue) {
|
||||
ls, _ := rightValue.(*list.LinkedList)
|
||||
v = ls.HasValue(leftValue)
|
||||
} else if interval.IsInterval(rightValue) {
|
||||
r, _ := rightValue.(*interval.IntervalType)
|
||||
v = r.Contains(leftValue)
|
||||
} else {
|
||||
err = opTerm.ErrIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
|
||||
+2
-1
@@ -45,10 +45,11 @@ func TestCollections(t *testing.T) {
|
||||
/* 29 */ {`"abcdef"['x'..]`, nil, `[1:14] interval expression expected integer, got string (x)`},
|
||||
/* 30 */ {`"abcdef"[1+..]`, nil, `[1:12] infix operator "+" requires two non-nil operands, got 1`},
|
||||
/* 31 */ {`"abcdef"[1..4+]`, nil, `[1:15] infix operator "+" requires two non-nil operands, got 1`},
|
||||
/* 32 */ {`r=-1..0..2; "abcdef"[r]`, "fdb", nil},
|
||||
}
|
||||
|
||||
t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// RunTestSuiteSpec(t, section, inputs, 30)
|
||||
// RunTestSuiteSpec(t, section, inputs, 32)
|
||||
RunTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
"git.portale-stac.it/go-pkg/expr/types"
|
||||
)
|
||||
|
||||
const TypeName = "interval"
|
||||
@@ -60,6 +61,17 @@ func (p *IntervalType) ToIntTriple() (b, e, s int) {
|
||||
return
|
||||
}
|
||||
|
||||
func (p *IntervalType) Contains(value any) (ok bool) {
|
||||
if v, err := types.ToGoInt64(value, TypeName); err == nil {
|
||||
if p.begin <= p.end {
|
||||
ok = (v >= p.begin && v < p.end && (v-p.begin)%p.step == 0)
|
||||
} else {
|
||||
ok = (v <= p.begin && v > p.end && (v-p.begin)%p.step == 0)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// func (b *bool) EqualTo(other kern.Equaler) (equal bool) {
|
||||
// if otherBool, ok := other.(*bool); ok {
|
||||
// equal = b == otherBool
|
||||
|
||||
Reference in New Issue
Block a user