add convenience method for casting payloads

This commit is contained in:
Joel Wetzell
2026-03-02 12:06:42 -06:00
parent af39c16f30
commit df14024012
38 changed files with 54 additions and 40 deletions

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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
}

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")

View File

@@ -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")