mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
Merge pull request #88 from jwetzell/remove-specific-filter-processors
remove specific filter processors
This commit is contained in:
@@ -1,48 +0,0 @@
|
|||||||
package processor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/jwetzell/artnet-go"
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ArtNetPacketFilter struct {
|
|
||||||
config config.ProcessorConfig
|
|
||||||
OpCode uint16
|
|
||||||
}
|
|
||||||
|
|
||||||
func (apf *ArtNetPacketFilter) Process(ctx context.Context, payload any) (any, error) {
|
|
||||||
payloadPacket, ok := GetAnyAs[artnet.ArtNetPacket](payload)
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("artnet.packet.filter processor only accepts an ArtNetPacket")
|
|
||||||
}
|
|
||||||
|
|
||||||
if payloadPacket.GetOpCode() != apf.OpCode {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return payloadPacket, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (apf *ArtNetPacketFilter) Type() string {
|
|
||||||
return apf.config.Type
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
RegisterProcessor(ProcessorRegistration{
|
|
||||||
Type: "artnet.packet.filter",
|
|
||||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
|
||||||
params := config.Params
|
|
||||||
|
|
||||||
opCodeNum, err := params.GetInt("opCode")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("artnet.packet.filter opCode error: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &ArtNetPacketFilter{config: config, OpCode: uint16(opCodeNum)}, nil
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
package processor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"regexp"
|
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
type HTTPRequestFilter struct {
|
|
||||||
config config.ProcessorConfig
|
|
||||||
Path *regexp.Regexp
|
|
||||||
Method string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hrf *HTTPRequestFilter) Process(ctx context.Context, payload any) (any, error) {
|
|
||||||
|
|
||||||
payloadRequest, ok := GetAnyAs[*http.Request](payload)
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
return nil, errors.New("http.request.filter can only operate on http.Request payloads")
|
|
||||||
}
|
|
||||||
|
|
||||||
if hrf.Method != "" {
|
|
||||||
if payloadRequest.Method != hrf.Method {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !hrf.Path.MatchString(payloadRequest.URL.Path) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return payloadRequest, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hrf *HTTPRequestFilter) Type() string {
|
|
||||||
return hrf.config.Type
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
RegisterProcessor(ProcessorRegistration{
|
|
||||||
Type: "http.request.filter",
|
|
||||||
New: func(moduleConfig config.ProcessorConfig) (Processor, error) {
|
|
||||||
params := moduleConfig.Params
|
|
||||||
pathString, err := params.GetString("path")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("http.request.filter path error: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pathRegexp, err := regexp.Compile(fmt.Sprintf("^%s$", pathString))
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
methodString, err := params.GetString("method")
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, config.ErrParamNotFound) {
|
|
||||||
return &HTTPRequestFilter{config: moduleConfig, Path: pathRegexp}, nil
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("http.request.filter method error: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &HTTPRequestFilter{config: moduleConfig, Path: pathRegexp, Method: methodString}, nil
|
|
||||||
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
//go:build cgo
|
|
||||||
|
|
||||||
package processor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
|
||||||
"gitlab.com/gomidi/midi/v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
type MIDIMessageFilter struct {
|
|
||||||
config config.ProcessorConfig
|
|
||||||
MIDIType string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mmf *MIDIMessageFilter) Process(ctx context.Context, payload any) (any, error) {
|
|
||||||
payloadMessage, ok := GetAnyAs[midi.Message](payload)
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
return nil, errors.New("midi.message.filter processor only accepts a midi.Message")
|
|
||||||
}
|
|
||||||
|
|
||||||
if payloadMessage.Type().String() != mmf.MIDIType {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return payloadMessage, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mmf *MIDIMessageFilter) Type() string {
|
|
||||||
return mmf.config.Type
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
RegisterProcessor(ProcessorRegistration{
|
|
||||||
Type: "midi.message.filter",
|
|
||||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
|
||||||
params := config.Params
|
|
||||||
msgTypeString, err := params.GetString("type")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("midi.message.filter type error: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &MIDIMessageFilter{config: config, MIDIType: msgTypeString}, nil
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
package processor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/jwetzell/osc-go"
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
type OSCMessageFilter struct {
|
|
||||||
config config.ProcessorConfig
|
|
||||||
Address *regexp.Regexp
|
|
||||||
}
|
|
||||||
|
|
||||||
func (omf *OSCMessageFilter) Process(ctx context.Context, payload any) (any, error) {
|
|
||||||
|
|
||||||
payloadMessage, ok := GetAnyAs[osc.OSCMessage](payload)
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
return nil, errors.New("osc.message.filter can only operate on OSCMessage payloads")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !omf.Address.MatchString(payloadMessage.Address) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return payloadMessage, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (omf *OSCMessageFilter) Type() string {
|
|
||||||
return omf.config.Type
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
RegisterProcessor(ProcessorRegistration{
|
|
||||||
Type: "osc.message.filter",
|
|
||||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
|
||||||
params := config.Params
|
|
||||||
addressString, err := params.GetString("address")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("osc.message.filter address error: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
addressPattern := strings.ReplaceAll(addressString, "?", ".")
|
|
||||||
addressPattern = strings.ReplaceAll(addressPattern, "*", "[^/]*")
|
|
||||||
addressPattern = strings.ReplaceAll(addressPattern, "[!", "[^")
|
|
||||||
addressPattern = strings.ReplaceAll(addressPattern, "{", "(")
|
|
||||||
addressPattern = strings.ReplaceAll(addressPattern, "}", ")")
|
|
||||||
addressPattern = strings.ReplaceAll(addressPattern, ",", "|")
|
|
||||||
|
|
||||||
addressPatternRegexp, err := regexp.Compile(fmt.Sprintf("^%s$", addressPattern))
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &OSCMessageFilter{config: config, Address: addressPatternRegexp}, nil
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,254 +0,0 @@
|
|||||||
package processor_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/jwetzell/artnet-go"
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestArtnetPacketFilterFromRegistry(t *testing.T) {
|
|
||||||
registration, ok := processor.ProcessorRegistry["artnet.packet.filter"]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("artnet.packet.filter processor not registered")
|
|
||||||
}
|
|
||||||
|
|
||||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
||||||
Type: "artnet.packet.filter",
|
|
||||||
Params: map[string]any{
|
|
||||||
"opCode": float64(artnet.OpTimeCode),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create artnet.packet.filter processor: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if processorInstance.Type() != "artnet.packet.filter" {
|
|
||||||
t.Fatalf("artnet.packet.filter processor has wrong type: %s", processorInstance.Type())
|
|
||||||
}
|
|
||||||
|
|
||||||
payload := &artnet.ArtTimeCode{
|
|
||||||
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
|
|
||||||
OpCode: artnet.OpTimeCode,
|
|
||||||
ProtVerHi: 0,
|
|
||||||
ProtVerLo: 14,
|
|
||||||
Filler1: 0,
|
|
||||||
StreamId: 0,
|
|
||||||
Frames: 11,
|
|
||||||
Seconds: 17,
|
|
||||||
Minutes: 3,
|
|
||||||
Hours: 0,
|
|
||||||
Type: 0,
|
|
||||||
}
|
|
||||||
expected := &artnet.ArtTimeCode{
|
|
||||||
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
|
|
||||||
OpCode: artnet.OpTimeCode,
|
|
||||||
ProtVerHi: 0,
|
|
||||||
ProtVerLo: 14,
|
|
||||||
Filler1: 0,
|
|
||||||
StreamId: 0,
|
|
||||||
Frames: 11,
|
|
||||||
Seconds: 17,
|
|
||||||
Minutes: 3,
|
|
||||||
Hours: 0,
|
|
||||||
Type: 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
got, err := processorInstance.Process(t.Context(), payload)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("artnet.packet.filter processing failed: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(got, expected) {
|
|
||||||
t.Fatalf("artnet.packet.filter got %+v, expected %+v", got, expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGoodArtnetPacketFilter(t *testing.T) {
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
params map[string]any
|
|
||||||
payload any
|
|
||||||
expected artnet.ArtNetPacket
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "tiemcode packet with matching opCode",
|
|
||||||
params: map[string]any{
|
|
||||||
"opCode": float64(artnet.OpTimeCode),
|
|
||||||
},
|
|
||||||
payload: &artnet.ArtTimeCode{
|
|
||||||
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
|
|
||||||
OpCode: artnet.OpTimeCode,
|
|
||||||
ProtVerHi: 0,
|
|
||||||
ProtVerLo: 14,
|
|
||||||
Filler1: 0,
|
|
||||||
StreamId: 0,
|
|
||||||
Frames: 11,
|
|
||||||
Seconds: 17,
|
|
||||||
Minutes: 3,
|
|
||||||
Hours: 0,
|
|
||||||
Type: 0,
|
|
||||||
},
|
|
||||||
expected: &artnet.ArtTimeCode{
|
|
||||||
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
|
|
||||||
OpCode: artnet.OpTimeCode,
|
|
||||||
ProtVerHi: 0,
|
|
||||||
ProtVerLo: 14,
|
|
||||||
Filler1: 0,
|
|
||||||
StreamId: 0,
|
|
||||||
Frames: 11,
|
|
||||||
Seconds: 17,
|
|
||||||
Minutes: 3,
|
|
||||||
Hours: 0,
|
|
||||||
Type: 0,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "timecode packet with mismatching opCode",
|
|
||||||
params: map[string]any{
|
|
||||||
"opCode": float64(artnet.OpDmx),
|
|
||||||
},
|
|
||||||
payload: &artnet.ArtTimeCode{
|
|
||||||
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
|
|
||||||
OpCode: artnet.OpTimeCode,
|
|
||||||
ProtVerHi: 0,
|
|
||||||
ProtVerLo: 14,
|
|
||||||
Filler1: 0,
|
|
||||||
StreamId: 0,
|
|
||||||
Frames: 11,
|
|
||||||
Seconds: 17,
|
|
||||||
Minutes: 3,
|
|
||||||
Hours: 0,
|
|
||||||
Type: 0,
|
|
||||||
},
|
|
||||||
expected: nil,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
registration, ok := processor.ProcessorRegistry["artnet.packet.filter"]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("artnet.packet.filter processor not registered")
|
|
||||||
}
|
|
||||||
|
|
||||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
||||||
Type: "artnet.packet.filter",
|
|
||||||
Params: test.params,
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("artnet.packet.filter failed to create processor: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("artnet.packet.filter processing failed: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if test.expected == nil {
|
|
||||||
if got != nil {
|
|
||||||
t.Fatalf("artnet.packet.filter got %+v, expected nil", got)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
gotPacket, ok := got.(artnet.ArtNetPacket)
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("artnet.packet.filter returned a %T payload: %s", got, got)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(gotPacket, test.expected) {
|
|
||||||
t.Fatalf("artnet.packet.filter got %+v, expected %+v", gotPacket, test.expected)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBadArtnetPacketFilter(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
params map[string]any
|
|
||||||
payload any
|
|
||||||
errorString string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "non-artnet input",
|
|
||||||
payload: []byte{0x01},
|
|
||||||
params: map[string]any{"opCode": float64(artnet.OpTimeCode)},
|
|
||||||
errorString: "artnet.packet.filter processor only accepts an ArtNetPacket",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "no opCode param",
|
|
||||||
payload: &artnet.ArtTimeCode{
|
|
||||||
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
|
|
||||||
OpCode: artnet.OpTimeCode,
|
|
||||||
ProtVerHi: 0,
|
|
||||||
ProtVerLo: 14,
|
|
||||||
Filler1: 0,
|
|
||||||
StreamId: 0,
|
|
||||||
Frames: 11,
|
|
||||||
Seconds: 17,
|
|
||||||
Minutes: 3,
|
|
||||||
Hours: 0,
|
|
||||||
Type: 0,
|
|
||||||
},
|
|
||||||
params: map[string]any{},
|
|
||||||
errorString: "artnet.packet.filter opCode error: not found",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "opCode not a number",
|
|
||||||
payload: &artnet.ArtTimeCode{
|
|
||||||
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
|
|
||||||
OpCode: artnet.OpTimeCode,
|
|
||||||
ProtVerHi: 0,
|
|
||||||
ProtVerLo: 14,
|
|
||||||
Filler1: 0,
|
|
||||||
StreamId: 0,
|
|
||||||
Frames: 11,
|
|
||||||
Seconds: 17,
|
|
||||||
Minutes: 3,
|
|
||||||
Hours: 0,
|
|
||||||
Type: 0,
|
|
||||||
},
|
|
||||||
params: map[string]any{"opCode": "100"},
|
|
||||||
errorString: "artnet.packet.filter opCode error: not a number",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
registration, ok := processor.ProcessorRegistry["artnet.packet.filter"]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("artnet.packet.filter processor not registered")
|
|
||||||
}
|
|
||||||
|
|
||||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
||||||
Type: "artnet.packet.filter",
|
|
||||||
Params: test.params,
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
if test.errorString != err.Error() {
|
|
||||||
t.Fatalf("artnet.packet.filter got error '%s', expected '%s'", err.Error(), test.errorString)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("artnet.packet.filter expected to fail but got payload: %s", got)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err.Error() != test.errorString {
|
|
||||||
t.Fatalf("artnet.packet.filter got error '%s', expected '%s'", err.Error(), test.errorString)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
package processor_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestHTTPRequestFilterFromRegistry(t *testing.T) {
|
|
||||||
registration, ok := processor.ProcessorRegistry["http.request.filter"]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("http.request.filter processor not registered")
|
|
||||||
}
|
|
||||||
|
|
||||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
||||||
Type: "http.request.filter",
|
|
||||||
Params: map[string]any{
|
|
||||||
"method": "GET",
|
|
||||||
"path": "/test",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create http.request.filter processor: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if processorInstance.Type() != "http.request.filter" {
|
|
||||||
t.Fatalf("http.request.filter processor has wrong type: %s", processorInstance.Type())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,157 +0,0 @@
|
|||||||
package processor_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
|
||||||
"gitlab.com/gomidi/midi/v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMIDIMessageFilterFromRegistry(t *testing.T) {
|
|
||||||
registration, ok := processor.ProcessorRegistry["midi.message.filter"]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("midi.message.filter processor not registered")
|
|
||||||
}
|
|
||||||
|
|
||||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
||||||
Type: "midi.message.filter",
|
|
||||||
Params: map[string]any{
|
|
||||||
"type": "NoteOn",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create midi.message.filter processor: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if processorInstance.Type() != "midi.message.filter" {
|
|
||||||
t.Fatalf("midi.message.filter processor has wrong type: %s", processorInstance.Type())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGoodMIDIMessageFilter(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
params map[string]any
|
|
||||||
payload midi.Message
|
|
||||||
expected midi.Message
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "matches pattern",
|
|
||||||
payload: midi.NoteOn(1, 60, 127),
|
|
||||||
params: map[string]any{"type": "NoteOn"},
|
|
||||||
expected: midi.NoteOn(1, 60, 127),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "does not match pattern",
|
|
||||||
payload: midi.NoteOn(1, 60, 127),
|
|
||||||
params: map[string]any{"type": "NoteOff"},
|
|
||||||
expected: nil,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
registration, ok := processor.ProcessorRegistry["midi.message.filter"]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("midi.message.filter processor not registered")
|
|
||||||
}
|
|
||||||
|
|
||||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
||||||
Type: "midi.message.filter",
|
|
||||||
Params: test.params,
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("midi.message.filter failed to create processor: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("midi.message.filter processing failed: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if test.expected == nil {
|
|
||||||
if got != nil {
|
|
||||||
t.Fatalf("midi.message.filter got %+v, expected nil", got)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
gotMIDIMessage, ok := got.(midi.Message)
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("midi.message.filter returned a %T payload: %s", got, got)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(gotMIDIMessage, test.expected) {
|
|
||||||
t.Fatalf("midi.message.filter got %+v, expected %+v", gotMIDIMessage, test.expected)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBadMIDIMessageFilter(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
params map[string]any
|
|
||||||
payload any
|
|
||||||
errorString string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "no type param",
|
|
||||||
params: map[string]any{},
|
|
||||||
payload: midi.NoteOn(1, 60, 127),
|
|
||||||
errorString: "midi.message.filter type error: not found",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "non-string type param",
|
|
||||||
params: map[string]any{
|
|
||||||
"type": 123,
|
|
||||||
},
|
|
||||||
payload: "hello",
|
|
||||||
errorString: "midi.message.filter type error: not a string",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "non-midi message input",
|
|
||||||
params: map[string]any{
|
|
||||||
"type": "NoteOn",
|
|
||||||
},
|
|
||||||
payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
|
||||||
errorString: "midi.message.filter processor only accepts a midi.Message",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
registration, ok := processor.ProcessorRegistry["midi.message.filter"]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("midi.message.filter processor not registered")
|
|
||||||
}
|
|
||||||
|
|
||||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
||||||
Type: "midi.message.filter",
|
|
||||||
Params: test.params,
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
if test.errorString != err.Error() {
|
|
||||||
t.Fatalf("midi.message.filter got error '%s', expected '%s'", err.Error(), test.errorString)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("midi.message.filter expected to fail but got payload: %s", got)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err.Error() != test.errorString {
|
|
||||||
t.Fatalf("midi.message.filter got error '%s', expected '%s'", err.Error(), test.errorString)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,169 +0,0 @@
|
|||||||
package processor_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/jwetzell/osc-go"
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestOSCMessageFilterFromRegistry(t *testing.T) {
|
|
||||||
registration, ok := processor.ProcessorRegistry["osc.message.filter"]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("osc.message.filter processor not registered")
|
|
||||||
}
|
|
||||||
|
|
||||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
||||||
Type: "osc.message.filter",
|
|
||||||
Params: map[string]any{
|
|
||||||
"address": "/test*",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to filter osc.message.filter processor: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if processorInstance.Type() != "osc.message.filter" {
|
|
||||||
t.Fatalf("osc.message.filter processor has wrong type: %s", processorInstance.Type())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGoodOSCMessageFilter(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
payload osc.OSCMessage
|
|
||||||
params map[string]any
|
|
||||||
expected any
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "basic address match",
|
|
||||||
params: map[string]any{
|
|
||||||
"address": "/test",
|
|
||||||
},
|
|
||||||
payload: osc.OSCMessage{Address: "/test"},
|
|
||||||
expected: osc.OSCMessage{Address: "/test"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "basic address no match",
|
|
||||||
params: map[string]any{
|
|
||||||
"address": "/test",
|
|
||||||
},
|
|
||||||
payload: osc.OSCMessage{Address: "/testing"},
|
|
||||||
expected: nil,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
registration, ok := processor.ProcessorRegistry["osc.message.filter"]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("osc.message.filter processor not registered")
|
|
||||||
}
|
|
||||||
|
|
||||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
||||||
Type: "osc.message.filter",
|
|
||||||
Params: test.params,
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("osc.message.filter failed to create processor: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("osc.message.filter processing failed: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if test.expected == nil {
|
|
||||||
if got != nil {
|
|
||||||
t.Fatalf("osc.message.filter got %+v, expected nil", got)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
gotMessage, ok := got.(osc.OSCMessage)
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("osc.message.filter returned a %T payload: %s", got, got)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(gotMessage, test.expected) {
|
|
||||||
t.Fatalf("osc.message.filter got %+v, expected %+v", gotMessage, test.expected)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBadOSCMessageFilter(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
params map[string]any
|
|
||||||
payload any
|
|
||||||
errorString string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "no address parameter",
|
|
||||||
params: map[string]any{},
|
|
||||||
payload: osc.OSCMessage{Address: "/test"},
|
|
||||||
errorString: "osc.message.filter address error: not found",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "non-string address parameter",
|
|
||||||
params: map[string]any{
|
|
||||||
"address": 123,
|
|
||||||
},
|
|
||||||
payload: osc.OSCMessage{Address: "/test"},
|
|
||||||
errorString: "osc.message.filter address error: not a string",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "bad address pattern",
|
|
||||||
params: map[string]any{
|
|
||||||
"address": "[",
|
|
||||||
},
|
|
||||||
payload: osc.OSCMessage{Address: "/test"},
|
|
||||||
errorString: "error parsing regexp: missing closing ]: `[$`",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "non-osc input",
|
|
||||||
params: map[string]any{
|
|
||||||
"address": "/test",
|
|
||||||
},
|
|
||||||
payload: []byte("hello"),
|
|
||||||
errorString: "osc.message.filter can only operate on OSCMessage payloads",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
registration, ok := processor.ProcessorRegistry["osc.message.filter"]
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("osc.message.filter processor not registered")
|
|
||||||
}
|
|
||||||
|
|
||||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
|
||||||
Type: "osc.message.filter",
|
|
||||||
Params: test.params,
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
if test.errorString != err.Error() {
|
|
||||||
t.Fatalf("string.create got error '%s', expected '%s'", err.Error(), test.errorString)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
t.Fatalf("osc.message.filter expected to fail but succeeded, got: %v", got)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err.Error() != test.errorString {
|
|
||||||
t.Fatalf("osc.message.filter got error '%s', expected '%s'", err.Error(), test.errorString)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -30,29 +30,6 @@
|
|||||||
"required": ["type"],
|
"required": ["type"],
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"type": "object",
|
|
||||||
"title": "Filter ArtNet Packet",
|
|
||||||
"properties": {
|
|
||||||
"type": {
|
|
||||||
"type": "string",
|
|
||||||
"const": "artnet.packet.filter"
|
|
||||||
},
|
|
||||||
"params": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"opCode": {
|
|
||||||
"title": "Op Code",
|
|
||||||
"type": "integer"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": ["opCode"],
|
|
||||||
"additionalProperties": false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": ["type", "params"],
|
|
||||||
"additionalProperties": false
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"title": "Debug Log",
|
"title": "Debug Log",
|
||||||
@@ -229,34 +206,6 @@
|
|||||||
"required": ["type", "params"],
|
"required": ["type", "params"],
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"type": "object",
|
|
||||||
"title": "Filter HTTP Request",
|
|
||||||
"properties": {
|
|
||||||
"type": {
|
|
||||||
"type": "string",
|
|
||||||
"const": "http.request.filter"
|
|
||||||
},
|
|
||||||
"params": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"path": {
|
|
||||||
"title": "Path",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"method": {
|
|
||||||
"title": "HTTP Method",
|
|
||||||
"type": "string",
|
|
||||||
"enum": ["GET", "POST"]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": ["path"],
|
|
||||||
"additionalProperties": false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": ["type", "params"],
|
|
||||||
"additionalProperties": false
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"title": "Create HTTP Response",
|
"title": "Create HTTP Response",
|
||||||
@@ -498,30 +447,6 @@
|
|||||||
"required": ["type"],
|
"required": ["type"],
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"type": "object",
|
|
||||||
"title": "Filter MIDI Message",
|
|
||||||
"properties": {
|
|
||||||
"type": {
|
|
||||||
"type": "string",
|
|
||||||
"const": "midi.message.filter"
|
|
||||||
},
|
|
||||||
"params": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"type": {
|
|
||||||
"title": "MIDI Message Type",
|
|
||||||
"type": "string",
|
|
||||||
"enum": ["NoteOn", "NoteOff", "ControlChange", "ProgramChange"]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": ["type"],
|
|
||||||
"additionalProperties": false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": ["type", "params"],
|
|
||||||
"additionalProperties": false
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"title": "Unpack MIDI Message",
|
"title": "Unpack MIDI Message",
|
||||||
@@ -666,29 +591,6 @@
|
|||||||
"required": ["type"],
|
"required": ["type"],
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"type": "object",
|
|
||||||
"title": "Filter OSC Message",
|
|
||||||
"properties": {
|
|
||||||
"type": {
|
|
||||||
"type": "string",
|
|
||||||
"const": "osc.message.filter"
|
|
||||||
},
|
|
||||||
"params": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"address": {
|
|
||||||
"title": "Address",
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": ["address"],
|
|
||||||
"additionalProperties": false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": ["type", "params"],
|
|
||||||
"additionalProperties": false
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"title": "Router Output",
|
"title": "Router Output",
|
||||||
|
|||||||
Reference in New Issue
Block a user