mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
stuff values into context
This commit is contained in:
@@ -2,12 +2,18 @@ package route
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
type routeContextKey string
|
||||
|
||||
var RouterContextKey routeContextKey = routeContextKey("router")
|
||||
var SourceContextKey routeContextKey = routeContextKey("source")
|
||||
|
||||
type RouteError struct {
|
||||
Index int
|
||||
Config config.RouteConfig
|
||||
@@ -21,13 +27,13 @@ type RouteIOError struct {
|
||||
|
||||
type RouteIO interface {
|
||||
HandleInput(sourceId string, payload any) []RouteIOError
|
||||
HandleOutput(sourceId string, destinationId string, payload any) error
|
||||
HandleOutput(ctx context.Context, destinationId string, payload any) error
|
||||
}
|
||||
|
||||
type Route interface {
|
||||
Input() string
|
||||
Output() string
|
||||
HandleInput(ctx context.Context, sourceId string, payload any, router RouteIO) error
|
||||
HandleInput(ctx context.Context, payload any) error
|
||||
}
|
||||
|
||||
type ProcessorRoute struct {
|
||||
@@ -65,17 +71,24 @@ func (r *ProcessorRoute) Output() string {
|
||||
return r.output
|
||||
}
|
||||
|
||||
func (r *ProcessorRoute) HandleInput(ctx context.Context, sourceId string, payload any, router RouteIO) error {
|
||||
var err error
|
||||
func (r *ProcessorRoute) HandleInput(ctx context.Context, payload any) error {
|
||||
router, ok := ctx.Value(RouterContextKey).(RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("unable to get router from context")
|
||||
}
|
||||
|
||||
for _, processor := range r.processors {
|
||||
payload, err = processor.Process(ctx, payload)
|
||||
processedPayload, err := processor.Process(ctx, payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//NOTE(jwetzell) nil payload will result in the route being "terminated"
|
||||
if payload == nil {
|
||||
if processedPayload == nil {
|
||||
return nil
|
||||
}
|
||||
payload = processedPayload
|
||||
}
|
||||
return router.HandleOutput(sourceId, r.output, payload)
|
||||
|
||||
return router.HandleOutput(ctx, r.output, payload)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package route_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -51,7 +52,7 @@ func TestGoodRouteHandleInput(t *testing.T) {
|
||||
}
|
||||
|
||||
inputData := "test input data"
|
||||
err = testRoute.HandleInput(t.Context(), "input", inputData, &MockRouter{})
|
||||
err = testRoute.HandleInput(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), inputData)
|
||||
if err != nil {
|
||||
t.Fatalf("route HandleOutput returned error: %v", err)
|
||||
}
|
||||
@@ -72,7 +73,7 @@ func TestRouteHandleInputWithProcessorError(t *testing.T) {
|
||||
}
|
||||
|
||||
inputData := "test input data"
|
||||
err = testRoute.HandleInput(t.Context(), "input", inputData, &MockRouter{})
|
||||
err = testRoute.HandleInput(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), inputData)
|
||||
if err == nil {
|
||||
t.Fatalf("route HandleOutput did not return error for bad processor")
|
||||
}
|
||||
@@ -91,7 +92,7 @@ func TestRouteHandleNilPayload(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
err = testRoute.HandleInput(t.Context(), "input", nil, &MockRouter{})
|
||||
err = testRoute.HandleInput(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("route HandleOutput returned error for nil payload: %v", err)
|
||||
}
|
||||
@@ -111,7 +112,7 @@ func TestRouteHandleNilPayloadFromProcessor(t *testing.T) {
|
||||
t.Fatalf("route failed to create: %v", err)
|
||||
}
|
||||
|
||||
err = testRoute.HandleInput(t.Context(), "input", nil, &MockRouter{})
|
||||
err = testRoute.HandleInput(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("route HandleOutput returned error for nil payload: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user