use a struct to pass multiple pieces of data into templating context

This commit is contained in:
Joel Wetzell
2026-03-04 12:38:51 -06:00
parent 6a21cc2639
commit 572a54d3b2
12 changed files with 82 additions and 46 deletions

View File

@@ -49,7 +49,7 @@ func TestGoodOSCMessageCreate(t *testing.T) {
{
name: "address with template and no args",
params: map[string]any{
"address": "/test/{{.Value}}",
"address": "/test/{{.Payload.Value}}",
},
payload: map[string]any{"Value": "value"},
expected: &osc.OSCMessage{Address: "/test/value"},
@@ -57,7 +57,7 @@ func TestGoodOSCMessageCreate(t *testing.T) {
{
name: "address with template and string arg",
params: map[string]any{
"address": "/test/{{.Value}}",
"address": "/test/{{.Payload.Value}}",
"args": []interface{}{"arg1"},
"types": "s",
},
@@ -67,7 +67,7 @@ func TestGoodOSCMessageCreate(t *testing.T) {
{
name: "address with template and mixed args",
params: map[string]any{
"address": "/test/{{.Value}}",
"address": "/test/{{.Payload.Value}}",
"args": []interface{}{"arg1", "42", "3.14"},
"types": "sif",
},
@@ -84,7 +84,7 @@ func TestGoodOSCMessageCreate(t *testing.T) {
{
name: "address with template and int64 arg",
params: map[string]any{
"address": "/test/{{.Value}}",
"address": "/test/{{.Payload.Value}}",
"args": []interface{}{"42"},
"types": "h",
},
@@ -94,7 +94,7 @@ func TestGoodOSCMessageCreate(t *testing.T) {
{
name: "address with template and double arg",
params: map[string]any{
"address": "/test/{{.Value}}",
"address": "/test/{{.Payload.Value}}",
"args": []interface{}{"42"},
"types": "d",
},
@@ -104,7 +104,7 @@ func TestGoodOSCMessageCreate(t *testing.T) {
{
name: "address with template and true arg",
params: map[string]any{
"address": "/test/{{.Value}}",
"address": "/test/{{.Payload.Value}}",
"args": []interface{}{""},
"types": "T",
},
@@ -114,7 +114,7 @@ func TestGoodOSCMessageCreate(t *testing.T) {
{
name: "address with template and false arg",
params: map[string]any{
"address": "/test/{{.Value}}",
"address": "/test/{{.Payload.Value}}",
"args": []interface{}{""},
"types": "F",
},
@@ -124,7 +124,7 @@ func TestGoodOSCMessageCreate(t *testing.T) {
{
name: "address with template and nil arg",
params: map[string]any{
"address": "/test/{{.Value}}",
"address": "/test/{{.Payload.Value}}",
"args": []interface{}{""},
"types": "N",
},
@@ -286,7 +286,7 @@ func TestBadOSCMessageCreate(t *testing.T) {
"address": "/test/{{.missing}}",
},
payload: "test",
errorString: "template: address:1:8: executing \"address\" at <.missing>: can't evaluate field missing in type string",
errorString: "template: address:1:8: executing \"address\" at <.missing>: can't evaluate field missing in type processor.TemplateData",
},
{
name: "address doesn't start with slash",
@@ -304,7 +304,7 @@ func TestBadOSCMessageCreate(t *testing.T) {
"types": "s",
},
payload: "test",
errorString: "template: arg:1:2: executing \"arg\" at <.missing>: can't evaluate field missing in type string",
errorString: "template: arg:1:2: executing \"arg\" at <.missing>: can't evaluate field missing in type processor.TemplateData",
},
}

View File

@@ -16,7 +16,7 @@ func TestStringCreateFromRegistry(t *testing.T) {
processorInstance, err := registration.New(config.ProcessorConfig{
Type: "string.create",
Params: map[string]any{
"template": "{{.}}",
"template": "{{.Payload}}",
},
})
if err != nil {
@@ -50,31 +50,31 @@ func TestGoodStringCreate(t *testing.T) {
}{
{
name: "string payload",
params: map[string]any{"template": "{{.}}"},
params: map[string]any{"template": "{{.Payload}}"},
payload: "hello",
expected: "hello",
},
{
name: "number payload",
params: map[string]any{"template": "{{.}}"},
params: map[string]any{"template": "{{.Payload}}"},
payload: 4,
expected: "4",
},
{
name: "boolean payload",
params: map[string]any{"template": "{{.}}"},
params: map[string]any{"template": "{{.Payload}}"},
payload: true,
expected: "true",
},
{
name: "struct payload - field",
params: map[string]any{"template": "{{.Data}}"},
params: map[string]any{"template": "{{.Payload.Data}}"},
payload: TestStruct{Data: "test"},
expected: "test",
},
{
name: "struct payload - method",
params: map[string]any{"template": "{{.GetData}}"},
params: map[string]any{"template": "{{.Payload.GetData}}"},
payload: TestStruct{Data: "test"},
expected: "test",
},
@@ -148,7 +148,7 @@ func TestBadStringCreate(t *testing.T) {
params: map[string]any{
"template": "{{.Invalid}}",
},
errorString: "template: template:1:2: executing \"template\" at <.Invalid>: can't evaluate field Invalid in type string",
errorString: "template: template:1:2: executing \"template\" at <.Invalid>: can't evaluate field Invalid in type processor.TemplateData",
},
}