diff --git a/internal/common/payload.go b/internal/common/payload.go new file mode 100644 index 0000000..c5bcca9 --- /dev/null +++ b/internal/common/payload.go @@ -0,0 +1,26 @@ +package common + +import ( + "context" +) + +type WrappedPayload struct { + Payload any + Modules any + Sender any + End bool +} + +func GetWrappedPayload(ctx context.Context, payload any) WrappedPayload { + templateData := WrappedPayload{Payload: payload} + modules := ctx.Value(ModulesContextKey) + if modules != nil { + templateData.Modules = modules + } + + sender := ctx.Value(SenderContextKey) + if sender != nil { + templateData.Sender = sender + } + return templateData +} diff --git a/internal/processor/artnet-packet-decode.go b/internal/processor/artnet-packet-decode.go index fad7273..9fed8ac 100644 --- a/internal/processor/artnet-packet-decode.go +++ b/internal/processor/artnet-packet-decode.go @@ -13,20 +13,25 @@ type ArtNetPacketDecode struct { config config.ProcessorConfig } -func (apd *ArtNetPacketDecode) Process(ctx context.Context, payload any) (any, error) { +func (apd *ArtNetPacketDecode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadBytes, ok := common.GetAnyAs[[]byte](payload) if !ok { - return nil, fmt.Errorf("artnet.packet.decode processor only accepts a []byte") + wrappedPayload.End = true + return wrappedPayload, fmt.Errorf("artnet.packet.decode processor only accepts a []byte") } payloadMessage, err := artnet.Decode(payloadBytes) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } - return payloadMessage, nil + wrappedPayload.Payload = payloadMessage + + return wrappedPayload, nil } func (apd *ArtNetPacketDecode) Type() string { diff --git a/internal/processor/artnet-packet-encode.go b/internal/processor/artnet-packet-encode.go index cd55db0..4bff064 100644 --- a/internal/processor/artnet-packet-encode.go +++ b/internal/processor/artnet-packet-encode.go @@ -13,19 +13,24 @@ type ArtNetPacketEncode struct { config config.ProcessorConfig } -func (ape *ArtNetPacketEncode) Process(ctx context.Context, payload any) (any, error) { +func (ape *ArtNetPacketEncode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadPacket, ok := common.GetAnyAs[artnet.ArtNetPacket](payload) if !ok { - return nil, fmt.Errorf("artnet.packet.encode processor only accepts an ArtNetPacket") + wrappedPayload.End = true + return wrappedPayload, fmt.Errorf("artnet.packet.encode processor only accepts an ArtNetPacket") } payloadBytes, err := payloadPacket.MarshalBinary() if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } - return payloadBytes, nil + wrappedPayload.Payload = payloadBytes + + return wrappedPayload, nil } func (ape *ArtNetPacketEncode) Type() string { diff --git a/internal/processor/debug-log.go b/internal/processor/debug-log.go index 7353193..10c442e 100644 --- a/internal/processor/debug-log.go +++ b/internal/processor/debug-log.go @@ -5,6 +5,7 @@ import ( "fmt" "log/slog" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -13,11 +14,12 @@ type DebugLog struct { logger *slog.Logger } -func (dl *DebugLog) Process(ctx context.Context, payload any) (any, error) { +func (dl *DebugLog) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadString := fmt.Sprintf("%+v", payload) payloadType := fmt.Sprintf("%T", payload) dl.logger.Debug("", "payload", payloadString, "payloadType", payloadType) - return payload, nil + return wrappedPayload, nil } func (dl *DebugLog) Type() string { diff --git a/internal/processor/filter-expr.go b/internal/processor/filter-expr.go index f725ac0..4ccb0de 100644 --- a/internal/processor/filter-expr.go +++ b/internal/processor/filter-expr.go @@ -6,6 +6,7 @@ import ( "github.com/expr-lang/expr" "github.com/expr-lang/expr/vm" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -15,24 +16,29 @@ type FilterExpr struct { Program *vm.Program } -func (fe *FilterExpr) Process(ctx context.Context, payload any) (any, error) { +func (fe *FilterExpr) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { - exprEnv := GetEnvData(ctx, payload) + exprEnv := wrappedPayload output, err := expr.Run(fe.Program, exprEnv) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } outputBool, ok := output.(bool) if !ok { - return nil, fmt.Errorf("filter.expr expression did not return a boolean") + wrappedPayload.End = true + return wrappedPayload, fmt.Errorf("filter.expr expression did not return a boolean") } if !outputBool { - return nil, nil + wrappedPayload.End = true + return wrappedPayload, nil } - return payload, nil + wrappedPayload.Payload = exprEnv.Payload + + return wrappedPayload, nil } func (fe *FilterExpr) Type() string { diff --git a/internal/processor/filter-regex.go b/internal/processor/filter-regex.go index 349c849..71018c7 100644 --- a/internal/processor/filter-regex.go +++ b/internal/processor/filter-regex.go @@ -15,18 +15,22 @@ type FilterRegex struct { Pattern *regexp.Regexp } -func (fr *FilterRegex) Process(ctx context.Context, payload any) (any, error) { +func (fr *FilterRegex) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadString, ok := common.GetAnyAs[string](payload) if !ok { - return nil, errors.New("filter.regex processor only accepts a string") + wrappedPayload.End = true + return wrappedPayload, errors.New("filter.regex processor only accepts a string") } if !fr.Pattern.MatchString(payloadString) { - return nil, nil + wrappedPayload.End = true + return wrappedPayload, nil } - return payloadString, nil + wrappedPayload.Payload = payloadString + return wrappedPayload, nil } func (fr *FilterRegex) Type() string { diff --git a/internal/processor/float-parse.go b/internal/processor/float-parse.go index 3b07701..b911660 100644 --- a/internal/processor/float-parse.go +++ b/internal/processor/float-parse.go @@ -15,18 +15,22 @@ type FloatParse struct { config config.ProcessorConfig } -func (fp *FloatParse) Process(ctx context.Context, payload any) (any, error) { +func (fp *FloatParse) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadString, ok := common.GetAnyAs[string](payload) if !ok { - return nil, errors.New("float.parse processor only accepts a string") + wrappedPayload.End = true + return wrappedPayload, errors.New("float.parse processor only accepts a string") } payloadFloat, err := strconv.ParseFloat(payloadString, fp.BitSize) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } - return payloadFloat, nil + wrappedPayload.Payload = payloadFloat + return wrappedPayload, nil } func (fp *FloatParse) Type() string { diff --git a/internal/processor/float-random.go b/internal/processor/float-random.go index b064683..cf43854 100644 --- a/internal/processor/float-random.go +++ b/internal/processor/float-random.go @@ -6,6 +6,7 @@ import ( "fmt" "math/rand/v2" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -16,16 +17,19 @@ type FloatRandom struct { config config.ProcessorConfig } -func (fr *FloatRandom) Process(ctx context.Context, payload any) (any, error) { +func (fr *FloatRandom) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { if fr.BitSize == 32 { payloadFloat := rand.Float32()*(float32(fr.Max)-float32(fr.Min)) + float32(fr.Min) - return payloadFloat, nil + wrappedPayload.Payload = payloadFloat + return wrappedPayload, nil } if fr.BitSize == 64 { payloadFloat := rand.Float64()*(fr.Max-fr.Min) + fr.Min - return payloadFloat, nil + wrappedPayload.Payload = payloadFloat + return wrappedPayload, nil } - return nil, errors.New("float.random bitSize error: must be 32 or 64") + wrappedPayload.End = true + return wrappedPayload, errors.New("float.random bitSize error: must be 32 or 64") } func (fr *FloatRandom) Type() string { diff --git a/internal/processor/free-d-create.go b/internal/processor/free-d-create.go index 8a17b95..aa7588d 100644 --- a/internal/processor/free-d-create.go +++ b/internal/processor/free-d-create.go @@ -8,6 +8,7 @@ import ( "text/template" freeD "github.com/jwetzell/free-d-go" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -24,15 +25,16 @@ type FreeDCreate struct { Focus *template.Template } -func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) { +func (fc *FreeDCreate) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { - templateData := GetTemplateData(ctx, payload) + templateData := wrappedPayload var idBuffer bytes.Buffer err := fc.Id.Execute(&idBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } idString := idBuffer.String() @@ -40,14 +42,16 @@ func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) { idNum, err := strconv.ParseUint(idString, 10, 8) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } var panBuffer bytes.Buffer err = fc.Pan.Execute(&panBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } panString := panBuffer.String() @@ -55,14 +59,16 @@ func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) { panNum, err := strconv.ParseFloat(panString, 32) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } var tiltBuffer bytes.Buffer err = fc.Tilt.Execute(&tiltBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } tiltString := tiltBuffer.String() @@ -70,14 +76,16 @@ func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) { tiltNum, err := strconv.ParseFloat(tiltString, 32) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } var rollBuffer bytes.Buffer err = fc.Tilt.Execute(&rollBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } rollString := rollBuffer.String() @@ -85,14 +93,16 @@ func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) { rollNum, err := strconv.ParseFloat(rollString, 32) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } var posXBuffer bytes.Buffer err = fc.PosX.Execute(&posXBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } posXString := posXBuffer.String() @@ -100,14 +110,16 @@ func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) { posXNum, err := strconv.ParseFloat(posXString, 32) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } var posYBuffer bytes.Buffer err = fc.PosY.Execute(&posYBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } posYString := posYBuffer.String() @@ -115,14 +127,16 @@ func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) { posYNum, err := strconv.ParseFloat(posYString, 32) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } var posZBuffer bytes.Buffer err = fc.PosZ.Execute(&posZBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } posZString := posZBuffer.String() @@ -130,14 +144,16 @@ func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) { posZNum, err := strconv.ParseFloat(posZString, 32) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } var zoomBuffer bytes.Buffer err = fc.Zoom.Execute(&zoomBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } zoomString := zoomBuffer.String() @@ -145,14 +161,16 @@ func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) { zoomNum, err := strconv.ParseInt(zoomString, 10, 32) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } var focusBuffer bytes.Buffer err = fc.Focus.Execute(&focusBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } focusString := focusBuffer.String() @@ -160,7 +178,8 @@ func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) { focusNum, err := strconv.ParseInt(focusString, 10, 32) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } payloadMessage := freeD.FreeDPosition{ @@ -175,7 +194,9 @@ func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) { Focus: int32(focusNum), } - return payloadMessage, nil + wrappedPayload.Payload = payloadMessage + + return wrappedPayload, nil } func (fc *FreeDCreate) Type() string { diff --git a/internal/processor/free-d-decode.go b/internal/processor/free-d-decode.go index 3c595f1..6f87b9a 100644 --- a/internal/processor/free-d-decode.go +++ b/internal/processor/free-d-decode.go @@ -13,18 +13,22 @@ type FreeDDecode struct { config config.ProcessorConfig } -func (fd *FreeDDecode) Process(ctx context.Context, payload any) (any, error) { +func (fd *FreeDDecode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadBytes, ok := common.GetAnyAs[[]byte](payload) if !ok { - return nil, errors.New("freed.decode processor only accepts a []byte") + wrappedPayload.End = true + return wrappedPayload, errors.New("freed.decode processor only accepts a []byte") } payloadMessage, err := freeD.Decode(payloadBytes) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } - return payloadMessage, nil + wrappedPayload.Payload = payloadMessage + return wrappedPayload, nil } func (fd *FreeDDecode) Type() string { diff --git a/internal/processor/free-d-encode.go b/internal/processor/free-d-encode.go index 919928c..d6d879d 100644 --- a/internal/processor/free-d-encode.go +++ b/internal/processor/free-d-encode.go @@ -13,15 +13,19 @@ type FreeDEncode struct { config config.ProcessorConfig } -func (fe *FreeDEncode) Process(ctx context.Context, payload any) (any, error) { +func (fe *FreeDEncode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadPosition, ok := common.GetAnyAs[freeD.FreeDPosition](payload) if !ok { - return nil, errors.New("freed.decode processor only accepts a FreeDEncode") + wrappedPayload.End = true + return wrappedPayload, errors.New("freed.decode processor only accepts a FreeDEncode") } payloadBytes := freeD.Encode(payloadPosition) - return payloadBytes, nil + + wrappedPayload.Payload = payloadBytes + return wrappedPayload, nil } func (fe *FreeDEncode) Type() string { diff --git a/internal/processor/http-request-do.go b/internal/processor/http-request-do.go index 8bb46bf..9ce0c11 100644 --- a/internal/processor/http-request-do.go +++ b/internal/processor/http-request-do.go @@ -9,6 +9,7 @@ import ( "text/template" "time" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -19,15 +20,16 @@ type HTTPRequestDo struct { URL *template.Template } -func (hrd *HTTPRequestDo) Process(ctx context.Context, payload any) (any, error) { +func (hrd *HTTPRequestDo) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { - templateData := GetTemplateData(ctx, payload) + templateData := wrappedPayload var urlBuffer bytes.Buffer err := hrd.URL.Execute(&urlBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } urlString := urlBuffer.String() @@ -36,26 +38,30 @@ func (hrd *HTTPRequestDo) Process(ctx context.Context, payload any) (any, error) request, err := http.NewRequest(hrd.Method, urlString, bytes.NewBuffer([]byte{})) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } response, err := hrd.client.Do(request) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } body, err := io.ReadAll(response.Body) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } //TODO(jwetzell): support headers, etc - return HTTPResponse{ + wrappedPayload.Payload = HTTPResponse{ Status: response.StatusCode, Body: body, - }, nil + } + return wrappedPayload, nil } func (hrd *HTTPRequestDo) Type() string { diff --git a/internal/processor/http-response-create.go b/internal/processor/http-response-create.go index f4a034c..c01c9b9 100644 --- a/internal/processor/http-response-create.go +++ b/internal/processor/http-response-create.go @@ -6,6 +6,7 @@ import ( "fmt" "text/template" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -20,20 +21,21 @@ type HTTPResponse struct { Body []byte } -func (hrc *HTTPResponseCreate) Process(ctx context.Context, payload any) (any, error) { - templateData := GetTemplateData(ctx, payload) +func (hrc *HTTPResponseCreate) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + templateData := wrappedPayload var bodyBuffer bytes.Buffer err := hrc.BodyTmpl.Execute(&bodyBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } - - return HTTPResponse{ + wrappedPayload.Payload = HTTPResponse{ Status: hrc.Status, Body: bodyBuffer.Bytes(), - }, nil + } + return wrappedPayload, nil } func (hrc *HTTPResponseCreate) Type() string { diff --git a/internal/processor/int-parse.go b/internal/processor/int-parse.go index 0370f5f..9c5e865 100644 --- a/internal/processor/int-parse.go +++ b/internal/processor/int-parse.go @@ -16,18 +16,22 @@ type IntParse struct { config config.ProcessorConfig } -func (ip *IntParse) Process(ctx context.Context, payload any) (any, error) { +func (ip *IntParse) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadString, ok := common.GetAnyAs[string](payload) if !ok { - return nil, errors.New("int.parse processor only accepts a string") + wrappedPayload.End = true + return wrappedPayload, errors.New("int.parse processor only accepts a string") } payloadInt, err := strconv.ParseInt(payloadString, ip.Base, ip.BitSize) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } - return payloadInt, nil + wrappedPayload.Payload = payloadInt + return wrappedPayload, nil } func (ip *IntParse) Type() string { diff --git a/internal/processor/int-random.go b/internal/processor/int-random.go index 272f21d..c324c5b 100644 --- a/internal/processor/int-random.go +++ b/internal/processor/int-random.go @@ -6,6 +6,7 @@ import ( "fmt" "math/rand/v2" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -15,9 +16,10 @@ type IntRandom struct { config config.ProcessorConfig } -func (ir *IntRandom) Process(ctx context.Context, payload any) (any, error) { +func (ir *IntRandom) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { payloadInt := rand.IntN(ir.Max-ir.Min+1) + ir.Min - return payloadInt, nil + wrappedPayload.Payload = payloadInt + return wrappedPayload, nil } func (ir *IntRandom) Type() string { diff --git a/internal/processor/int-scale.go b/internal/processor/int-scale.go index d51c79d..1d30ae2 100644 --- a/internal/processor/int-scale.go +++ b/internal/processor/int-scale.go @@ -17,14 +17,17 @@ type IntScale struct { config config.ProcessorConfig } -func (ir *IntScale) Process(ctx context.Context, payload any) (any, error) { +func (ir *IntScale) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadInt, ok := common.GetAnyAs[int](payload) if !ok { - return nil, errors.New("int.scale can only process an int") + wrappedPayload.End = true + return wrappedPayload, errors.New("int.scale can only process an int") } payloadInt = (payloadInt-ir.InMin)*(ir.OutMax-ir.OutMin)/(ir.InMax-ir.InMin) + ir.OutMin - return payloadInt, nil + wrappedPayload.Payload = payloadInt + return wrappedPayload, nil } func (ir *IntScale) Type() string { diff --git a/internal/processor/json-decode.go b/internal/processor/json-decode.go index df5282f..8930537 100644 --- a/internal/processor/json-decode.go +++ b/internal/processor/json-decode.go @@ -13,14 +13,16 @@ type JsonDecode struct { config config.ProcessorConfig } -func (jd *JsonDecode) Process(ctx context.Context, payload any) (any, error) { +func (jd *JsonDecode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadBytes, ok := common.GetAnyAsByteSlice(payload) if !ok { payloadString, ok := common.GetAnyAs[string](payload) if !ok { - return nil, errors.New("json.decode can only process a string or []byte") + wrappedPayload.End = true + return wrappedPayload, errors.New("json.decode can only process a string or []byte") } payloadBytes = []byte(payloadString) } @@ -29,10 +31,12 @@ func (jd *JsonDecode) Process(ctx context.Context, payload any) (any, error) { err := json.Unmarshal(payloadBytes, &payloadJson) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } - return payloadJson, nil + wrappedPayload.Payload = payloadJson + return wrappedPayload, nil } diff --git a/internal/processor/json-encode.go b/internal/processor/json-encode.go index acfd12f..696a5d8 100644 --- a/internal/processor/json-encode.go +++ b/internal/processor/json-encode.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -12,20 +13,23 @@ type JsonEncode struct { config config.ProcessorConfig } -func (je *JsonEncode) Process(ctx context.Context, payload any) (any, error) { +func (je *JsonEncode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload var payloadBuffer bytes.Buffer err := json.NewEncoder(&payloadBuffer).Encode(payload) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } payloadBytes := payloadBuffer.Bytes() payloadBytes = payloadBytes[0 : len(payloadBytes)-1] - return payloadBytes, nil + wrappedPayload.Payload = payloadBytes + return wrappedPayload, nil } func (je *JsonEncode) Type() string { diff --git a/internal/processor/midi-message-create.go b/internal/processor/midi-message-create.go index 9099bef..560a21d 100644 --- a/internal/processor/midi-message-create.go +++ b/internal/processor/midi-message-create.go @@ -9,6 +9,7 @@ import ( "strconv" "text/template" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "gitlab.com/gomidi/midi/v2" ) @@ -16,11 +17,11 @@ import ( // TODO(jwetzell): support using numbers in config file treated as hardcoded values type MIDIMessageCreate struct { config config.ProcessorConfig - ProcessFunc func(ctx context.Context, payload any) (any, error) + ProcessFunc func(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) } -func (mmc *MIDIMessageCreate) Process(ctx context.Context, payload any) (any, error) { - return mmc.ProcessFunc(ctx, payload) +func (mmc *MIDIMessageCreate) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + return mmc.ProcessFunc(ctx, wrappedPayload) } func (mmc *MIDIMessageCreate) Type() string { @@ -64,14 +65,15 @@ func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) { return nil, err } - return &MIDIMessageCreate{config: config, ProcessFunc: func(ctx context.Context, payload any) (any, error) { - templateData := GetTemplateData(ctx, payload) + return &MIDIMessageCreate{config: config, ProcessFunc: func(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + templateData := wrappedPayload var channelBuffer bytes.Buffer err := channelTemplate.Execute(&channelBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } channelValue, err := strconv.ParseUint(channelBuffer.String(), 10, 8) @@ -80,7 +82,8 @@ func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) { err = noteTemplate.Execute(¬eBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } noteValue, err := strconv.ParseUint(noteBuffer.String(), 10, 8) @@ -89,12 +92,14 @@ func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) { err = velocityTemplate.Execute(&velocityBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } velocityValue, err := strconv.ParseUint(velocityBuffer.String(), 10, 8) payloadMessage := midi.NoteOn(uint8(channelValue), uint8(noteValue), uint8(velocityValue)) - return payloadMessage, nil + wrappedPayload.Payload = payloadMessage + return wrappedPayload, nil }}, nil } @@ -135,15 +140,16 @@ func newMidiNoteOffCreate(config config.ProcessorConfig) (Processor, error) { return nil, err } - return &MIDIMessageCreate{config: config, ProcessFunc: func(ctx context.Context, payload any) (any, error) { + return &MIDIMessageCreate{config: config, ProcessFunc: func(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { - templateData := GetTemplateData(ctx, payload) + templateData := wrappedPayload var channelBuffer bytes.Buffer err := channelTemplate.Execute(&channelBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } channelValue, err := strconv.ParseUint(channelBuffer.String(), 10, 8) @@ -152,7 +158,8 @@ func newMidiNoteOffCreate(config config.ProcessorConfig) (Processor, error) { err = noteTemplate.Execute(¬eBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } noteValue, err := strconv.ParseUint(noteBuffer.String(), 10, 8) @@ -161,13 +168,15 @@ func newMidiNoteOffCreate(config config.ProcessorConfig) (Processor, error) { err = velocityTemplate.Execute(&velocityBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } velocityValue, err := strconv.ParseUint(velocityBuffer.String(), 10, 8) payloadMessage := midi.NoteOffVelocity(uint8(channelValue), uint8(noteValue), uint8(velocityValue)) - return payloadMessage, nil + wrappedPayload.Payload = payloadMessage + return wrappedPayload, nil }}, nil } @@ -208,15 +217,16 @@ func newMidiControlChangeCreate(config config.ProcessorConfig) (Processor, error return nil, err } - return &MIDIMessageCreate{config: config, ProcessFunc: func(ctx context.Context, payload any) (any, error) { + return &MIDIMessageCreate{config: config, ProcessFunc: func(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { - templateData := GetTemplateData(ctx, payload) + templateData := wrappedPayload var channelBuffer bytes.Buffer err := channelTemplate.Execute(&channelBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } channelValue, err := strconv.ParseUint(channelBuffer.String(), 10, 8) @@ -225,7 +235,8 @@ func newMidiControlChangeCreate(config config.ProcessorConfig) (Processor, error err = controlTemplate.Execute(&controlBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } controlValue, err := strconv.ParseUint(controlBuffer.String(), 10, 8) @@ -234,13 +245,15 @@ func newMidiControlChangeCreate(config config.ProcessorConfig) (Processor, error err = valueTemplate.Execute(&valueBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } valueValue, err := strconv.ParseUint(valueBuffer.String(), 10, 8) payloadMessage := midi.ControlChange(uint8(channelValue), uint8(controlValue), uint8(valueValue)) - return payloadMessage, nil + wrappedPayload.Payload = payloadMessage + return wrappedPayload, nil }}, nil } @@ -270,14 +283,15 @@ func newMidiProgramChangeCreate(config config.ProcessorConfig) (Processor, error return nil, err } - return &MIDIMessageCreate{config: config, ProcessFunc: func(ctx context.Context, payload any) (any, error) { - templateData := GetTemplateData(ctx, payload) + return &MIDIMessageCreate{config: config, ProcessFunc: func(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + templateData := wrappedPayload var channelBuffer bytes.Buffer err := channelTemplate.Execute(&channelBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } channelValue, err := strconv.ParseUint(channelBuffer.String(), 10, 8) @@ -286,13 +300,15 @@ func newMidiProgramChangeCreate(config config.ProcessorConfig) (Processor, error err = programTemplate.Execute(&programBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } programValue, err := strconv.ParseUint(programBuffer.String(), 10, 8) payloadMessage := midi.ProgramChange(uint8(channelValue), uint8(programValue)) - return payloadMessage, nil + wrappedPayload.Payload = payloadMessage + return wrappedPayload, nil }}, nil } diff --git a/internal/processor/midi-message-decode.go b/internal/processor/midi-message-decode.go index 28e205d..d387fb3 100644 --- a/internal/processor/midi-message-decode.go +++ b/internal/processor/midi-message-decode.go @@ -15,16 +15,19 @@ type MIDIMessageDecode struct { config config.ProcessorConfig } -func (mmd *MIDIMessageDecode) Process(ctx context.Context, payload any) (any, error) { +func (mmd *MIDIMessageDecode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadBytes, ok := common.GetAnyAs[[]byte](payload) if !ok { - return nil, errors.New("midi.message.decode processor only accepts a []byte") + wrappedPayload.End = true + return wrappedPayload, errors.New("midi.message.decode processor only accepts a []byte") } payloadMessage := midi.Message(payloadBytes) - return payloadMessage, nil + wrappedPayload.Payload = payloadMessage + return wrappedPayload, nil } func (mmd *MIDIMessageDecode) Type() string { diff --git a/internal/processor/midi-message-encode.go b/internal/processor/midi-message-encode.go index 2aa257b..95d0203 100644 --- a/internal/processor/midi-message-encode.go +++ b/internal/processor/midi-message-encode.go @@ -15,14 +15,17 @@ type MIDIMessageEncode struct { config config.ProcessorConfig } -func (mme *MIDIMessageEncode) Process(ctx context.Context, payload any) (any, error) { +func (mme *MIDIMessageEncode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadMessage, ok := common.GetAnyAs[midi.Message](payload) if !ok { - return nil, errors.New("midi.message.encode processor only accepts a midi.Message") + wrappedPayload.End = true + return wrappedPayload, errors.New("midi.message.encode processor only accepts a midi.Message") } - return payloadMessage.Bytes(), nil + wrappedPayload.Payload = payloadMessage.Bytes() + return wrappedPayload, nil } func (mme *MIDIMessageEncode) Type() string { diff --git a/internal/processor/midi-message-unpack.go b/internal/processor/midi-message-unpack.go index 2209d77..a844e0b 100644 --- a/internal/processor/midi-message-unpack.go +++ b/internal/processor/midi-message-unpack.go @@ -45,36 +45,44 @@ type MIDIPitchBend struct { Absolute uint16 } -func (mmu *MIDIMessageUnpack) Process(ctx context.Context, payload any) (any, error) { +func (mmu *MIDIMessageUnpack) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadMidi, ok := common.GetAnyAs[midi.Message](payload) if !ok { - return nil, errors.New("midi.message.unpack processor only accepts a midi.Message") + wrappedPayload.End = true + return wrappedPayload, errors.New("midi.message.unpack processor only accepts a midi.Message") } switch payloadMidi.Type() { case midi.NoteOnMsg: noteOnMsg := MIDINoteOn{} payloadMidi.GetNoteOn(¬eOnMsg.Channel, ¬eOnMsg.Note, ¬eOnMsg.Velocity) - return noteOnMsg, nil + wrappedPayload.Payload = noteOnMsg + return wrappedPayload, nil case midi.NoteOffMsg: noteOffMsg := MIDINoteOff{} payloadMidi.GetNoteOff(¬eOffMsg.Channel, ¬eOffMsg.Note, ¬eOffMsg.Velocity) - return noteOffMsg, nil + wrappedPayload.Payload = noteOffMsg + return wrappedPayload, nil case midi.ControlChangeMsg: controlChangeMsg := MIDIControlChange{} payloadMidi.GetControlChange(&controlChangeMsg.Channel, &controlChangeMsg.Control, &controlChangeMsg.Value) - return controlChangeMsg, nil + wrappedPayload.Payload = controlChangeMsg + return wrappedPayload, nil case midi.ProgramChangeMsg: programChangeMsg := MIDIProgramChange{} payloadMidi.GetProgramChange(&programChangeMsg.Channel, &programChangeMsg.Program) - return programChangeMsg, nil + wrappedPayload.Payload = programChangeMsg + return wrappedPayload, nil case midi.PitchBendMsg: pitchBendMsg := MIDIPitchBend{} payloadMidi.GetPitchBend(&pitchBendMsg.Channel, &pitchBendMsg.Relative, &pitchBendMsg.Absolute) - return pitchBendMsg, nil + wrappedPayload.Payload = pitchBendMsg + return wrappedPayload, nil default: - return nil, fmt.Errorf("midi.message.unpack message type not supported %v", payloadMidi.Type()) + wrappedPayload.End = true + return wrappedPayload, fmt.Errorf("midi.message.unpack message type not supported %v", payloadMidi.Type()) } } diff --git a/internal/processor/mqtt-message-create.go b/internal/processor/mqtt-message-create.go index ad06c60..5ebb461 100644 --- a/internal/processor/mqtt-message-create.go +++ b/internal/processor/mqtt-message-create.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -60,17 +61,17 @@ func (mm MQTTMessage) Payload() []byte { func (mm MQTTMessage) Ack() {} -func (mmc *MQTTMessageCreate) Process(ctx context.Context, payload any) (any, error) { +func (mmc *MQTTMessageCreate) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { // TODO(jwetzell): support templating - message := MQTTMessage{ + wrappedPayload.Payload = MQTTMessage{ topic: mmc.Topic, qos: mmc.QoS, retained: mmc.Retained, payload: mmc.Payload, } - return message, nil + return wrappedPayload, nil } func (mmc *MQTTMessageCreate) Type() string { diff --git a/internal/processor/mqtt-message-encode.go b/internal/processor/mqtt-message-encode.go index c1ed7a5..359b79b 100644 --- a/internal/processor/mqtt-message-encode.go +++ b/internal/processor/mqtt-message-encode.go @@ -13,14 +13,16 @@ type MQTTMessageEncode struct { config config.ProcessorConfig } -func (mme *MQTTMessageEncode) Process(ctx context.Context, payload any) (any, error) { +func (mme *MQTTMessageEncode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadMessage, ok := common.GetAnyAs[mqtt.Message](payload) if !ok { - return nil, errors.New("mqtt.message.encode processor only accepts an mqtt.Message") + wrappedPayload.End = true + return wrappedPayload, errors.New("mqtt.message.encode processor only accepts an mqtt.Message") } - - return payloadMessage.Payload(), nil + wrappedPayload.Payload = payloadMessage.Payload() + return wrappedPayload, nil } func (mme *MQTTMessageEncode) Type() string { diff --git a/internal/processor/nats-message-create.go b/internal/processor/nats-message-create.go index 1207103..0296405 100644 --- a/internal/processor/nats-message-create.go +++ b/internal/processor/nats-message-create.go @@ -6,6 +6,7 @@ import ( "fmt" "text/template" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -20,14 +21,16 @@ type NATSMessageCreate struct { Payload *template.Template } -func (nmc *NATSMessageCreate) Process(ctx context.Context, payload any) (any, error) { - templateData := GetTemplateData(ctx, payload) +func (nmc *NATSMessageCreate) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + + templateData := wrappedPayload var payloadBuffer bytes.Buffer err := nmc.Payload.Execute(&payloadBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } payloadString := payloadBuffer.String() @@ -36,17 +39,18 @@ func (nmc *NATSMessageCreate) Process(ctx context.Context, payload any) (any, er err = nmc.Subject.Execute(&subjectBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } subjectString := subjectBuffer.String() - message := NATSMessage{ + wrappedPayload.Payload = NATSMessage{ Subject: subjectString, Payload: []byte(payloadString), } - return message, nil + return wrappedPayload, nil } func (nmc *NATSMessageCreate) Type() string { diff --git a/internal/processor/osc-message-create.go b/internal/processor/osc-message-create.go index 6e62e16..57fb65d 100644 --- a/internal/processor/osc-message-create.go +++ b/internal/processor/osc-message-create.go @@ -10,6 +10,7 @@ import ( "text/template" "github.com/jwetzell/osc-go" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -20,25 +21,28 @@ type OSCMessageCreate struct { Types string } -func (omc *OSCMessageCreate) Process(ctx context.Context, payload any) (any, error) { +func (omc *OSCMessageCreate) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { - templateData := GetTemplateData(ctx, payload) + templateData := wrappedPayload var addressBuffer bytes.Buffer err := omc.Address.Execute(&addressBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } addressString := addressBuffer.String() if len(addressString) == 0 { - return nil, errors.New("osc.message.create address must not be empty") + wrappedPayload.End = true + return wrappedPayload, errors.New("osc.message.create address must not be empty") } if addressString[0] != '/' { - return nil, errors.New("osc.message.create address must start with '/'") + wrappedPayload.End = true + return wrappedPayload, errors.New("osc.message.create address must start with '/'") } payloadMessage := &osc.OSCMessage{ @@ -52,7 +56,8 @@ func (omc *OSCMessageCreate) Process(ctx context.Context, payload any) (any, err err := argTemplate.Execute(&argBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } argString := argBuffer.String() @@ -60,7 +65,8 @@ func (omc *OSCMessageCreate) Process(ctx context.Context, payload any) (any, err typedArg, err := argToTypedArg(argString, omc.Types[argIndex]) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } args = append(args, typedArg) @@ -70,7 +76,8 @@ func (omc *OSCMessageCreate) Process(ctx context.Context, payload any) (any, err payloadMessage.Args = args } - return payloadMessage, nil + wrappedPayload.Payload = payloadMessage + return wrappedPayload, nil } func (omc *OSCMessageCreate) Type() string { diff --git a/internal/processor/osc-message-decode.go b/internal/processor/osc-message-decode.go index c595193..f1cb4e3 100644 --- a/internal/processor/osc-message-decode.go +++ b/internal/processor/osc-message-decode.go @@ -14,26 +14,32 @@ type OSCMessageDecode struct { config config.ProcessorConfig } -func (omd *OSCMessageDecode) Process(ctx context.Context, payload any) (any, error) { +func (omd *OSCMessageDecode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadBytes, ok := common.GetAnyAs[[]byte](payload) if !ok { - return nil, errors.New("osc.message.decode processor only accepts a []byte payload") + wrappedPayload.End = true + return wrappedPayload, errors.New("osc.message.decode processor only accepts a []byte payload") } if len(payloadBytes) == 0 { - return nil, errors.New("osc.message.decode processor can't work on empty []byte") + wrappedPayload.End = true + return wrappedPayload, errors.New("osc.message.decode processor can't work on empty []byte") } if payloadBytes[0] != '/' { - return nil, errors.New("osc.message.decode processor needs an OSC looking []byte") + wrappedPayload.End = true + return wrappedPayload, errors.New("osc.message.decode processor needs an OSC looking []byte") } message, err := osc.MessageFromBytes(payloadBytes) if err != nil { - return nil, fmt.Errorf("osc.message.decode processor failed to decode OSC message: %w", err) + wrappedPayload.End = true + return wrappedPayload, fmt.Errorf("osc.message.decode processor failed to decode OSC message: %w", err) } - return message, nil + wrappedPayload.Payload = message + return wrappedPayload, nil } func (omd *OSCMessageDecode) Type() string { diff --git a/internal/processor/osc-message-encode.go b/internal/processor/osc-message-encode.go index cd1e6ab..1585d8b 100644 --- a/internal/processor/osc-message-encode.go +++ b/internal/processor/osc-message-encode.go @@ -13,15 +13,17 @@ type OSCMessageEncode struct { config config.ProcessorConfig } -func (ome *OSCMessageEncode) Process(ctx context.Context, payload any) (any, error) { +func (ome *OSCMessageEncode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadMessage, ok := common.GetAnyAs[*osc.OSCMessage](payload) if !ok { - return nil, errors.New("osc.message.encode processor only accepts an *OSCMessage") + wrappedPayload.End = true + return wrappedPayload, errors.New("osc.message.encode processor only accepts an *OSCMessage") } - bytes := payloadMessage.ToBytes() - return bytes, nil + wrappedPayload.Payload = payloadMessage.ToBytes() + return wrappedPayload, nil } func (ome *OSCMessageEncode) Type() string { diff --git a/internal/processor/processor.go b/internal/processor/processor.go index ee1f287..0b15a5d 100644 --- a/internal/processor/processor.go +++ b/internal/processor/processor.go @@ -11,7 +11,7 @@ import ( type Processor interface { Type() string - Process(context.Context, any) (any, error) + Process(context.Context, common.WrappedPayload) (common.WrappedPayload, error) } type ProcessorRegistration struct { @@ -41,38 +41,3 @@ var ( processorRegistryMu sync.RWMutex ProcessorRegistry = make(map[string]ProcessorRegistration) ) - -type TemplateData struct { - Payload any - Modules any - Sender any -} - -type EnvData struct { - Payload any - Sender any -} - -func GetTemplateData(ctx context.Context, payload any) TemplateData { - templateData := TemplateData{Payload: payload} - modules := ctx.Value(common.ModulesContextKey) - if modules != nil { - templateData.Modules = modules - } - - sender := ctx.Value(common.SenderContextKey) - if sender != nil { - templateData.Sender = sender - } - 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 -} diff --git a/internal/processor/router-input.go b/internal/processor/router-input.go index fe81cd6..1a86d3d 100644 --- a/internal/processor/router-input.go +++ b/internal/processor/router-input.go @@ -16,20 +16,26 @@ type RouterInput struct { logger *slog.Logger } -func (ro *RouterInput) Process(ctx context.Context, payload any) (any, error) { +func (ro *RouterInput) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO) if !ok { - return nil, errors.New("router.input no router found") + + wrappedPayload.End = true + return wrappedPayload, errors.New("router.input no router found") } _, err := router.HandleInput(ctx, ro.SourceId, payload) if err != nil { - return nil, errors.New("router.input failed to send input") + wrappedPayload.End = true + return wrappedPayload, errors.New("router.input failed to send input") } - return payload, nil + wrappedPayload.Payload = payload + + return wrappedPayload, nil } func (ro *RouterInput) Type() string { diff --git a/internal/processor/router-output.go b/internal/processor/router-output.go index 1e83ff1..3bb4899 100644 --- a/internal/processor/router-output.go +++ b/internal/processor/router-output.go @@ -16,20 +16,22 @@ type RouterOutput struct { logger *slog.Logger } -func (ro *RouterOutput) Process(ctx context.Context, payload any) (any, error) { +func (ro *RouterOutput) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO) if !ok { - return nil, errors.New("router.output no router found") + wrappedPayload.End = true + return wrappedPayload, errors.New("router.output no router found") } - err := router.HandleOutput(ctx, ro.ModuleId, payload) + err := router.HandleOutput(ctx, ro.ModuleId, wrappedPayload.Payload) if err != nil { - return nil, fmt.Errorf("router.output failed to send output: %w", err) + wrappedPayload.End = true + return wrappedPayload, fmt.Errorf("router.output failed to send output: %w", err) } - return payload, nil + return wrappedPayload, nil } func (ro *RouterOutput) Type() string { diff --git a/internal/processor/script-expr.go b/internal/processor/script-expr.go index 0e84b51..183bf6b 100644 --- a/internal/processor/script-expr.go +++ b/internal/processor/script-expr.go @@ -6,6 +6,7 @@ import ( "github.com/expr-lang/expr" "github.com/expr-lang/expr/vm" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -15,16 +16,18 @@ type ScriptExpr struct { Program *vm.Program } -func (se *ScriptExpr) Process(ctx context.Context, payload any) (any, error) { +func (se *ScriptExpr) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { - exprEnv := GetEnvData(ctx, payload) + exprEnv := wrappedPayload output, err := expr.Run(se.Program, exprEnv) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } - return output, nil + wrappedPayload.Payload = output + return wrappedPayload, nil } func (se *ScriptExpr) Type() string { diff --git a/internal/processor/script-js.go b/internal/processor/script-js.go index 2c46bbc..f952212 100644 --- a/internal/processor/script-js.go +++ b/internal/processor/script-js.go @@ -18,42 +18,45 @@ type ScriptJS struct { Program string } -func (sj *ScriptJS) Process(ctx context.Context, payload any) (any, error) { +func (sj *ScriptJS) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { //NOTE(jwetzell): some weird conversion going on with these types - _, isUint8Slice := common.GetAnyAs[[]uint8](payload) - _, isbyteSlice := common.GetAnyAs[[]byte](payload) + _, isUint8Slice := common.GetAnyAs[[]uint8](wrappedPayload.Payload) + _, isbyteSlice := common.GetAnyAs[[]byte](wrappedPayload.Payload) if isUint8Slice || isbyteSlice { - intSlice, ok := common.GetAnyAsIntSlice(payload) + intSlice, ok := common.GetAnyAsIntSlice(wrappedPayload.Payload) if ok { - payload = intSlice + wrappedPayload.Payload = intSlice } } - sj.vm.SetProperty(sj.vm.GlobalObject(), sj.payloadAtom, payload) + sj.vm.SetProperty(sj.vm.GlobalObject(), sj.payloadAtom, wrappedPayload.Payload) - sender := ctx.Value(common.SenderContextKey) - sj.vm.SetProperty(sj.vm.GlobalObject(), sj.senderAtom, sender) + sj.vm.SetProperty(sj.vm.GlobalObject(), sj.senderAtom, wrappedPayload.Sender) _, err := sj.vm.Eval(sj.Program, quickjs.EvalGlobal) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } output, err := sj.vm.GetProperty(sj.vm.GlobalObject(), sj.payloadAtom) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } // NOTE(jwetzell): turn undefined into nil _, ok := output.(quickjs.Undefined) if ok { - return nil, nil + wrappedPayload.End = true + wrappedPayload.Payload = nil + return wrappedPayload, nil } // NOTE(jwetzell): turn object into map[string]interface{} @@ -61,12 +64,13 @@ func (sj *ScriptJS) Process(ctx context.Context, payload any) (any, error) { if ok { var outputMap map[string]interface{} - fmt.Println(outputObject.String()) err := json.Unmarshal([]byte(outputObject.String()), &outputMap) - return outputMap, err + wrappedPayload.Payload = outputMap + return wrappedPayload, err } - return output, nil + wrappedPayload.Payload = output + return wrappedPayload, nil } func (sj *ScriptJS) Type() string { diff --git a/internal/processor/script-wasm.go b/internal/processor/script-wasm.go index 0b0fad8..d87d129 100644 --- a/internal/processor/script-wasm.go +++ b/internal/processor/script-wasm.go @@ -16,27 +16,32 @@ type ScriptWASM struct { Function string } -func (sw *ScriptWASM) Process(ctx context.Context, payload any) (any, error) { +func (sw *ScriptWASM) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadBytes, ok := common.GetAnyAs[[]byte](payload) if !ok { - return nil, fmt.Errorf("script.wasm can only process a byte array") + wrappedPayload.End = true + return wrappedPayload, fmt.Errorf("script.wasm can only process a byte array") } program, err := sw.Program.Instance(ctx, extism.PluginInstanceConfig{}) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } _, output, err := program.Call(sw.Function, payloadBytes) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } + wrappedPayload.Payload = output - return output, nil + return wrappedPayload, nil } func (sw *ScriptWASM) Type() string { diff --git a/internal/processor/sip-response-audio-create.go b/internal/processor/sip-response-audio-create.go index e84e217..ad883de 100644 --- a/internal/processor/sip-response-audio-create.go +++ b/internal/processor/sip-response-audio-create.go @@ -6,6 +6,7 @@ import ( "fmt" "text/template" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -22,24 +23,26 @@ type SipAudioFileResponse struct { AudioFile string } -func (srac *SipResponseAudioCreate) Process(ctx context.Context, payload any) (any, error) { +func (srac *SipResponseAudioCreate) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { - templateData := GetTemplateData(ctx, payload) + templateData := wrappedPayload var audioFileBuffer bytes.Buffer err := srac.AudioFile.Execute(&audioFileBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } audioFileString := audioFileBuffer.String() - return SipAudioFileResponse{ + wrappedPayload.Payload = SipAudioFileResponse{ PreWait: srac.PreWait, PostWait: srac.PostWait, AudioFile: audioFileString, - }, nil + } + return wrappedPayload, nil } func (srac *SipResponseAudioCreate) Type() string { diff --git a/internal/processor/sip-response-dtmf-create.go b/internal/processor/sip-response-dtmf-create.go index e7dddb2..6d63cbc 100644 --- a/internal/processor/sip-response-dtmf-create.go +++ b/internal/processor/sip-response-dtmf-create.go @@ -8,6 +8,7 @@ import ( "regexp" "text/template" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -25,28 +26,31 @@ type SipDTMFResponse struct { Digits string } -func (srdc *SipResponseDTMFCreate) Process(ctx context.Context, payload any) (any, error) { +func (srdc *SipResponseDTMFCreate) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { - templateData := GetTemplateData(ctx, payload) + templateData := wrappedPayload var digitsBuffer bytes.Buffer err := srdc.Digits.Execute(&digitsBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } digitsString := digitsBuffer.String() if !srdc.validDTMF.MatchString(digitsString) { - return nil, errors.New("sip.response.dtmf.create result of digits template contains invalid characters") + wrappedPayload.End = true + return wrappedPayload, errors.New("sip.response.dtmf.create result of digits template contains invalid characters") } - return SipDTMFResponse{ + wrappedPayload.Payload = SipDTMFResponse{ PreWait: srdc.PreWait, PostWait: srdc.PostWait, Digits: digitsString, - }, nil + } + return wrappedPayload, nil } func (srdc *SipResponseDTMFCreate) Type() string { diff --git a/internal/processor/string-create.go b/internal/processor/string-create.go index eef5498..0beddaf 100644 --- a/internal/processor/string-create.go +++ b/internal/processor/string-create.go @@ -6,6 +6,7 @@ import ( "fmt" "text/template" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -14,19 +15,20 @@ type StringCreate struct { Template *template.Template } -func (sc *StringCreate) Process(ctx context.Context, payload any) (any, error) { - templateData := GetTemplateData(ctx, payload) +func (sc *StringCreate) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + templateData := wrappedPayload var templateBuffer bytes.Buffer err := sc.Template.Execute(&templateBuffer, templateData) if err != nil { - return nil, err + wrappedPayload.End = true + return wrappedPayload, err } - payloadString := templateBuffer.String() + wrappedPayload.Payload = templateBuffer.String() - return payloadString, nil + return wrappedPayload, nil } func (sc *StringCreate) Type() string { diff --git a/internal/processor/string-decode.go b/internal/processor/string-decode.go index bd212cc..66827d3 100644 --- a/internal/processor/string-decode.go +++ b/internal/processor/string-decode.go @@ -12,16 +12,19 @@ type StringDecode struct { config config.ProcessorConfig } -func (sd *StringDecode) Process(ctx context.Context, payload any) (any, error) { +func (sd *StringDecode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadBytes, ok := common.GetAnyAs[[]byte](payload) if !ok { - return nil, errors.New("string.decode processor only accepts a []byte") + wrappedPayload.End = true + return wrappedPayload, errors.New("string.decode processor only accepts a []byte") } payloadMessage := string(payloadBytes) - return payloadMessage, nil + wrappedPayload.Payload = payloadMessage + return wrappedPayload, nil } func (sd *StringDecode) Type() string { diff --git a/internal/processor/string-encode.go b/internal/processor/string-encode.go index 91592ca..f9b9a56 100644 --- a/internal/processor/string-encode.go +++ b/internal/processor/string-encode.go @@ -12,16 +12,18 @@ type StringEncode struct { config config.ProcessorConfig } -func (se *StringEncode) Process(ctx context.Context, payload any) (any, error) { +func (se *StringEncode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadString, ok := common.GetAnyAs[string](payload) if !ok { - return nil, errors.New("string.encode processor only accepts a string") + wrappedPayload.End = true + return wrappedPayload, errors.New("string.encode processor only accepts a string") } - payloadBytes := []byte(payloadString) + wrappedPayload.Payload = []byte(payloadString) - return payloadBytes, nil + return wrappedPayload, nil } func (se *StringEncode) Type() string { diff --git a/internal/processor/string-split.go b/internal/processor/string-split.go index 168586c..96da0a7 100644 --- a/internal/processor/string-split.go +++ b/internal/processor/string-split.go @@ -15,16 +15,18 @@ type StringSplit struct { Separator string } -func (ss *StringSplit) Process(ctx context.Context, payload any) (any, error) { +func (ss *StringSplit) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload payloadString, ok := common.GetAnyAs[string](payload) if !ok { - return nil, errors.New("string.split only accepts a string") + wrappedPayload.End = true + return wrappedPayload, errors.New("string.split only accepts a string") } - payloadParts := strings.Split(payloadString, ss.Separator) + wrappedPayload.Payload = strings.Split(payloadString, ss.Separator) - return payloadParts, nil + return wrappedPayload, nil } func (ss *StringSplit) Type() string { diff --git a/internal/processor/struct-field-get.go b/internal/processor/struct-field-get.go index 46fee69..b108d25 100644 --- a/internal/processor/struct-field-get.go +++ b/internal/processor/struct-field-get.go @@ -6,6 +6,7 @@ import ( "fmt" "reflect" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -14,23 +15,27 @@ type StructFieldGet struct { Name string } -func (sf *StructFieldGet) Process(ctx context.Context, payload any) (any, error) { +func (sf *StructFieldGet) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload s := reflect.ValueOf(payload) if s.Kind() != reflect.Struct { if s.Kind() == reflect.Pointer && s.Elem().Kind() == reflect.Struct { s = s.Elem() } else { - return nil, errors.New("struct.field.get processor only accepts a struct payload") + wrappedPayload.End = true + return wrappedPayload, errors.New("struct.field.get processor only accepts a struct payload") } } field := s.FieldByName(sf.Name) if !field.IsValid() { - return nil, fmt.Errorf("struct.field.get field '%s' does not exist", sf.Name) + wrappedPayload.End = true + return wrappedPayload, fmt.Errorf("struct.field.get field '%s' does not exist", sf.Name) } - return field.Interface(), nil + wrappedPayload.Payload = field.Interface() + return wrappedPayload, nil } func (sf *StructFieldGet) Type() string { diff --git a/internal/processor/struct-method-get.go b/internal/processor/struct-method-get.go index 2c42e42..a7be3ba 100644 --- a/internal/processor/struct-method-get.go +++ b/internal/processor/struct-method-get.go @@ -6,6 +6,7 @@ import ( "fmt" "reflect" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -14,30 +15,36 @@ type StructMethodGet struct { Name string } -func (sm *StructMethodGet) Process(ctx context.Context, payload any) (any, error) { +func (sm *StructMethodGet) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + payload := wrappedPayload.Payload s := reflect.ValueOf(payload) if s.Kind() != reflect.Struct { if s.Kind() == reflect.Pointer && s.Elem().Kind() == reflect.Struct { s = s.Elem() } else { - return nil, errors.New("struct.method.get processor only accepts a struct payload") + wrappedPayload.End = true + return wrappedPayload, errors.New("struct.method.get processor only accepts a struct payload") } } method := s.MethodByName(sm.Name) if !method.IsValid() { - return nil, fmt.Errorf("struct.method.get method '%s' does not exist", sm.Name) + wrappedPayload.End = true + return wrappedPayload, fmt.Errorf("struct.method.get method '%s' does not exist", sm.Name) } value := method.Call(nil) if len(value) == 0 { - return nil, nil + wrappedPayload.End = true + wrappedPayload.Payload = nil + return wrappedPayload, nil } if len(value) == 1 { - return value[0].Interface(), nil + wrappedPayload.Payload = value[0].Interface() + return wrappedPayload, nil } results := make([]any, len(value)) @@ -46,7 +53,8 @@ func (sm *StructMethodGet) Process(ctx context.Context, payload any) (any, error results[i] = v.Interface() } - return results, nil + wrappedPayload.Payload = results + return wrappedPayload, nil } func (sm *StructMethodGet) Type() string { diff --git a/internal/processor/test/artnet-packet-decode_test.go b/internal/processor/test/artnet-packet-decode_test.go index 4b73e66..e58b48a 100644 --- a/internal/processor/test/artnet-packet-decode_test.go +++ b/internal/processor/test/artnet-packet-decode_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/jwetzell/artnet-go" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -57,15 +58,15 @@ func TestGoodArtnetPacketDecode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := packetDecoder.Process(t.Context(), test.payload) + got, err := packetDecoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("artnet.packet.decode processing failed: %s", err) } //TODO(jwetzell): work out better way to compare the any/any - if !reflect.DeepEqual(got, test.expected) { - t.Fatalf("artnet.packet.decode got %+v (%T), expected %+v (%T)", got, got, test.expected, test.expected) + if !reflect.DeepEqual(got.Payload, test.expected) { + t.Fatalf("artnet.packet.decode got %+v (%T), expected %+v (%T)", got.Payload, got.Payload, test.expected, test.expected) } }) } @@ -93,7 +94,7 @@ func TestBadArtnetPacketDecode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := packetDecoder.Process(t.Context(), test.payload) + got, err := packetDecoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("artnet.packet.decode expected to fail but succeeded, got: %v", got) diff --git a/internal/processor/test/artnet-packet-encode_test.go b/internal/processor/test/artnet-packet-encode_test.go index 6c40470..eb8a54a 100644 --- a/internal/processor/test/artnet-packet-encode_test.go +++ b/internal/processor/test/artnet-packet-encode_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/jwetzell/artnet-go" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -57,14 +58,14 @@ func TestGoodArtnetPacketEncode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := packetEncoder.Process(t.Context(), test.payload) + got, err := packetEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("artnet.packet.encode processing failed: %s", err) } //TODO(jwetzell): work out better way to compare the any/any - if !reflect.DeepEqual(got, test.expected) { + if !reflect.DeepEqual(got.Payload, test.expected) { t.Fatalf("artnet.packet.encode got %+v (%T), expected %+v (%T)", got, got, test.expected, test.expected) } }) @@ -88,7 +89,7 @@ func TestBadArtnetPacketEncode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := packetEncoder.Process(t.Context(), test.payload) + got, err := packetEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("artnet.packet.encode expected to fail but succeeded, got: %v", got) diff --git a/internal/processor/test/debug-log_test.go b/internal/processor/test/debug-log_test.go index 31ff3da..8eb2d7d 100644 --- a/internal/processor/test/debug-log_test.go +++ b/internal/processor/test/debug-log_test.go @@ -3,6 +3,7 @@ package processor_test import ( "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -28,12 +29,12 @@ func TestDebugLogFromRegistry(t *testing.T) { payload := "test" expected := "test" - got, err := processorInstance.Process(t.Context(), payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("debug.log processing failed: %s", err) } - if got != expected { + if got.Payload != expected { t.Fatalf("debug.log got %+v, expected %+v", got, expected) } } diff --git a/internal/processor/test/filter-expr_test.go b/internal/processor/test/filter-expr_test.go index 906f475..f76036d 100644 --- a/internal/processor/test/filter-expr_test.go +++ b/internal/processor/test/filter-expr_test.go @@ -1,9 +1,9 @@ package processor_test import ( - "reflect" "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -31,10 +31,10 @@ func TestFilterExprFromRegistry(t *testing.T) { func TestGoodFilterExpr(t *testing.T) { tests := []struct { - name string - params map[string]any - payload any - expected any + name string + params map[string]any + payload any + match bool }{ { name: "number", @@ -44,9 +44,7 @@ func TestGoodFilterExpr(t *testing.T) { payload: TestStruct{ Int: 1, }, - expected: TestStruct{ - Int: 1, - }, + match: true, }, { name: "string", @@ -56,9 +54,7 @@ func TestGoodFilterExpr(t *testing.T) { payload: TestStruct{ String: "hello", }, - expected: TestStruct{ - String: "hello", - }, + match: true, }, { name: "not matching", @@ -68,7 +64,7 @@ func TestGoodFilterExpr(t *testing.T) { payload: TestStruct{ Int: 0, }, - expected: nil, + match: false, }, } @@ -88,15 +84,15 @@ func TestGoodFilterExpr(t *testing.T) { t.Fatalf("filter.expr failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("filter.expr processing failed: %s", err) } //TODO(jwetzell): work out better way to compare the any/any - if !reflect.DeepEqual(got, test.expected) { - t.Fatalf("filter.expr got %+v (%T), expected %+v (%T)", got, got, test.expected, test.expected) + if got.End != !test.match { + t.Fatalf("filter.expr did fitler properly %+v (%T), expected %+v (%T)", got, got, test.match, test.match) } }) } @@ -162,7 +158,7 @@ func TestBadFilterExpr(t *testing.T) { } return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("filter.expr expected to fail but succeeded, got: %v", got) diff --git a/internal/processor/test/filter-regex_test.go b/internal/processor/test/filter-regex_test.go index 224f879..6f6f94a 100644 --- a/internal/processor/test/filter-regex_test.go +++ b/internal/processor/test/filter-regex_test.go @@ -1,9 +1,9 @@ package processor_test import ( - "reflect" "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -31,12 +31,12 @@ func TestStringFilterFromRegistry(t *testing.T) { payload := "hello" expected := "hello" - got, err := processorInstance.Process(t.Context(), payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("filter.regex processing failed: %s", err) } - gotString, ok := got.(string) + gotString, ok := got.Payload.(string) if !ok { t.Fatalf("filter.regex should return byte slice") @@ -49,28 +49,28 @@ func TestStringFilterFromRegistry(t *testing.T) { func TestGoodStringFilter(t *testing.T) { tests := []struct { - name string - params map[string]any - payload string - expected any + name string + params map[string]any + payload string + match bool }{ { - name: "matches pattern", - payload: "hello", - params: map[string]any{"pattern": "hello"}, - expected: "hello", + name: "matches pattern", + payload: "hello", + params: map[string]any{"pattern": "hello"}, + match: true, }, { - name: "does not match pattern", - payload: "hello", - params: map[string]any{"pattern": "world"}, - expected: nil, + name: "does not match pattern", + payload: "hello", + params: map[string]any{"pattern": "world"}, + match: false, }, { - name: "basic regex", - payload: "hello world", - params: map[string]any{"pattern": ".* world"}, - expected: "hello world", + name: "basic regex", + payload: "hello world", + params: map[string]any{"pattern": ".* world"}, + match: true, }, } @@ -90,26 +90,14 @@ func TestGoodStringFilter(t *testing.T) { t.Fatalf("filter.regex failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("filter.regex processing failed: %s", err) } - if test.expected == nil { - if got != nil { - t.Fatalf("filter.regex got %+v, expected nil", got) - } - return - } - - gotString, ok := got.(string) - if !ok { - t.Fatalf("filter.regex returned a %T payload: %s", got, got) - } - - if !reflect.DeepEqual(gotString, test.expected) { - t.Fatalf("filter.regex got %+v, expected %+v", gotString, test.expected) + if got.End != !test.match { + t.Fatalf("filter.regex did not filter properly %+v, expected %+v", got, test.match) } }) } @@ -173,10 +161,10 @@ func TestBadStringFilter(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("filter.regex expected to fail but got payload: %s", got) + t.Fatalf("filter.regex expected to fail but got payload: %+v", got) } if err.Error() != test.errorString { diff --git a/internal/processor/test/float-parse_test.go b/internal/processor/test/float-parse_test.go index 087d464..76dd0c2 100644 --- a/internal/processor/test/float-parse_test.go +++ b/internal/processor/test/float-parse_test.go @@ -3,6 +3,7 @@ package processor_test import ( "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -75,14 +76,14 @@ func TestGoodFloatParse(t *testing.T) { t.Fatalf("float.parse failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("float.parse processing failed: %s", err) } - gotFloat, ok := got.(float64) + gotFloat, ok := got.Payload.(float64) if !ok { - t.Fatalf("float.parse returned a %T payload: %s", got, got) + t.Fatalf("float.parse returned a %T payload: %+v", got, got) } if gotFloat != test.expected { t.Fatalf("float.parse got %f, expected %f", gotFloat, test.expected) @@ -151,7 +152,7 @@ func TestBadFloatParse(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("float.parse expected to fail but succeeded, got: %v", got) diff --git a/internal/processor/test/float-random_test.go b/internal/processor/test/float-random_test.go index cbc22d1..4f923b2 100644 --- a/internal/processor/test/float-random_test.go +++ b/internal/processor/test/float-random_test.go @@ -3,6 +3,7 @@ package processor_test import ( "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -70,7 +71,7 @@ func TestGoodFloatRandom(t *testing.T) { t.Fatalf("float.random failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("float.random processing failed: %s", err) } @@ -82,16 +83,16 @@ func TestGoodFloatRandom(t *testing.T) { var gotFloat float64 if bitSize == 32 { - gotFloat32, ok := got.(float32) + gotFloat32, ok := got.Payload.(float32) if !ok { - t.Fatalf("float.random returned a %T payload: %s", got, got) + t.Fatalf("float.random returned a %T payload: %+v", got, got) } gotFloat = float64(gotFloat32) } if bitSize == 64 { - gotFloat64, ok := got.(float64) + gotFloat64, ok := got.Payload.(float64) if !ok { - t.Fatalf("float.random returned a %T payload: %s", got, got) + t.Fatalf("float.random returned a %T payload: %+v", got, got) } gotFloat = gotFloat64 } @@ -171,10 +172,10 @@ func TestBadFloatRandom(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("float.random expected to fail but got payload: %s", got) + t.Fatalf("float.random expected to fail but got payload: %+v", got) } if err.Error() != test.errorString { diff --git a/internal/processor/test/int-parse_test.go b/internal/processor/test/int-parse_test.go index c0e55a5..fac379c 100644 --- a/internal/processor/test/int-parse_test.go +++ b/internal/processor/test/int-parse_test.go @@ -3,6 +3,7 @@ package processor_test import ( "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -96,14 +97,14 @@ func TestGoodIntParse(t *testing.T) { t.Fatalf("int.parse failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("int.parse processing failed: %s", err) } - gotInt, ok := got.(int64) + gotInt, ok := got.Payload.(int64) if !ok { - t.Fatalf("int.parse returned a %T payload: %s", got, got) + t.Fatalf("int.parse returned a %T payload: %+v", got, got) } if gotInt != test.expected { t.Fatalf("int.parse got %d, expected %d", gotInt, test.expected) @@ -185,7 +186,7 @@ func TestBadIntParse(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("int.parse expected to fail but succeeded, got: %v", got) diff --git a/internal/processor/test/int-random_test.go b/internal/processor/test/int-random_test.go index b9a1d1b..0ac570c 100644 --- a/internal/processor/test/int-random_test.go +++ b/internal/processor/test/int-random_test.go @@ -3,6 +3,7 @@ package processor_test import ( "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -50,14 +51,14 @@ func TestIntRandomGoodConfig(t *testing.T) { payload := "12345" - got, err := processorInstance.Process(t.Context(), payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("int.random processing failed: %s", err) } - gotInt, ok := got.(int) + gotInt, ok := got.Payload.(int) if !ok { - t.Fatalf("int.random returned a %T payload: %s", got, got) + t.Fatalf("int.random returned a %T payload: %+v", got, got) } if gotInt < 1 || gotInt > 10 { @@ -97,14 +98,14 @@ func TestGoodIntRandom(t *testing.T) { t.Fatalf("int.random failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("int.random processing failed: %s", err) } - gotInt, ok := got.(int) + gotInt, ok := got.Payload.(int) if !ok { - t.Fatalf("int.random returned a %T payload: %s", got, got) + t.Fatalf("int.random returned a %T payload: %+v", got, got) } minNum, ok := test.params["min"].(int) @@ -182,10 +183,10 @@ func TestBadIntRandom(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("int.random expected to fail but got payload: %s", got) + t.Fatalf("int.random expected to fail but got payload: %+v", got) } if err.Error() != test.errorString { diff --git a/internal/processor/test/int-scale_test.go b/internal/processor/test/int-scale_test.go index 6ab44a6..1a1dbb3 100644 --- a/internal/processor/test/int-scale_test.go +++ b/internal/processor/test/int-scale_test.go @@ -3,6 +3,7 @@ package processor_test import ( "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -68,14 +69,14 @@ func TestGoodIntScale(t *testing.T) { t.Fatalf("int.scale failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("int.scale processing failed: %s", err) } - gotInt, ok := got.(int) + gotInt, ok := got.Payload.(int) if !ok { - t.Fatalf("int.scale returned a %T payload: %s", got, got) + t.Fatalf("int.scale returned a %T payload: %+v", got, got) } if gotInt != test.expected { @@ -156,10 +157,10 @@ func TestBadIntScale(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("int.scale expected to fail but got payload: %s", got) + t.Fatalf("int.scale expected to fail but got payload: %+v", got) } if err.Error() != test.errorString { diff --git a/internal/processor/test/json-decode_test.go b/internal/processor/test/json-decode_test.go index 602b3e5..525c17f 100644 --- a/internal/processor/test/json-decode_test.go +++ b/internal/processor/test/json-decode_test.go @@ -4,6 +4,7 @@ import ( "reflect" "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -31,12 +32,12 @@ func TestJsonDecodeFromRegistry(t *testing.T) { "property": "hello", } - got, err := processorInstance.Process(t.Context(), payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("json.decode processing failed: %s", err) } - gotMap, ok := got.(map[string]any) + gotMap, ok := got.Payload.(map[string]any) if !ok { t.Fatalf("json.decode should return byte slice") @@ -74,18 +75,18 @@ func TestGoodJsonDecode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := jsonDecoder.Process(t.Context(), test.payload) + got, err := jsonDecoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("json.decode processing failed: %s", err) } - gotMap, ok := got.(map[string]any) + gotMap, ok := got.Payload.(map[string]any) if !ok { - t.Fatalf("json.decode returned a %T payload: %s", got, got) + t.Fatalf("json.decode returned a %T payload: %+v", got, got) } if !reflect.DeepEqual(gotMap, test.expected) { - t.Fatalf("json.decode got %x, expected %s", got, test.expected) + t.Fatalf("json.decode got %+v, expected %s", got, test.expected) } }) } @@ -112,7 +113,7 @@ func TestBadJsonDecode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := stringEncoder.Process(t.Context(), test.payload) + got, err := stringEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("json.decode expected to fail but got payload: %+v", got) diff --git a/internal/processor/test/json-encode_test.go b/internal/processor/test/json-encode_test.go index ed7c48f..a74b87a 100644 --- a/internal/processor/test/json-encode_test.go +++ b/internal/processor/test/json-encode_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/jwetzell/osc-go" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -34,15 +35,15 @@ func TestJsonEncodeFromRegistry(t *testing.T) { expected := []byte("{\"property\":\"hello\"}") - got, err := processorInstance.Process(t.Context(), payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("json.encode processing failed: %s", err) } - gotBytes, ok := got.([]byte) + gotBytes, ok := got.Payload.([]byte) if !ok { - t.Fatalf("json.encode should return byte slice") + t.Fatalf("json.encode should return byte slice got %T: %+v", got.Payload, got.Payload) } if !slices.Equal(gotBytes, expected) { @@ -68,18 +69,18 @@ func TestGoodJsonEncode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := jsonEncoder.Process(t.Context(), test.payload) + got, err := jsonEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("json.encode processing failed: %s", err) } - gotBytes, ok := got.([]byte) + gotBytes, ok := got.Payload.([]byte) if !ok { - t.Fatalf("json.encode returned a %T payload: %s", got, got) + t.Fatalf("json.encode returned a %T payload: %+v", got.Payload, got.Payload) } if !slices.Equal(gotBytes, test.expected) { - t.Fatalf("json.encode got %x, expected %s", got, test.expected) + t.Fatalf("json.encode got %+v, expected %s", got, test.expected) } }) } @@ -101,7 +102,7 @@ func TestBadJsonEncode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := stringEncoder.Process(t.Context(), test.payload) + got, err := stringEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("json.encode expected to fail but got payload: %+v", got) diff --git a/internal/processor/test/midi-message-create_test.go b/internal/processor/test/midi-message-create_test.go index a3b6c9f..808b8a2 100644 --- a/internal/processor/test/midi-message-create_test.go +++ b/internal/processor/test/midi-message-create_test.go @@ -4,6 +4,7 @@ import ( "reflect" "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "gitlab.com/gomidi/midi/v2" @@ -103,14 +104,14 @@ func TestGoodMIDIMessageCreate(t *testing.T) { t.Fatalf("midi.message.create failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("midi.message.create processing failed: %s", err) } - gotMessage, ok := got.(midi.Message) + gotMessage, ok := got.Payload.(midi.Message) if !ok { - t.Fatalf("midi.message.create returned a %T payload: %s", got, got) + t.Fatalf("midi.message.create returned a %T payload: %+v", got, got) } if !reflect.DeepEqual(gotMessage, test.expected) { @@ -279,7 +280,7 @@ func TestBadMIDIMessageCreate(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("midi.message.create expected to fail but succeeded, got: %v", got) diff --git a/internal/processor/test/midi-message-decode_test.go b/internal/processor/test/midi-message-decode_test.go index 9cfc0b1..a2c8fdf 100644 --- a/internal/processor/test/midi-message-decode_test.go +++ b/internal/processor/test/midi-message-decode_test.go @@ -4,6 +4,7 @@ import ( "reflect" "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "gitlab.com/gomidi/midi/v2" @@ -44,14 +45,14 @@ func TestGoodMIDIMessageDecode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("midi.message.decode processing failed: %s", err) } - gotMessage, ok := got.(midi.Message) + gotMessage, ok := got.Payload.(midi.Message) if !ok { - t.Fatalf("midi.message.decode returned a %T payload: %s", got, got) + t.Fatalf("midi.message.decode returned a %T payload: %+v", got, got) } if !reflect.DeepEqual(gotMessage, test.expected) { @@ -78,7 +79,7 @@ func TestBadMIDIMessageDecode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("midi.message.decode expected to fail but succeeded, got: %v", got) diff --git a/internal/processor/test/midi-message-encode_test.go b/internal/processor/test/midi-message-encode_test.go index e42ede8..e5326fe 100644 --- a/internal/processor/test/midi-message-encode_test.go +++ b/internal/processor/test/midi-message-encode_test.go @@ -4,6 +4,7 @@ import ( "slices" "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "gitlab.com/gomidi/midi/v2" @@ -44,14 +45,14 @@ func TestGoodMIDIMessageEncode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := midiMessageEncoder.Process(t.Context(), test.payload) + got, err := midiMessageEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("midi.message.encode processing failed: %s", err) } - gotBytes, ok := got.([]byte) + gotBytes, ok := got.Payload.([]byte) if !ok { - t.Fatalf("midi.message.encode returned a %T payload: %s", got, got) + t.Fatalf("midi.message.encode returned a %T payload: %+v", got, got) } if !slices.Equal(gotBytes, test.expected) { @@ -77,10 +78,10 @@ func TestBadMIDIMessageEncode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := midiMessageEncoder.Process(t.Context(), test.payload) + got, err := midiMessageEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("midi.message.encode expected to fail but got payload: %s", got) + t.Fatalf("midi.message.encode expected to fail but got payload: %+v", got) } if err.Error() != test.errorString { t.Fatalf("midi.message.encode got error '%s', expected '%s'", err.Error(), test.errorString) diff --git a/internal/processor/test/mqtt-message-create_test.go b/internal/processor/test/mqtt-message-create_test.go index 351efa8..f767d70 100644 --- a/internal/processor/test/mqtt-message-create_test.go +++ b/internal/processor/test/mqtt-message-create_test.go @@ -5,6 +5,7 @@ import ( "testing" mqtt "github.com/eclipse/paho.mqtt.golang" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -103,22 +104,22 @@ func TestGoodMQTTMessageCreate(t *testing.T) { t.Fatalf("mqtt.message.create failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("mqtt.message.create processing failed: %s", err) } if test.expected == nil { - if got != nil { + if got.Payload != nil { t.Fatalf("mqtt.message.create got %+v, expected nil", got) } return } - gotMessage, ok := got.(mqtt.Message) + gotMessage, ok := got.Payload.(mqtt.Message) if !ok { - t.Fatalf("mqtt.message.create returned a %T payload: %s", got, got) + t.Fatalf("mqtt.message.create returned a %T payload: %+v", got, got) } if !reflect.DeepEqual(gotMessage, test.expected) { @@ -227,7 +228,7 @@ func TestBadMQTTMessageCreate(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("mqtt.message.create expected to fail but succeeded, got: %v", got) diff --git a/internal/processor/test/mqtt-message-encode_test.go b/internal/processor/test/mqtt-message-encode_test.go index ae34778..03286c9 100644 --- a/internal/processor/test/mqtt-message-encode_test.go +++ b/internal/processor/test/mqtt-message-encode_test.go @@ -5,6 +5,7 @@ import ( "testing" mqtt "github.com/eclipse/paho.mqtt.golang" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -44,18 +45,18 @@ func TestGoodMQTTMessageEncode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := stringEncoder.Process(t.Context(), test.payload) + got, err := stringEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("mqtt.message.encode processing failed: %s", err) } - gotBytes, ok := got.([]byte) + gotBytes, ok := got.Payload.([]byte) if !ok { - t.Fatalf("mqtt.message.encode returned a %T payload: %s", got, got) + t.Fatalf("mqtt.message.encode returned a %T payload: %+v", got, got) } if !slices.Equal(gotBytes, test.expected) { - t.Fatalf("mqtt.message.encode got %s, expected %s", got, test.expected) + t.Fatalf("mqtt.message.encode got %+v, expected %s", got, test.expected) } }) } @@ -77,10 +78,10 @@ func TestBadMQTTMessageEncode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := stringEncoder.Process(t.Context(), test.payload) + got, err := stringEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("mqtt.message.encode expected to fail but got payload: %s", got) + t.Fatalf("mqtt.message.encode expected to fail but got payload: %+v", got) } if err.Error() != test.errorString { t.Fatalf("mqtt.message.encode got error '%s', expected '%s'", err.Error(), test.errorString) diff --git a/internal/processor/test/osc-message-create_test.go b/internal/processor/test/osc-message-create_test.go index 6f09ce2..c92b412 100644 --- a/internal/processor/test/osc-message-create_test.go +++ b/internal/processor/test/osc-message-create_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/jwetzell/osc-go" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -149,22 +150,22 @@ func TestGoodOSCMessageCreate(t *testing.T) { t.Fatalf("osc.message.create failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("osc.message.create processing failed: %s", err) } if test.expected == nil { - if got != nil { + if got.Payload != nil { t.Fatalf("osc.message.create got %+v, expected nil", got) } return } - gotMessage, ok := got.(*osc.OSCMessage) + gotMessage, ok := got.Payload.(*osc.OSCMessage) if !ok { - t.Fatalf("osc.message.create returned a %T payload: %s", got, got) + t.Fatalf("osc.message.create returned a %T payload: %+v", got, got) } if !reflect.DeepEqual(gotMessage, test.expected) { @@ -286,7 +287,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 processor.TemplateData", + errorString: "template: address:1:8: executing \"address\" at <.missing>: can't evaluate field missing in type common.WrappedPayload", }, { name: "address doesn't start with slash", @@ -304,7 +305,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 processor.TemplateData", + errorString: "template: arg:1:2: executing \"arg\" at <.missing>: can't evaluate field missing in type common.WrappedPayload", }, } @@ -327,7 +328,7 @@ func TestBadOSCMessageCreate(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("osc.message.create expected to fail but succeeded, got: %v", got) diff --git a/internal/processor/test/osc-message-decode_test.go b/internal/processor/test/osc-message-decode_test.go index 8848772..d3b4331 100644 --- a/internal/processor/test/osc-message-decode_test.go +++ b/internal/processor/test/osc-message-decode_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/jwetzell/osc-go" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -60,15 +61,15 @@ func TestGoodOSCMessageDecode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("osc.message.decode processing failed: %s", err) } - gotMessage, ok := got.(*osc.OSCMessage) + gotMessage, ok := got.Payload.(*osc.OSCMessage) if !ok { - t.Fatalf("osc.message.decode returned a %T payload: %s", got, got) + t.Fatalf("osc.message.decode returned a %T payload: %+v", got, got) } if !reflect.DeepEqual(gotMessage, test.expected) { @@ -109,10 +110,10 @@ func TestBadOSCMessageDecode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("osc.message.decode expected to fail but got payload: %s", got) + t.Fatalf("osc.message.decode expected to fail but got payload: %+v", got) } if err.Error() != test.errorString { t.Fatalf("osc.message.decode got error '%s', expected '%s'", err.Error(), test.errorString) diff --git a/internal/processor/test/osc-message-encode_test.go b/internal/processor/test/osc-message-encode_test.go index 24419ab..ae2ad65 100644 --- a/internal/processor/test/osc-message-encode_test.go +++ b/internal/processor/test/osc-message-encode_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/jwetzell/osc-go" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -59,14 +60,14 @@ func TestGoodOSCMessageEncode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("osc.message.encode processing failed: %s", err) } - gotBytes, ok := got.([]byte) + gotBytes, ok := got.Payload.([]byte) if !ok { - t.Fatalf("osc.message.encode returned a %T payload: %s", got, got) + t.Fatalf("osc.message.encode returned a %T payload: %+v", got, got) } if !slices.Equal(gotBytes, test.expected) { @@ -92,10 +93,10 @@ func TestBadOSCMessageEncode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("osc.message.encode expected to fail but got payload: %s", got) + t.Fatalf("osc.message.encode expected to fail but got payload: %+v", got) } if err.Error() != test.errorString { t.Fatalf("osc.message.encode got error '%s', expected '%s'", err.Error(), test.errorString) diff --git a/internal/processor/test/processor_test.go b/internal/processor/test/processor_test.go index ee4a0c4..c77125e 100644 --- a/internal/processor/test/processor_test.go +++ b/internal/processor/test/processor_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -44,8 +45,8 @@ type TestProcessor struct { func (p *TestProcessor) Type() string { return "test" } -func (p *TestProcessor) Process(ctx context.Context, input any) (any, error) { - return input, nil +func (p *TestProcessor) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { + return wrappedPayload, nil } func TestProcessorBadRegistrationNoType(t *testing.T) { diff --git a/internal/processor/test/script-expr_test.go b/internal/processor/test/script-expr_test.go index a87fa9a..c70d705 100644 --- a/internal/processor/test/script-expr_test.go +++ b/internal/processor/test/script-expr_test.go @@ -3,6 +3,7 @@ package processor_test import ( "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -75,14 +76,14 @@ func TestGoodScriptExpr(t *testing.T) { t.Fatalf("script.expr failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("script.expr processing failed: %s", err) } //TODO(jwetzell): work out better way to compare the any/any - if got != test.expected { + if got.Payload != test.expected { t.Fatalf("script.expr got %+v (%T), expected %+v (%T)", got, got, test.expected, test.expected) } }) @@ -133,7 +134,7 @@ func TestBadScriptExpr(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("script.expr expected to fail but succeeded, got: %v", got) diff --git a/internal/processor/test/script-js_test.go b/internal/processor/test/script-js_test.go index f0759b1..19d0737 100644 --- a/internal/processor/test/script-js_test.go +++ b/internal/processor/test/script-js_test.go @@ -4,6 +4,7 @@ import ( "maps" "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -33,12 +34,12 @@ func TestScriptJSFromRegistry(t *testing.T) { payload := 1 expected := 2 - got, err := processorInstance.Process(t.Context(), payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("script.js processing failed: %s", err) } - if got != expected { + if got.Payload != expected { t.Fatalf("script.js got %+v, expected %+v", got, expected) } } @@ -142,7 +143,7 @@ func TestGoodScriptJS(t *testing.T) { t.Fatalf("script.js failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("script.js processing failed: %s", err) @@ -150,7 +151,7 @@ func TestGoodScriptJS(t *testing.T) { //TODO(jwetzell): work out better way to compare the any/any - gotMap, ok := got.(map[string]interface{}) + gotMap, ok := got.Payload.(map[string]interface{}) if ok { // got a map expectedMap, ok := test.expected.(map[string]interface{}) @@ -162,8 +163,8 @@ func TestGoodScriptJS(t *testing.T) { t.Fatalf("script.js got %+v, expected %+v", got, test.expected) } } else { - if got != test.expected { - t.Fatalf("script.js got %+v, expected %+v", got, test.expected) + if got.Payload != test.expected { + t.Fatalf("script.js got %+v, expected %+v", got.Payload, test.expected) } } }) @@ -201,7 +202,7 @@ func TestBadScriptJS(t *testing.T) { Params: test.params, }) - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("script.js expected to fail but succeeded, got: %v", got) diff --git a/internal/processor/test/script-wasm_test.go b/internal/processor/test/script-wasm_test.go index b5aa685..1b84229 100644 --- a/internal/processor/test/script-wasm_test.go +++ b/internal/processor/test/script-wasm_test.go @@ -4,6 +4,7 @@ import ( "slices" "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -74,15 +75,15 @@ func TestGoodScriptWASM(t *testing.T) { t.Fatalf("script.wasm failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("script.wasm processing failed: %s", err) } - gotBytes, ok := got.([]byte) + gotBytes, ok := got.Payload.([]byte) if !ok { - t.Fatalf("script.wasm returned a %T payload: %s", got, got) + t.Fatalf("script.wasm returned a %T payload: %+v", got, got) } if !slices.Equal(gotBytes, test.expected) { @@ -180,7 +181,7 @@ func TestBadScriptWASM(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("script.wasm expected to fail but succeeded, got: %v", got) diff --git a/internal/processor/test/string-create_test.go b/internal/processor/test/string-create_test.go index d060b52..9fbf824 100644 --- a/internal/processor/test/string-create_test.go +++ b/internal/processor/test/string-create_test.go @@ -3,6 +3,7 @@ package processor_test import ( "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -30,12 +31,12 @@ func TestStringCreateFromRegistry(t *testing.T) { payload := "hello" expected := "hello" - got, err := processorInstance.Process(t.Context(), payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("string.create processing failed: %s", err) } - if got != expected { + if got.Payload != expected { t.Fatalf("string.create got %+v, expected %+v", got, expected) } } @@ -96,18 +97,18 @@ func TestGoodStringCreate(t *testing.T) { t.Fatalf("string.create failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("string.create processing failed: %s", err) } - gotStrings, ok := got.(string) + gotStrings, ok := got.Payload.(string) if !ok { - t.Fatalf("string.create returned a %T payload: %s", got, got) + t.Fatalf("string.create returned a %T payload: %+v", got, got) } if gotStrings != test.expected { - t.Fatalf("string.create got %s, expected %s", got, test.expected) + t.Fatalf("string.create got %+v, expected %s", got, test.expected) } }) } @@ -148,7 +149,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 processor.TemplateData", + errorString: "template: template:1:2: executing \"template\" at <.Invalid>: can't evaluate field Invalid in type common.WrappedPayload", }, } @@ -172,10 +173,10 @@ func TestBadStringCreate(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("string.create expected to fail but got payload: %s", got) + t.Fatalf("string.create expected to fail but got payload: %+v", got) } if err.Error() != test.errorString { diff --git a/internal/processor/test/string-decode_test.go b/internal/processor/test/string-decode_test.go index 9b9e112..0a1c6fa 100644 --- a/internal/processor/test/string-decode_test.go +++ b/internal/processor/test/string-decode_test.go @@ -3,6 +3,7 @@ package processor_test import ( "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -27,12 +28,12 @@ func TestStringDecodeFromRegistry(t *testing.T) { payload := []byte{'h', 'e', 'l', 'l', 'o'} expected := "hello" - got, err := processorInstance.Process(t.Context(), payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("string.decode processing failed: %s", err) } - if got != expected { + if got.Payload != expected { t.Fatalf("string.decode got %+v, expected %+v", got, expected) } } @@ -53,18 +54,18 @@ func TestGoodStringDecode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := stringDecoder.Process(t.Context(), test.payload) + got, err := stringDecoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("string.decode processing failed: %s", err) } - gotString, ok := got.(string) + gotString, ok := got.Payload.(string) if !ok { - t.Fatalf("string.decode returned a %T payload: %s", got, got) + t.Fatalf("string.decode returned a %T payload: %+v", got, got) } if gotString != test.expected { - t.Fatalf("string.decode got %s, expected %s", got, test.expected) + t.Fatalf("string.decode got %+v, expected %s", got, test.expected) } }) } @@ -86,10 +87,10 @@ func TestBadStringDecode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := stringDecoder.Process(t.Context(), test.payload) + got, err := stringDecoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("string.decode expected to fail but got payload: %s", got) + t.Fatalf("string.decode expected to fail but got payload: %+v", got) } if err.Error() != test.errorString { t.Fatalf("string.decode got error '%s', expected '%s'", err.Error(), test.errorString) diff --git a/internal/processor/test/string-encode_test.go b/internal/processor/test/string-encode_test.go index 8b34198..cec7d86 100644 --- a/internal/processor/test/string-encode_test.go +++ b/internal/processor/test/string-encode_test.go @@ -4,6 +4,7 @@ import ( "slices" "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -28,12 +29,12 @@ func TestStringEncodeFromRegistry(t *testing.T) { payload := "hello" expected := []byte{'h', 'e', 'l', 'l', 'o'} - got, err := processorInstance.Process(t.Context(), payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("string.encode processing failed: %s", err) } - gotBytes, ok := got.([]byte) + gotBytes, ok := got.Payload.([]byte) if !ok { t.Fatalf("string.encode should return byte slice") @@ -60,14 +61,14 @@ func TestGoodStringEncode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := stringEncoder.Process(t.Context(), test.payload) + got, err := stringEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("string.encode processing failed: %s", err) } - gotBytes, ok := got.([]byte) + gotBytes, ok := got.Payload.([]byte) if !ok { - t.Fatalf("string.encode returned a %T payload: %s", got, got) + t.Fatalf("string.encode returned a %T payload: %+v", got, got) } if !slices.Equal(gotBytes, test.expected) { t.Fatalf("string.encode got %+v, expected %+v", gotBytes, test.expected) @@ -92,10 +93,10 @@ func TestBadStringEncode(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := stringEncoder.Process(t.Context(), test.payload) + got, err := stringEncoder.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("string.encode expected to fail but got payload: %s", got) + t.Fatalf("string.encode expected to fail but got payload: %+v", got) } if err.Error() != test.errorString { t.Fatalf("string.encode got error '%s', expected '%s'", err.Error(), test.errorString) diff --git a/internal/processor/test/string-split_test.go b/internal/processor/test/string-split_test.go index 381d4c8..d2a9246 100644 --- a/internal/processor/test/string-split_test.go +++ b/internal/processor/test/string-split_test.go @@ -4,6 +4,7 @@ import ( "slices" "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -31,12 +32,12 @@ func TestStringSplitFromRegistry(t *testing.T) { payload := "part1,part2,part3" expected := []string{"part1", "part2", "part3"} - got, err := processorInstance.Process(t.Context(), payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("string.split processing failed: %s", err) } - gotStrings, ok := got.([]string) + gotStrings, ok := got.Payload.([]string) if !ok { t.Fatalf("string.split should return a slice of strings") @@ -78,14 +79,14 @@ func TestGoodStringSplit(t *testing.T) { t.Fatalf("string.split failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("string.split processing failed: %s", err) } - gotStrings, ok := got.([]string) + gotStrings, ok := got.Payload.([]string) if !ok { - t.Fatalf("string.split returned a %T payload: %s", got, got) + t.Fatalf("string.split returned a %T payload: %+v", got, got) } if !slices.Equal(gotStrings, test.expected) { @@ -141,10 +142,10 @@ func TestBadStringSplit(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("string.split expected error but got none, payload: %s", got) + t.Fatalf("string.split expected error but got none, payload: %+v", got) } if err.Error() != test.errorString { t.Fatalf("string.split got error '%s', expected '%s'", err.Error(), test.errorString) diff --git a/internal/processor/test/struct-field-get_test.go b/internal/processor/test/struct-field-get_test.go index 5361ba6..1a691d1 100644 --- a/internal/processor/test/struct-field-get_test.go +++ b/internal/processor/test/struct-field-get_test.go @@ -3,6 +3,7 @@ package processor_test import ( "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -30,12 +31,12 @@ func TestStructFieldGetFromRegistry(t *testing.T) { payload := TestStruct{Data: "hello"} expected := "hello" - got, err := processorInstance.Process(t.Context(), payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("struct.field.get processing failed: %s", err) } - if got != expected { + if got.Payload != expected { t.Fatalf("struct.field.get got %+v, expected %+v", got, expected) } } @@ -96,14 +97,14 @@ func TestGoodStructFieldGet(t *testing.T) { t.Fatalf("struct.field.get failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("struct.field.get processing failed: %s", err) } - if got != test.expected { - t.Fatalf("struct.field.get got %s, expected %s", got, test.expected) + if got.Payload != test.expected { + t.Fatalf("struct.field.get got %+v, expected %s", got, test.expected) } }) } @@ -168,10 +169,10 @@ func TestBadStructFieldGet(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("struct.field.get expected to fail but got payload: %s", got) + t.Fatalf("struct.field.get expected to fail but got payload: %+v", got) } if err.Error() != test.errorString { diff --git a/internal/processor/test/struct-method-get_test.go b/internal/processor/test/struct-method-get_test.go index 153ec70..32afaaa 100644 --- a/internal/processor/test/struct-method-get_test.go +++ b/internal/processor/test/struct-method-get_test.go @@ -4,6 +4,7 @@ import ( "reflect" "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -31,12 +32,12 @@ func TestStructMethodGetFromRegistry(t *testing.T) { payload := TestStruct{Data: "hello"} expected := "hello" - got, err := processorInstance.Process(t.Context(), payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), payload)) if err != nil { t.Fatalf("struct.method.get processing failed: %s", err) } - if got != expected { + if got.Payload != expected { t.Fatalf("struct.method.get got %+v, expected %+v", got, expected) } } @@ -115,14 +116,14 @@ func TestGoodStructMethodGet(t *testing.T) { t.Fatalf("struct.method.get failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("struct.method.get processing failed: %s", err) } - if !reflect.DeepEqual(got, test.expected) { - t.Fatalf("struct.method.get got %+v, expected %+v", got, test.expected) + if !reflect.DeepEqual(got.Payload, test.expected) { + t.Fatalf("struct.method.get got payload: %+v, expected %+v", got.Payload, test.expected) } }) } @@ -187,10 +188,10 @@ func TestBadStructMethodGet(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { - t.Fatalf("struct.method.get expected to fail but got payload: %s", got) + t.Fatalf("struct.method.get expected to fail but got payload: %+v", got) } if err.Error() != test.errorString { diff --git a/internal/processor/test/time-sleep_test.go b/internal/processor/test/time-sleep_test.go index 49f1ee9..6fa75d3 100644 --- a/internal/processor/test/time-sleep_test.go +++ b/internal/processor/test/time-sleep_test.go @@ -3,6 +3,7 @@ package processor_test import ( "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" ) @@ -58,13 +59,13 @@ func TestGoodTimeSleep(t *testing.T) { t.Fatalf("time.sleep failed to create processor: %s", err) } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err != nil { t.Fatalf("time.sleep processing failed: %s", err) } - if got != test.payload { + if got.Payload != test.payload { t.Fatalf("time.sleep got %+v, expected %+v", got, test.payload) } }) @@ -113,7 +114,7 @@ func TestBadTimeSleep(t *testing.T) { return } - got, err := processorInstance.Process(t.Context(), test.payload) + got, err := processorInstance.Process(t.Context(), common.GetWrappedPayload(t.Context(), test.payload)) if err == nil { t.Fatalf("time.sleep expected to fail but succeeded, got: %v", got) diff --git a/internal/processor/time-sleep.go b/internal/processor/time-sleep.go index fd7bb18..4157c5f 100644 --- a/internal/processor/time-sleep.go +++ b/internal/processor/time-sleep.go @@ -5,6 +5,7 @@ import ( "fmt" "time" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) @@ -13,9 +14,9 @@ type TimeSleep struct { Duration time.Duration } -func (ts *TimeSleep) Process(ctx context.Context, payload any) (any, error) { +func (ts *TimeSleep) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { time.Sleep(ts.Duration) - return payload, nil + return wrappedPayload, nil } func (ts *TimeSleep) Type() string { diff --git a/internal/route/route.go b/internal/route/route.go index 680a1f4..5d26ded 100644 --- a/internal/route/route.go +++ b/internal/route/route.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "go.opentelemetry.io/otel" @@ -48,12 +49,13 @@ func (r *Route) Input() string { } func (r *Route) ProcessPayload(ctx context.Context, payload any) (any, error) { + wrappedPayload := common.GetWrappedPayload(ctx, payload) tracer := otel.Tracer("route") processCtx, processSpan := tracer.Start(ctx, "ProcessPayload", trace.WithAttributes(attribute.String("payload.type", fmt.Sprintf("%T", payload)))) defer processSpan.End() for processorIndex, processor := range r.processors { processorCtx, processorSpan := otel.Tracer("processor").Start(processCtx, "process", trace.WithAttributes(attribute.Int("processor.index", processorIndex), attribute.String("processor.type", processor.Type()))) - processedPayload, err := processor.Process(processorCtx, payload) + processedPayload, err := processor.Process(processorCtx, wrappedPayload) if err != nil { processorSpan.SetStatus(codes.Error, "route processor error") processorSpan.RecordError(err) @@ -63,16 +65,16 @@ func (r *Route) ProcessPayload(ctx context.Context, payload any) (any, error) { return nil, err } processorSpan.SetStatus(codes.Ok, "processor successful") - //NOTE(jwetzell) nil payload will result in the route being "terminated" - if processedPayload == nil { - processSpan.SetStatus(codes.Ok, "route processing terminated early due to nil payload") + //NOTE(jwetzell) payload has been marked as an end + if processedPayload.End { + processSpan.SetStatus(codes.Ok, "route processing terminated early due to processor signal") processorSpan.End() - return nil, nil + return processedPayload.Payload, nil } - payload = processedPayload + wrappedPayload = processedPayload processorSpan.End() } processSpan.SetStatus(codes.Ok, "route processing successful") - return payload, nil + return wrappedPayload.Payload, nil } diff --git a/internal/route/route_test.go b/internal/route/route_test.go index 93bf253..7705f89 100644 --- a/internal/route/route_test.go +++ b/internal/route/route_test.go @@ -120,7 +120,7 @@ func TestRouteHandleNilPayload(t *testing.T) { t.Fatalf("route ProcessPayload returned error: %v", err) } if payload != nil { - t.Fatalf("route returned the wrong payload") + t.Fatalf("route returned the wrong payload: expected nil got %+v (%T)", payload, payload) } } @@ -143,14 +143,10 @@ func TestRouteHandleNilPayloadFromProcessor(t *testing.T) { t.Fatalf("route failed to create: %v", err) } - payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), common.RouterContextKey, &MockRouter{}), "test") + _, err = testRoute.ProcessPayload(context.WithValue(t.Context(), common.RouterContextKey, &MockRouter{}), "test") if err != nil { t.Fatalf("route HandleOutput returned error for nil payload: %v", err) } - - if payload != nil { - t.Fatalf("route returned the wrong payload") - } } func TestRouteUnknownProcessor(t *testing.T) {