Skip to content

Commit 3085316

Browse files
committed
Expose the 'this' property to where clauses
This commit allows for accessing the current object using the 'this' property, which allows for queries such as: SCAN myfleet WHERE 'this["top-speed"] > 75' Prior to this change it would not be possible to access fields that had identifiers with non-javascript conforming characters.
1 parent 3fe57a7 commit 3085316

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

internal/server/expr.go

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,40 +60,49 @@ func resultToValue(r gjson.Result) expr.Value {
6060
}
6161
}
6262

63+
func objExpr(o *object.Object, info expr.RefInfo) (expr.Value, error) {
64+
if r := gjson.Get(o.Geo().Members(), info.Ident); r.Exists() {
65+
return resultToValue(r), nil
66+
}
67+
switch info.Ident {
68+
case "id":
69+
return expr.String(o.ID()), nil
70+
case "type":
71+
return typeForObject(o), nil
72+
default:
73+
var rf field.Field
74+
var ok bool
75+
o.Fields().Scan(func(f field.Field) bool {
76+
if f.Name() == info.Ident {
77+
rf = f
78+
ok = true
79+
return false
80+
}
81+
return true
82+
})
83+
if ok {
84+
r := gjson.Parse(rf.Value().JSON())
85+
return resultToValue(r), nil
86+
}
87+
}
88+
return expr.Number(0), nil
89+
}
90+
6391
func newExprPool(s *Server) *exprPool {
6492
ext := expr.NewExtender(
6593
// ref
6694
func(info expr.RefInfo, ctx *expr.Context) (expr.Value, error) {
6795
o := ctx.UserData.(*object.Object)
6896
if !info.Chain {
6997
// root
70-
if r := gjson.Get(o.Geo().Members(), info.Ident); r.Exists() {
71-
return resultToValue(r), nil
72-
}
73-
switch info.Ident {
74-
case "id":
75-
return expr.String(o.ID()), nil
76-
case "type":
77-
return typeForObject(o), nil
78-
default:
79-
var rf field.Field
80-
var ok bool
81-
o.Fields().Scan(func(f field.Field) bool {
82-
if f.Name() == info.Ident {
83-
rf = f
84-
ok = true
85-
return false
86-
}
87-
return true
88-
})
89-
if ok {
90-
r := gjson.Parse(rf.Value().JSON())
91-
return resultToValue(r), nil
92-
}
98+
if info.Ident == "this" {
99+
return expr.Object(o), nil
93100
}
94-
return expr.Number(0), nil
101+
return objExpr(o, info)
95102
} else {
96103
switch v := info.Value.Value().(type) {
104+
case *object.Object:
105+
return objExpr(o, info)
97106
case gjson.Result:
98107
return resultToValue(v.Get(info.Ident)), nil
99108
default:

0 commit comments

Comments
 (0)