wrap all payload types for expr env

This commit is contained in:
Joel Wetzell
2026-03-01 23:49:53 -06:00
parent 8fe1463198
commit 9d14e5929f
4 changed files with 34 additions and 94 deletions

View File

@@ -15,27 +15,21 @@ type FilterExpr struct {
Program *vm.Program Program *vm.Program
} }
type PayloadStruct struct { func SafeExprEnv(payload any) any {
exprEnv := ExprEnv{
Payload: payload,
}
return exprEnv
}
type ExprEnv struct {
Payload any Payload any
} }
func (se *FilterExpr) Process(ctx context.Context, payload any) (any, error) { func (se *FilterExpr) Process(ctx context.Context, payload any) (any, error) {
payloadType := fmt.Sprintf("%T", payload)
exprEnv := payload exprEnv := SafeExprEnv(payload)
switch payloadType {
case "uint", "uint8", "uint16", "uint32", "uint64":
exprEnv = PayloadStruct{Payload: payload}
case "int", "int8", "int16", "int32", "int64":
exprEnv = PayloadStruct{Payload: payload}
case "float32", "float64":
exprEnv = PayloadStruct{Payload: payload}
case "string":
exprEnv = PayloadStruct{Payload: payload}
case "bool":
exprEnv = PayloadStruct{Payload: payload}
}
output, err := expr.Run(se.Program, exprEnv) output, err := expr.Run(se.Program, exprEnv)
if err != nil { if err != nil {

View File

@@ -17,22 +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) {
payloadType := fmt.Sprintf("%T", payload) exprEnv := SafeExprEnv(payload)
exprEnv := payload
switch payloadType {
case "uint", "uint8", "uint16", "uint32", "uint64":
exprEnv = PayloadStruct{Payload: payload}
case "int", "int8", "int16", "int32", "int64":
exprEnv = PayloadStruct{Payload: payload}
case "float32", "float64":
exprEnv = PayloadStruct{Payload: payload}
case "string":
exprEnv = PayloadStruct{Payload: payload}
case "bool":
exprEnv = PayloadStruct{Payload: payload}
}
output, err := expr.Run(se.Program, exprEnv) output, err := expr.Run(se.Program, exprEnv)
if err != nil { if err != nil {

View File

@@ -39,7 +39,7 @@ func TestGoodFilterExpr(t *testing.T) {
{ {
name: "number", name: "number",
params: map[string]any{ params: map[string]any{
"expression": "Int > 0", "expression": "Payload.Int > 0",
}, },
payload: TestStruct{ payload: TestStruct{
Int: 1, Int: 1,
@@ -51,7 +51,7 @@ func TestGoodFilterExpr(t *testing.T) {
{ {
name: "string", name: "string",
params: map[string]any{ params: map[string]any{
"expression": "String == 'hello'", "expression": "Payload.String == 'hello'",
}, },
payload: TestStruct{ payload: TestStruct{
String: "hello", String: "hello",
@@ -63,7 +63,7 @@ func TestGoodFilterExpr(t *testing.T) {
{ {
name: "not matching", name: "not matching",
params: map[string]any{ params: map[string]any{
"expression": "Int > 0", "expression": "Payload.Int > 0",
}, },
payload: TestStruct{ payload: TestStruct{
Int: 0, Int: 0,
@@ -136,12 +136,12 @@ func TestBadFilterExpr(t *testing.T) {
{ {
name: "accessing missing field", name: "accessing missing field",
params: map[string]any{ params: map[string]any{
"expression": "foo + bar", "expression": "Payload.foo + Payload.bar",
}, },
payload: map[string]any{ payload: map[string]any{
"foo": 1, "foo": 1,
}, },
errorString: "invalid operation: int + <nil> (1:5)\n | foo + bar\n | ....^", errorString: "invalid operation: int + <nil> (1:13)\n | Payload.foo + Payload.bar\n | ............^",
}, },
} }

View File

@@ -28,58 +28,6 @@ func TestScriptExprFromRegistry(t *testing.T) {
} }
} }
func TestScriptExprNoProgram(t *testing.T) {
registration, ok := processor.ProcessorRegistry["script.expr"]
if !ok {
t.Fatalf("script.expr processor not registered")
}
_, err := registration.New(config.ProcessorConfig{
Type: "script.expr",
Params: map[string]any{},
})
if err == nil {
t.Fatalf("script.expr processor should have thrown an error when creating")
}
}
func TestScriptExprBadConfigWrongExpressionType(t *testing.T) {
registration, ok := processor.ProcessorRegistry["script.expr"]
if !ok {
t.Fatalf("script.expr processor not registered")
}
_, err := registration.New(config.ProcessorConfig{
Type: "script.expr",
Params: map[string]any{
"expression": 12345,
},
})
if err == nil {
t.Fatalf("script.expr processor should have thrown an error when creating with non-string expression")
}
}
func TestScriptExprBadConfigNonCompilingExpression(t *testing.T) {
registration, ok := processor.ProcessorRegistry["script.expr"]
if !ok {
t.Fatalf("script.expr processor not registered")
}
_, err := registration.New(config.ProcessorConfig{
Type: "script.expr",
Params: map[string]any{
"expression": "foo + ",
},
})
if err == nil {
t.Fatalf("script.expr processor should have thrown an error when creating with non-compiling expression")
}
}
func TestGoodScriptExpr(t *testing.T) { func TestGoodScriptExpr(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -90,7 +38,7 @@ func TestGoodScriptExpr(t *testing.T) {
{ {
name: "number", name: "number",
params: map[string]any{ params: map[string]any{
"expression": "foo + bar", "expression": "Payload.foo + Payload.bar",
}, },
payload: map[string]any{ payload: map[string]any{
"foo": 1, "foo": 1,
@@ -101,7 +49,7 @@ func TestGoodScriptExpr(t *testing.T) {
{ {
name: "string", name: "string",
params: map[string]any{ params: map[string]any{
"expression": "foo + bar", "expression": "Payload.foo + Payload.bar",
}, },
payload: map[string]any{ payload: map[string]any{
"foo": "1", "foo": "1",
@@ -145,18 +93,24 @@ func TestBadScriptExpr(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
params map[string]any params map[string]any
payload map[string]any payload any
errorString string errorString string
}{ }{
{
name: "no expression parameter",
params: map[string]any{},
payload: map[string]any{"foo": 1, "bar": 1},
errorString: "script.expr expression error: not found",
},
{ {
name: "accessing missing field", name: "accessing missing field",
params: map[string]any{ params: map[string]any{
"expression": "foo + bar", "expression": "Payload.foo + Payload.bar",
}, },
payload: map[string]any{ payload: map[string]any{
"foo": 1, "foo": 1,
}, },
errorString: "invalid operation: int + <nil> (1:5)\n | foo + bar\n | ....^", errorString: "invalid operation: int + <nil> (1:13)\n | Payload.foo + Payload.bar\n | ............^",
}, },
} }
@@ -172,6 +126,13 @@ func TestBadScriptExpr(t *testing.T) {
Params: test.params, Params: test.params,
}) })
if err != nil {
if err.Error() != test.errorString {
t.Fatalf("script.expr got error '%s', expected '%s'", err.Error(), test.errorString)
}
return
}
got, err := processorInstance.Process(t.Context(), test.payload) got, err := processorInstance.Process(t.Context(), test.payload)
if err == nil { if err == nil {