add expr lang processor

This commit is contained in:
Joel Wetzell
2025-11-25 07:08:33 -06:00
parent 052213459b
commit 7497f6d589
3 changed files with 60 additions and 0 deletions

1
go.mod
View File

@@ -4,6 +4,7 @@ go 1.25.3
require (
github.com/eclipse/paho.mqtt.golang v1.5.1
github.com/expr-lang/expr v1.17.6
github.com/jwetzell/osc-go v0.1.0
github.com/jwetzell/psn-go v0.2.0
github.com/urfave/cli/v3 v3.6.1

2
go.sum
View File

@@ -2,6 +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/eclipse/paho.mqtt.golang v1.5.1 h1:/VSOv3oDLlpqR2Epjn1Q7b2bSTplJIeV2ISgCl2W7nE=
github.com/eclipse/paho.mqtt.golang v1.5.1/go.mod h1:1/yJCneuyOoCOzKSsOTUc0AJfpsItBGWvYpBLimhArU=
github.com/expr-lang/expr v1.17.6 h1:1h6i8ONk9cexhDmowO/A64VPxHScu7qfSl2k8OlINec=
github.com/expr-lang/expr v1.17.6/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=

View File

@@ -0,0 +1,57 @@
package processing
import (
"context"
"fmt"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/vm"
)
// NOTE(jwetzell): see language definition https://expr-lang.org/docs/language-definition
type DebugExpr struct {
config ProcessorConfig
Program *vm.Program
}
func (dl *DebugExpr) Process(ctx context.Context, payload any) (any, error) {
output, err := expr.Run(dl.Program, payload)
if err != nil {
return nil, err
}
return output, nil
}
func (dl *DebugExpr) Type() string {
return dl.config.Type
}
func init() {
RegisterProcessor(ProcessorRegistration{
Type: "debug.expr",
New: func(config ProcessorConfig) (Processor, error) {
params := config.Params
expression, ok := params["expression"]
if !ok {
return nil, fmt.Errorf("debug.expr requires an expression parameter")
}
expressionString, ok := expression.(string)
if !ok {
return nil, fmt.Errorf("debug.expr url must be a string")
}
program, err := expr.Compile(expressionString)
if err != nil {
return nil, err
}
return &DebugExpr{config: config, Program: program}, nil
},
})
}