Skip to content

Commit

Permalink
Use expr.WithContext for the patcher
Browse files Browse the repository at this point in the history
With Expr 1.16.5 my fix was merged so we no longer need the custom
patcher for the context. It now uses expr.WithContext directly.
  • Loading branch information
daenney committed Apr 23, 2024
1 parent b8d3f05 commit e5e0869
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 41 deletions.
39 changes: 1 addition & 38 deletions expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"io/fs"
"os"
"path/filepath"
"reflect"

"code.dny.dev/gopherxlr/dbus"
"code.dny.dev/gopherxlr/websocket"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/ast"
"github.com/expr-lang/expr/vm"
)

Expand Down Expand Up @@ -48,7 +46,7 @@ func LoadPrograms(dir string) ([]Program, error) {
if err != nil {
return err
}
prog, err := expr.Compile(string(src), expr.Env(Env{}), expr.Patch(patcher{}))
prog, err := expr.Compile(string(src), expr.Env(Env{}), expr.WithContext("ctx"))
if err != nil {
return err
}
Expand All @@ -61,38 +59,3 @@ func LoadPrograms(dir string) ([]Program, error) {
}
return res, nil
}

type patcher struct{}

// Until https://github.com/expr-lang/expr/pull/602 is merged
func (patcher) Visit(node *ast.Node) {
switch call := (*node).(type) {
case *ast.CallNode:
fn := call.Callee.Type()
if fn == nil {
return
}
if fn.Kind() != reflect.Func {
return
}
switch fn.NumIn() {
case 0:
return
case 1:
if fn.In(0).String() != "context.Context" {
return
}
default:
if fn.In(0).String() != "context.Context" &&
fn.In(1).String() != "context.Context" {
return
}
}
ast.Patch(node, &ast.CallNode{
Callee: call.Callee,
Arguments: append([]ast.Node{
&ast.IdentifierNode{Value: "ctx"},
}, call.Arguments...),
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.0

require (
github.com/evanphx/json-patch/v5 v5.9.0
github.com/expr-lang/expr v1.16.1
github.com/expr-lang/expr v1.16.5
github.com/godbus/dbus/v5 v5.1.0
golang.org/x/net v0.24.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0/FOJfg=
github.com/evanphx/json-patch/v5 v5.9.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
github.com/expr-lang/expr v1.16.1 h1:Na8CUcMdyGbnNpShY7kzcHCU7WqxuL+hnxgHZ4vaz/A=
github.com/expr-lang/expr v1.16.1/go.mod h1:uCkhfG+x7fcZ5A5sXHKuQ07jGZRl6J0FCAaf2k4PtVQ=
github.com/expr-lang/expr v1.16.5 h1:m2hvtguFeVaVNTHj8L7BoAyt7O0PAIBaSVbjdHgRXMs=
github.com/expr-lang/expr v1.16.5/go.mod h1:uCkhfG+x7fcZ5A5sXHKuQ07jGZRl6J0FCAaf2k4PtVQ=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down

0 comments on commit e5e0869

Please sign in to comment.