From 7a01ce66916847615ec9799a93246b01e4faa0a4 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sat, 6 Dec 2025 23:50:44 -0600 Subject: [PATCH] convert Route to interface --- route.go | 36 +++++++++++++++++++++++++----------- router.go | 8 ++++---- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/route.go b/route.go index f2f11cc..47660f2 100644 --- a/route.go +++ b/route.go @@ -13,14 +13,28 @@ type RouteError struct { Error error } -type Route struct { - index int - Input string - Processors []processing.Processor - Output string +type Route interface { + Input() string + Output() string + HandleInput(sourceId string, payload any, router *Router) error + HandleOutput(sourceId string, payload any, router *Router) error } -func NewRoute(index int, config config.RouteConfig) (*Route, error) { +type ProcessorRoute struct { + input string + processors []processing.Processor + output string +} + +func (r *ProcessorRoute) Input() string { + return r.input +} + +func (r *ProcessorRoute) Output() string { + return r.output +} + +func NewRoute(config config.RouteConfig) (Route, error) { processors := []processing.Processor{} if len(config.Processors) > 0 { @@ -38,12 +52,12 @@ func NewRoute(index int, config config.RouteConfig) (*Route, error) { } } - return &Route{Input: config.Input, Processors: processors, Output: config.Output, index: index}, nil + return &ProcessorRoute{input: config.Input, processors: processors, output: config.Output}, nil } -func (r *Route) HandleInput(sourceId string, payload any, router *Router) error { +func (r *ProcessorRoute) HandleInput(sourceId string, payload any, router *Router) error { var err error - for _, processor := range r.Processors { + for _, processor := range r.processors { payload, err = processor.Process(router.Context, payload) if err != nil { return err @@ -56,6 +70,6 @@ func (r *Route) HandleInput(sourceId string, payload any, router *Router) error return r.HandleOutput(sourceId, payload, router) } -func (r *Route) HandleOutput(sourceId string, payload any, router *Router) error { - return router.HandleOutput(sourceId, r.Output, payload) +func (r *ProcessorRoute) HandleOutput(sourceId string, payload any, router *Router) error { + return router.HandleOutput(sourceId, r.output, payload) } diff --git a/router.go b/router.go index 8f488d8..04c8e01 100644 --- a/router.go +++ b/router.go @@ -19,7 +19,7 @@ type Router struct { contextCancel context.CancelFunc Context context.Context ModuleInstances []Module - RouteInstances []*Route + RouteInstances []Route moduleWait sync.WaitGroup } @@ -38,7 +38,7 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []ModuleErro Context: routerContext, contextCancel: cancel, ModuleInstances: []Module{}, - RouteInstances: []*Route{}, + RouteInstances: []Route{}, } var moduleErrors []ModuleError @@ -95,7 +95,7 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []ModuleErro var routeErrors []RouteError for routeIndex, routeDecl := range config.Routes { - route, err := NewRoute(routeIndex, routeDecl) + route, err := NewRoute(routeDecl) if err != nil { if routeErrors == nil { routeErrors = []RouteError{} @@ -137,7 +137,7 @@ func (r *Router) Stop() { func (r *Router) HandleInput(sourceId string, payload any) []RoutingError { var routingErrors []RoutingError for routeIndex, route := range r.RouteInstances { - if route.Input == sourceId { + if route.Input() == sourceId { err := route.HandleInput(sourceId, payload, r) if err != nil { if routingErrors == nil {