mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add convenience method for casting payloads
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user