mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
Merge pull request #87 from jwetzell/feat/script-processor-env-data
consistent script processor environments
This commit is contained in:
@@ -15,21 +15,9 @@ type FilterExpr struct {
|
|||||||
Program *vm.Program
|
Program *vm.Program
|
||||||
}
|
}
|
||||||
|
|
||||||
func SafeExprEnv(payload any) any {
|
|
||||||
exprEnv := ExprEnv{
|
|
||||||
Payload: payload,
|
|
||||||
}
|
|
||||||
|
|
||||||
return exprEnv
|
|
||||||
}
|
|
||||||
|
|
||||||
type ExprEnv struct {
|
|
||||||
Payload any
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fe *FilterExpr) Process(ctx context.Context, payload any) (any, error) {
|
func (fe *FilterExpr) Process(ctx context.Context, payload any) (any, error) {
|
||||||
|
|
||||||
exprEnv := SafeExprEnv(payload)
|
exprEnv := GetEnvData(ctx, payload)
|
||||||
|
|
||||||
output, err := expr.Run(fe.Program, exprEnv)
|
output, err := expr.Run(fe.Program, exprEnv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -101,6 +101,11 @@ type TemplateData struct {
|
|||||||
Sender any
|
Sender any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EnvData struct {
|
||||||
|
Payload any
|
||||||
|
Sender any
|
||||||
|
}
|
||||||
|
|
||||||
func GetTemplateData(ctx context.Context, payload any) TemplateData {
|
func GetTemplateData(ctx context.Context, payload any) TemplateData {
|
||||||
templateData := TemplateData{Payload: payload}
|
templateData := TemplateData{Payload: payload}
|
||||||
modules := ctx.Value(common.ModulesContextKey)
|
modules := ctx.Value(common.ModulesContextKey)
|
||||||
@@ -114,3 +119,13 @@ func GetTemplateData(ctx context.Context, payload any) TemplateData {
|
|||||||
}
|
}
|
||||||
return templateData
|
return templateData
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetEnvData(ctx context.Context, payload any) EnvData {
|
||||||
|
envData := EnvData{Payload: payload}
|
||||||
|
|
||||||
|
sender := ctx.Value(common.SenderContextKey)
|
||||||
|
if sender != nil {
|
||||||
|
envData.Sender = sender
|
||||||
|
}
|
||||||
|
return envData
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type ScriptExpr struct {
|
|||||||
|
|
||||||
func (se *ScriptExpr) Process(ctx context.Context, payload any) (any, error) {
|
func (se *ScriptExpr) Process(ctx context.Context, payload any) (any, error) {
|
||||||
|
|
||||||
exprEnv := SafeExprEnv(payload)
|
exprEnv := GetEnvData(ctx, payload)
|
||||||
|
|
||||||
output, err := expr.Run(se.Program, exprEnv)
|
output, err := expr.Run(se.Program, exprEnv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/common"
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"modernc.org/quickjs"
|
"modernc.org/quickjs"
|
||||||
)
|
)
|
||||||
@@ -30,6 +31,16 @@ func (sj *ScriptJS) Process(ctx context.Context, payload any) (any, error) {
|
|||||||
|
|
||||||
vm.SetProperty(vm.GlobalObject(), payloadAtom, payload)
|
vm.SetProperty(vm.GlobalObject(), payloadAtom, payload)
|
||||||
|
|
||||||
|
sender := ctx.Value(common.SenderContextKey)
|
||||||
|
if sender != nil {
|
||||||
|
senderAtom, err := vm.NewAtom("sender")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
vm.SetProperty(vm.GlobalObject(), senderAtom, sender)
|
||||||
|
}
|
||||||
|
|
||||||
_, err = vm.Eval(sj.Program, quickjs.EvalGlobal)
|
_, err = vm.Eval(sj.Program, quickjs.EvalGlobal)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user