From 5a0f21bd64df4a3cc3c3af5909e0f9f073016078 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Tue, 10 Mar 2026 18:14:30 -0500 Subject: [PATCH] move any helper methods to common and reuse for params getter --- internal/common/common.go | 179 ++++++++++++ internal/config/config.go | 259 ------------------ internal/config/params.go | 140 ++++++++++ internal/module/http-server.go | 2 +- internal/module/midi-output.go | 3 +- internal/module/mqtt-client.go | 3 +- internal/module/nats-client.go | 2 +- internal/module/serial-client.go | 3 +- internal/module/sip-call-server.go | 4 +- internal/module/sip-dtmf-server.go | 4 +- internal/module/tcp-client.go | 3 +- internal/module/tcp-server.go | 3 +- internal/module/udp-client.go | 3 +- internal/module/udp-multicast.go | 3 +- internal/processor/artnet-packet-decode.go | 3 +- internal/processor/artnet-packet-encode.go | 3 +- internal/processor/filter-regex.go | 3 +- internal/processor/float-parse.go | 3 +- internal/processor/free-d-decode.go | 3 +- internal/processor/free-d-encode.go | 3 +- internal/processor/int-parse.go | 3 +- internal/processor/int-scale.go | 3 +- internal/processor/json-decode.go | 5 +- internal/processor/midi-message-decode.go | 3 +- internal/processor/midi-message-encode.go | 3 +- internal/processor/midi-message-unpack.go | 3 +- internal/processor/mqtt-message-encode.go | 3 +- internal/processor/osc-message-decode.go | 3 +- internal/processor/osc-message-encode.go | 3 +- internal/processor/processor.go | 53 ---- internal/processor/script-wasm.go | 3 +- internal/processor/string-decode.go | 3 +- internal/processor/string-encode.go | 3 +- internal/processor/string-split.go | 3 +- .../test/mqtt-message-create_test.go | 2 +- internal/route/route_test.go | 3 +- 36 files changed, 373 insertions(+), 355 deletions(-) create mode 100644 internal/common/common.go create mode 100644 internal/config/params.go diff --git a/internal/common/common.go b/internal/common/common.go new file mode 100644 index 0000000..c4ca9fa --- /dev/null +++ b/internal/common/common.go @@ -0,0 +1,179 @@ +package common + +import ( + "math" + "reflect" +) + +func GetAnyAs[T any](value any) (T, bool) { + typed, ok := value.(T) + return typed, ok +} + +func GetAnyAsInt(value any) (int, bool) { + + intValue, ok := value.(int) + if ok { + return intValue, true + } + + uintValue, ok := value.(uint) + if ok { + return int(uintValue), true + } + + byteValue, ok := value.(byte) + if ok { + return int(byteValue), true + } + + floatValue, ok := value.(float64) + if ok { + if floatValue != math.Floor(floatValue) { + return 0, false + } + return int(floatValue), true + } + return 0, false +} + +func GetAnyAsByteSlice(value any) ([]byte, bool) { + v := reflect.ValueOf(value) + 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 +} + +func GetAnyAsIntSlice(value any) ([]int, bool) { + v := reflect.ValueOf(value) + if v.Kind() != reflect.Slice { + return nil, false + } + + result := make([]int, v.Len()) + for i := 0; i < v.Len(); i++ { + elem := v.Index(i).Interface() + byteValue, ok := elem.(byte) + if ok { + result[i] = int(byteValue) + continue + } + uintValue, ok := elem.(uint) + if ok { + result[i] = int(uintValue) + continue + } + intValue, ok := elem.(int) + if ok { + result[i] = int(intValue) + continue + } + floatValue, ok := elem.(float64) + if ok { + if floatValue != math.Floor(floatValue) { + return nil, false + } + result[i] = int(floatValue) + continue + } + return nil, false + } + return result, true +} + +func GetAnyAsFloat32(value any) (float32, bool) { + float32Value, ok := value.(float32) + if ok { + return float32Value, true + } + + float64Value, ok := value.(float64) + if ok { + return float32(float64Value), true + } + + intValue, ok := value.(int) + if ok { + return float32(intValue), true + } + + uintValue, ok := value.(uint) + if ok { + return float32(uintValue), true + } + + byteValue, ok := value.(byte) + if ok { + return float32(byteValue), true + } + + return 0, false +} + +func GetAnyAsFloat64(value any) (float64, bool) { + float64Value, ok := value.(float64) + if ok { + return float64Value, true + } + + float32Value, ok := value.(float32) + if ok { + return float64(float32Value), true + } + + intValue, ok := value.(int) + if ok { + return float64(intValue), true + } + + uintValue, ok := value.(uint) + if ok { + return float64(uintValue), true + } + + byteValue, ok := value.(byte) + if ok { + return float64(byteValue), true + } + + return 0, false +} diff --git a/internal/config/config.go b/internal/config/config.go index 1ae3e43..fc1b5eb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,269 +1,10 @@ package config -import ( - "errors" - "fmt" - "math" - "reflect" -) - type Config struct { Modules []ModuleConfig `json:"modules"` Routes []RouteConfig `json:"routes"` } -type Params map[string]any - -var ( - ErrParamNotFound = errors.New("not found") - ErrParamNotString = errors.New("not a string") - ErrParamNotNumber = errors.New("not a number") - ErrParamNotInteger = errors.New("not an integer") - ErrParamNotBool = errors.New("not a boolean") - ErrParamNotSlice = errors.New("not a slice") -) - -func (p Params) GetString(key string) (string, error) { - value, ok := p[key] - if !ok { - return "", ErrParamNotFound - } - - stringValue, ok := value.(string) - if !ok { - return "", ErrParamNotString - } - return stringValue, nil -} - -func (p Params) GetInt(key string) (int, error) { - value, ok := p[key] - if !ok { - return 0, ErrParamNotFound - } - - intValue, ok := value.(int) - if ok { - return intValue, nil - } - - uintValue, ok := value.(uint) - if ok { - return int(uintValue), nil - } - - byteValue, ok := value.(byte) - if ok { - return int(byteValue), nil - } - - floatValue, ok := value.(float64) - if ok { - if floatValue != math.Floor(floatValue) { - return 0, ErrParamNotInteger - } - return int(floatValue), nil - } - - return 0, ErrParamNotNumber -} - -func (p Params) GetFloat32(key string) (float32, error) { - value, ok := p[key] - if !ok { - return 0, ErrParamNotFound - } - - float32Value, ok := value.(float32) - if ok { - return float32Value, nil - } - - float64Value, ok := value.(float64) - if ok { - return float32(float64Value), nil - } - - intValue, ok := value.(int) - if ok { - return float32(intValue), nil - } - - uintValue, ok := value.(uint) - if ok { - return float32(uintValue), nil - } - - byteValue, ok := value.(byte) - if ok { - return float32(byteValue), nil - } - - return 0, ErrParamNotNumber -} - -func (p Params) GetFloat64(key string) (float64, error) { - value, ok := p[key] - if !ok { - return 0, ErrParamNotFound - } - - float64Value, ok := value.(float64) - if ok { - return float64Value, nil - } - - float32Value, ok := value.(float32) - if ok { - return float64(float32Value), nil - } - - intValue, ok := value.(int) - if ok { - return float64(intValue), nil - } - - uintValue, ok := value.(uint) - if ok { - return float64(uintValue), nil - } - - byteValue, ok := value.(byte) - if ok { - return float64(byteValue), nil - } - - return 0, ErrParamNotNumber -} - -func (p Params) GetBool(key string) (bool, error) { - value, ok := p[key] - if !ok { - return false, ErrParamNotFound - } - - boolValue, ok := value.(bool) - if !ok { - return false, ErrParamNotBool - } - return boolValue, nil -} - -func (p Params) GetStringSlice(key string) ([]string, error) { - value, ok := p[key] - if !ok { - return nil, ErrParamNotFound - } - - interfaceSlice, ok := value.([]any) - if !ok { - return nil, ErrParamNotSlice - } - - stringSlice := make([]string, len(interfaceSlice)) - for i, v := range interfaceSlice { - str, ok := v.(string) - if !ok { - return nil, fmt.Errorf("element at index %d is not a string", i) - } - stringSlice[i] = str - } - return stringSlice, nil -} - -func (p Params) GetIntSlice(key string) ([]int, error) { - value, ok := p[key] - if !ok { - return nil, ErrParamNotFound - } - - v := reflect.ValueOf(value) - if v.Kind() != reflect.Slice { - return nil, ErrParamNotSlice - } - - result := make([]int, v.Len()) - for i := 0; i < v.Len(); i++ { - elem := v.Index(i).Interface() - byteValue, ok := elem.(byte) - if ok { - result[i] = int(byteValue) - continue - } - uintValue, ok := elem.(uint) - if ok { - result[i] = int(uintValue) - continue - } - intValue, ok := elem.(int) - if ok { - result[i] = int(intValue) - continue - } - floatValue, ok := elem.(float64) - if ok { - if floatValue != math.Floor(floatValue) { - return nil, fmt.Errorf("element at index %d is not an integer", i) - } - result[i] = int(floatValue) - continue - } - return nil, fmt.Errorf("element at index %d is not a number", i) - } - return result, nil -} - -func (p Params) GetByteSlice(key string) ([]byte, error) { - value, ok := p[key] - if !ok { - return nil, ErrParamNotFound - } - - v := reflect.ValueOf(value) - if v.Kind() != reflect.Slice { - return nil, ErrParamNotSlice - } - - 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, fmt.Errorf("element at index %d is out of byte range", i) - } - result[i] = byte(uintValue) - continue - } - intValue, ok := elem.(int) - if ok { - if intValue < 0 || intValue > 255 { - return nil, fmt.Errorf("element at index %d is out of byte range", i) - } - result[i] = byte(intValue) - continue - } - floatValue, ok := elem.(float64) - if ok { - if floatValue != math.Floor(floatValue) { - return nil, fmt.Errorf("element at index %d is not an integer", i) - } - if floatValue < 0 || floatValue > 255 { - return nil, fmt.Errorf("element at index %d is out of byte range", i) - } - result[i] = byte(floatValue) - continue - } - return nil, fmt.Errorf("element at index %d is not a number", i) - } - return result, nil -} - type ModuleConfig struct { Id string `json:"id"` Type string `json:"type"` diff --git a/internal/config/params.go b/internal/config/params.go new file mode 100644 index 0000000..ad80dcb --- /dev/null +++ b/internal/config/params.go @@ -0,0 +1,140 @@ +package config + +import ( + "errors" + "fmt" + + "github.com/jwetzell/showbridge-go/internal/common" +) + +type Params map[string]any + +var ( + ErrParamNotFound = errors.New("not found") + ErrParamNotString = errors.New("not a string") + ErrParamNotNumber = errors.New("not a number") + ErrParamNotInteger = errors.New("not an integer") + ErrParamNotBool = errors.New("not a boolean") + ErrParamNotSlice = errors.New("not a slice") + ErrParamNotByteSlice = errors.New("not a byte slice") + ErrParamNotIntSlice = errors.New("not an int slice") +) + +func (p Params) GetString(key string) (string, error) { + value, ok := p[key] + if !ok { + return "", ErrParamNotFound + } + + stringValue, ok := value.(string) + if !ok { + return "", ErrParamNotString + } + return stringValue, nil +} + +func (p Params) GetInt(key string) (int, error) { + value, ok := p[key] + if !ok { + return 0, ErrParamNotFound + } + + intValue, ok := common.GetAnyAsInt(value) + if ok { + return intValue, nil + } + + return 0, ErrParamNotNumber +} + +func (p Params) GetFloat32(key string) (float32, error) { + value, ok := p[key] + if !ok { + return 0, ErrParamNotFound + } + + floatValue, ok := common.GetAnyAsFloat32(value) + if ok { + return floatValue, nil + } + + return 0, ErrParamNotNumber +} + +func (p Params) GetFloat64(key string) (float64, error) { + value, ok := p[key] + if !ok { + return 0, ErrParamNotFound + } + + floatValue, ok := common.GetAnyAsFloat64(value) + if ok { + return floatValue, nil + } + + return 0, ErrParamNotNumber +} + +func (p Params) GetBool(key string) (bool, error) { + value, ok := p[key] + if !ok { + return false, ErrParamNotFound + } + + boolValue, ok := value.(bool) + if !ok { + return false, ErrParamNotBool + } + return boolValue, nil +} + +func (p Params) GetStringSlice(key string) ([]string, error) { + value, ok := p[key] + if !ok { + return nil, ErrParamNotFound + } + + interfaceSlice, ok := value.([]any) + if !ok { + return nil, ErrParamNotSlice + } + + stringSlice := make([]string, len(interfaceSlice)) + for i, v := range interfaceSlice { + str, ok := v.(string) + if !ok { + return nil, fmt.Errorf("element at index %d is not a string", i) + } + stringSlice[i] = str + } + return stringSlice, nil +} + +func (p Params) GetIntSlice(key string) ([]int, error) { + value, ok := p[key] + if !ok { + return nil, ErrParamNotFound + } + + intSlice, ok := common.GetAnyAsIntSlice(value) + if !ok { + return nil, ErrParamNotIntSlice + } + + return intSlice, nil +} + +func (p Params) GetByteSlice(key string) ([]byte, error) { + value, ok := p[key] + if !ok { + return nil, ErrParamNotFound + } + + byteSlice, ok := common.GetAnyAsByteSlice(value) + + if !ok { + return nil, ErrParamNotByteSlice + } + + return byteSlice, nil +} diff --git a/internal/module/http-server.go b/internal/module/http-server.go index eaaef72..b80083a 100644 --- a/internal/module/http-server.go +++ b/internal/module/http-server.go @@ -187,7 +187,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 := processor.GetAnyAs[processor.HTTPResponse](payload) + payloadResponse, ok := common.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 8503859..ce8ce50 100644 --- a/internal/module/midi-output.go +++ b/internal/module/midi-output.go @@ -10,7 +10,6 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" - "github.com/jwetzell/showbridge-go/internal/processor" "gitlab.com/gomidi/midi/v2" _ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv" ) @@ -85,7 +84,7 @@ func (mo *MIDIOutput) Output(ctx context.Context, payload any) error { return errors.New("midi.output output is not setup") } - payloadMessage, ok := processor.GetAnyAs[midi.Message](payload) + payloadMessage, ok := common.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 692dc88..2e6bac1 100644 --- a/internal/module/mqtt-client.go +++ b/internal/module/mqtt-client.go @@ -9,7 +9,6 @@ import ( mqtt "github.com/eclipse/paho.mqtt.golang" "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" - "github.com/jwetzell/showbridge-go/internal/processor" ) type MQTTClient struct { @@ -102,7 +101,7 @@ func (mc *MQTTClient) Start(ctx context.Context) error { } func (mc *MQTTClient) Output(ctx context.Context, payload any) error { - payloadMessage, ok := processor.GetAnyAs[mqtt.Message](payload) + payloadMessage, ok := common.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 2e25d04..fc7cfcd 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 := processor.GetAnyAs[processor.NATSMessage](payload) + payloadMessage, ok := common.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 6d73dac..e4c38b0 100644 --- a/internal/module/serial-client.go +++ b/internal/module/serial-client.go @@ -12,7 +12,6 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/framer" - "github.com/jwetzell/showbridge-go/internal/processor" "go.bug.st/serial" ) @@ -157,7 +156,7 @@ func (sc *SerialClient) Start(ctx context.Context) error { func (sc *SerialClient) Output(ctx context.Context, payload any) error { - payloadBytes, ok := processor.GetAnyAs[[]byte](payload) + payloadBytes, ok := common.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 24392b8..05b1e68 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 := processor.GetAnyAs[processor.SipDTMFResponse](payload) + payloadDTMFResponse, ok := common.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 := processor.GetAnyAs[processor.SipAudioFileResponse](payload) + payloadAudioFileResponse, ok := common.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 b609928..f2b71e2 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 := processor.GetAnyAs[processor.SipDTMFResponse](payload) + payloadDTMFResponse, ok := common.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 := processor.GetAnyAs[processor.SipAudioFileResponse](payload) + payloadAudioFileResponse, ok := common.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 a79325e..2bd1b26 100644 --- a/internal/module/tcp-client.go +++ b/internal/module/tcp-client.go @@ -11,7 +11,6 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/framer" - "github.com/jwetzell/showbridge-go/internal/processor" ) type TCPClient struct { @@ -153,7 +152,7 @@ func (tc *TCPClient) Output(ctx context.Context, payload any) error { return err } } - payloadBytes, ok := processor.GetAnyAs[[]byte](payload) + payloadBytes, ok := common.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 bad9b0a..fdd19eb 100644 --- a/internal/module/tcp-server.go +++ b/internal/module/tcp-server.go @@ -14,7 +14,6 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/framer" - "github.com/jwetzell/showbridge-go/internal/processor" ) type TCPServer struct { @@ -207,7 +206,7 @@ AcceptLoop: } func (ts *TCPServer) Output(ctx context.Context, payload any) error { - payloadBytes, ok := processor.GetAnyAs[[]byte](payload) + payloadBytes, ok := common.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 d07131f..891f712 100644 --- a/internal/module/udp-client.go +++ b/internal/module/udp-client.go @@ -9,7 +9,6 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" - "github.com/jwetzell/showbridge-go/internal/processor" ) type UDPClient struct { @@ -88,7 +87,7 @@ func (uc *UDPClient) Start(ctx context.Context) error { func (uc *UDPClient) Output(ctx context.Context, payload any) error { - payloadBytes, ok := processor.GetAnyAs[[]byte](payload) + payloadBytes, ok := common.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 54e4087..f06887c 100644 --- a/internal/module/udp-multicast.go +++ b/internal/module/udp-multicast.go @@ -10,7 +10,6 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" - "github.com/jwetzell/showbridge-go/internal/processor" ) type UDPMulticast struct { @@ -109,7 +108,7 @@ func (um *UDPMulticast) Start(ctx context.Context) error { func (um *UDPMulticast) Output(ctx context.Context, payload any) error { - payloadBytes, ok := processor.GetAnyAs[[]byte](payload) + payloadBytes, ok := common.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 ff525b8..fad7273 100644 --- a/internal/processor/artnet-packet-decode.go +++ b/internal/processor/artnet-packet-decode.go @@ -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") diff --git a/internal/processor/artnet-packet-encode.go b/internal/processor/artnet-packet-encode.go index ebc11e9..cd55db0 100644 --- a/internal/processor/artnet-packet-encode.go +++ b/internal/processor/artnet-packet-encode.go @@ -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") diff --git a/internal/processor/filter-regex.go b/internal/processor/filter-regex.go index 0dba8b1..349c849 100644 --- a/internal/processor/filter-regex.go +++ b/internal/processor/filter-regex.go @@ -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") diff --git a/internal/processor/float-parse.go b/internal/processor/float-parse.go index 5542bf4..3b07701 100644 --- a/internal/processor/float-parse.go +++ b/internal/processor/float-parse.go @@ -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") diff --git a/internal/processor/free-d-decode.go b/internal/processor/free-d-decode.go index a5312b1..3c595f1 100644 --- a/internal/processor/free-d-decode.go +++ b/internal/processor/free-d-decode.go @@ -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") diff --git a/internal/processor/free-d-encode.go b/internal/processor/free-d-encode.go index 9211a24..919928c 100644 --- a/internal/processor/free-d-encode.go +++ b/internal/processor/free-d-encode.go @@ -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") diff --git a/internal/processor/int-parse.go b/internal/processor/int-parse.go index 53093ad..0370f5f 100644 --- a/internal/processor/int-parse.go +++ b/internal/processor/int-parse.go @@ -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") diff --git a/internal/processor/int-scale.go b/internal/processor/int-scale.go index 615fb34..d51c79d 100644 --- a/internal/processor/int-scale.go +++ b/internal/processor/int-scale.go @@ -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") } diff --git a/internal/processor/json-decode.go b/internal/processor/json-decode.go index 11a805f..df5282f 100644 --- a/internal/processor/json-decode.go +++ b/internal/processor/json-decode.go @@ -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") } diff --git a/internal/processor/midi-message-decode.go b/internal/processor/midi-message-decode.go index 96aa5a2..28e205d 100644 --- a/internal/processor/midi-message-decode.go +++ b/internal/processor/midi-message-decode.go @@ -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") diff --git a/internal/processor/midi-message-encode.go b/internal/processor/midi-message-encode.go index a3694af..2aa257b 100644 --- a/internal/processor/midi-message-encode.go +++ b/internal/processor/midi-message-encode.go @@ -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") diff --git a/internal/processor/midi-message-unpack.go b/internal/processor/midi-message-unpack.go index 8853d57..2209d77 100644 --- a/internal/processor/midi-message-unpack.go +++ b/internal/processor/midi-message-unpack.go @@ -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") diff --git a/internal/processor/mqtt-message-encode.go b/internal/processor/mqtt-message-encode.go index 9975bbb..c1ed7a5 100644 --- a/internal/processor/mqtt-message-encode.go +++ b/internal/processor/mqtt-message-encode.go @@ -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") diff --git a/internal/processor/osc-message-decode.go b/internal/processor/osc-message-decode.go index 18102ba..c595193 100644 --- a/internal/processor/osc-message-decode.go +++ b/internal/processor/osc-message-decode.go @@ -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") diff --git a/internal/processor/osc-message-encode.go b/internal/processor/osc-message-encode.go index 9c7c1da..cd1e6ab 100644 --- a/internal/processor/osc-message-encode.go +++ b/internal/processor/osc-message-encode.go @@ -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") diff --git a/internal/processor/processor.go b/internal/processor/processor.go index 7a5317f..ee1f287 100644 --- a/internal/processor/processor.go +++ b/internal/processor/processor.go @@ -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 diff --git a/internal/processor/script-wasm.go b/internal/processor/script-wasm.go index 2ac83d3..0b0fad8 100644 --- a/internal/processor/script-wasm.go +++ b/internal/processor/script-wasm.go @@ -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") diff --git a/internal/processor/string-decode.go b/internal/processor/string-decode.go index 433affc..bd212cc 100644 --- a/internal/processor/string-decode.go +++ b/internal/processor/string-decode.go @@ -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") diff --git a/internal/processor/string-encode.go b/internal/processor/string-encode.go index 4a62ccf..91592ca 100644 --- a/internal/processor/string-encode.go +++ b/internal/processor/string-encode.go @@ -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") diff --git a/internal/processor/string-split.go b/internal/processor/string-split.go index 1132400..168586c 100644 --- a/internal/processor/string-split.go +++ b/internal/processor/string-split.go @@ -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") diff --git a/internal/processor/test/mqtt-message-create_test.go b/internal/processor/test/mqtt-message-create_test.go index 0fc14ec..351efa8 100644 --- a/internal/processor/test/mqtt-message-create_test.go +++ b/internal/processor/test/mqtt-message-create_test.go @@ -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", }, } diff --git a/internal/route/route_test.go b/internal/route/route_test.go index 79a9472..93bf253 100644 --- a/internal/route/route_test.go +++ b/internal/route/route_test.go @@ -7,7 +7,6 @@ import ( "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" - "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" ) @@ -61,7 +60,7 @@ func TestGoodRouteHandleInput(t *testing.T) { t.Fatalf("route ProcessPayload returned error: %v", err) } - payloadBytes, ok := processor.GetAnyAs[[]byte](payload) + payloadBytes, ok := common.GetAnyAs[[]byte](payload) if !ok { t.Fatalf("payload should be []byte got %T", payload) }