mirror of
https://github.com/MontFerret/ferret.git
synced 2025-06-27 00:41:09 +02:00
Added possibility to use string literals as object poperties
This commit is contained in:
@ -2,6 +2,7 @@ package compiler_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"github.com/MontFerret/ferret/pkg/compiler"
|
"github.com/MontFerret/ferret/pkg/compiler"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
"testing"
|
"testing"
|
||||||
@ -64,6 +65,42 @@ func TestMember(t *testing.T) {
|
|||||||
So(string(out), ShouldEqual, `"wsx"`)
|
So(string(out), ShouldEqual, `"wsx"`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("Object by literal with property defined as a string", func() {
|
||||||
|
c := compiler.New()
|
||||||
|
|
||||||
|
prog, err := c.Compile(`
|
||||||
|
LET obj = { "foo": "bar", "qaz": "wsx"}
|
||||||
|
|
||||||
|
RETURN obj["qaz"]
|
||||||
|
`)
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
out, err := prog.Run(context.Background())
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
So(string(out), ShouldEqual, `"wsx"`)
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Object by literal with property defined as a multi line string", func() {
|
||||||
|
c := compiler.New()
|
||||||
|
|
||||||
|
prog, err := c.Compile(fmt.Sprintf(`
|
||||||
|
LET obj = { "foo": "bar", %s: "wsx"}
|
||||||
|
|
||||||
|
RETURN obj["qaz"]
|
||||||
|
`, "`qaz`"))
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
out, err := prog.Run(context.Background())
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
So(string(out), ShouldEqual, `"wsx"`)
|
||||||
|
})
|
||||||
|
|
||||||
Convey("Object by variable", func() {
|
Convey("Object by variable", func() {
|
||||||
c := compiler.New()
|
c := compiler.New()
|
||||||
|
|
||||||
|
@ -560,7 +560,26 @@ func (v *visitor) doVisitObjectLiteral(ctx *fql.ObjectLiteralContext, scope *sco
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (v *visitor) doVisitPropertyNameContext(ctx *fql.PropertyNameContext, _ *scope) (core.Expression, error) {
|
func (v *visitor) doVisitPropertyNameContext(ctx *fql.PropertyNameContext, _ *scope) (core.Expression, error) {
|
||||||
return literals.NewStringLiteral(ctx.Identifier().GetText()), nil
|
var name string
|
||||||
|
|
||||||
|
identifier := ctx.Identifier()
|
||||||
|
|
||||||
|
if identifier != nil {
|
||||||
|
name = identifier.GetText()
|
||||||
|
} else {
|
||||||
|
stringLiteral := ctx.StringLiteral()
|
||||||
|
|
||||||
|
if stringLiteral != nil {
|
||||||
|
runes := []rune(stringLiteral.GetText())
|
||||||
|
name = string(runes[1 : len(runes)-1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if name == "" {
|
||||||
|
return nil, core.Error(core.ErrNotFound, "property name")
|
||||||
|
}
|
||||||
|
|
||||||
|
return literals.NewStringLiteral(name), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *visitor) doVisitComputedPropertyNameContext(ctx *fql.ComputedPropertyNameContext, scope *scope) (core.Expression, error) {
|
func (v *visitor) doVisitComputedPropertyNameContext(ctx *fql.ComputedPropertyNameContext, scope *scope) (core.Expression, error) {
|
||||||
|
@ -195,6 +195,7 @@ computedPropertyName
|
|||||||
|
|
||||||
propertyName
|
propertyName
|
||||||
: Identifier
|
: Identifier
|
||||||
|
| stringLiteral
|
||||||
;
|
;
|
||||||
|
|
||||||
expressionSequence
|
expressionSequence
|
||||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user