mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +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
|
Error error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Route struct {
|
type Route interface {
|
||||||
index int
|
Input() string
|
||||||
Input string
|
Output() string
|
||||||
Processors []processing.Processor
|
HandleInput(sourceId string, payload any, router *Router) error
|
||||||
Output string
|
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{}
|
processors := []processing.Processor{}
|
||||||
|
|
||||||
if len(config.Processors) > 0 {
|
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
|
var err error
|
||||||
for _, processor := range r.Processors {
|
for _, processor := range r.processors {
|
||||||
payload, err = processor.Process(router.Context, payload)
|
payload, err = processor.Process(router.Context, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -56,6 +70,6 @@ func (r *Route) HandleInput(sourceId string, payload any, router *Router) error
|
|||||||
return r.HandleOutput(sourceId, payload, router)
|
return r.HandleOutput(sourceId, payload, router)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) HandleOutput(sourceId string, payload any, router *Router) error {
|
func (r *ProcessorRoute) HandleOutput(sourceId string, payload any, router *Router) error {
|
||||||
return router.HandleOutput(sourceId, r.Output, payload)
|
return router.HandleOutput(sourceId, r.output, payload)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ type Router struct {
|
|||||||
contextCancel context.CancelFunc
|
contextCancel context.CancelFunc
|
||||||
Context context.Context
|
Context context.Context
|
||||||
ModuleInstances []Module
|
ModuleInstances []Module
|
||||||
RouteInstances []*Route
|
RouteInstances []Route
|
||||||
moduleWait sync.WaitGroup
|
moduleWait sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []ModuleErro
|
|||||||
Context: routerContext,
|
Context: routerContext,
|
||||||
contextCancel: cancel,
|
contextCancel: cancel,
|
||||||
ModuleInstances: []Module{},
|
ModuleInstances: []Module{},
|
||||||
RouteInstances: []*Route{},
|
RouteInstances: []Route{},
|
||||||
}
|
}
|
||||||
|
|
||||||
var moduleErrors []ModuleError
|
var moduleErrors []ModuleError
|
||||||
@@ -95,7 +95,7 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []ModuleErro
|
|||||||
|
|
||||||
var routeErrors []RouteError
|
var routeErrors []RouteError
|
||||||
for routeIndex, routeDecl := range config.Routes {
|
for routeIndex, routeDecl := range config.Routes {
|
||||||
route, err := NewRoute(routeIndex, routeDecl)
|
route, err := NewRoute(routeDecl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if routeErrors == nil {
|
if routeErrors == nil {
|
||||||
routeErrors = []RouteError{}
|
routeErrors = []RouteError{}
|
||||||
@@ -137,7 +137,7 @@ func (r *Router) Stop() {
|
|||||||
func (r *Router) HandleInput(sourceId string, payload any) []RoutingError {
|
func (r *Router) HandleInput(sourceId string, payload any) []RoutingError {
|
||||||
var routingErrors []RoutingError
|
var routingErrors []RoutingError
|
||||||
for routeIndex, route := range r.RouteInstances {
|
for routeIndex, route := range r.RouteInstances {
|
||||||
if route.Input == sourceId {
|
if route.Input() == sourceId {
|
||||||
err := route.HandleInput(sourceId, payload, r)
|
err := route.HandleInput(sourceId, payload, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if routingErrors == nil {
|
if routingErrors == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user