diff --git a/internal/module/http-client.go b/internal/module/http-client.go index e043383..fc38932 100644 --- a/internal/module/http-client.go +++ b/internal/module/http-client.go @@ -8,6 +8,7 @@ import ( "time" "github.com/jwetzell/showbridge-go/internal/config" + "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" ) @@ -61,7 +62,7 @@ func (hc *HTTPClient) Start(ctx context.Context) error { func (hc *HTTPClient) Output(ctx context.Context, payload any) error { - payloadRequest, ok := payload.(*http.Request) + payloadRequest, ok := processor.GetAnyAs[*http.Request](payload) if !ok { return errors.New("http.client is only able to output an http.Request") diff --git a/internal/module/http-server.go b/internal/module/http-server.go index f20746d..85b5a2b 100644 --- a/internal/module/http-server.go +++ b/internal/module/http-server.go @@ -182,7 +182,7 @@ func (hs *HTTPServer) Output(ctx context.Context, payload any) error { return errors.New("http.server output must originate from an http.server input") } - payloadResponse, ok := payload.(processor.HTTPResponse) + payloadResponse, ok := processor.GetAnyAs[processor.HTTPResponse](payload) if !ok { return errors.New("http.server is only able to output HTTPResponse") diff --git a/internal/module/midi-output.go b/internal/module/midi-output.go index d92d632..fbecb50 100644 --- a/internal/module/midi-output.go +++ b/internal/module/midi-output.go @@ -9,6 +9,7 @@ import ( "log/slog" "github.com/jwetzell/showbridge-go/internal/config" + "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" "gitlab.com/gomidi/midi/v2" _ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv" @@ -84,7 +85,7 @@ func (mo *MIDIOutput) Output(ctx context.Context, payload any) error { return errors.New("midi.output output is not setup") } - payloadMessage, ok := payload.(midi.Message) + payloadMessage, ok := processor.GetAnyAs[midi.Message](payload) if !ok { return errors.New("midi.output can only ouptut midi.Message") diff --git a/internal/module/mqtt-client.go b/internal/module/mqtt-client.go index 32fc23c..560237f 100644 --- a/internal/module/mqtt-client.go +++ b/internal/module/mqtt-client.go @@ -8,6 +8,7 @@ import ( mqtt "github.com/eclipse/paho.mqtt.golang" "github.com/jwetzell/showbridge-go/internal/config" + "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" ) @@ -101,7 +102,7 @@ func (mc *MQTTClient) Start(ctx context.Context) error { } func (mc *MQTTClient) Output(ctx context.Context, payload any) error { - payloadMessage, ok := payload.(mqtt.Message) + payloadMessage, ok := processor.GetAnyAs[mqtt.Message](payload) if !ok { return errors.New("mqtt.client is only able to output a MQTTMessage") diff --git a/internal/module/nats-client.go b/internal/module/nats-client.go index e393cd5..d488a15 100644 --- a/internal/module/nats-client.go +++ b/internal/module/nats-client.go @@ -94,7 +94,7 @@ func (nc *NATSClient) Start(ctx context.Context) error { func (nc *NATSClient) Output(ctx context.Context, payload any) error { - payloadMessage, ok := payload.(processor.NATSMessage) + payloadMessage, ok := processor.GetAnyAs[processor.NATSMessage](payload) if !ok { return errors.New("nats.client is only able to output NATSMessage") diff --git a/internal/module/serial-client.go b/internal/module/serial-client.go index 048ea00..5cb38dc 100644 --- a/internal/module/serial-client.go +++ b/internal/module/serial-client.go @@ -11,6 +11,7 @@ import ( "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/framer" + "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" "go.bug.st/serial" ) @@ -156,7 +157,7 @@ func (sc *SerialClient) Start(ctx context.Context) error { func (sc *SerialClient) Output(ctx context.Context, payload any) error { - payloadBytes, ok := payload.([]byte) + payloadBytes, ok := processor.GetAnyAs[[]byte](payload) if !ok { return errors.New("serial.client can only ouptut bytes") diff --git a/internal/module/sip-call-server.go b/internal/module/sip-call-server.go index 64798bd..9dd616f 100644 --- a/internal/module/sip-call-server.go +++ b/internal/module/sip-call-server.go @@ -173,7 +173,7 @@ func (scs *SIPCallServer) Output(ctx context.Context, payload any) error { return errors.New("sip.call.server inDialog already ended") } - payloadDTMFResponse, ok := payload.(processor.SipDTMFResponse) + payloadDTMFResponse, ok := processor.GetAnyAs[processor.SipDTMFResponse](payload) if ok { dtmfWriter := call.inDialog.AudioWriterDTMF() @@ -189,7 +189,7 @@ func (scs *SIPCallServer) Output(ctx context.Context, payload any) error { return nil } - payloadAudioFileResponse, ok := payload.(processor.SipAudioFileResponse) + payloadAudioFileResponse, ok := processor.GetAnyAs[processor.SipAudioFileResponse](payload) if ok { audioFile, err := os.Open(payloadAudioFileResponse.AudioFile) diff --git a/internal/module/sip-dtmf-server.go b/internal/module/sip-dtmf-server.go index 1cceede..eab9ec7 100644 --- a/internal/module/sip-dtmf-server.go +++ b/internal/module/sip-dtmf-server.go @@ -199,7 +199,7 @@ func (sds *SIPDTMFServer) Output(ctx context.Context, payload any) error { return errors.New("sip.dtmf.server inDialog already ended") } - payloadDTMFResponse, ok := payload.(processor.SipDTMFResponse) + payloadDTMFResponse, ok := processor.GetAnyAs[processor.SipDTMFResponse](payload) if ok { dtmfWriter := call.inDialog.AudioWriterDTMF() @@ -216,7 +216,7 @@ func (sds *SIPDTMFServer) Output(ctx context.Context, payload any) error { return nil } - payloadAudioFileResponse, ok := payload.(processor.SipAudioFileResponse) + payloadAudioFileResponse, ok := processor.GetAnyAs[processor.SipAudioFileResponse](payload) if ok { audioFile, err := os.Open(payloadAudioFileResponse.AudioFile) diff --git a/internal/module/tcp-client.go b/internal/module/tcp-client.go index 58fa4af..6c7fccd 100644 --- a/internal/module/tcp-client.go +++ b/internal/module/tcp-client.go @@ -10,6 +10,7 @@ import ( "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/framer" + "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" ) @@ -152,7 +153,7 @@ func (tc *TCPClient) Output(ctx context.Context, payload any) error { return err } } - payloadBytes, ok := payload.([]byte) + payloadBytes, ok := processor.GetAnyAs[[]byte](payload) if !ok { return errors.New("net.tcp.client is only able to output bytes") } diff --git a/internal/module/tcp-server.go b/internal/module/tcp-server.go index a025499..36b4603 100644 --- a/internal/module/tcp-server.go +++ b/internal/module/tcp-server.go @@ -13,6 +13,7 @@ import ( "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/framer" + "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" ) @@ -200,7 +201,7 @@ AcceptLoop: } func (ts *TCPServer) Output(ctx context.Context, payload any) error { - payloadBytes, ok := payload.([]byte) + payloadBytes, ok := processor.GetAnyAs[[]byte](payload) if !ok { return errors.New("net.tcp.server is only able to output bytes") diff --git a/internal/module/udp-client.go b/internal/module/udp-client.go index f40aa87..9866a55 100644 --- a/internal/module/udp-client.go +++ b/internal/module/udp-client.go @@ -8,6 +8,7 @@ import ( "net" "github.com/jwetzell/showbridge-go/internal/config" + "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" ) @@ -87,7 +88,7 @@ func (uc *UDPClient) Start(ctx context.Context) error { func (uc *UDPClient) Output(ctx context.Context, payload any) error { - payloadBytes, ok := payload.([]byte) + payloadBytes, ok := processor.GetAnyAs[[]byte](payload) if !ok { return errors.New("net.udp.client is only able to output bytes") } diff --git a/internal/module/udp-multicast.go b/internal/module/udp-multicast.go index ddeaba7..a4262d0 100644 --- a/internal/module/udp-multicast.go +++ b/internal/module/udp-multicast.go @@ -9,6 +9,7 @@ import ( "time" "github.com/jwetzell/showbridge-go/internal/config" + "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" ) @@ -108,7 +109,7 @@ func (um *UDPMulticast) Start(ctx context.Context) error { func (um *UDPMulticast) Output(ctx context.Context, payload any) error { - payloadBytes, ok := payload.([]byte) + payloadBytes, ok := processor.GetAnyAs[[]byte](payload) if !ok { return errors.New("net.udp.multicast can only output bytes") } diff --git a/internal/processor/artnet-packet-decode.go b/internal/processor/artnet-packet-decode.go index 9bda0d8..ff525b8 100644 --- a/internal/processor/artnet-packet-decode.go +++ b/internal/processor/artnet-packet-decode.go @@ -13,7 +13,7 @@ type ArtNetPacketDecode struct { } func (apd *ArtNetPacketDecode) Process(ctx context.Context, payload any) (any, error) { - payloadBytes, ok := payload.([]byte) + payloadBytes, ok := GetAnyAs[[]byte](payload) if !ok { return nil, fmt.Errorf("artnet.packet.decode processor only accepts a []byte") diff --git a/internal/processor/artnet-packet-encode.go b/internal/processor/artnet-packet-encode.go index b820909..ebc11e9 100644 --- a/internal/processor/artnet-packet-encode.go +++ b/internal/processor/artnet-packet-encode.go @@ -13,7 +13,7 @@ type ArtNetPacketEncode struct { } func (ape *ArtNetPacketEncode) Process(ctx context.Context, payload any) (any, error) { - payloadPacket, ok := payload.(artnet.ArtNetPacket) + payloadPacket, ok := GetAnyAs[artnet.ArtNetPacket](payload) if !ok { return nil, fmt.Errorf("artnet.packet.encode processor only accepts an ArtNetPacket") diff --git a/internal/processor/artnet-packet-filter.go b/internal/processor/artnet-packet-filter.go index b77a4d7..e318b3a 100644 --- a/internal/processor/artnet-packet-filter.go +++ b/internal/processor/artnet-packet-filter.go @@ -14,7 +14,7 @@ type ArtNetPacketFilter struct { } func (apf *ArtNetPacketFilter) Process(ctx context.Context, payload any) (any, error) { - payloadPacket, ok := payload.(artnet.ArtNetPacket) + payloadPacket, ok := GetAnyAs[artnet.ArtNetPacket](payload) if !ok { return nil, fmt.Errorf("artnet.packet.filter processor only accepts an ArtNetPacket") diff --git a/internal/processor/float-parse.go b/internal/processor/float-parse.go index d7dd6ea..5542bf4 100644 --- a/internal/processor/float-parse.go +++ b/internal/processor/float-parse.go @@ -15,7 +15,7 @@ type FloatParse struct { } func (fp *FloatParse) Process(ctx context.Context, payload any) (any, error) { - payloadString, ok := payload.(string) + payloadString, ok := GetAnyAs[string](payload) if !ok { return nil, errors.New("float.parse processor only accepts a string") diff --git a/internal/processor/free-d-decode.go b/internal/processor/free-d-decode.go index 3329b01..6b169cf 100644 --- a/internal/processor/free-d-decode.go +++ b/internal/processor/free-d-decode.go @@ -13,7 +13,7 @@ type FreeDDecode struct { } func (fdd *FreeDDecode) Process(ctx context.Context, payload any) (any, error) { - payloadBytes, ok := payload.([]byte) + payloadBytes, ok := GetAnyAs[[]byte](payload) if !ok { return nil, errors.New("freed.decode processor only accepts a []byte") diff --git a/internal/processor/free-d-encode.go b/internal/processor/free-d-encode.go index e6e903c..e7efbbd 100644 --- a/internal/processor/free-d-encode.go +++ b/internal/processor/free-d-encode.go @@ -13,7 +13,7 @@ type FreeDEncode struct { } func (fde *FreeDEncode) Process(ctx context.Context, payload any) (any, error) { - payloadPosition, ok := payload.(freeD.FreeDPosition) + payloadPosition, ok := GetAnyAs[freeD.FreeDPosition](payload) if !ok { return nil, errors.New("freed.decode processor only accepts a FreeDEncode") diff --git a/internal/processor/http-request-filter.go b/internal/processor/http-request-filter.go index 9732a0d..c1ca8ba 100644 --- a/internal/processor/http-request-filter.go +++ b/internal/processor/http-request-filter.go @@ -18,7 +18,7 @@ type HTTPRequestFilter struct { func (hrf *HTTPRequestFilter) Process(ctx context.Context, payload any) (any, error) { - payloadRequest, ok := payload.(*http.Request) + payloadRequest, ok := GetAnyAs[*http.Request](payload) if !ok { return nil, errors.New("http.request.filter can only operate on http.Request payloads") diff --git a/internal/processor/int-parse.go b/internal/processor/int-parse.go index 63cd931..53093ad 100644 --- a/internal/processor/int-parse.go +++ b/internal/processor/int-parse.go @@ -16,7 +16,7 @@ type IntParse struct { } func (ip *IntParse) Process(ctx context.Context, payload any) (any, error) { - payloadString, ok := payload.(string) + payloadString, ok := GetAnyAs[string](payload) if !ok { return nil, errors.New("int.parse processor only accepts a string") diff --git a/internal/processor/json-decode.go b/internal/processor/json-decode.go index 7b9520f..406acf1 100644 --- a/internal/processor/json-decode.go +++ b/internal/processor/json-decode.go @@ -13,7 +13,7 @@ type JsonDecode struct { } func (jd *JsonDecode) Process(ctx context.Context, payload any) (any, error) { - payloadString, ok := payload.(string) + payloadString, ok := GetAnyAs[string](payload) if !ok { return nil, errors.New("json.decode processor only accepts a string") diff --git a/internal/processor/midi-message-decode.go b/internal/processor/midi-message-decode.go index a2601dd..96aa5a2 100644 --- a/internal/processor/midi-message-decode.go +++ b/internal/processor/midi-message-decode.go @@ -15,7 +15,7 @@ type MIDIMessageDecode struct { } func (mmd *MIDIMessageDecode) Process(ctx context.Context, payload any) (any, error) { - payloadBytes, ok := payload.([]byte) + payloadBytes, ok := GetAnyAs[[]byte](payload) if !ok { return nil, errors.New("midi.message.decode processor only accepts a []byte") diff --git a/internal/processor/midi-message-encode.go b/internal/processor/midi-message-encode.go index 8cf344d..a3694af 100644 --- a/internal/processor/midi-message-encode.go +++ b/internal/processor/midi-message-encode.go @@ -15,7 +15,7 @@ type MIDIMessageEncode struct { } func (mme *MIDIMessageEncode) Process(ctx context.Context, payload any) (any, error) { - payloadMessage, ok := payload.(midi.Message) + payloadMessage, ok := GetAnyAs[midi.Message](payload) if !ok { return nil, errors.New("midi.message.encode processor only accepts a midi.Message") diff --git a/internal/processor/midi-message-filter.go b/internal/processor/midi-message-filter.go index 1e17e78..6b3037f 100644 --- a/internal/processor/midi-message-filter.go +++ b/internal/processor/midi-message-filter.go @@ -17,7 +17,7 @@ type MIDIMessageFilter struct { } func (mmf *MIDIMessageFilter) Process(ctx context.Context, payload any) (any, error) { - payloadMessage, ok := payload.(midi.Message) + payloadMessage, ok := GetAnyAs[midi.Message](payload) if !ok { return nil, errors.New("midi.message.filter processor only accepts a midi.Message") diff --git a/internal/processor/midi-message-unpack.go b/internal/processor/midi-message-unpack.go index d863dd2..8853d57 100644 --- a/internal/processor/midi-message-unpack.go +++ b/internal/processor/midi-message-unpack.go @@ -45,7 +45,7 @@ type MIDIPitchBend struct { } func (mmu *MIDIMessageUnpack) Process(ctx context.Context, payload any) (any, error) { - payloadMidi, ok := payload.(midi.Message) + payloadMidi, ok := GetAnyAs[midi.Message](payload) if !ok { return nil, errors.New("midi.message.unpack processor only accepts a midi.Message") diff --git a/internal/processor/mqtt-message-create.go b/internal/processor/mqtt-message-create.go index 713f512..4affb97 100644 --- a/internal/processor/mqtt-message-create.go +++ b/internal/processor/mqtt-message-create.go @@ -104,11 +104,11 @@ func init() { return nil, errors.New("mqtt.message.create payload error: not found") } - if payloadBytes, ok := payload.([]byte); ok { + if payloadBytes, ok := GetAnyAs[[]byte](payload); ok { return &MQTTMessageCreate{config: config, Topic: topicString, QoS: byte(qosByte), Retained: retainedBool, Payload: payloadBytes}, nil } - payloadString, ok := payload.(string) + payloadString, ok := GetAnyAs[string](payload) if !ok { return nil, errors.New("mqtt.message.create payload error: not a string or byte array") diff --git a/internal/processor/mqtt-message-encode.go b/internal/processor/mqtt-message-encode.go index eb6236f..9975bbb 100644 --- a/internal/processor/mqtt-message-encode.go +++ b/internal/processor/mqtt-message-encode.go @@ -13,7 +13,7 @@ type MQTTMessageEncode struct { } func (mme *MQTTMessageEncode) Process(ctx context.Context, payload any) (any, error) { - payloadMessage, ok := payload.(mqtt.Message) + payloadMessage, ok := GetAnyAs[mqtt.Message](payload) if !ok { return nil, errors.New("mqtt.message.encode processor only accepts an mqtt.Message") diff --git a/internal/processor/osc-message-decode.go b/internal/processor/osc-message-decode.go index d1332e0..ef35e46 100644 --- a/internal/processor/osc-message-decode.go +++ b/internal/processor/osc-message-decode.go @@ -13,7 +13,7 @@ type OSCMessageDecode struct { } func (omd *OSCMessageDecode) Process(ctx context.Context, payload any) (any, error) { - payloadBytes, ok := payload.([]byte) + payloadBytes, ok := GetAnyAs[[]byte](payload) if !ok { return nil, errors.New("osc.message.decode processor only accepts a []byte payload") diff --git a/internal/processor/osc-message-encode.go b/internal/processor/osc-message-encode.go index c2c6275..3a35143 100644 --- a/internal/processor/osc-message-encode.go +++ b/internal/processor/osc-message-encode.go @@ -13,7 +13,7 @@ type OSCMessageEncode struct { } func (ome *OSCMessageEncode) Process(ctx context.Context, payload any) (any, error) { - payloadMessage, ok := payload.(osc.OSCMessage) + payloadMessage, ok := GetAnyAs[osc.OSCMessage](payload) if !ok { return nil, errors.New("osc.message.encode processor only accepts an OSCMessage") diff --git a/internal/processor/osc-message-filter.go b/internal/processor/osc-message-filter.go index c31c685..912afac 100644 --- a/internal/processor/osc-message-filter.go +++ b/internal/processor/osc-message-filter.go @@ -18,7 +18,7 @@ type OSCMessageFilter struct { func (omf *OSCMessageFilter) Process(ctx context.Context, payload any) (any, error) { - payloadMessage, ok := payload.(osc.OSCMessage) + payloadMessage, ok := GetAnyAs[osc.OSCMessage](payload) if !ok { return nil, errors.New("osc.message.filter can only operate on OSCMessage payloads") diff --git a/internal/processor/processor.go b/internal/processor/processor.go index 026b481..dd837dd 100644 --- a/internal/processor/processor.go +++ b/internal/processor/processor.go @@ -40,3 +40,8 @@ var ( processorRegistryMu sync.RWMutex ProcessorRegistry = make(map[string]ProcessorRegistration) ) + +func GetAnyAs[T any](p any) (T, bool) { + typed, ok := p.(T) + return typed, ok +} diff --git a/internal/processor/script-wasm.go b/internal/processor/script-wasm.go index 8859809..829fbfd 100644 --- a/internal/processor/script-wasm.go +++ b/internal/processor/script-wasm.go @@ -17,7 +17,7 @@ type ScriptWASM struct { func (se *ScriptWASM) Process(ctx context.Context, payload any) (any, error) { - payloadBytes, ok := payload.([]byte) + payloadBytes, ok := GetAnyAs[[]byte](payload) if !ok { return nil, fmt.Errorf("script.wasm can only operator on byte array") diff --git a/internal/processor/string-decode.go b/internal/processor/string-decode.go index b69038a..433affc 100644 --- a/internal/processor/string-decode.go +++ b/internal/processor/string-decode.go @@ -12,7 +12,7 @@ type StringDecode struct { } func (sd *StringDecode) Process(ctx context.Context, payload any) (any, error) { - payloadBytes, ok := payload.([]byte) + payloadBytes, ok := GetAnyAs[[]byte](payload) if !ok { return nil, errors.New("string.decode processor only accepts a []byte") diff --git a/internal/processor/string-encode.go b/internal/processor/string-encode.go index 98e82ae..4a62ccf 100644 --- a/internal/processor/string-encode.go +++ b/internal/processor/string-encode.go @@ -12,7 +12,7 @@ type StringEncode struct { } func (se *StringEncode) Process(ctx context.Context, payload any) (any, error) { - payloadString, ok := payload.(string) + payloadString, ok := GetAnyAs[string](payload) if !ok { return nil, errors.New("string.encode processor only accepts a string") diff --git a/internal/processor/string-filter.go b/internal/processor/string-filter.go index e595561..03e3df8 100644 --- a/internal/processor/string-filter.go +++ b/internal/processor/string-filter.go @@ -15,7 +15,7 @@ type StringFilter struct { } func (sf *StringFilter) Process(ctx context.Context, payload any) (any, error) { - payloadString, ok := payload.(string) + payloadString, ok := GetAnyAs[string](payload) if !ok { return nil, errors.New("string.filter processor only accepts a string") diff --git a/internal/processor/string-split.go b/internal/processor/string-split.go index 5e5cc48..1132400 100644 --- a/internal/processor/string-split.go +++ b/internal/processor/string-split.go @@ -15,7 +15,7 @@ type StringSplit struct { } func (ss *StringSplit) Process(ctx context.Context, payload any) (any, error) { - payloadString, ok := payload.(string) + payloadString, ok := GetAnyAs[string](payload) if !ok { return nil, errors.New("string.split only accepts a string") diff --git a/internal/processor/uint-parse.go b/internal/processor/uint-parse.go index 167fdd3..cbcbeb5 100644 --- a/internal/processor/uint-parse.go +++ b/internal/processor/uint-parse.go @@ -16,7 +16,7 @@ type UintParse struct { } func (up *UintParse) Process(ctx context.Context, payload any) (any, error) { - payloadString, ok := payload.(string) + payloadString, ok := GetAnyAs[string](payload) if !ok { return nil, errors.New("uint.parse processor only accepts a string") diff --git a/internal/route/route_test.go b/internal/route/route_test.go index 4190baa..8c3f751 100644 --- a/internal/route/route_test.go +++ b/internal/route/route_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/jwetzell/showbridge-go/internal/config" + "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" ) @@ -58,7 +59,7 @@ func TestGoodRouteHandleInput(t *testing.T) { t.Fatalf("route ProcessPayload returned error: %v", err) } - payloadBytes, ok := payload.([]byte) + payloadBytes, ok := processor.GetAnyAs[[]byte](payload) if !ok { t.Fatalf("payload should be []byte got %T", payload) }