mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
move any helper methods to common and reuse for params getter
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/artnet-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -13,7 +14,7 @@ type ArtNetPacketDecode struct {
|
||||
}
|
||||
|
||||
func (apd *ArtNetPacketDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := GetAnyAs[[]byte](payload)
|
||||
payloadBytes, ok := common.GetAnyAs[[]byte](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.decode processor only accepts a []byte")
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/artnet-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -13,7 +14,7 @@ type ArtNetPacketEncode struct {
|
||||
}
|
||||
|
||||
func (ape *ArtNetPacketEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadPacket, ok := GetAnyAs[artnet.ArtNetPacket](payload)
|
||||
payloadPacket, ok := common.GetAnyAs[artnet.ArtNetPacket](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.encode processor only accepts an ArtNetPacket")
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -15,7 +16,7 @@ type FilterRegex struct {
|
||||
}
|
||||
|
||||
func (fr *FilterRegex) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := GetAnyAs[string](payload)
|
||||
payloadString, ok := common.GetAnyAs[string](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("filter.regex processor only accepts a string")
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -15,7 +16,7 @@ type FloatParse struct {
|
||||
}
|
||||
|
||||
func (fp *FloatParse) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := GetAnyAs[string](payload)
|
||||
payloadString, ok := common.GetAnyAs[string](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("float.parse processor only accepts a string")
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
|
||||
freeD "github.com/jwetzell/free-d-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -13,7 +14,7 @@ type FreeDDecode struct {
|
||||
}
|
||||
|
||||
func (fd *FreeDDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := GetAnyAs[[]byte](payload)
|
||||
payloadBytes, ok := common.GetAnyAs[[]byte](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.decode processor only accepts a []byte")
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
|
||||
freeD "github.com/jwetzell/free-d-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -13,7 +14,7 @@ type FreeDEncode struct {
|
||||
}
|
||||
|
||||
func (fe *FreeDEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadPosition, ok := GetAnyAs[freeD.FreeDPosition](payload)
|
||||
payloadPosition, ok := common.GetAnyAs[freeD.FreeDPosition](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.decode processor only accepts a FreeDEncode")
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -16,7 +17,7 @@ type IntParse struct {
|
||||
}
|
||||
|
||||
func (ip *IntParse) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := GetAnyAs[string](payload)
|
||||
payloadString, ok := common.GetAnyAs[string](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("int.parse processor only accepts a string")
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -17,7 +18,7 @@ type IntScale struct {
|
||||
}
|
||||
|
||||
func (ir *IntScale) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadInt, ok := GetAnyAs[int](payload)
|
||||
payloadInt, ok := common.GetAnyAs[int](payload)
|
||||
if !ok {
|
||||
return nil, errors.New("int.scale can only process an int")
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -14,10 +15,10 @@ type JsonDecode struct {
|
||||
|
||||
func (jd *JsonDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
payloadBytes, ok := GetAnyAsByteSlice(payload)
|
||||
payloadBytes, ok := common.GetAnyAsByteSlice(payload)
|
||||
|
||||
if !ok {
|
||||
payloadString, ok := GetAnyAs[string](payload)
|
||||
payloadString, ok := common.GetAnyAs[string](payload)
|
||||
if !ok {
|
||||
return nil, errors.New("json.decode can only process a string or []byte")
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
@@ -15,7 +16,7 @@ type MIDIMessageDecode struct {
|
||||
}
|
||||
|
||||
func (mmd *MIDIMessageDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := GetAnyAs[[]byte](payload)
|
||||
payloadBytes, ok := common.GetAnyAs[[]byte](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.decode processor only accepts a []byte")
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
@@ -15,7 +16,7 @@ type MIDIMessageEncode struct {
|
||||
}
|
||||
|
||||
func (mme *MIDIMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadMessage, ok := GetAnyAs[midi.Message](payload)
|
||||
payloadMessage, ok := common.GetAnyAs[midi.Message](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.encode processor only accepts a midi.Message")
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
@@ -45,7 +46,7 @@ type MIDIPitchBend struct {
|
||||
}
|
||||
|
||||
func (mmu *MIDIMessageUnpack) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadMidi, ok := GetAnyAs[midi.Message](payload)
|
||||
payloadMidi, ok := common.GetAnyAs[midi.Message](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.unpack processor only accepts a midi.Message")
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -13,7 +14,7 @@ type MQTTMessageEncode struct {
|
||||
}
|
||||
|
||||
func (mme *MQTTMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadMessage, ok := GetAnyAs[mqtt.Message](payload)
|
||||
payloadMessage, ok := common.GetAnyAs[mqtt.Message](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.message.encode processor only accepts an mqtt.Message")
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
osc "github.com/jwetzell/osc-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -14,7 +15,7 @@ type OSCMessageDecode struct {
|
||||
}
|
||||
|
||||
func (omd *OSCMessageDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := GetAnyAs[[]byte](payload)
|
||||
payloadBytes, ok := common.GetAnyAs[[]byte](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("osc.message.decode processor only accepts a []byte payload")
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
|
||||
osc "github.com/jwetzell/osc-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -13,7 +14,7 @@ type OSCMessageEncode struct {
|
||||
}
|
||||
|
||||
func (ome *OSCMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadMessage, ok := GetAnyAs[*osc.OSCMessage](payload)
|
||||
payloadMessage, ok := common.GetAnyAs[*osc.OSCMessage](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("osc.message.encode processor only accepts an *OSCMessage")
|
||||
|
||||
@@ -3,8 +3,6 @@ package processor
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
@@ -44,57 +42,6 @@ var (
|
||||
ProcessorRegistry = make(map[string]ProcessorRegistration)
|
||||
)
|
||||
|
||||
func GetAnyAs[T any](p any) (T, bool) {
|
||||
typed, ok := p.(T)
|
||||
return typed, ok
|
||||
}
|
||||
|
||||
func GetAnyAsByteSlice(p any) ([]byte, bool) {
|
||||
v := reflect.ValueOf(p)
|
||||
if v.Kind() != reflect.Slice {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
result := make([]byte, v.Len())
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
elem := v.Index(i).Interface()
|
||||
byteValue, ok := elem.(byte)
|
||||
if ok {
|
||||
result[i] = byteValue
|
||||
continue
|
||||
}
|
||||
uintValue, ok := elem.(uint)
|
||||
if ok {
|
||||
if uintValue > 255 {
|
||||
return nil, false
|
||||
}
|
||||
result[i] = byte(uintValue)
|
||||
continue
|
||||
}
|
||||
intValue, ok := elem.(int)
|
||||
if ok {
|
||||
if intValue < 0 || intValue > 255 {
|
||||
return nil, false
|
||||
}
|
||||
result[i] = byte(intValue)
|
||||
continue
|
||||
}
|
||||
floatValue, ok := elem.(float64)
|
||||
if ok {
|
||||
if floatValue != math.Floor(floatValue) {
|
||||
return nil, false
|
||||
}
|
||||
if floatValue < 0 || floatValue > 255 {
|
||||
return nil, false
|
||||
}
|
||||
result[i] = byte(floatValue)
|
||||
continue
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
return result, true
|
||||
}
|
||||
|
||||
type TemplateData struct {
|
||||
Payload any
|
||||
Modules any
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
extism "github.com/extism/go-sdk"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -17,7 +18,7 @@ type ScriptWASM struct {
|
||||
|
||||
func (sw *ScriptWASM) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
payloadBytes, ok := GetAnyAs[[]byte](payload)
|
||||
payloadBytes, ok := common.GetAnyAs[[]byte](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.wasm can only process a byte array")
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -12,7 +13,7 @@ type StringDecode struct {
|
||||
}
|
||||
|
||||
func (sd *StringDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := GetAnyAs[[]byte](payload)
|
||||
payloadBytes, ok := common.GetAnyAs[[]byte](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("string.decode processor only accepts a []byte")
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -12,7 +13,7 @@ type StringEncode struct {
|
||||
}
|
||||
|
||||
func (se *StringEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := GetAnyAs[string](payload)
|
||||
payloadString, ok := common.GetAnyAs[string](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("string.encode processor only accepts a string")
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -15,7 +16,7 @@ type StringSplit struct {
|
||||
}
|
||||
|
||||
func (ss *StringSplit) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := GetAnyAs[string](payload)
|
||||
payloadString, ok := common.GetAnyAs[string](payload)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("string.split only accepts a string")
|
||||
|
||||
@@ -204,7 +204,7 @@ func TestBadMQTTMessageCreate(t *testing.T) {
|
||||
"payload": 123,
|
||||
},
|
||||
payload: 1,
|
||||
errorString: "mqtt.message.create payload error: not a slice",
|
||||
errorString: "mqtt.message.create payload error: not a byte slice",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user