mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
move expr and js to different namespace
This commit is contained in:
@@ -9,14 +9,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// NOTE(jwetzell): see language definition https://expr-lang.org/docs/language-definition
|
// NOTE(jwetzell): see language definition https://expr-lang.org/docs/language-definition
|
||||||
type ProgramExpr struct {
|
type ScriptExpr struct {
|
||||||
config ProcessorConfig
|
config ProcessorConfig
|
||||||
Program *vm.Program
|
Program *vm.Program
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pe *ProgramExpr) Process(ctx context.Context, payload any) (any, error) {
|
func (se *ScriptExpr) Process(ctx context.Context, payload any) (any, error) {
|
||||||
|
|
||||||
output, err := expr.Run(pe.Program, payload)
|
output, err := expr.Run(se.Program, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -24,26 +24,26 @@ func (pe *ProgramExpr) Process(ctx context.Context, payload any) (any, error) {
|
|||||||
return output, nil
|
return output, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pe *ProgramExpr) Type() string {
|
func (se *ScriptExpr) Type() string {
|
||||||
return pe.config.Type
|
return se.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "program.expr",
|
Type: "script.expr",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
expression, ok := params["expression"]
|
expression, ok := params["expression"]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("program.expr requires an expression parameter")
|
return nil, fmt.Errorf("script.expr requires an expression parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
expressionString, ok := expression.(string)
|
expressionString, ok := expression.(string)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("program.expr expression must be a string")
|
return nil, fmt.Errorf("script.expr expression must be a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
program, err := expr.Compile(expressionString)
|
program, err := expr.Compile(expressionString)
|
||||||
@@ -51,7 +51,7 @@ func init() {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ProgramExpr{config: config, Program: program}, nil
|
return &ScriptExpr{config: config, Program: program}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -7,12 +7,12 @@ import (
|
|||||||
"modernc.org/quickjs"
|
"modernc.org/quickjs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProgramJS struct {
|
type ScriptJS struct {
|
||||||
config ProcessorConfig
|
config ProcessorConfig
|
||||||
Program string
|
Program string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pj *ProgramJS) Process(ctx context.Context, payload any) (any, error) {
|
func (sj *ScriptJS) Process(ctx context.Context, payload any) (any, error) {
|
||||||
|
|
||||||
vm, err := quickjs.NewVM()
|
vm, err := quickjs.NewVM()
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ func (pj *ProgramJS) Process(ctx context.Context, payload any) (any, error) {
|
|||||||
|
|
||||||
vm.SetProperty(vm.GlobalObject(), payloadAtom, payload)
|
vm.SetProperty(vm.GlobalObject(), payloadAtom, payload)
|
||||||
|
|
||||||
output, err := vm.Eval(pj.Program, quickjs.EvalGlobal)
|
output, err := vm.Eval(sj.Program, quickjs.EvalGlobal)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -36,29 +36,29 @@ func (pj *ProgramJS) Process(ctx context.Context, payload any) (any, error) {
|
|||||||
return output, nil
|
return output, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pj *ProgramJS) Type() string {
|
func (sj *ScriptJS) Type() string {
|
||||||
return pj.config.Type
|
return sj.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "program.js",
|
Type: "script.js",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
program, ok := params["program"]
|
program, ok := params["program"]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("program.js requires a program parameter")
|
return nil, fmt.Errorf("script.js requires a program parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
programString, ok := program.(string)
|
programString, ok := program.(string)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("program.js program must be a string")
|
return nil, fmt.Errorf("script.js program must be a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ProgramJS{config: config, Program: programString}, nil
|
return &ScriptJS{config: config, Program: programString}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user