Skip to content

Commit 24608fb

Browse files
committed
Add expr.ExperimentalPipes() config
1 parent cee681e commit 24608fb

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

conf/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Config struct {
2222
ConstFns map[string]reflect.Value
2323
Visitors []ast.Visitor
2424
Functions map[string]*builtin.Function
25+
Pipes bool
2526
}
2627

2728
// CreateNew creates new config with default values.

expr.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ func Function(name string, fn func(params ...interface{}) (interface{}, error),
130130
}
131131
}
132132

133+
// ExperimentalPipes enables pipes syntax.
134+
func ExperimentalPipes() Option {
135+
return func(c *conf.Config) {
136+
c.Pipes = true
137+
}
138+
}
139+
133140
// Compile parses and compiles given input expression to bytecode program.
134141
func Compile(input string, ops ...Option) (*vm.Program, error) {
135142
config := conf.CreateNew()
@@ -146,7 +153,11 @@ func Compile(input string, ops ...Option) (*vm.Program, error) {
146153
})
147154
}
148155

149-
tree, err := parser.Parse(input)
156+
parseConfig := parser.Config{
157+
Pipes: config.Pipes,
158+
}
159+
160+
tree, err := parser.ParseWithConfig(input, parseConfig)
150161
if err != nil {
151162
return nil, err
152163
}

parser/parser.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,23 @@ type parser struct {
7777
pos int
7878
err *file.Error
7979
depth int // closure call depth
80+
pipes bool
8081
}
8182

8283
type Tree struct {
8384
Node Node
8485
Source *file.Source
8586
}
8687

88+
type Config struct {
89+
Pipes bool
90+
}
91+
8792
func Parse(input string) (*Tree, error) {
93+
return ParseWithConfig(input, Config{})
94+
}
95+
96+
func ParseWithConfig(input string, config Config) (*Tree, error) {
8897
source := file.NewSource(input)
8998

9099
tokens, err := Lex(source)
@@ -95,6 +104,7 @@ func Parse(input string) (*Tree, error) {
95104
p := &parser{
96105
tokens: tokens,
97106
current: tokens[0],
107+
pipes: config.Pipes,
98108
}
99109

100110
node := p.parseExpression(0)
@@ -592,6 +602,11 @@ func (p *parser) parsePostfixExpression(node Node) Node {
592602
}
593603

594604
func (p *parser) parsePipe(node Node) Node {
605+
if !p.pipes {
606+
p.error("enable Pipes via expr.ExperimentalPipes()")
607+
return &NilNode{}
608+
}
609+
595610
identifier := p.current
596611
p.expect(Identifier)
597612

parser/parser_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ func TestParse(t *testing.T) {
453453
&BoolNode{Value: true}}}},
454454
}
455455
for _, test := range parseTests {
456-
actual, err := parser.Parse(test.input)
456+
actual, err := parser.ParseWithConfig(test.input, parser.Config{Pipes: true})
457457
if err != nil {
458458
t.Errorf("%s:\n%v", test.input, err)
459459
continue
@@ -657,7 +657,7 @@ func TestParse_pipe_operator(t *testing.T) {
657657
Property: &StringNode{Value: "foo"},
658658
}}}}}}}}
659659

660-
actual, err := parser.Parse(input)
660+
actual, err := parser.ParseWithConfig(input, parser.Config{Pipes: true})
661661
require.NoError(t, err)
662662
assert.Equal(t, Dump(expect), Dump(actual.Node))
663663
}

test/pipes/pipes_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestPipes(t *testing.T) {
3737

3838
for _, test := range tests {
3939
t.Run(test.input, func(t *testing.T) {
40-
program, err := expr.Compile(test.input, expr.Env(env))
40+
program, err := expr.Compile(test.input, expr.Env(env), expr.ExperimentalPipes())
4141
require.NoError(t, err)
4242

4343
out, err := expr.Run(program, env)
@@ -48,7 +48,7 @@ func TestPipes(t *testing.T) {
4848
}
4949

5050
func TestPipes_map_filter(t *testing.T) {
51-
program, err := expr.Compile(`1..9 | map(# + 1) | filter(# % 2 == 0)`)
51+
program, err := expr.Compile(`1..9 | map(# + 1) | filter(# % 2 == 0)`, expr.ExperimentalPipes())
5252
require.NoError(t, err)
5353

5454
out, err := expr.Run(program, nil)

test/time/time_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ func TestTime_date(t *testing.T) {
120120
want time.Time
121121
}{
122122
{
123-
`'2017-10-23' | date()`,
123+
`date('2017-10-23')`,
124124
time.Date(2017, 10, 23, 0, 0, 0, 0, time.UTC),
125125
},
126126
{
127-
`'24.11.1987 20:30' | date("02.01.2006 15:04", "Europe/Zurich")`,
127+
`date('24.11.1987 20:30', "02.01.2006 15:04", "Europe/Zurich")`,
128128
time.Date(1987, 11, 24, 20, 30, 0, 0, time.FixedZone("Europe/Zurich", 3600)),
129129
},
130130
{
131-
`'24.11.1987 20:30 MSK' | date("02.01.2006 15:04 MST", "Europe/Zurich")`,
131+
`date('24.11.1987 20:30 MSK', "02.01.2006 15:04 MST", "Europe/Zurich")`,
132132
time.Date(1987, 11, 24, 20, 30, 0, 0, time.FixedZone("MSK", 0)),
133133
},
134134
}

0 commit comments

Comments
 (0)