mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-08-16 11:53:23 +00:00
Merge pull request #167 from jwetzell/feat/kv-set-from-payload
switch kv.set to take value from incoming payload
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log/slog"
|
||||
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
@@ -17,7 +15,6 @@ type KVSet struct {
|
||||
config config.ProcessorConfig
|
||||
ModuleId string
|
||||
Key string
|
||||
Value *template.Template
|
||||
logger *slog.Logger
|
||||
module common.KeyValueModule
|
||||
}
|
||||
@@ -43,15 +40,7 @@ func (kvs *KVSet) Process(ctx context.Context, wrappedPayload common.WrappedPayl
|
||||
kvs.module = kvModule
|
||||
}
|
||||
|
||||
var valueBuffer bytes.Buffer
|
||||
err := kvs.Value.Execute(&valueBuffer, wrappedPayload)
|
||||
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
}
|
||||
|
||||
err = kvs.module.Set(kvs.Key, valueBuffer.String())
|
||||
err := kvs.module.Set(kvs.Key, wrappedPayload.Payload)
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("kv.set error setting key: %w", err)
|
||||
@@ -79,12 +68,8 @@ func init() {
|
||||
Title: "Key",
|
||||
Type: "string",
|
||||
},
|
||||
"value": {
|
||||
Title: "Value",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"module", "key", "value"},
|
||||
Required: []string{"module", "key"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
@@ -101,17 +86,7 @@ func init() {
|
||||
return nil, fmt.Errorf("kv.set key error: %w", err)
|
||||
}
|
||||
|
||||
valueString, err := params.GetString("value")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kv.set value error: %w", err)
|
||||
}
|
||||
valueTemplate, err := template.New("template").Parse(valueString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &KVSet{config: config, ModuleId: moduleIdString, Key: keyString, Value: valueTemplate, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
return &KVSet{config: config, ModuleId: moduleIdString, Key: keyString, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ func TestKvSetFromRegistry(t *testing.T) {
|
||||
Params: map[string]any{
|
||||
"module": "test",
|
||||
"key": "test",
|
||||
"value": "hello",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
@@ -32,8 +31,8 @@ func TestKvSetFromRegistry(t *testing.T) {
|
||||
t.Fatalf("kv.set processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
|
||||
payload := ""
|
||||
expected := ""
|
||||
payload := "test"
|
||||
expected := "test"
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), common.WrappedPayload{
|
||||
Modules: map[string]common.Module{
|
||||
@@ -63,10 +62,9 @@ func TestGoodKvSet(t *testing.T) {
|
||||
params: map[string]any{
|
||||
"module": "test",
|
||||
"key": "test",
|
||||
"value": "hello",
|
||||
},
|
||||
payload: "",
|
||||
expected: "",
|
||||
payload: "test",
|
||||
expected: "test",
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
@@ -116,7 +114,6 @@ func TestBadKvSet(t *testing.T) {
|
||||
payload: test.TestStruct{Data: "hello"},
|
||||
params: map[string]any{
|
||||
"key": "test",
|
||||
"value": "test",
|
||||
},
|
||||
wrappedPayloadModules: map[string]common.Module{
|
||||
"test": &test.TestKVModule{},
|
||||
@@ -129,7 +126,6 @@ func TestBadKvSet(t *testing.T) {
|
||||
params: map[string]any{
|
||||
"module": 1,
|
||||
"key": "test",
|
||||
"value": "test",
|
||||
},
|
||||
wrappedPayloadModules: map[string]common.Module{
|
||||
"test": &test.TestKVModule{},
|
||||
@@ -141,7 +137,6 @@ func TestBadKvSet(t *testing.T) {
|
||||
payload: test.TestStruct{Data: "hello"},
|
||||
params: map[string]any{
|
||||
"module": "test",
|
||||
"value": "test",
|
||||
},
|
||||
wrappedPayloadModules: map[string]common.Module{
|
||||
"test": &test.TestKVModule{},
|
||||
@@ -154,82 +149,28 @@ func TestBadKvSet(t *testing.T) {
|
||||
params: map[string]any{
|
||||
"module": "test",
|
||||
"key": 1,
|
||||
"value": "test",
|
||||
},
|
||||
wrappedPayloadModules: map[string]common.Module{
|
||||
"test": &test.TestKVModule{},
|
||||
},
|
||||
errorString: "kv.set key error: not a string",
|
||||
},
|
||||
{
|
||||
name: "no value param",
|
||||
payload: test.TestStruct{Data: "hello"},
|
||||
params: map[string]any{
|
||||
"module": "test",
|
||||
"key": "test",
|
||||
},
|
||||
wrappedPayloadModules: map[string]common.Module{
|
||||
"test": &test.TestKVModule{},
|
||||
},
|
||||
errorString: "kv.set value error: not found",
|
||||
},
|
||||
{
|
||||
name: "non string value",
|
||||
payload: test.TestStruct{Data: "hello"},
|
||||
params: map[string]any{
|
||||
"module": "test",
|
||||
"key": "test",
|
||||
"value": 1,
|
||||
},
|
||||
wrappedPayloadModules: map[string]common.Module{
|
||||
"test": &test.TestKVModule{},
|
||||
},
|
||||
errorString: "kv.set value error: not a string",
|
||||
},
|
||||
{
|
||||
name: "no modules in context",
|
||||
payload: test.TestStruct{Data: "hello"},
|
||||
params: map[string]any{
|
||||
"module": "test",
|
||||
"key": "test",
|
||||
"value": "hello",
|
||||
},
|
||||
wrappedPayloadModules: nil,
|
||||
errorString: "kv.set wrapped payload has no modules",
|
||||
},
|
||||
{
|
||||
name: "value template syntax error",
|
||||
payload: test.TestStruct{Data: "hello"},
|
||||
params: map[string]any{
|
||||
"module": "test",
|
||||
"key": "test",
|
||||
"value": "{{",
|
||||
},
|
||||
wrappedPayloadModules: map[string]common.Module{
|
||||
"test": &test.TestKVModule{},
|
||||
},
|
||||
errorString: "template: template:1: unclosed action",
|
||||
},
|
||||
{
|
||||
name: "value template execution error",
|
||||
payload: test.TestStruct{Data: "hello"},
|
||||
params: map[string]any{
|
||||
"module": "test",
|
||||
"key": "test",
|
||||
"value": "{{.Data}}",
|
||||
},
|
||||
wrappedPayloadModules: map[string]common.Module{
|
||||
"test": &test.TestKVModule{},
|
||||
},
|
||||
errorString: "template: template:1:2: executing \"template\" at <.Data>: can't evaluate field Data in type common.WrappedPayload",
|
||||
},
|
||||
{
|
||||
name: "module not found in context",
|
||||
payload: test.TestStruct{Data: "hello"},
|
||||
params: map[string]any{
|
||||
"module": "test",
|
||||
"key": "test",
|
||||
"value": "hello",
|
||||
},
|
||||
wrappedPayloadModules: map[string]common.Module{},
|
||||
errorString: "kv.set unable to find module with id: test",
|
||||
@@ -240,7 +181,6 @@ func TestBadKvSet(t *testing.T) {
|
||||
params: map[string]any{
|
||||
"module": "test",
|
||||
"key": "test",
|
||||
"value": "hello",
|
||||
},
|
||||
wrappedPayloadModules: map[string]common.Module{
|
||||
"test": test.NewTestDBModule("test"),
|
||||
@@ -293,7 +233,6 @@ func BenchmarkKvSet(b *testing.B) {
|
||||
Params: map[string]any{
|
||||
"module": "test",
|
||||
"key": "test",
|
||||
"value": "{{.Payload}}",
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user