Add match method to expressions

SCAN fleet WHERE "properties.speed > 45 && id.match('truck*')"
This commit is contained in:
tidwall 2022-10-21 04:20:46 -07:00
parent bdc80a7f70
commit 54590bb452
1 changed files with 22 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/tidwall/expr" "github.com/tidwall/expr"
"github.com/tidwall/geojson" "github.com/tidwall/geojson"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/tidwall/match"
"github.com/tidwall/tile38/internal/field" "github.com/tidwall/tile38/internal/field"
"github.com/tidwall/tile38/internal/object" "github.com/tidwall/tile38/internal/object"
) )
@ -53,7 +54,7 @@ func resultToValue(r gjson.Result) expr.Value {
case gjson.Number: case gjson.Number:
return expr.Number(r.Float()) return expr.Number(r.Float())
case gjson.JSON: case gjson.JSON:
return expr.String(r.String()) return expr.Object(r)
default: default:
return expr.Null return expr.Null
} }
@ -91,17 +92,32 @@ func newExprPool(s *Server) *exprPool {
} }
} }
} else { } else {
switch info.Value.Value().(type) { switch v := info.Value.Value().(type) {
case string: case gjson.Result:
r := gjson.Get(info.Value.String(), info.Ident) return resultToValue(v.Get(info.Ident)), nil
return resultToValue(r), nil default:
// object methods
switch info.Ident {
case "match":
return expr.Function("match"), nil
}
} }
} }
return expr.Undefined, nil return expr.Undefined, nil
}, },
// call // call
func(info expr.CallInfo, ctx *expr.Context) (expr.Value, error) { func(info expr.CallInfo, ctx *expr.Context) (expr.Value, error) {
// No custom calls if info.Chain {
switch info.Ident {
case "match":
args, err := info.Args.Compute()
if err != nil {
return expr.Undefined, err
}
t := match.Match(info.Value.String(), args.Get(0).String())
return expr.Bool(t), nil
}
}
return expr.Undefined, nil return expr.Undefined, nil
}, },
// op // op