1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-07-15 01:25:00 +02:00

Improved path lookup

This commit is contained in:
Tim Voronov
2019-09-07 01:47:58 -04:00
parent 9c0e81a2f4
commit 0a2fd478cd
3 changed files with 100 additions and 11 deletions

View File

@ -120,6 +120,66 @@ func TestMember(t *testing.T) {
So(string(out), ShouldEqual, `"wsx"`)
})
Convey("Deep path", func() {
c := compiler.New()
p, err := c.Compile(`
LET obj = {
first: {
second: {
third: {
fourth: {
fifth: {
bottom: true
}
}
}
}
}
}
RETURN obj.first.second.third.fourth.fifth.bottom
`)
So(err, ShouldBeNil)
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `true`)
})
Convey("Deep computed path", func() {
c := compiler.New()
p, err := c.Compile(`
LET obj = {
first: {
second: {
third: {
fourth: {
fifth: {
bottom: true
}
}
}
}
}
}
RETURN obj["first"]["second"]["third"]["fourth"]["fifth"].bottom
`)
So(err, ShouldBeNil)
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `true`)
})
Convey("Prop after a func call", func() {
c := compiler.New()
@ -160,9 +220,9 @@ func TestMember(t *testing.T) {
func BenchmarkMemberArray(b *testing.B) {
p := compiler.New().MustCompile(`
LET arr = [1]
LET arr = [[[[1]]]]
RETURN arr[0]
RETURN arr[0][0][0][0]
`)
for n := 0; n < b.N; n++ {
@ -172,9 +232,21 @@ func BenchmarkMemberArray(b *testing.B) {
func BenchmarkMemberObject(b *testing.B) {
p := compiler.New().MustCompile(`
LET obj = { "foo": "bar"}
LET obj = {
first: {
second: {
third: {
fourth: {
fifth: {
bottom: true
}
}
}
}
}
}
RETURN obj.foo
RETURN obj.first.second.third.fourth.fifth.bottom
`)
for n := 0; n < b.N; n++ {

View File

@ -80,4 +80,19 @@ func TestParam(t *testing.T) {
So(string(out), ShouldEqual, `"foobar"`)
})
Convey("Should be possible to use in member expression as a computed property", t, func() {
prog := compiler.New().
MustCompile(`
LET obj = { foo: "bar" }
RETURN obj[@param]
`)
out := prog.MustRun(
context.Background(),
runtime.WithParam("param", "foo"),
)
So(string(out), ShouldEqual, `"bar"`)
})
}

View File

@ -35,22 +35,24 @@ func (e *MemberExpression) Exec(ctx context.Context, scope *core.Scope) (core.Va
)
}
strPath := make([]core.Value, len(e.path))
out := val
path := make([]core.Value, 1, 1)
for idx, exp := range e.path {
for _, exp := range e.path {
segment, err := exp.Exec(ctx, scope)
if err != nil {
return values.None, err
}
strPath[idx] = segment
}
path[0] = segment
c, err := values.GetIn(ctx, out, path)
out, err := values.GetIn(ctx, val, strPath)
if err != nil {
return values.None, core.SourceError(e.src, err)
}
if err != nil {
return values.None, core.SourceError(e.src, err)
out = c
}
return out, nil