mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 12:55:29 +00:00
convert Route to interface
This commit is contained in:
36
route.go
36
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)
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user