Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/arbitrary process flow marking #327

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/simulator/simulator.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ sub vcl_recv {
// @debugger
set req.backend = example_com;
set req.http.Foo = {" foo bar baz "};
// @process inject dict
set req.http.Item = table.lookup(injectable_dict, "virtual");
call custom_logger;
return (pass);
Expand Down
18 changes: 18 additions & 0 deletions interpreter/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package interpreter

import (
"strings"

"github.com/ysugimoto/falco/ast"
)

func findProcessMark(comments ast.Comments) (string, bool) {
for i := range comments {
l := strings.TrimLeft(comments[i].Value, " */#")
if strings.HasPrefix(l, "@process") {
return strings.TrimSpace(strings.TrimPrefix(l, "@process")), true
}
}

return "", false
}
16 changes: 8 additions & 8 deletions interpreter/process/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ package process
import (
"context"

"github.com/ysugimoto/falco/ast"
icontext "github.com/ysugimoto/falco/interpreter/context"
)

type Flow struct {
File string `json:"file"`
Line int `json:"line"`
Position int `json:"position"`
Subroutine string `json:"subroutine"`
Subroutine string `json:"subroutine,omitempty"`
Name string `json:"name,omitempty"`
Scope string `json:"scope"`
Request *HttpFlow `json:"req,omitempty"`
BackendRequest *HttpFlow `json:"bereq,omitempty"`
BackendResponse *HttpFlow `json:"beresp,omitempty"`
Response *HttpFlow `json:"resp,omitempty"`
Object *HttpFlow `json:"object,omitempty"`
}

func NewFlow(ctx *icontext.Context, sub *ast.SubroutineDeclaration) *Flow {
func NewFlow(ctx *icontext.Context, opts ...Option) *Flow {
c := context.Background()

token := sub.GetMeta().Token
f := &Flow{
File: token.File,
Line: token.Line,
Position: token.Position,
Subroutine: sub.Name.Value,
Scope: ctx.Scope.String(),
}
for i := range opts {
opts[i](f)
}
if ctx.Request != nil {
f.Request = newFlowRequest(ctx.Request.Clone(c))
Expand Down
32 changes: 32 additions & 0 deletions interpreter/process/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package process

import (
"github.com/ysugimoto/falco/ast"
"github.com/ysugimoto/falco/token"
)

type Option func(f *Flow)

func WithSubroutine(sub *ast.SubroutineDeclaration) Option {
return func(f *Flow) {
tok := sub.GetMeta().Token
f.Subroutine = sub.Name.Value
f.File = tok.File
f.Line = tok.Line
f.Position = tok.Position
}
}

func WithName(name string) Option {
return func(f *Flow) {
f.Name = name
}
}

func WithToken(tok token.Token) Option {
return func(f *Flow) {
f.File = tok.File
f.Line = tok.Line
f.Position = tok.Position
}
}
12 changes: 12 additions & 0 deletions interpreter/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"strings"

"github.com/k0kubun/pp"
"github.com/pkg/errors"
"github.com/ysugimoto/falco/ast"
"github.com/ysugimoto/falco/interpreter/assign"
Expand Down Expand Up @@ -35,6 +36,17 @@ func (i *Interpreter) ProcessBlockStatement(
debugState = i.Debugger.Run(stmt)
}

// Find process marker and add flow if found
if stmt.GetMeta() == nil {
pp.Println(stmt)
}
if name, found := findProcessMark(stmt.GetMeta().Leading); found {
i.process.Flows = append(
i.process.Flows,
process.NewFlow(i.ctx, process.WithName(name), process.WithToken(stmt.GetMeta().Token)),
)
}

switch t := stmt.(type) {
// Common logic statements (nothing to change state)
case *ast.DeclareStatement:
Expand Down
28 changes: 25 additions & 3 deletions interpreter/statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func TestSetStatement(t *testing.T) {
name: "set local variable",
scope: context.RecvScope,
stmt: &ast.SetStatement{
Meta: &ast.Meta{},
Ident: &ast.Ident{Value: "var.foo"},
Operator: &ast.Operator{Operator: "="},
Value: &ast.Integer{Value: 100},
Expand All @@ -163,6 +164,7 @@ func TestSetStatement(t *testing.T) {
name: "set client.geo.ip_override in vcl_recv",
scope: context.RecvScope,
stmt: &ast.SetStatement{
Meta: &ast.Meta{},
Ident: &ast.Ident{Value: "client.geo.ip_override"},
Operator: &ast.Operator{Operator: "="},
Value: &ast.String{Value: "127.0.0.1"},
Expand All @@ -172,6 +174,7 @@ func TestSetStatement(t *testing.T) {
name: "set bereq.http.Foo in vcl_miss",
scope: context.MissScope,
stmt: &ast.SetStatement{
Meta: &ast.Meta{},
Ident: &ast.Ident{Value: "bereq.http.Foo"},
Operator: &ast.Operator{Operator: "="},
Value: &ast.String{Value: "test"},
Expand All @@ -181,6 +184,7 @@ func TestSetStatement(t *testing.T) {
name: "set bereq.http.Foo in vcl_pass",
scope: context.PassScope,
stmt: &ast.SetStatement{
Meta: &ast.Meta{},
Ident: &ast.Ident{Value: "bereq.http.Foo"},
Operator: &ast.Operator{Operator: "="},
Value: &ast.String{Value: "test"},
Expand Down Expand Up @@ -218,7 +222,9 @@ func TestBlockStatement(t *testing.T) {
name: "block statement with bare return",
scope: context.RecvScope,
stmts: []ast.Statement{
&ast.ReturnStatement{},
&ast.ReturnStatement{
Meta: &ast.Meta{},
},
},
expected_state: BARE_RETURN,
},
Expand All @@ -227,16 +233,20 @@ func TestBlockStatement(t *testing.T) {
scope: context.RecvScope,
stmts: []ast.Statement{
&ast.BlockStatement{
Meta: &ast.Meta{},
Statements: []ast.Statement{
&ast.ReturnStatement{
Meta: &ast.Meta{},
ReturnExpression: &ast.Ident{
Value: "pass",
Meta: &ast.Meta{},
},
},
},
},
&ast.ReturnStatement{},
&ast.ReturnStatement{
Meta: &ast.Meta{},
},
},
expected_state: PASS,
},
Expand All @@ -245,14 +255,19 @@ func TestBlockStatement(t *testing.T) {
scope: context.RecvScope,
stmts: []ast.Statement{
&ast.IfStatement{
Meta: &ast.Meta{},
Condition: &ast.Boolean{Value: true},
Consequence: &ast.BlockStatement{
Meta: &ast.Meta{},
Statements: []ast.Statement{
&ast.ReturnStatement{},
&ast.ReturnStatement{
Meta: &ast.Meta{},
},
},
},
},
&ast.ReturnStatement{
Meta: &ast.Meta{},
ReturnExpression: &ast.Ident{
Value: "pass",
Meta: &ast.Meta{},
Expand Down Expand Up @@ -295,6 +310,7 @@ func TestBlockStatementWithReturnValue(t *testing.T) {
scope: context.RecvScope,
stmts: []ast.Statement{
&ast.ReturnStatement{
Meta: &ast.Meta{},
ReturnExpression: &ast.Integer{
Value: 1,
Meta: &ast.Meta{},
Expand All @@ -307,8 +323,10 @@ func TestBlockStatementWithReturnValue(t *testing.T) {
scope: context.RecvScope,
stmts: []ast.Statement{
&ast.BlockStatement{
Meta: &ast.Meta{},
Statements: []ast.Statement{
&ast.ReturnStatement{
Meta: &ast.Meta{},
ReturnExpression: &ast.Integer{
Value: 1,
Meta: &ast.Meta{},
Expand All @@ -317,6 +335,7 @@ func TestBlockStatementWithReturnValue(t *testing.T) {
},
},
&ast.ReturnStatement{
Meta: &ast.Meta{},
ReturnExpression: &ast.String{
Value: "invalid",
Meta: &ast.Meta{},
Expand All @@ -329,10 +348,12 @@ func TestBlockStatementWithReturnValue(t *testing.T) {
scope: context.RecvScope,
stmts: []ast.Statement{
&ast.IfStatement{
Meta: &ast.Meta{},
Condition: &ast.Boolean{Value: true},
Consequence: &ast.BlockStatement{
Statements: []ast.Statement{
&ast.ReturnStatement{
Meta: &ast.Meta{},
ReturnExpression: &ast.Integer{
Value: 1,
Meta: &ast.Meta{},
Expand All @@ -342,6 +363,7 @@ func TestBlockStatementWithReturnValue(t *testing.T) {
},
},
&ast.ReturnStatement{
Meta: &ast.Meta{},
ReturnExpression: &ast.String{
Value: "invalid",
Meta: &ast.Meta{},
Expand Down
12 changes: 10 additions & 2 deletions interpreter/subroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (i *Interpreter) ProcessSubroutine(sub *ast.SubroutineDeclaration, ds DebugState) (State, error) {
i.process.Flows = append(i.process.Flows, process.NewFlow(i.ctx, sub))
i.process.Flows = append(i.process.Flows, process.NewFlow(i.ctx, process.WithSubroutine(sub)))

// Store the current values and restore after subroutine has ended
regex := i.ctx.RegexMatchedValues
Expand Down Expand Up @@ -44,7 +44,7 @@ func (i *Interpreter) ProcessSubroutine(sub *ast.SubroutineDeclaration, ds Debug

// nolint: gocognit
func (i *Interpreter) ProcessFunctionSubroutine(sub *ast.SubroutineDeclaration, ds DebugState) (value.Value, State, error) {
i.process.Flows = append(i.process.Flows, process.NewFlow(i.ctx, sub))
i.process.Flows = append(i.process.Flows, process.NewFlow(i.ctx, process.WithSubroutine(sub)))

// Store the current values and restore after subroutine has ended
regex := i.ctx.RegexMatchedValues
Expand All @@ -67,6 +67,14 @@ func (i *Interpreter) ProcessFunctionSubroutine(sub *ast.SubroutineDeclaration,
debugState = i.Debugger.Run(stmt)
}

// Find process marker and add flow if found
if name, found := findProcessMark(stmt.GetMeta().Leading); found {
i.process.Flows = append(
i.process.Flows,
process.NewFlow(i.ctx, process.WithName(name), process.WithToken(stmt.GetMeta().Token)),
)
}

switch t := stmt.(type) {
// Common logic statements (nothing to change state)
case *ast.DeclareStatement:
Expand Down