Skip to content

Commit 8ced811

Browse files
committed
Fix tests for new =~ operator
1 parent efa3caa commit 8ced811

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

internal/server/expr.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/tidwall/gjson"
1111
"github.com/tidwall/match"
1212
"github.com/tidwall/tile38/internal/field"
13+
"github.com/tidwall/tile38/internal/log"
1314
"github.com/tidwall/tile38/internal/object"
1415
"github.com/tidwall/tinylru"
1516
)
@@ -104,10 +105,6 @@ func newExprPool(s *Server) *exprPool {
104105
if info.Ident == "this" {
105106
return expr.Object(o), nil
106107
}
107-
// Register regex as a root function
108-
if info.Ident == "regex" {
109-
return expr.Function("regex"), nil
110-
}
111108
return objExpr(o, info)
112109
} else {
113110
switch v := info.Value.Value().(type) {
@@ -146,17 +143,16 @@ func newExprPool(s *Server) *exprPool {
146143
case expr.OpRegex:
147144
field := info.Left.String()
148145
pattern := info.Right.String()
149-
150146
re, ok := pool.regexCache.Get(pattern)
151147
if !ok {
152148
var err error
153149
re, err = regexp.Compile(pattern)
154150
if err != nil {
155-
return expr.Undefined, fmt.Errorf("invalid regex pattern: %v", err)
151+
return expr.Undefined,
152+
fmt.Errorf("invalid regex pattern: %v", err)
156153
}
157154
pool.regexCache.Set(pattern, re)
158155
}
159-
160156
return expr.Bool(re.MatchString(field)), nil
161157
}
162158
return expr.Undefined, nil
@@ -188,7 +184,10 @@ func (p *exprPool) Put(ctx *expr.Context) {
188184

189185
func (where whereT) matchExpr(s *Server, o *object.Object) bool {
190186
ctx := s.epool.Get(o)
191-
res, _ := expr.Eval(where.name, ctx)
187+
res, err := expr.Eval(where.name, ctx)
188+
if err != nil {
189+
log.Debugf("%v", err)
190+
}
192191
s.epool.Put(ctx)
193192
return res.Bool()
194193
}

tests/keys_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -561,19 +561,19 @@ func keys_FIELDS_test(mc *mockServer) error {
561561
Do("SCAN", "fleet", "WHERE", "hello.world", "<", `uom`, "IDS").JSON().Str(`{"ok":true,"ids":["truck1","truck2"],"count":2,"cursor":0}`),
562562
Do("SCAN", "fleet", "WHERE", "hello.world", "!=", `tom`, "IDS").JSON().Str(`{"ok":true,"ids":["truck1"],"count":1,"cursor":0}`),
563563
// Test REGEX on FIELD
564-
Do("SCAN", "fleet", "WHERE", "regex(hello.world, 'tom.*')", "IDS").JSON().Str(`{"ok":true,"ids":["truck2"],"count":1,"cursor":0}`),
565-
Do("SCAN", "fleet", "WHERE", "regex(hello.world, 'foo.*')", "IDS").JSON().Str(`{"ok":true,"ids":[],"count":0,"cursor":0}`),
566-
Do("SCAN", "fleet", "WHERE", "regex(hello.world, '(*')", "IDS").JSON().Str(`{"ok":true,"ids":[],"count":0,"cursor":0}`),
564+
Do("SCAN", "fleet", "WHERE", "hello.world =~ 'tom.*'", "IDS").JSON().Str(`{"ok":true,"ids":["truck2"],"count":1,"cursor":0}`),
565+
Do("SCAN", "fleet", "WHERE", "hello.world =~ 'foo.*'", "IDS").JSON().Str(`{"ok":true,"ids":[],"count":0,"cursor":0}`),
566+
Do("SCAN", "fleet", "WHERE", "hello.world =~ '(*'", "IDS").JSON().Str(`{"ok":true,"ids":[],"count":0,"cursor":0}`),
567567

568568
Do("SET", "fleet", "truck1", "OBJECT", `{"type":"Feature","geometry":{"type":"Point","coordinates":[-112,33]},"properties":{"speed":50},"asdf":"Adsf"}`).JSON().OK(),
569569
Do("SCAN", "fleet", "WHERE", "properties.speed", ">", 49, "IDS").JSON().Str(`{"ok":true,"ids":["truck1"],"count":1,"cursor":0}`),
570570
Do("SCAN", "fleet", "WHERE", "properties.speed", ">", 50, "IDS").JSON().Str(`{"ok":true,"ids":[],"count":0,"cursor":0}`),
571571
Do("SCAN", "fleet", "WHERE", "properties.speed", "<", 51, "IDS").JSON().Str(`{"ok":true,"ids":["truck1","truck2"],"count":2,"cursor":0}`),
572572
// Test REGEX on OBJECT properties
573573
Do("SET", "fleet", "truck3", "OBJECT", `{"type":"Feature","geometry":{"type":"Point","coordinates":[-112,33]},"properties":{"name":"truck01"}}`).JSON().OK(),
574-
Do("SCAN", "fleet", "WHERE", "regex(properties.name, 'truck.*')", "IDS").JSON().Str(`{"ok":true,"ids":["truck3"],"count":1,"cursor":0}`),
575-
Do("SCAN", "fleet", "WHERE", "regex(properties.name, 'foo.*')", "IDS").JSON().Str(`{"ok":true,"ids":[],"count":0,"cursor":0}`),
576-
Do("SCAN", "fleet", "WHERE", "regex(properties.name, '(*')", "IDS").JSON().Str(`{"ok":true,"ids":[],"count":0,"cursor":0}`),
574+
Do("SCAN", "fleet", "WHERE", "properties.name =~ 'truck.*'", "IDS").JSON().Str(`{"ok":true,"ids":["truck3"],"count":1,"cursor":0}`),
575+
Do("SCAN", "fleet", "WHERE", "properties.name =~ 'foo.*'", "IDS").JSON().Str(`{"ok":true,"ids":[],"count":0,"cursor":0}`),
576+
Do("SCAN", "fleet", "WHERE", "properties.name =~ '(*'", "IDS").JSON().Str(`{"ok":true,"ids":[],"count":0,"cursor":0}`),
577577

578578
Do("DROP", "fleet").JSON().OK(),
579579
Do("SET", "fleet", "truck1", "FIELD", "speed", "50", "POINT", "-112", "33").JSON().OK(),

0 commit comments

Comments
 (0)