mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-29 06:15:31 +00:00
Compare commits
73 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb599f1057 | ||
|
|
a2efed0ee2 | ||
|
|
533fe150c8 | ||
|
|
b653179e6d | ||
|
|
695cabf15e | ||
|
|
ccac116f8d | ||
|
|
c298f63ffc | ||
|
|
183182e6cd | ||
|
|
01f172dbee | ||
|
|
20fd4170ed | ||
|
|
bcf9299505 | ||
|
|
4f7820af5e | ||
|
|
87fcc63068 | ||
|
|
1c49776662 | ||
|
|
1a7207ae4c | ||
|
|
f86a34cc92 | ||
|
|
d9d64bd248 | ||
|
|
e828dc1b0b | ||
|
|
cc4d50bc17 | ||
|
|
abd37439c5 | ||
|
|
8c3f93b601 | ||
|
|
2f7f6967e1 | ||
|
|
09b030efa6 | ||
|
|
bb49dba22e | ||
|
|
4ed6866731 | ||
|
|
2d392873ca | ||
|
|
11e25ab8f7 | ||
|
|
87e6b09156 | ||
|
|
8cb2a0e9f0 | ||
|
|
263943f4d6 | ||
|
|
f9865765c6 | ||
|
|
e6aaffbb95 | ||
|
|
90a14b4d1b | ||
|
|
8bdd338a27 | ||
|
|
b20c16be4f | ||
|
|
de060057e6 | ||
|
|
4526e805af | ||
|
|
1d8baa8c17 | ||
|
|
060c12512c | ||
|
|
444093d375 | ||
|
|
a217b729cd | ||
|
|
ad76480008 | ||
|
|
a408e281a3 | ||
|
|
f028634401 | ||
|
|
961316d28b | ||
|
|
e22a941e4a | ||
|
|
b0cffe819d | ||
|
|
0cdd1f6e6a | ||
|
|
a9245ab88c | ||
|
|
d69d3a370d | ||
|
|
019624f152 | ||
|
|
90a3119fb8 | ||
|
|
54c14cbbae | ||
|
|
969ac6e04b | ||
|
|
bfa63499c3 | ||
|
|
6b178d1ae4 | ||
|
|
988437fccf | ||
|
|
dc19d18b81 | ||
|
|
86f1082159 | ||
|
|
a46054c427 | ||
|
|
9dc4706fd8 | ||
|
|
f91cb9dbbf | ||
|
|
10b5cbae6c | ||
|
|
a9f9f1ba2c | ||
|
|
8e27dc81a2 | ||
|
|
42bd5c46b5 | ||
|
|
515a8d14fd | ||
|
|
1f3a2b9076 | ||
|
|
5dc897c4b2 | ||
|
|
7015a9d7a2 | ||
|
|
3c6d98638e | ||
|
|
8ec8ad91a6 | ||
|
|
f919017ca6 |
4
.github/workflows/release-showbridge.yaml
vendored
4
.github/workflows/release-showbridge.yaml
vendored
@@ -21,7 +21,7 @@ jobs:
|
||||
with:
|
||||
go-version-file: 'go.mod'
|
||||
- name: release
|
||||
uses: goreleaser/goreleaser-action@v6
|
||||
uses: goreleaser/goreleaser-action@v7
|
||||
with:
|
||||
workdir: cmd/showbridge
|
||||
distribution: goreleaser
|
||||
@@ -56,7 +56,7 @@ jobs:
|
||||
jwetzell/showbridge
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
|
||||
uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2
|
||||
with:
|
||||
push: true
|
||||
context: ./
|
||||
|
||||
16
.github/workflows/test-showbridge.yaml
vendored
16
.github/workflows/test-showbridge.yaml
vendored
@@ -17,7 +17,7 @@ on:
|
||||
- 'go.sum'
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
test:
|
||||
@@ -34,4 +34,16 @@ jobs:
|
||||
run: go mod tidy
|
||||
|
||||
- name: run tests
|
||||
run: go test ./...
|
||||
run: go test ./... -coverpkg "github.com/jwetzell/showbridge-go/..." -coverprofile coverage.out
|
||||
|
||||
- name: clean up coverage report
|
||||
run: cat coverage.out | grep -v "github.com/jwetzell/showbridge-go/cmd" > coverage.nocmd.out
|
||||
- name: Update coverage report
|
||||
uses: ncruces/go-coverage-report@v0.3.2
|
||||
with:
|
||||
coverage-file: coverage.nocmd.out
|
||||
report: true
|
||||
chart: true
|
||||
amend: true
|
||||
if: github.event_name == 'push'
|
||||
continue-on-error: true
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
<div align="center">
|
||||
|
||||
# showbridge (go edition)
|
||||
# showbridge (go edition)
|
||||
|
||||
[](https://raw.githack.com/wiki/jwetzell/showbridge-go/coverage.html)
|
||||
|
||||
Simple protocol router _/s_
|
||||
|
||||
</div>
|
||||
|
||||
@@ -19,7 +19,6 @@ import (
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.39.0"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
@@ -90,7 +89,6 @@ type showbridgeApp struct {
|
||||
logger *slog.Logger
|
||||
router *showbridge.Router
|
||||
routerRunner *sync.WaitGroup
|
||||
tracer trace.Tracer
|
||||
routerMutex sync.Mutex
|
||||
}
|
||||
|
||||
@@ -155,7 +153,6 @@ func run(ctx context.Context, c *cli.Command) error {
|
||||
|
||||
slog.SetDefault(slog.New(logHandler))
|
||||
|
||||
var tracer trace.Tracer
|
||||
if c.Bool("trace") {
|
||||
exporter, err := otlptracehttp.New(ctx)
|
||||
if err != nil {
|
||||
@@ -166,9 +163,7 @@ func run(ctx context.Context, c *cli.Command) error {
|
||||
otel.SetTracerProvider(tracerProvider)
|
||||
defer tracerProvider.Shutdown(ctx)
|
||||
|
||||
tracer = tracerProvider.Tracer("showbridge")
|
||||
} else {
|
||||
tracer = otel.Tracer("showbridge")
|
||||
otel.SetTracerProvider(tracerProvider)
|
||||
}
|
||||
|
||||
showbridgeApp := &showbridgeApp{
|
||||
@@ -176,7 +171,6 @@ func run(ctx context.Context, c *cli.Command) error {
|
||||
configPath: configPath,
|
||||
logger: slog.Default().With("component", "cmd"),
|
||||
routerRunner: &sync.WaitGroup{},
|
||||
tracer: tracer,
|
||||
}
|
||||
|
||||
router, err := showbridgeApp.getNewRouter()
|
||||
@@ -233,7 +227,7 @@ func (app *showbridgeApp) getNewRouter() (*showbridge.Router, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
router, moduleErrors, routeErrors := showbridge.NewRouter(config, app.tracer)
|
||||
router, moduleErrors, routeErrors := showbridge.NewRouter(config)
|
||||
|
||||
for _, moduleError := range moduleErrors {
|
||||
app.logger.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
|
||||
|
||||
44
go.mod
44
go.mod
@@ -4,20 +4,21 @@ go 1.25.5
|
||||
|
||||
require (
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.1
|
||||
github.com/emiago/diago v0.26.2
|
||||
github.com/emiago/sipgo v1.1.2
|
||||
github.com/expr-lang/expr v1.17.7
|
||||
github.com/emiago/diago v0.27.0
|
||||
github.com/emiago/sipgo v1.2.0
|
||||
github.com/expr-lang/expr v1.17.8
|
||||
github.com/extism/go-sdk v1.7.1
|
||||
github.com/jwetzell/artnet-go v0.2.1
|
||||
github.com/jwetzell/free-d-go v0.1.0
|
||||
github.com/jwetzell/osc-go v0.1.0
|
||||
github.com/jwetzell/psn-go v0.3.0
|
||||
github.com/nats-io/nats.go v1.48.0
|
||||
github.com/nats-io/nats-server/v2 v2.12.4
|
||||
github.com/nats-io/nats.go v1.49.0
|
||||
github.com/urfave/cli/v3 v3.6.2
|
||||
gitlab.com/gomidi/midi/v2 v2.3.18
|
||||
gitlab.com/gomidi/midi/v2 v2.3.22
|
||||
go.bug.st/serial v1.6.4
|
||||
go.opentelemetry.io/otel v1.40.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.40.0
|
||||
go.opentelemetry.io/otel/sdk v1.40.0
|
||||
go.opentelemetry.io/otel/trace v1.40.0
|
||||
modernc.org/quickjs v0.17.1
|
||||
@@ -25,7 +26,8 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||
github.com/antithesishq/antithesis-sdk-go v0.5.0-default-no-op // indirect
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/creack/goselect v0.1.2 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
@@ -38,14 +40,17 @@ require (
|
||||
github.com/gobwas/httphead v0.1.0 // indirect
|
||||
github.com/gobwas/pool v0.2.1 // indirect
|
||||
github.com/gobwas/ws v1.4.0 // indirect
|
||||
github.com/google/go-tpm v0.9.8 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.21.0 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.7 // indirect
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20240805132620-81f5be970eca // indirect
|
||||
github.com/icholy/digest v1.1.0 // indirect
|
||||
github.com/klauspost/compress v1.18.0 // indirect
|
||||
github.com/klauspost/compress v1.18.3 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/nats-io/nkeys v0.4.11 // indirect
|
||||
github.com/minio/highwayhash v1.0.4-0.20251030100505-070ab1a87a76 // indirect
|
||||
github.com/nats-io/jwt/v2 v2.8.0 // indirect
|
||||
github.com/nats-io/nkeys v0.4.12 // indirect
|
||||
github.com/nats-io/nuid v1.0.1 // indirect
|
||||
github.com/ncruces/go-strftime v1.0.0 // indirect
|
||||
github.com/pion/logging v0.2.4 // indirect
|
||||
@@ -60,20 +65,21 @@ require (
|
||||
github.com/tetratelabs/wazero v1.9.0 // indirect
|
||||
github.com/zaf/g711 v1.4.0 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 // indirect
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.40.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.40.0 // indirect
|
||||
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
|
||||
go.opentelemetry.io/proto/otlp v1.9.0 // indirect
|
||||
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
||||
golang.org/x/crypto v0.42.0 // indirect
|
||||
golang.org/x/crypto v0.47.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect
|
||||
golang.org/x/net v0.44.0 // indirect
|
||||
golang.org/x/net v0.49.0 // indirect
|
||||
golang.org/x/sync v0.19.0 // indirect
|
||||
golang.org/x/sys v0.40.0 // indirect
|
||||
golang.org/x/text v0.29.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
|
||||
google.golang.org/grpc v1.65.0 // indirect
|
||||
google.golang.org/protobuf v1.34.2 // indirect
|
||||
golang.org/x/text v0.33.0 // indirect
|
||||
golang.org/x/time v0.14.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260128011058-8636f8732409 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260128011058-8636f8732409 // indirect
|
||||
google.golang.org/grpc v1.78.0 // indirect
|
||||
google.golang.org/protobuf v1.36.11 // indirect
|
||||
gopkg.in/hraban/opus.v2 v2.0.0-20230925203106-0188a62cb302 // indirect
|
||||
modernc.org/libc v1.67.1 // indirect
|
||||
modernc.org/libquickjs v0.12.3 // indirect
|
||||
|
||||
105
go.sum
105
go.sum
@@ -1,5 +1,7 @@
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||
github.com/antithesishq/antithesis-sdk-go v0.5.0-default-no-op h1:Ucf+QxEKMbPogRO5guBNe5cgd9uZgfoJLOYs8WWhtjM=
|
||||
github.com/antithesishq/antithesis-sdk-go v0.5.0-default-no-op/go.mod h1:IUpT2DPAKh6i/YhSbt6Gl3v2yvUZjmKncl7U91fup7E=
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM=
|
||||
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
|
||||
@@ -12,14 +14,14 @@ github.com/dylibso/observe-sdk/go v0.0.0-20240819160327-2d926c5d788a h1:UwSIFv5g
|
||||
github.com/dylibso/observe-sdk/go v0.0.0-20240819160327-2d926c5d788a/go.mod h1:C8DzXehI4zAbrdlbtOByKX6pfivJTBiV9Jjqv56Yd9Q=
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.1 h1:/VSOv3oDLlpqR2Epjn1Q7b2bSTplJIeV2ISgCl2W7nE=
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.1/go.mod h1:1/yJCneuyOoCOzKSsOTUc0AJfpsItBGWvYpBLimhArU=
|
||||
github.com/emiago/diago v0.26.2 h1:3QL03V0drX96eIBFBpfueNcywydRgYqffKihluGL0gA=
|
||||
github.com/emiago/diago v0.26.2/go.mod h1:jZ+7EnKcmgqKnLjCHPqfbP4Y/9Q/JLSLxMflDrp2J1M=
|
||||
github.com/emiago/diago v0.27.0 h1:5SQBbcLR9ooxhSMlTkU9QSuDv/2nhMO9lxuWJTd/7rE=
|
||||
github.com/emiago/diago v0.27.0/go.mod h1:8hUxCDPJY2p32hh+4ed7vHW/3yTMmEa3BjNctUPeGD0=
|
||||
github.com/emiago/dtls/v3 v3.0.0-20260122183559-8b8d23e359c0 h1:o4LxpUnZ1zxiQ+Qjc9kLwXcjz31NGAHmnZ7xoJto3VM=
|
||||
github.com/emiago/dtls/v3 v3.0.0-20260122183559-8b8d23e359c0/go.mod h1:ydcZ977eS1I6uOWodzMuw30BwvNAzT9su/xcNYSJqjA=
|
||||
github.com/emiago/sipgo v1.1.2 h1:JvLqEvqNSQm2mBX40qZ7O0WC3Ee67Z0UrfmBI7y6Beo=
|
||||
github.com/emiago/sipgo v1.1.2/go.mod h1:DuwAxBZhKMqIzQFPGZb1MVAGU6Wuxj64oTOhd5dx/FY=
|
||||
github.com/expr-lang/expr v1.17.7 h1:Q0xY/e/2aCIp8g9s/LGvMDCC5PxYlvHgDZRQ4y16JX8=
|
||||
github.com/expr-lang/expr v1.17.7/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
|
||||
github.com/emiago/sipgo v1.2.0 h1:rmHFdCu9zu2Cabfd8+/eC9HQWyooqk8x+ti550z5lBw=
|
||||
github.com/emiago/sipgo v1.2.0/go.mod h1:DuwAxBZhKMqIzQFPGZb1MVAGU6Wuxj64oTOhd5dx/FY=
|
||||
github.com/expr-lang/expr v1.17.8 h1:W1loDTT+0PQf5YteHSTpju2qfUfNoBt4yw9+wOEU9VM=
|
||||
github.com/expr-lang/expr v1.17.8/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
|
||||
github.com/extism/go-sdk v1.7.1 h1:lWJos6uY+tRFdlIHR+SJjwFDApY7OypS/2nMhiVQ9Sw=
|
||||
github.com/extism/go-sdk v1.7.1/go.mod h1:IT+Xdg5AZM9hVtpFUA+uZCJMge/hbvshl8bwzLtFyKA=
|
||||
github.com/go-audio/riff v1.0.0 h1:d8iCGbDvox9BfLagY94fBynxSPHO80LmZCaOsmKxokA=
|
||||
@@ -37,14 +39,18 @@ github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
|
||||
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
|
||||
github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs=
|
||||
github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/go-tpm v0.9.8 h1:slArAR9Ft+1ybZu0lBwpSmpwhRXaa85hWtMinMyRAWo=
|
||||
github.com/google/go-tpm v0.9.8/go.mod h1:h9jEsEECg7gtLis0upRBQU+GhYVH6jMjrFxI8u6bVUY=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.21.0 h1:CWyXh/jylQWp2dtiV33mY4iSSp6yf4lmn+c7/tN+ObI=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.21.0/go.mod h1:nCLIt0w3Ept2NwF8ThLmrppXsfT07oC8k0XNDxd8sVU=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.7 h1:X+2YciYSxvMQK0UZ7sg45ZVabVZBeBuvMkmuI2V3Fak=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.7/go.mod h1:lW34nIZuQ8UDPdkon5fmfp2l3+ZkQ2me/+oecHYLOII=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20240805132620-81f5be970eca h1:T54Ema1DU8ngI+aef9ZhAhNGQhcRTrWxVeG07F+c/Rw=
|
||||
@@ -59,18 +65,24 @@ github.com/jwetzell/osc-go v0.1.0 h1:EXxup5VWBErHot2Ri4MFToPf6KCzLDTbCt2x6GLfw8I
|
||||
github.com/jwetzell/osc-go v0.1.0/go.mod h1:xLz0jTwebSxtx1TkKN1YVdeRqvpFNweDhTut5TE393A=
|
||||
github.com/jwetzell/psn-go v0.3.0 h1:WVpCEmExYE8a+I5hQak5jNJJp2x35VdGX/VuMUKPmhY=
|
||||
github.com/jwetzell/psn-go v0.3.0/go.mod h1:bcEAeti4sQM375buujb3mIfmUstD4Aby18gq3ENb6+o=
|
||||
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
||||
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
|
||||
github.com/klauspost/compress v1.18.3 h1:9PJRvfbmTabkOX8moIpXPbMMbYN60bWImDDU7L+/6zw=
|
||||
github.com/klauspost/compress v1.18.3/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/nats-io/nats.go v1.48.0 h1:pSFyXApG+yWU/TgbKCjmm5K4wrHu86231/w84qRVR+U=
|
||||
github.com/nats-io/nats.go v1.48.0/go.mod h1:iRWIPokVIFbVijxuMQq4y9ttaBTMe0SFdlZfMDd+33g=
|
||||
github.com/nats-io/nkeys v0.4.11 h1:q44qGV008kYd9W1b1nEBkNzvnWxtRSQ7A8BoqRrcfa0=
|
||||
github.com/nats-io/nkeys v0.4.11/go.mod h1:szDimtgmfOi9n25JpfIdGw12tZFYXqhGxjhVxsatHVE=
|
||||
github.com/minio/highwayhash v1.0.4-0.20251030100505-070ab1a87a76 h1:KGuD/pM2JpL9FAYvBrnBBeENKZNh6eNtjqytV6TYjnk=
|
||||
github.com/minio/highwayhash v1.0.4-0.20251030100505-070ab1a87a76/go.mod h1:GGYsuwP/fPD6Y9hMiXuapVvlIUEhFhMTh0rxU3ik1LQ=
|
||||
github.com/nats-io/jwt/v2 v2.8.0 h1:K7uzyz50+yGZDO5o772eRE7atlcSEENpL7P+b74JV1g=
|
||||
github.com/nats-io/jwt/v2 v2.8.0/go.mod h1:me11pOkwObtcBNR8AiMrUbtVOUGkqYjMQZ6jnSdVUIA=
|
||||
github.com/nats-io/nats-server/v2 v2.12.4 h1:ZnT10v2LU2Xcoiy8ek9X6Se4YG8EuMfIfvAEuFVx1Ts=
|
||||
github.com/nats-io/nats-server/v2 v2.12.4/go.mod h1:5MCp/pqm5SEfsvVZ31ll1088ZTwEUdvRX1Hmh/mTTDg=
|
||||
github.com/nats-io/nats.go v1.49.0 h1:yh/WvY59gXqYpgl33ZI+XoVPKyut/IcEaqtsiuTJpoE=
|
||||
github.com/nats-io/nats.go v1.49.0/go.mod h1:fDCn3mN5cY8HooHwE2ukiLb4p4G4ImmzvXyJt+tGwdw=
|
||||
github.com/nats-io/nkeys v0.4.12 h1:nssm7JKOG9/x4J8II47VWCL1Ds29avyiQDRn0ckMvDc=
|
||||
github.com/nats-io/nkeys v0.4.12/go.mod h1:MT59A1HYcjIcyQDJStTfaOY6vhy9XTUjOFo+SVsvpBg=
|
||||
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
|
||||
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
|
||||
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
||||
@@ -105,18 +117,18 @@ github.com/urfave/cli/v3 v3.6.2 h1:lQuqiPrZ1cIz8hz+HcrG0TNZFxU70dPZ3Yl+pSrH9A8=
|
||||
github.com/urfave/cli/v3 v3.6.2/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
|
||||
github.com/zaf/g711 v1.4.0 h1:XZYkjjiAg9QTBnHqEg37m2I9q3IIDv5JRYXs2N8ma7c=
|
||||
github.com/zaf/g711 v1.4.0/go.mod h1:eCDXt3dSp/kYYAoooba7ukD/Q75jvAaS4WOMr0l1Roo=
|
||||
gitlab.com/gomidi/midi/v2 v2.3.18 h1:sj2fOhtvOe+zI8YJe8qTxLw5zv0ntULLUDwcFOaZQbI=
|
||||
gitlab.com/gomidi/midi/v2 v2.3.18/go.mod h1:jDpP4O4skYi+7iVwt6Zyp18bd2M4hkjtMuw2cmgKgfw=
|
||||
gitlab.com/gomidi/midi/v2 v2.3.22 h1:4Q20o6q4BDo7i/KGvnwASeytOlrPI7MwsS7F2hA7fOM=
|
||||
gitlab.com/gomidi/midi/v2 v2.3.22/go.mod h1:jDpP4O4skYi+7iVwt6Zyp18bd2M4hkjtMuw2cmgKgfw=
|
||||
go.bug.st/serial v1.6.4 h1:7FmqNPgVp3pu2Jz5PoPtbZ9jJO5gnEnZIvnI1lzve8A=
|
||||
go.bug.st/serial v1.6.4/go.mod h1:nofMJxTeNVny/m6+KaafC6vJGj3miwQZ6vW4BZUGJPI=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||
go.opentelemetry.io/otel v1.40.0 h1:oA5YeOcpRTXq6NN7frwmwFR0Cn3RhTVZvXsP4duvCms=
|
||||
go.opentelemetry.io/otel v1.40.0/go.mod h1:IMb+uXZUKkMXdPddhwAHm6UfOwJyh4ct1ybIlV14J0g=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 h1:3Q/xZUyC1BBkualc9ROb4G8qkH90LXEIICcs5zv1OYY=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0/go.mod h1:s75jGIWA9OfCMzF0xr+ZgfrB5FEbbV7UuYo32ahUiFI=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0 h1:j9+03ymgYhPKmeXGk5Zu+cIZOlVzd9Zv7QIiyItjFBU=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0/go.mod h1:Y5+XiUG4Emn1hTfciPzGPJaSI+RpDts6BnCIir0SLqk=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.40.0 h1:QKdN8ly8zEMrByybbQgv8cWBcdAarwmIPZ6FThrWXJs=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.40.0/go.mod h1:bTdK1nhqF76qiPoCCdyFIV+N/sRHYXYCTQc+3VCi3MI=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.40.0 h1:wVZXIWjQSeSmMoxF74LzAnpVQOAFDo3pPji9Y4SOFKc=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.40.0/go.mod h1:khvBS2IggMFNwZK/6lEeHg/W57h/IX6J4URh57fuI40=
|
||||
go.opentelemetry.io/otel/metric v1.40.0 h1:rcZe317KPftE2rstWIBitCdVp89A2HqjkxR3c11+p9g=
|
||||
go.opentelemetry.io/otel/metric v1.40.0/go.mod h1:ib/crwQH7N3r5kfiBZQbwrTge743UDc7DTFVZrrXnqc=
|
||||
go.opentelemetry.io/otel/sdk v1.40.0 h1:KHW/jUzgo6wsPh9At46+h4upjtccTmuZCFAc9OJ71f8=
|
||||
@@ -125,39 +137,44 @@ go.opentelemetry.io/otel/sdk/metric v1.40.0 h1:mtmdVqgQkeRxHgRv4qhyJduP3fYJRMX4A
|
||||
go.opentelemetry.io/otel/sdk/metric v1.40.0/go.mod h1:4Z2bGMf0KSK3uRjlczMOeMhKU2rhUqdWNoKcYrtcBPg=
|
||||
go.opentelemetry.io/otel/trace v1.40.0 h1:WA4etStDttCSYuhwvEa8OP8I5EWu24lkOzp+ZYblVjw=
|
||||
go.opentelemetry.io/otel/trace v1.40.0/go.mod h1:zeAhriXecNGP/s2SEG3+Y8X9ujcJOTqQ5RgdEJcawiA=
|
||||
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
|
||||
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
|
||||
go.opentelemetry.io/proto/otlp v1.9.0 h1:l706jCMITVouPOqEnii2fIAuO3IVGBRPV5ICjceRb/A=
|
||||
go.opentelemetry.io/proto/otlp v1.9.0/go.mod h1:xE+Cx5E/eEHw+ISFkwPLwCZefwVjY+pqKg1qcK03+/4=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
||||
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
||||
go.yaml.in/yaml/v3 v3.0.3 h1:bXOww4E/J3f66rav3pX3m8w6jDE4knZjGOw8b5Y6iNE=
|
||||
go.yaml.in/yaml/v3 v3.0.3/go.mod h1:tBHosrYAkRZjRAOREWbDnBXUf08JOwYq++0QNwQiWzI=
|
||||
golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
|
||||
golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8=
|
||||
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
|
||||
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||
golang.org/x/crypto v0.47.0 h1:V6e3FRj+n4dbpw86FJ8Fv7XVOql7TEwpHapKoMJ/GO8=
|
||||
golang.org/x/crypto v0.47.0/go.mod h1:ff3Y9VzzKbwSSEzWqJsJVBnWmRwRSHt/6Op5n9bQc4A=
|
||||
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 h1:mgKeJMpvi0yx/sU5GsxQ7p6s2wtOnGAHZWCHUM4KGzY=
|
||||
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546/go.mod h1:j/pmGrbnkbPtQfxEe5D0VQhZC6qKbfKifgD0oM7sR70=
|
||||
golang.org/x/mod v0.30.0 h1:fDEXFVZ/fmCKProc/yAXXUijritrDzahmwwefnjoPFk=
|
||||
golang.org/x/mod v0.30.0/go.mod h1:lAsf5O2EvJeSFMiBxXDki7sCgAxEUcZHXoXMKT4GJKc=
|
||||
golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I=
|
||||
golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
|
||||
golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI=
|
||||
golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg=
|
||||
golang.org/x/net v0.49.0 h1:eeHFmOGUTtaaPSGNmjBKpbng9MulQsJURQUAfUwY++o=
|
||||
golang.org/x/net v0.49.0/go.mod h1:/ysNB2EvaqvesRkuLAyjI1ycPZlQHM3q01F02UY/MV8=
|
||||
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
|
||||
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
|
||||
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
|
||||
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
|
||||
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
|
||||
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 h1:wKguEg1hsxI2/L3hUYrpo1RVi48K+uTyzKqprwLXsb8=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142/go.mod h1:d6be+8HhtEtucleCbxpPW9PA9XwISACu8nvpPqF0BVo=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 h1:e7S5W7MGGLaSu8j3YjdezkZ+m1/Nm0uRVRMEMGk26Xs=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
|
||||
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
|
||||
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
|
||||
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||
golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE=
|
||||
golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8=
|
||||
golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI=
|
||||
golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
|
||||
golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA=
|
||||
golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc=
|
||||
gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk=
|
||||
gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260128011058-8636f8732409 h1:merA0rdPeUV3YIIfHHcH4qBkiQAc1nfCKSI7lB4cV2M=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260128011058-8636f8732409/go.mod h1:fl8J1IvUjCilwZzQowmw2b7HQB2eAuYBabMXzWurF+I=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260128011058-8636f8732409 h1:H86B94AW+VfJWDqFeEbBPhEtHzJwJfTbgE2lZa54ZAQ=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260128011058-8636f8732409/go.mod h1:j9x/tPzZkyxcgEFkiKEEGxfvyumM01BEtsW8xzOahRQ=
|
||||
google.golang.org/grpc v1.78.0 h1:K1XZG/yGDJnzMdd/uZHAkVqJE+xIDOcmdSFZkBUicNc=
|
||||
google.golang.org/grpc v1.78.0/go.mod h1:I47qjTo4OKbMkjA/aOOwxDIiPSBofUtQUI5EfpWvW7U=
|
||||
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
|
||||
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
|
||||
@@ -1,14 +1,69 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
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")
|
||||
)
|
||||
|
||||
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 {
|
||||
floatValue, ok := value.(float64)
|
||||
if !ok {
|
||||
return 0, ErrParamNotNumber
|
||||
}
|
||||
intValue = int(floatValue)
|
||||
}
|
||||
return intValue, nil
|
||||
}
|
||||
|
||||
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, errors.New("not a boolean")
|
||||
}
|
||||
return boolValue, nil
|
||||
}
|
||||
|
||||
type ModuleConfig struct {
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Params map[string]any `json:"params"`
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Params Params `json:"params"`
|
||||
}
|
||||
|
||||
type RouteConfig struct {
|
||||
@@ -18,6 +73,6 @@ type RouteConfig struct {
|
||||
}
|
||||
|
||||
type ProcessorConfig struct {
|
||||
Type string `json:"type"`
|
||||
Params map[string]any `json:"params"`
|
||||
Type string `json:"type"`
|
||||
Params Params `json:"params"`
|
||||
}
|
||||
|
||||
@@ -57,17 +57,10 @@ func init() {
|
||||
Type: "http.server",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, errors.New("http.server requires a port parameter")
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.server port error: %w", err)
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.server port must be uint16")
|
||||
}
|
||||
|
||||
return &HTTPServer{Port: uint16(portNum), config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
|
||||
@@ -29,16 +29,9 @@ func init() {
|
||||
Type: "midi.input",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.input requires a port parameter")
|
||||
}
|
||||
|
||||
portString, ok := port.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.input port must be a string")
|
||||
portString, err := params.GetString("port")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.input port error: %w", err)
|
||||
}
|
||||
|
||||
return &MIDIInput{config: config, Port: portString, logger: CreateLogger(config)}, nil
|
||||
|
||||
@@ -30,16 +30,9 @@ func init() {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
|
||||
port, ok := params["port"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.output requires a port parameter")
|
||||
}
|
||||
|
||||
portString, ok := port.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.output port must be a string")
|
||||
portString, err := params.GetString("port")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.output port error: %w", err)
|
||||
}
|
||||
|
||||
return &MIDIOutput{config: config, Port: portString, logger: CreateLogger(config)}, nil
|
||||
|
||||
@@ -3,6 +3,7 @@ package module
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
@@ -27,40 +28,22 @@ func init() {
|
||||
Type: "mqtt.client",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
broker, ok := params["broker"]
|
||||
brokerString, err := params.GetString("broker")
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.client requires a broker parameter")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mqtt.client broker error: %w", err)
|
||||
}
|
||||
|
||||
brokerString, ok := broker.(string)
|
||||
topicString, err := params.GetString("topic")
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.client broker must be string")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mqtt.client topic error: %w", err)
|
||||
}
|
||||
|
||||
topic, ok := params["topic"]
|
||||
clientIdString, err := params.GetString("clientId")
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.client requires a topic parameter")
|
||||
}
|
||||
|
||||
topicString, ok := topic.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.client topic must be string")
|
||||
}
|
||||
|
||||
clientId, ok := params["clientId"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.client requires a clientId parameter")
|
||||
}
|
||||
|
||||
clientIdString, ok := clientId.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.client clientId must be string")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mqtt.client clientId error: %w", err)
|
||||
}
|
||||
|
||||
return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString, logger: CreateLogger(config)}, nil
|
||||
|
||||
@@ -27,28 +27,15 @@ func init() {
|
||||
Type: "nats.client",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
url, ok := params["url"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("nats.client requires a url parameter")
|
||||
urlString, err := params.GetString("url")
|
||||
if err != nil {
|
||||
return nil, errors.New("nats.client url error: " + err.Error())
|
||||
}
|
||||
|
||||
urlString, ok := url.(string)
|
||||
subjectString, err := params.GetString("subject")
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("nats.client url must be string")
|
||||
}
|
||||
|
||||
subject, ok := params["subject"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("nats.client requires a subject parameter")
|
||||
}
|
||||
|
||||
subjectString, ok := subject.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("nats.client subject must be string")
|
||||
if err != nil {
|
||||
return nil, errors.New("nats.client subject error: " + err.Error())
|
||||
}
|
||||
|
||||
return &NATSClient{config: config, URL: urlString, Subject: subjectString, logger: CreateLogger(config)}, nil
|
||||
|
||||
114
internal/module/nats-server.go
Normal file
114
internal/module/nats-server.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
"github.com/nats-io/nats-server/v2/server"
|
||||
)
|
||||
|
||||
type NATSServer struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
Ip string
|
||||
Port int
|
||||
router route.RouteIO
|
||||
server *server.Server
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "nats.server",
|
||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
||||
params := moduleConfig.Params
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
portNum = 4222
|
||||
} else {
|
||||
return nil, fmt.Errorf("nats.server port error: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
ipString, err := params.GetString("ip")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
ipString = "0.0.0.0"
|
||||
} else {
|
||||
return nil, fmt.Errorf("nats.server ip error: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", ipString, uint16(portNum)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &NATSServer{config: moduleConfig, logger: CreateLogger(moduleConfig), Ip: ipString, Port: portNum}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (ns *NATSServer) Id() string {
|
||||
return ns.config.Id
|
||||
}
|
||||
|
||||
func (ns *NATSServer) Type() string {
|
||||
return ns.config.Type
|
||||
}
|
||||
|
||||
func (ns *NATSServer) Start(ctx context.Context) error {
|
||||
ns.logger.Debug("running")
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("nats.server unable to get router from context")
|
||||
}
|
||||
|
||||
ns.router = router
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
ns.ctx = moduleContext
|
||||
ns.cancel = cancel
|
||||
|
||||
natsServer, err := server.NewServer(&server.Options{
|
||||
Host: ns.Ip,
|
||||
Port: ns.Port,
|
||||
NoLog: true,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ns.server = natsServer
|
||||
natsServer.Start()
|
||||
defer natsServer.Shutdown()
|
||||
|
||||
if !natsServer.ReadyForConnections(5 * time.Second) {
|
||||
return errors.New("nats.server failed to start")
|
||||
}
|
||||
ns.logger.Info("NATS server started", "client_url", natsServer.ClientURL())
|
||||
|
||||
<-ns.ctx.Done()
|
||||
|
||||
ns.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ns *NATSServer) Output(ctx context.Context, payload any) error {
|
||||
return errors.ErrUnsupported
|
||||
}
|
||||
|
||||
func (ns *NATSServer) Stop() {
|
||||
ns.cancel()
|
||||
if ns.server != nil {
|
||||
ns.server.Shutdown()
|
||||
}
|
||||
}
|
||||
@@ -32,48 +32,29 @@ func init() {
|
||||
Type: "serial.client",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("serial.client requires a port parameter")
|
||||
portString, err := params.GetString("port")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("serial.client port error: %w", err)
|
||||
}
|
||||
|
||||
portString, ok := port.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("serial.client port must be a string")
|
||||
}
|
||||
|
||||
framingMethod, ok := params["framing"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("serial.client requires a framing parameter")
|
||||
}
|
||||
|
||||
framingMethodString, ok := framingMethod.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("serial.client framing method must be a string")
|
||||
framingMethodString, err := params.GetString("framing")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("serial.client framing error: %w", err)
|
||||
}
|
||||
|
||||
framer := framer.GetFramer(framingMethodString)
|
||||
|
||||
if framer == nil {
|
||||
return nil, fmt.Errorf("serial.client unknown framing method: %s", framingMethod)
|
||||
return nil, fmt.Errorf("serial.client unknown framing method: %s", framingMethodString)
|
||||
}
|
||||
|
||||
buadRate, ok := params["baudRate"]
|
||||
if !ok {
|
||||
return nil, errors.New("serial.client requires a baudRate parameter")
|
||||
}
|
||||
|
||||
baudRateNum, ok := buadRate.(float64)
|
||||
if !ok {
|
||||
return nil, errors.New("serial.client baudRate must be a number")
|
||||
baudRateInt, err := params.GetInt("baudRate")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("serial.client baudRate error: %w", err)
|
||||
}
|
||||
|
||||
mode := serial.Mode{
|
||||
BaudRate: int(baudRateNum),
|
||||
BaudRate: baudRateInt,
|
||||
}
|
||||
|
||||
return &SerialClient{config: config, Port: portString, Framer: framer, Mode: &mode, logger: CreateLogger(config)}, nil
|
||||
|
||||
@@ -46,60 +46,46 @@ type sipCallContextKey string
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "sip.call.server",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
portNum := 5060
|
||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
||||
params := moduleConfig.Params
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
|
||||
port, ok := params["port"]
|
||||
if ok {
|
||||
specificPortNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.call.server port must be a number")
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
portNum = 5060
|
||||
} else {
|
||||
return nil, fmt.Errorf("sip.call.server port error: %w", err)
|
||||
}
|
||||
portNum = int(specificPortNum)
|
||||
}
|
||||
|
||||
ipString := "0.0.0.0"
|
||||
|
||||
ip, ok := params["ip"]
|
||||
if ok {
|
||||
|
||||
specificIpString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.call.server ip must be a string")
|
||||
ipString, err := params.GetString("ip")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
ipString = "0.0.0.0"
|
||||
} else {
|
||||
return nil, fmt.Errorf("sip.call.server ip error: %w", err)
|
||||
}
|
||||
ipString = specificIpString
|
||||
}
|
||||
|
||||
transportString := "udp"
|
||||
|
||||
transport, ok := params["transport"]
|
||||
if ok {
|
||||
|
||||
specificTransportString, ok := transport.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.call.server transport must be a string")
|
||||
transportString, err := params.GetString("transport")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
transportString = "udp"
|
||||
} else {
|
||||
return nil, fmt.Errorf("sip.call.server transport error: %w", err)
|
||||
}
|
||||
transportString = specificTransportString
|
||||
}
|
||||
|
||||
userAgentString := "showbridge"
|
||||
|
||||
userAgent, ok := params["userAgent"]
|
||||
if ok {
|
||||
|
||||
specificTransportString, ok := userAgent.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.call.server userAgent must be a string")
|
||||
userAgentString, err := params.GetString("userAgent")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
userAgentString = "showbridge"
|
||||
} else {
|
||||
return nil, fmt.Errorf("sip.call.server userAgent error: %w", err)
|
||||
}
|
||||
userAgentString = specificTransportString
|
||||
}
|
||||
|
||||
return &SIPCallServer{config: config, IP: ipString, Port: int(portNum), Transport: transportString, UserAgent: userAgentString, logger: CreateLogger(config)}, nil
|
||||
return &SIPCallServer{config: moduleConfig, IP: ipString, Port: int(portNum), Transport: transportString, UserAgent: userAgentString, logger: CreateLogger(moduleConfig)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -191,7 +177,6 @@ func (scs *SIPCallServer) Output(ctx context.Context, payload any) error {
|
||||
|
||||
if ok {
|
||||
dtmfWriter := call.inDialog.AudioWriterDTMF()
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadDTMFResponse.PreWait))
|
||||
for i, dtmfRune := range payloadDTMFResponse.Digits {
|
||||
err := dtmfWriter.WriteDTMF(dtmfRune)
|
||||
@@ -200,7 +185,7 @@ func (scs *SIPCallServer) Output(ctx context.Context, payload any) error {
|
||||
return fmt.Errorf("sip.dtmf.server error output dtmf digit at index %d", i)
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadDTMFResponse.PreWait))
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadDTMFResponse.PostWait))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ type SIPDTMFServer struct {
|
||||
IP string
|
||||
Port int
|
||||
Transport string
|
||||
UserAgent string
|
||||
Separator string
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
@@ -45,53 +46,49 @@ type SIPDTMFCall struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "sip.dtmf.server",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
portNum := 5060
|
||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
||||
params := moduleConfig.Params
|
||||
|
||||
port, ok := params["port"]
|
||||
if ok {
|
||||
specificPortNum, ok := port.(float64)
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.dtmf.server port must be a number")
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
portNum = 5060
|
||||
} else {
|
||||
return nil, fmt.Errorf("sip.dtmf.server port error: %w", err)
|
||||
}
|
||||
portNum = int(specificPortNum)
|
||||
}
|
||||
|
||||
ipString := "0.0.0.0"
|
||||
|
||||
ip, ok := params["ip"]
|
||||
if ok {
|
||||
|
||||
specificIpString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.dtmf.server ip must be a string")
|
||||
ipString, err := params.GetString("ip")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
ipString = "0.0.0.0"
|
||||
} else {
|
||||
return nil, fmt.Errorf("sip.dtmf.server ip error: %w", err)
|
||||
}
|
||||
ipString = specificIpString
|
||||
}
|
||||
|
||||
transportString := "udp"
|
||||
|
||||
transport, ok := params["transport"]
|
||||
if ok {
|
||||
|
||||
specificTransportString, ok := transport.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.dtmf.server transport must be a string")
|
||||
transportString, err := params.GetString("transport")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
transportString = "udp"
|
||||
} else {
|
||||
return nil, fmt.Errorf("sip.dtmf.server transport error: %w", err)
|
||||
}
|
||||
transportString = specificTransportString
|
||||
}
|
||||
|
||||
separator, ok := params["separator"]
|
||||
if !ok {
|
||||
return nil, errors.New("sip.dtmf.server requires a separator parameter")
|
||||
userAgentString, err := params.GetString("userAgent")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
userAgentString = "showbridge"
|
||||
} else {
|
||||
return nil, fmt.Errorf("sip.dtmf.server userAgent error: %w", err)
|
||||
}
|
||||
}
|
||||
separatorString, ok := separator.(string)
|
||||
if !ok {
|
||||
return nil, errors.New("sip.dtmf.server separator must be a string")
|
||||
|
||||
separatorString, err := params.GetString("separator")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sip.dtmf.server separator error: %w", err)
|
||||
}
|
||||
|
||||
if len(separatorString) != 1 {
|
||||
@@ -101,7 +98,7 @@ func init() {
|
||||
if !strings.ContainsRune("0123456789*#ABCD", rune(separatorString[0])) {
|
||||
return nil, errors.New("sip.dtmf.server separator must be a valid DTMF character")
|
||||
}
|
||||
return &SIPDTMFServer{config: config, IP: ipString, Port: int(portNum), Transport: transportString, Separator: separatorString, logger: CreateLogger(config)}, nil
|
||||
return &SIPDTMFServer{config: moduleConfig, IP: ipString, Port: int(portNum), Transport: transportString, UserAgent: userAgentString, Separator: separatorString, logger: CreateLogger(moduleConfig)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -129,6 +126,7 @@ func (sds *SIPDTMFServer) Start(ctx context.Context) error {
|
||||
diagoLogger := slog.New(slog.NewJSONHandler(io.Discard, nil))
|
||||
|
||||
ua, _ := sipgo.NewUA(
|
||||
sipgo.WithUserAgent(sds.UserAgent),
|
||||
sipgo.WithUserAgentTransportLayerOptions(sip.WithTransportLayerLogger(diagoLogger)),
|
||||
sipgo.WithUserAgentTransactionLayerOptions(sip.WithTransactionLayerLogger(diagoLogger)),
|
||||
)
|
||||
@@ -214,7 +212,7 @@ func (sds *SIPDTMFServer) Output(ctx context.Context, payload any) error {
|
||||
return fmt.Errorf("sip.dtmf.server error output dtmf digit at index %d", i)
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadDTMFResponse.PreWait))
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadDTMFResponse.PostWait))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -29,27 +29,14 @@ func init() {
|
||||
Type: "net.tcp.client",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
host, ok := params["host"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.client requires a host parameter")
|
||||
hostString, err := params.GetString("host")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("net.tcp.client host error: %w", err)
|
||||
}
|
||||
|
||||
hostString, ok := host.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.client host must be string")
|
||||
}
|
||||
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.client requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.client port must be a number")
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("net.tcp.client port error: %w", err)
|
||||
}
|
||||
|
||||
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", hostString, uint16(portNum)))
|
||||
@@ -57,22 +44,15 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
framingMethod, ok := params["framing"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.client requires a framing parameter")
|
||||
}
|
||||
|
||||
framingMethodString, ok := framingMethod.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.client framing method must be a string")
|
||||
framingMethodString, err := params.GetString("framing")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("net.tcp.client framing error: %w", err)
|
||||
}
|
||||
|
||||
framer := framer.GetFramer(framingMethodString)
|
||||
|
||||
if framer == nil {
|
||||
return nil, fmt.Errorf("net.tcp.client unknown framing method: %s", framingMethod)
|
||||
return nil, fmt.Errorf("net.tcp.client unknown framing method: %s", framingMethodString)
|
||||
}
|
||||
return &TCPClient{framer: framer, Addr: addr, config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
|
||||
@@ -33,55 +33,38 @@ type TCPServer struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.tcp.server",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.server requires a port parameter")
|
||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
||||
params := moduleConfig.Params
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("net.tcp.server port error: %w", err)
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.server port must be a number")
|
||||
}
|
||||
|
||||
framingMethod, ok := params["framing"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.server requires a framing parameter")
|
||||
}
|
||||
|
||||
framingMethodString, ok := framingMethod.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.server framing method must be a string")
|
||||
framingMethodString, err := params.GetString("framing")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("net.tcp.server framing error: %w", err)
|
||||
}
|
||||
|
||||
framer := framer.GetFramer(framingMethodString)
|
||||
|
||||
if framer == nil {
|
||||
return nil, fmt.Errorf("net.tcp.server unknown framing method: %s", framingMethod)
|
||||
return nil, fmt.Errorf("net.tcp.server unknown framing method: %s", framingMethodString)
|
||||
}
|
||||
|
||||
ipString := "0.0.0.0"
|
||||
|
||||
ip, ok := params["ip"]
|
||||
if ok {
|
||||
|
||||
specificIpString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.server ip must be a string")
|
||||
ipString, err := params.GetString("ip")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
ipString = "0.0.0.0"
|
||||
} else {
|
||||
return nil, fmt.Errorf("net.tcp.server ip error: %w", err)
|
||||
}
|
||||
ipString = specificIpString
|
||||
}
|
||||
|
||||
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", ipString, uint16(portNum)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &TCPServer{Framer: framer, Addr: addr, config: config, quit: make(chan interface{}), logger: CreateLogger(config)}, nil
|
||||
return &TCPServer{Framer: framer, Addr: addr, config: moduleConfig, quit: make(chan interface{}), logger: CreateLogger(moduleConfig)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
73
internal/module/test/http-client_test.go
Normal file
73
internal/module/test/http-client_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestHTTPClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["http.client"]
|
||||
if !ok {
|
||||
t.Fatalf("http.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "http.client",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create http.client module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("http.client module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "http.client" {
|
||||
t.Fatalf("http.client module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadHTTPClient(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["http.client"]
|
||||
if !ok {
|
||||
t.Fatalf("http.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "http.client",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("http.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("http.client expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("http.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
87
internal/module/test/http-server_test.go
Normal file
87
internal/module/test/http-server_test.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestHTTPServerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["http.server"]
|
||||
if !ok {
|
||||
t.Fatalf("http.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "http.server",
|
||||
Params: map[string]any{
|
||||
"port": 3000.0,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create http.server module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("http.server module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "http.server" {
|
||||
t.Fatalf("http.server module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadHTTPServer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no port param",
|
||||
params: map[string]any{},
|
||||
errorString: "http.server port error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-numeric port",
|
||||
params: map[string]any{"port": "3000"},
|
||||
errorString: "http.server port error: not a number",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["http.server"]
|
||||
if !ok {
|
||||
t.Fatalf("http.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "http.server",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("http.server got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("http.server expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("http.server got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
87
internal/module/test/midi-input_test.go
Normal file
87
internal/module/test/midi-input_test.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestMIDIInputFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["midi.input"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.input module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "midi.input",
|
||||
Params: map[string]any{
|
||||
"port": "test",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create midi.input module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("midi.input module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "midi.input" {
|
||||
t.Fatalf("midi.input module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadMIDIInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no port param",
|
||||
params: map[string]any{},
|
||||
errorString: "midi.input port error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string port",
|
||||
params: map[string]any{"port": 123},
|
||||
errorString: "midi.input port error: not a string",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["midi.input"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.input module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "midi.input",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("midi.input got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("midi.input expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("midi.input got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
87
internal/module/test/midi-output_test.go
Normal file
87
internal/module/test/midi-output_test.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestMIDIOutputFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["midi.output"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.output module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "midi.output",
|
||||
Params: map[string]any{
|
||||
"port": "test",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create midi.output module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("midi.output module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "midi.output" {
|
||||
t.Fatalf("midi.output module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadMIDIOutput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no port param",
|
||||
params: map[string]any{},
|
||||
errorString: "midi.output port error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string port",
|
||||
params: map[string]any{"port": 123},
|
||||
errorString: "midi.output port error: not a string",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["midi.output"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.output module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "midi.output",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("midi.output got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("midi.output expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("midi.output got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
81
internal/module/test/module_test.go
Normal file
81
internal/module/test/module_test.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
type TestModule struct {
|
||||
}
|
||||
|
||||
func (m *TestModule) Output(ctx context.Context, payload any) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TestModule) Start(ctx context.Context) error {
|
||||
<-ctx.Done()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TestModule) Stop() {}
|
||||
|
||||
func (m *TestModule) Type() string {
|
||||
return "module.test"
|
||||
}
|
||||
|
||||
func (m *TestModule) Id() string {
|
||||
return "test"
|
||||
}
|
||||
|
||||
func TestModuleBadRegistrationNoType(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Fatalf("module registration should have panicked but did not")
|
||||
}
|
||||
}()
|
||||
|
||||
module.RegisterModule(module.ModuleRegistration{
|
||||
Type: "",
|
||||
New: func(config config.ModuleConfig) (module.Module, error) {
|
||||
return &TestModule{}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestModuleBadRegistrationNoNew(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Fatalf("processor registration should have panicked but did not")
|
||||
}
|
||||
}()
|
||||
|
||||
module.RegisterModule(module.ModuleRegistration{
|
||||
Type: "module.test",
|
||||
New: nil,
|
||||
})
|
||||
}
|
||||
|
||||
func TestModuleBadRegistrationExistingType(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Fatalf("processor registration should have panicked but did not")
|
||||
}
|
||||
}()
|
||||
|
||||
module.RegisterModule(module.ModuleRegistration{
|
||||
Type: "module.test",
|
||||
New: func(config config.ModuleConfig) (module.Module, error) {
|
||||
return &TestModule{}, nil
|
||||
},
|
||||
})
|
||||
|
||||
module.RegisterModule(module.ModuleRegistration{
|
||||
Type: "module.test",
|
||||
New: func(config config.ModuleConfig) (module.Module, error) {
|
||||
return &TestModule{}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
130
internal/module/test/mqtt-client_test.go
Normal file
130
internal/module/test/mqtt-client_test.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestMQTTClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["mqtt.client"]
|
||||
if !ok {
|
||||
t.Fatalf("mqtt.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "mqtt.client",
|
||||
Params: map[string]any{
|
||||
"broker": "mqtt://localhost:1883",
|
||||
"topic": "test/topic",
|
||||
"clientId": "test",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create mqtt.client module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("mqtt.client module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "mqtt.client" {
|
||||
t.Fatalf("mqtt.client module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadMQTTClient(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no broker param",
|
||||
params: map[string]any{
|
||||
"topic": "test/topic",
|
||||
"clientId": "test",
|
||||
},
|
||||
errorString: "mqtt.client broker error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string broker",
|
||||
params: map[string]any{
|
||||
"broker": 123,
|
||||
"topic": "test/topic",
|
||||
"clientId": "test",
|
||||
},
|
||||
errorString: "mqtt.client broker error: not a string",
|
||||
},
|
||||
{
|
||||
name: "no topic param",
|
||||
params: map[string]any{
|
||||
"broker": "mqtt://localhost:1883",
|
||||
"clientId": "test",
|
||||
},
|
||||
errorString: "mqtt.client topic error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string topic",
|
||||
params: map[string]any{
|
||||
"broker": "mqtt://localhost:1883",
|
||||
"topic": 123,
|
||||
"clientId": "test",
|
||||
},
|
||||
errorString: "mqtt.client topic error: not a string",
|
||||
},
|
||||
{
|
||||
name: "no clientId param",
|
||||
params: map[string]any{
|
||||
"broker": "mqtt://localhost:1883",
|
||||
"topic": "test/topic",
|
||||
},
|
||||
errorString: "mqtt.client clientId error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string clientId",
|
||||
params: map[string]any{
|
||||
"broker": "mqtt://localhost:1883",
|
||||
"topic": "test/topic",
|
||||
"clientId": 123,
|
||||
},
|
||||
errorString: "mqtt.client clientId error: not a string",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["mqtt.client"]
|
||||
if !ok {
|
||||
t.Fatalf("mqtt.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "mqtt.client",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("mqtt.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("mqtt.client expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("mqtt.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
108
internal/module/test/nats-client_test.go
Normal file
108
internal/module/test/nats-client_test.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestNATSClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["nats.client"]
|
||||
if !ok {
|
||||
t.Fatalf("nats.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "nats.client",
|
||||
Params: map[string]any{
|
||||
"url": "nats://127.0.0.1:4222",
|
||||
"subject": "test",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create nats.client module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("nats.client module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "nats.client" {
|
||||
t.Fatalf("nats.client module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadNATSClient(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no url param",
|
||||
params: map[string]any{
|
||||
"subject": "test/subject",
|
||||
},
|
||||
errorString: "nats.client url error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string url",
|
||||
params: map[string]any{
|
||||
"url": 123,
|
||||
"subject": "test/subject",
|
||||
},
|
||||
errorString: "nats.client url error: not a string",
|
||||
},
|
||||
{
|
||||
name: "no subject param",
|
||||
params: map[string]any{
|
||||
"url": "nats://127.0.0.1:4222",
|
||||
},
|
||||
errorString: "nats.client subject error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string subject",
|
||||
params: map[string]any{
|
||||
"url": "nats://127.0.0.1:4222",
|
||||
"subject": 123,
|
||||
},
|
||||
errorString: "nats.client subject error: not a string",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["nats.client"]
|
||||
if !ok {
|
||||
t.Fatalf("nats.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "nats.client",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("nats.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("nats.client expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("nats.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
85
internal/module/test/nats-server_test.go
Normal file
85
internal/module/test/nats-server_test.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestNATSServerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["nats.server"]
|
||||
if !ok {
|
||||
t.Fatalf("nats.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "nats.server",
|
||||
Params: map[string]any{
|
||||
"ip": "127.0.0.1",
|
||||
"port": 4222,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create nats.server module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("nats.server module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "nats.server" {
|
||||
t.Fatalf("nats.server module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadNATSServer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "non-string ip",
|
||||
params: map[string]any{
|
||||
"ip": 123,
|
||||
},
|
||||
errorString: "nats.server ip error: not a string",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["nats.server"]
|
||||
if !ok {
|
||||
t.Fatalf("nats.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "nats.server",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("nats.server got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("nats.server expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("nats.server got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
73
internal/module/test/psn-client_test.go
Normal file
73
internal/module/test/psn-client_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestPSNClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["psn.client"]
|
||||
if !ok {
|
||||
t.Fatalf("psn.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "psn.client",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create psn.client module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("psn.client module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "psn.client" {
|
||||
t.Fatalf("psn.client module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadPSNClient(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["psn.client"]
|
||||
if !ok {
|
||||
t.Fatalf("psn.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "psn.client",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("psn.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("psn.client expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("psn.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
117
internal/module/test/serial-client_test.go
Normal file
117
internal/module/test/serial-client_test.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestSerialClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["serial.client"]
|
||||
if !ok {
|
||||
t.Fatalf("serial.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "serial.client",
|
||||
Params: map[string]any{
|
||||
"port": "/dev/ttyUSB0",
|
||||
"framing": "LF",
|
||||
"baudRate": 9600.0,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create serial.client module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("serial.client module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "serial.client" {
|
||||
t.Fatalf("serial.client module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadSerialClient(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no port param",
|
||||
params: map[string]any{
|
||||
"framing": "LF",
|
||||
},
|
||||
errorString: "serial.client port error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string port param",
|
||||
params: map[string]any{
|
||||
"port": 8000,
|
||||
"framing": "LF",
|
||||
},
|
||||
errorString: "serial.client port error: not a string",
|
||||
},
|
||||
{
|
||||
name: "no framing param",
|
||||
params: map[string]any{
|
||||
"port": "/dev/ttyUSB0",
|
||||
},
|
||||
errorString: "serial.client framing error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string framing param",
|
||||
params: map[string]any{
|
||||
"port": "/dev/ttyUSB0",
|
||||
"framing": 1,
|
||||
},
|
||||
errorString: "serial.client framing error: not a string",
|
||||
},
|
||||
{
|
||||
name: "unkown framing method",
|
||||
params: map[string]any{
|
||||
"port": "/dev/ttyUSB0",
|
||||
"framing": "asdfasdfasdfasdflkj",
|
||||
},
|
||||
errorString: "serial.client unknown framing method: asdfasdfasdfasdflkj",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["serial.client"]
|
||||
if !ok {
|
||||
t.Fatalf("serial.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "serial.client",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("serial.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("serial.client expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("serial.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
102
internal/module/test/sip-call-server_test.go
Normal file
102
internal/module/test/sip-call-server_test.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestSIPCallServerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["sip.call.server"]
|
||||
if !ok {
|
||||
t.Fatalf("sip.call.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "sip.call.server",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create sip.call.server module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("sip.call.server module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "sip.call.server" {
|
||||
t.Fatalf("sip.call.server module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadSIPCallServer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "non-number port param",
|
||||
params: map[string]any{
|
||||
"port": "8000",
|
||||
},
|
||||
errorString: "sip.call.server port error: not a number",
|
||||
},
|
||||
{
|
||||
name: "non-string ip param",
|
||||
params: map[string]any{
|
||||
"ip": 123,
|
||||
},
|
||||
errorString: "sip.call.server ip error: not a string",
|
||||
},
|
||||
{
|
||||
name: "non-string transport param",
|
||||
params: map[string]any{
|
||||
"transport": 123,
|
||||
},
|
||||
errorString: "sip.call.server transport error: not a string",
|
||||
},
|
||||
{
|
||||
name: "non-string userAgent param",
|
||||
params: map[string]any{
|
||||
"userAgent": 123,
|
||||
},
|
||||
errorString: "sip.call.server userAgent error: not a string",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["sip.call.server"]
|
||||
if !ok {
|
||||
t.Fatalf("sip.call.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "sip.call.server",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("sip.call.server got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("sip.call.server expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("sip.call.server got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
121
internal/module/test/sip-dtmf-server_test.go
Normal file
121
internal/module/test/sip-dtmf-server_test.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestSIPDTMFServerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["sip.dtmf.server"]
|
||||
if !ok {
|
||||
t.Fatalf("sip.dtmf.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "sip.dtmf.server",
|
||||
Params: map[string]any{
|
||||
"separator": "#",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create sip.dtmf.server module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("sip.dtmf.server module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "sip.dtmf.server" {
|
||||
t.Fatalf("sip.dtmf.server module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadSIPDTMFServer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no separator param",
|
||||
params: map[string]any{},
|
||||
errorString: "sip.dtmf.server separator error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string separator param",
|
||||
params: map[string]any{
|
||||
"separator": 123,
|
||||
},
|
||||
errorString: "sip.dtmf.server separator error: not a string",
|
||||
},
|
||||
{
|
||||
name: "non-number port param",
|
||||
params: map[string]any{
|
||||
"separator": "#",
|
||||
"port": "8000",
|
||||
},
|
||||
errorString: "sip.dtmf.server port error: not a number",
|
||||
},
|
||||
{
|
||||
name: "non-string ip param",
|
||||
params: map[string]any{
|
||||
"separator": "#",
|
||||
"ip": 123,
|
||||
},
|
||||
errorString: "sip.dtmf.server ip error: not a string",
|
||||
},
|
||||
{
|
||||
name: "non-string transport param",
|
||||
params: map[string]any{
|
||||
"separator": "#",
|
||||
"transport": 123,
|
||||
},
|
||||
errorString: "sip.dtmf.server transport error: not a string",
|
||||
},
|
||||
{
|
||||
name: "non-string userAgent param",
|
||||
params: map[string]any{
|
||||
"separator": "#",
|
||||
"userAgent": 123,
|
||||
},
|
||||
errorString: "sip.dtmf.server userAgent error: not a string",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["sip.dtmf.server"]
|
||||
if !ok {
|
||||
t.Fatalf("sip.dtmf.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "sip.dtmf.server",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("sip.dtmf.server got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("sip.dtmf.server expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("sip.dtmf.server got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
109
internal/module/test/tcp-client_test.go
Normal file
109
internal/module/test/tcp-client_test.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestTCPClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["net.tcp.client"]
|
||||
if !ok {
|
||||
t.Fatalf("net.tcp.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "net.tcp.client",
|
||||
Params: map[string]any{
|
||||
"host": "localhost",
|
||||
"port": 8000.0,
|
||||
"framing": "LF",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create net.tcp.client module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("net.tcp.client module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "net.tcp.client" {
|
||||
t.Fatalf("net.tcp.client module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadTCPClient(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no port param",
|
||||
params: map[string]any{
|
||||
"host": "localhost",
|
||||
},
|
||||
errorString: "net.tcp.client port error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-number port param",
|
||||
params: map[string]any{
|
||||
"host": "localhost",
|
||||
"port": "8000",
|
||||
},
|
||||
errorString: "net.tcp.client port error: not a number",
|
||||
},
|
||||
{
|
||||
name: "no host param",
|
||||
params: map[string]any{
|
||||
"port": 8000.0,
|
||||
},
|
||||
errorString: "net.tcp.client host error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string host param",
|
||||
params: map[string]any{
|
||||
"host": 123,
|
||||
"port": 8000.0,
|
||||
},
|
||||
errorString: "net.tcp.client host error: not a string",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["net.tcp.client"]
|
||||
if !ok {
|
||||
t.Fatalf("net.tcp.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "net.tcp.client",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("net.tcp.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("net.tcp.client expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("net.tcp.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
134
internal/module/test/tcp-server_test.go
Normal file
134
internal/module/test/tcp-server_test.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestTCPServerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["net.tcp.server"]
|
||||
if !ok {
|
||||
t.Fatalf("net.tcp.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "net.tcp.server",
|
||||
Params: map[string]any{
|
||||
"port": 8000.0,
|
||||
"framing": "LF",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create net.tcp.server module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("net.tcp.server module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "net.tcp.server" {
|
||||
t.Fatalf("net.tcp.server module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadTCPServer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no port param",
|
||||
params: map[string]any{
|
||||
"framing": "LF",
|
||||
},
|
||||
errorString: "net.tcp.server port error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-number port param",
|
||||
params: map[string]any{
|
||||
"port": "8000",
|
||||
"framing": "LF",
|
||||
},
|
||||
errorString: "net.tcp.server port error: not a number",
|
||||
},
|
||||
{
|
||||
name: "no framing param",
|
||||
params: map[string]any{
|
||||
"port": 8000.0,
|
||||
},
|
||||
errorString: "net.tcp.server framing error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string framing param",
|
||||
params: map[string]any{
|
||||
"port": 8000.0,
|
||||
"framing": 1,
|
||||
},
|
||||
errorString: "net.tcp.server framing error: not a string",
|
||||
},
|
||||
{
|
||||
name: "unkown framing method",
|
||||
params: map[string]any{
|
||||
"port": 8000.0,
|
||||
"framing": "asdfasdfasdfasdflkj",
|
||||
},
|
||||
errorString: "net.tcp.server unknown framing method: asdfasdfasdfasdflkj",
|
||||
},
|
||||
{
|
||||
name: "non-string ip param",
|
||||
params: map[string]any{
|
||||
"port": 8000.0,
|
||||
"framing": "LF",
|
||||
"ip": 123,
|
||||
},
|
||||
errorString: "net.tcp.server ip error: not a string",
|
||||
},
|
||||
{
|
||||
name: "invalid addr",
|
||||
params: map[string]any{
|
||||
"ip": "127.0.0.",
|
||||
"port": 8000.0,
|
||||
"framing": "LF",
|
||||
},
|
||||
errorString: "lookup 127.0.0.: no such host",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["net.tcp.server"]
|
||||
if !ok {
|
||||
t.Fatalf("net.tcp.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "net.tcp.server",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("net.tcp.server got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("net.tcp.server expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("net.tcp.server got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
89
internal/module/test/time-interval_test.go
Normal file
89
internal/module/test/time-interval_test.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestTimeIntervalFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["time.interval"]
|
||||
if !ok {
|
||||
t.Fatalf("time.interval module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "time.interval",
|
||||
Params: map[string]any{
|
||||
"duration": 1000.0,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create time.interval module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("time.interval module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "time.interval" {
|
||||
t.Fatalf("time.interval module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadTimeInterval(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no duration param",
|
||||
params: map[string]any{},
|
||||
errorString: "time.interval duration error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-number duration param",
|
||||
params: map[string]any{
|
||||
"duration": "8000",
|
||||
},
|
||||
errorString: "time.interval duration error: not a number",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["time.interval"]
|
||||
if !ok {
|
||||
t.Fatalf("time.interval module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "time.interval",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("time.interval got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("time.interval expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("time.interval got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
89
internal/module/test/time-timer_test.go
Normal file
89
internal/module/test/time-timer_test.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestTimeTimerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["time.timer"]
|
||||
if !ok {
|
||||
t.Fatalf("time.timer module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "time.timer",
|
||||
Params: map[string]any{
|
||||
"duration": 1000.0,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create time.timer module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("time.timer module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "time.timer" {
|
||||
t.Fatalf("time.timer module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadTimeTimer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no duration param",
|
||||
params: map[string]any{},
|
||||
errorString: "time.timer duration error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-number duration param",
|
||||
params: map[string]any{
|
||||
"duration": "8000",
|
||||
},
|
||||
errorString: "time.timer duration error: not a number",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["time.timer"]
|
||||
if !ok {
|
||||
t.Fatalf("time.timer module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "time.timer",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("time.timer got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("time.timer expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("time.timer got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
109
internal/module/test/udp-client_test.go
Normal file
109
internal/module/test/udp-client_test.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestUDPClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["net.udp.client"]
|
||||
if !ok {
|
||||
t.Fatalf("udp.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "net.udp.client",
|
||||
Params: map[string]any{
|
||||
"host": "localhost",
|
||||
"port": 8000.0,
|
||||
"framing": "LF",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create net.udp.client module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("net.udp.client module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "net.udp.client" {
|
||||
t.Fatalf("net.udp.client module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadUDPClient(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no port param",
|
||||
params: map[string]any{
|
||||
"host": "localhost",
|
||||
},
|
||||
errorString: "net.udp.client port error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-number port param",
|
||||
params: map[string]any{
|
||||
"host": "localhost",
|
||||
"port": "8000",
|
||||
},
|
||||
errorString: "net.udp.client port error: not a number",
|
||||
},
|
||||
{
|
||||
name: "no host param",
|
||||
params: map[string]any{
|
||||
"port": 8000.0,
|
||||
},
|
||||
errorString: "net.udp.client host error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string host param",
|
||||
params: map[string]any{
|
||||
"host": 123,
|
||||
"port": 8000.0,
|
||||
},
|
||||
errorString: "net.udp.client host error: not a string",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["net.udp.client"]
|
||||
if !ok {
|
||||
t.Fatalf("net.udp.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "net.udp.client",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("net.udp.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("net.udp.client expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("net.udp.client got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
116
internal/module/test/udp-multicast_test.go
Normal file
116
internal/module/test/udp-multicast_test.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestUDPMulticastFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["net.udp.multicast"]
|
||||
if !ok {
|
||||
t.Fatalf("udp.multicast module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "net.udp.multicast",
|
||||
Params: map[string]any{
|
||||
"ip": "236.10.10.10",
|
||||
"port": 56565.0,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create net.udp.multicast module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("net.udp.multicast module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "net.udp.multicast" {
|
||||
t.Fatalf("net.udp.multicast module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadUDPMulticast(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no port param",
|
||||
params: map[string]any{
|
||||
"ip": "localhost",
|
||||
},
|
||||
errorString: "net.udp.multicast port error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-number port param",
|
||||
params: map[string]any{
|
||||
"ip": "localhost",
|
||||
"port": "8000",
|
||||
},
|
||||
errorString: "net.udp.multicast port error: not a number",
|
||||
},
|
||||
{
|
||||
name: "no ip param",
|
||||
params: map[string]any{
|
||||
"port": 8000.0,
|
||||
},
|
||||
errorString: "net.udp.multicast ip error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string ip param",
|
||||
params: map[string]any{
|
||||
"ip": 123,
|
||||
"port": 8000.0,
|
||||
},
|
||||
errorString: "net.udp.multicast ip error: not a string",
|
||||
},
|
||||
{
|
||||
name: "invalid addr",
|
||||
params: map[string]any{
|
||||
"ip": "127.0.0.",
|
||||
"port": 8000.0,
|
||||
},
|
||||
errorString: "lookup 127.0.0.: no such host",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["net.udp.multicast"]
|
||||
if !ok {
|
||||
t.Fatalf("net.udp.multicast module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "net.udp.multicast",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("net.udp.multicast got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("net.udp.multicast expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("net.udp.multicast got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
113
internal/module/test/udp-server_test.go
Normal file
113
internal/module/test/udp-server_test.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestUDPServerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["net.udp.server"]
|
||||
if !ok {
|
||||
t.Fatalf("net.udp.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "net.udp.server",
|
||||
Params: map[string]any{
|
||||
"port": 8000.0,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create udp.server module: %s", err)
|
||||
}
|
||||
|
||||
if moduleInstance.Id() != "test" {
|
||||
t.Fatalf("udp.server module has wrong id: %s", moduleInstance.Id())
|
||||
}
|
||||
|
||||
if moduleInstance.Type() != "net.udp.server" {
|
||||
t.Fatalf("net.udp.server module has wrong type: %s", moduleInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadUDPServer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no port param",
|
||||
params: map[string]any{},
|
||||
errorString: "net.udp.server port error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-number port param",
|
||||
params: map[string]any{
|
||||
"port": "8000",
|
||||
},
|
||||
errorString: "net.udp.server port error: not a number",
|
||||
},
|
||||
{
|
||||
name: "non-string ip param",
|
||||
params: map[string]any{
|
||||
"port": 8000.0,
|
||||
"ip": 123,
|
||||
},
|
||||
errorString: "net.udp.server ip error: not a string",
|
||||
},
|
||||
{
|
||||
name: "non-number bufferSize param",
|
||||
params: map[string]any{
|
||||
"port": 8000.0,
|
||||
"bufferSize": "1024",
|
||||
},
|
||||
errorString: "net.udp.server bufferSize error: not a number",
|
||||
},
|
||||
{
|
||||
name: "invalid addr",
|
||||
params: map[string]any{
|
||||
"ip": "127.0.0.",
|
||||
"port": 8000.0,
|
||||
},
|
||||
errorString: "lookup 127.0.0.: no such host",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["net.udp.server"]
|
||||
if !ok {
|
||||
t.Fatalf("net.udp.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "net.udp.server",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("net.udp.server got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
err = moduleInstance.Start(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("net.udp.server expected to fail")
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("net.udp.server got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package module
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
@@ -26,17 +27,11 @@ func init() {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
|
||||
duration, ok := params["duration"]
|
||||
if !ok {
|
||||
return nil, errors.New("time.interval requires a duration parameter")
|
||||
durationInt, err := params.GetInt("duration")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("time.interval duration error: %w", err)
|
||||
}
|
||||
|
||||
durationNum, ok := duration.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("time.interval duration must be number")
|
||||
}
|
||||
return &TimeInterval{Duration: uint32(durationNum), config: config, logger: CreateLogger(config)}, nil
|
||||
return &TimeInterval{Duration: uint32(durationInt), config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package module
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
@@ -26,15 +27,9 @@ func init() {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
|
||||
duration, ok := params["duration"]
|
||||
if !ok {
|
||||
return nil, errors.New("time.timer requires a duration parameter")
|
||||
}
|
||||
|
||||
durationNum, ok := duration.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("time.timer duration must be a number")
|
||||
durationNum, err := params.GetInt("duration")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("time.timer duration error: %w", err)
|
||||
}
|
||||
|
||||
return &TimeTimer{Duration: uint32(durationNum), config: config, logger: CreateLogger(config)}, nil
|
||||
|
||||
@@ -27,27 +27,14 @@ func init() {
|
||||
Type: "net.udp.client",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
host, ok := params["host"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.client requires a host parameter")
|
||||
hostString, err := params.GetString("host")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("net.udp.client host error: %w", err)
|
||||
}
|
||||
|
||||
hostString, ok := host.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.client host must be a string")
|
||||
}
|
||||
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.client requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.client port must be a number")
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("net.udp.client port error: %w", err)
|
||||
}
|
||||
|
||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", hostString, uint16(portNum)))
|
||||
|
||||
@@ -25,36 +25,23 @@ type UDPMulticast struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.multicast",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
ip, ok := params["ip"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.multicast requires an ip parameter")
|
||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
||||
params := moduleConfig.Params
|
||||
ipString, err := params.GetString("ip")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("net.udp.multicast ip error: %w", err)
|
||||
}
|
||||
|
||||
ipString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.multicast ip must be a string")
|
||||
}
|
||||
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.multicast requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.multicast port must be a number")
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("net.udp.multicast port error: %w", err)
|
||||
}
|
||||
|
||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", ipString, uint16(portNum)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &UDPMulticast{config: config, Addr: addr, logger: CreateLogger(config)}, nil
|
||||
return &UDPMulticast{config: moduleConfig, Addr: addr, logger: CreateLogger(moduleConfig)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net"
|
||||
"time"
|
||||
@@ -26,49 +25,36 @@ type UDPServer struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.server",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.server requires a port parameter")
|
||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
||||
params := moduleConfig.Params
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("net.udp.server port error: %w", err)
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.server port must be a number")
|
||||
}
|
||||
|
||||
ipString := "0.0.0.0"
|
||||
|
||||
ip, ok := params["ip"]
|
||||
if ok {
|
||||
|
||||
specificIpString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.server ip must be a string")
|
||||
ipString, err := params.GetString("ip")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
ipString = "0.0.0.0"
|
||||
} else {
|
||||
return nil, fmt.Errorf("net.udp.server ip error: %w", err)
|
||||
}
|
||||
ipString = specificIpString
|
||||
}
|
||||
|
||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", ipString, uint16(portNum)))
|
||||
if err != nil {
|
||||
log.Fatalf("error resolving UDP address: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bufferSizeNum := 2048
|
||||
bufferSize, ok := params["bufferSize"]
|
||||
|
||||
if ok {
|
||||
bufferSizeFloat, ok := bufferSize.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.server bufferSize must be a number")
|
||||
bufferSizeNum, err := params.GetInt("bufferSize")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
bufferSizeNum = 2048
|
||||
} else {
|
||||
return nil, fmt.Errorf("net.udp.server bufferSize error: %w", err)
|
||||
}
|
||||
bufferSizeNum = int(bufferSizeFloat)
|
||||
}
|
||||
return &UDPServer{Addr: addr, BufferSize: bufferSizeNum, config: config, logger: CreateLogger(config)}, nil
|
||||
return &UDPServer{Addr: addr, BufferSize: bufferSizeNum, config: moduleConfig, logger: CreateLogger(moduleConfig)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -78,7 +64,7 @@ func (us *UDPServer) Id() string {
|
||||
}
|
||||
|
||||
func (us *UDPServer) Type() string {
|
||||
return us.config.Id
|
||||
return us.config.Type
|
||||
}
|
||||
|
||||
func (us *UDPServer) Start(ctx context.Context) error {
|
||||
|
||||
@@ -37,13 +37,9 @@ func init() {
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
opCode, ok := params["opCode"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.filter requires an opCode parameter")
|
||||
}
|
||||
opCodeNum, ok := opCode.(float64)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.filter opCode must be a number")
|
||||
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
|
||||
|
||||
@@ -3,6 +3,7 @@ package processor
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -34,20 +35,18 @@ func (fp *FloatParse) Type() string {
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "float.parse",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
bitSizeNum := 64
|
||||
bitSize, ok := params["bitSize"]
|
||||
if ok {
|
||||
bitSizeFloat, ok := bitSize.(float64)
|
||||
New: func(moduleConfig config.ProcessorConfig) (Processor, error) {
|
||||
params := moduleConfig.Params
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("float.parse bitSize must be a number")
|
||||
bitSizeNum, err := params.GetInt("bitSize")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
bitSizeNum = 64
|
||||
} else {
|
||||
return nil, fmt.Errorf("float.parse bitSize error: %w", err)
|
||||
}
|
||||
|
||||
bitSizeNum = int(bitSizeFloat)
|
||||
}
|
||||
return &FloatParse{config: config, BitSize: bitSizeNum}, nil
|
||||
return &FloatParse{config: moduleConfig, BitSize: bitSizeNum}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"text/template"
|
||||
|
||||
@@ -187,16 +187,9 @@ func init() {
|
||||
|
||||
// TODO(jwetzell): make some params optional
|
||||
params := config.Params
|
||||
id, ok := params["id"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create requires an id parameter")
|
||||
}
|
||||
|
||||
idString, ok := id.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create id must be a string")
|
||||
idString, err := params.GetString("id")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create id error: %w", err)
|
||||
}
|
||||
|
||||
idTemplate, err := template.New("id").Parse(idString)
|
||||
@@ -205,44 +198,23 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pan, ok := params["pan"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create requires a pan parameter")
|
||||
}
|
||||
|
||||
panString, ok := pan.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create pan must be a string")
|
||||
panString, err := params.GetString("pan")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create pan error: %w", err)
|
||||
}
|
||||
|
||||
panTemplate, err := template.New("pan").Parse(panString)
|
||||
|
||||
tilt, ok := params["tilt"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create requires a tilt parameter")
|
||||
}
|
||||
|
||||
tiltString, ok := tilt.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create tilt must be a string")
|
||||
tiltString, err := params.GetString("tilt")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create tilt error: %w", err)
|
||||
}
|
||||
|
||||
tiltTemplate, err := template.New("tilt").Parse(tiltString)
|
||||
|
||||
roll, ok := params["roll"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create requires a roll parameter")
|
||||
}
|
||||
|
||||
rollString, ok := roll.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create roll must be a string")
|
||||
rollString, err := params.GetString("roll")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create roll error: %w", err)
|
||||
}
|
||||
|
||||
rollTemplate, err := template.New("roll").Parse(rollString)
|
||||
@@ -251,16 +223,9 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
posX, ok := params["posX"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create requires a posX parameter")
|
||||
}
|
||||
|
||||
posXString, ok := posX.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create posX must be a string")
|
||||
posXString, err := params.GetString("posX")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create posX error: %w", err)
|
||||
}
|
||||
|
||||
posXTemplate, err := template.New("posX").Parse(posXString)
|
||||
@@ -269,16 +234,9 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
posY, ok := params["posY"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create requires a posY parameter")
|
||||
}
|
||||
|
||||
posYString, ok := posY.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create posY must be a string")
|
||||
posYString, err := params.GetString("posY")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create posY error: %w", err)
|
||||
}
|
||||
|
||||
posYTemplate, err := template.New("posY").Parse(posYString)
|
||||
@@ -287,16 +245,9 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
posZ, ok := params["posZ"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create requires a posZ parameter")
|
||||
}
|
||||
|
||||
posZString, ok := posZ.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create posZ must be a string")
|
||||
posZString, err := params.GetString("posZ")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create posZ error: %w", err)
|
||||
}
|
||||
|
||||
posZTemplate, err := template.New("posZ").Parse(posZString)
|
||||
@@ -305,30 +256,16 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
zoom, ok := params["zoom"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create requires a zoom parameter")
|
||||
}
|
||||
|
||||
zoomString, ok := zoom.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create zoom must be a string")
|
||||
zoomString, err := params.GetString("zoom")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create zoom error: %w", err)
|
||||
}
|
||||
|
||||
zoomTemplate, err := template.New("zoom").Parse(zoomString)
|
||||
|
||||
focus, ok := params["focus"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create requires a focus parameter")
|
||||
}
|
||||
|
||||
focusString, ok := focus.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("freed.create focus must be a string")
|
||||
focusString, err := params.GetString("focus")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create focus error: %w", err)
|
||||
}
|
||||
|
||||
focusTemplate, err := template.New("focus").Parse(focusString)
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"text/template"
|
||||
|
||||
@@ -47,28 +47,14 @@ func init() {
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
method, ok := params["method"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.request.create requires a method parameter")
|
||||
methodString, err := params.GetString("method")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.request.create method error: %w", err)
|
||||
}
|
||||
|
||||
methodString, ok := method.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.request.create url must be a string")
|
||||
}
|
||||
|
||||
url, ok := params["url"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.request.create requires a url parameter")
|
||||
}
|
||||
|
||||
urlString, ok := url.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.request.create url must be a string")
|
||||
urlString, err := params.GetString("url")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.request.create url error: %w", err)
|
||||
}
|
||||
|
||||
urlTemplate, err := template.New("url").Parse(urlString)
|
||||
|
||||
@@ -44,18 +44,11 @@ func (hrf *HTTPRequestFilter) Type() string {
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "http.request.filter",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
path, ok := params["path"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.request.filter requires a path parameter")
|
||||
}
|
||||
|
||||
pathString, ok := path.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.request.filter path must be a string")
|
||||
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))
|
||||
@@ -64,18 +57,17 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
method, ok := params["method"]
|
||||
|
||||
if ok {
|
||||
methodString, ok := method.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.request.filter method must be a string")
|
||||
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: config, Path: pathRegexp, Method: methodString}, nil
|
||||
}
|
||||
|
||||
return &HTTPRequestFilter{config: config, Path: pathRegexp}, nil
|
||||
return &HTTPRequestFilter{config: moduleConfig, Path: pathRegexp, Method: methodString}, nil
|
||||
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -44,28 +44,14 @@ func init() {
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
status, ok := params["status"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.response.create requires a status parameter")
|
||||
statusNum, err := params.GetInt("status")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.response.create status error: %w", err)
|
||||
}
|
||||
|
||||
statusNum, ok := status.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.response.create status must be a number")
|
||||
}
|
||||
|
||||
bodyTmpl, ok := params["bodyTemplate"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.response.create requires a bodyTemplate parameter")
|
||||
}
|
||||
|
||||
bodyTemplateString, ok := bodyTmpl.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.response.create bodyTemplate must be a string")
|
||||
bodyTemplateString, err := params.GetString("bodyTemplate")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.response.create bodyTemplate error: %w", err)
|
||||
}
|
||||
|
||||
bodyTemplate, err := template.New("body").Parse(bodyTemplateString)
|
||||
|
||||
@@ -3,6 +3,7 @@ package processor
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -35,33 +36,27 @@ func (ip *IntParse) Type() string {
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "int.parse",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
New: func(moduleConfig config.ProcessorConfig) (Processor, error) {
|
||||
params := moduleConfig.Params
|
||||
|
||||
baseNum := 10
|
||||
base, ok := params["base"]
|
||||
if ok {
|
||||
baseFloat, ok := base.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("int.parse base must be a number")
|
||||
baseNum, err := params.GetInt("base")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
baseNum = 10
|
||||
} else {
|
||||
return nil, fmt.Errorf("int.parse base error: %w", err)
|
||||
}
|
||||
|
||||
baseNum = int(baseFloat)
|
||||
}
|
||||
|
||||
bitSizeNum := 64
|
||||
bitSize, ok := params["bitSize"]
|
||||
if ok {
|
||||
bitSizeFloat, ok := bitSize.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("int.parse bitSize must be a number")
|
||||
bitSizeNum, err := params.GetInt("bitSize")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
bitSizeNum = 64
|
||||
} else {
|
||||
return nil, fmt.Errorf("int.parse bitSize error: %w", err)
|
||||
}
|
||||
|
||||
bitSizeNum = int(bitSizeFloat)
|
||||
}
|
||||
return &IntParse{config: config, Base: baseNum, BitSize: bitSizeNum}, nil
|
||||
return &IntParse{config: moduleConfig, Base: baseNum, BitSize: bitSizeNum}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package processor
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -29,33 +30,21 @@ func init() {
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
min, ok := params["min"]
|
||||
if !ok {
|
||||
return nil, errors.New("int.random requires a min parameter")
|
||||
minInt, err := params.GetInt("min")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("int.random min error: %w", err)
|
||||
}
|
||||
|
||||
minFloat, ok := min.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("int.random min must be a number")
|
||||
maxInt, err := params.GetInt("max")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("int.random max error: %w", err)
|
||||
}
|
||||
|
||||
max, ok := params["max"]
|
||||
if !ok {
|
||||
return nil, errors.New("int.random requires a max parameter")
|
||||
}
|
||||
|
||||
maxFloat, ok := max.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("int.random max must be a number")
|
||||
}
|
||||
|
||||
if maxFloat < minFloat {
|
||||
if maxInt < minInt {
|
||||
return nil, errors.New("int.random max must be greater than min")
|
||||
}
|
||||
|
||||
return &IntRandom{config: config, Min: int(minFloat), Max: int(maxFloat)}, nil
|
||||
return &IntRandom{config: config, Min: int(minInt), Max: int(maxInt)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"text/template"
|
||||
@@ -32,16 +31,9 @@ func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
params := config.Params
|
||||
|
||||
channel, ok := params["channel"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create NoteOn requires a channel parameter")
|
||||
}
|
||||
|
||||
channelString, ok := channel.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create NoteOn channel must be a string")
|
||||
channelString, err := params.GetString("channel")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.message.create channel error: %w", err)
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
@@ -50,16 +42,9 @@ func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
note, ok := params["note"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create NoteOn requires a note parameter")
|
||||
}
|
||||
|
||||
noteString, ok := note.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create NoteOn note must be a string")
|
||||
noteString, err := params.GetString("note")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.message.create note error: %w", err)
|
||||
}
|
||||
|
||||
noteTemplate, err := template.New("note").Parse(noteString)
|
||||
@@ -68,16 +53,9 @@ func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
velocity, ok := params["velocity"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create NoteOn requires a velocity parameter")
|
||||
}
|
||||
|
||||
velocityString, ok := velocity.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create NoteOn velocity must be a string")
|
||||
velocityString, err := params.GetString("velocity")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.message.create velocity error: %w", err)
|
||||
}
|
||||
|
||||
velocityTemplate, err := template.New("velocity").Parse(velocityString)
|
||||
@@ -123,16 +101,9 @@ func newMidiNoteOffCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
params := config.Params
|
||||
|
||||
channel, ok := params["channel"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create NoteOn requires a channel parameter")
|
||||
}
|
||||
|
||||
channelString, ok := channel.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create NoteOn channel must be a string")
|
||||
channelString, err := params.GetString("channel")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.message.create channel error: %w", err)
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
@@ -141,16 +112,9 @@ func newMidiNoteOffCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
note, ok := params["note"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create NoteOn requires a note parameter")
|
||||
}
|
||||
|
||||
noteString, ok := note.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create NoteOn note must be a string")
|
||||
noteString, err := params.GetString("note")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.message.create note error: %w", err)
|
||||
}
|
||||
|
||||
noteTemplate, err := template.New("note").Parse(noteString)
|
||||
@@ -159,6 +123,17 @@ func newMidiNoteOffCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
velocityString, err := params.GetString("velocity")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.message.create velocity error: %w", err)
|
||||
}
|
||||
|
||||
velocityTemplate, err := template.New("velocity").Parse(velocityString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &MIDIMessageCreate{config: config, ProcessFunc: func(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
var channelBuffer bytes.Buffer
|
||||
@@ -179,7 +154,16 @@ func newMidiNoteOffCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
noteValue, err := strconv.ParseUint(noteBuffer.String(), 10, 8)
|
||||
|
||||
payloadMessage := midi.NoteOff(uint8(channelValue), uint8(noteValue))
|
||||
var velocityBuffer bytes.Buffer
|
||||
err = velocityTemplate.Execute(&velocityBuffer, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
velocityValue, err := strconv.ParseUint(velocityBuffer.String(), 10, 8)
|
||||
|
||||
payloadMessage := midi.NoteOffVelocity(uint8(channelValue), uint8(noteValue), uint8(velocityValue))
|
||||
return payloadMessage, nil
|
||||
}}, nil
|
||||
}
|
||||
@@ -188,16 +172,9 @@ func newMidiControlChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
|
||||
params := config.Params
|
||||
|
||||
channel, ok := params["channel"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create ControlChange requires a channel parameter")
|
||||
}
|
||||
|
||||
channelString, ok := channel.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create ControlChange channel must be a string")
|
||||
channelString, err := params.GetString("channel")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.message.create channel error: %w", err)
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
@@ -206,34 +183,20 @@ func newMidiControlChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
return nil, err
|
||||
}
|
||||
|
||||
controller, ok := params["controller"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create ControlChange requires a controller parameter")
|
||||
controlString, err := params.GetString("control")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.message.create control error: %w", err)
|
||||
}
|
||||
|
||||
controllerString, ok := controller.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create ControlChange controller must be a string")
|
||||
}
|
||||
|
||||
controllerTemplate, err := template.New("controller").Parse(controllerString)
|
||||
controlTemplate, err := template.New("control").Parse(controlString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
value, ok := params["value"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create ControlChange requires a value parameter")
|
||||
}
|
||||
|
||||
valueString, ok := value.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create ControlChange value must be a string")
|
||||
valueString, err := params.GetString("value")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.message.create value error: %w", err)
|
||||
}
|
||||
|
||||
valueTemplate, err := template.New("value").Parse(valueString)
|
||||
@@ -253,14 +216,14 @@ func newMidiControlChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
|
||||
channelValue, err := strconv.ParseUint(channelBuffer.String(), 10, 8)
|
||||
|
||||
var controllerBuffer bytes.Buffer
|
||||
err = controllerTemplate.Execute(&controllerBuffer, payload)
|
||||
var controlBuffer bytes.Buffer
|
||||
err = controlTemplate.Execute(&controlBuffer, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
controllerValue, err := strconv.ParseUint(controllerBuffer.String(), 10, 8)
|
||||
controlValue, err := strconv.ParseUint(controlBuffer.String(), 10, 8)
|
||||
|
||||
var valueBuffer bytes.Buffer
|
||||
err = valueTemplate.Execute(&valueBuffer, payload)
|
||||
@@ -271,7 +234,7 @@ func newMidiControlChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
|
||||
valueValue, err := strconv.ParseUint(valueBuffer.String(), 10, 8)
|
||||
|
||||
payloadMessage := midi.ControlChange(uint8(channelValue), uint8(controllerValue), uint8(valueValue))
|
||||
payloadMessage := midi.ControlChange(uint8(channelValue), uint8(controlValue), uint8(valueValue))
|
||||
return payloadMessage, nil
|
||||
}}, nil
|
||||
}
|
||||
@@ -280,16 +243,9 @@ func newMidiProgramChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
|
||||
params := config.Params
|
||||
|
||||
channel, ok := params["channel"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create ProgramChange requires a channel parameter")
|
||||
}
|
||||
|
||||
channelString, ok := channel.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create ProgramChange channel must be a string")
|
||||
channelString, err := params.GetString("channel")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.message.create channel error: %w", err)
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
@@ -298,16 +254,9 @@ func newMidiProgramChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
return nil, err
|
||||
}
|
||||
|
||||
program, ok := params["program"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create ProgramChange requires a program parameter")
|
||||
}
|
||||
|
||||
programString, ok := program.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create ProgramChange program must be a string")
|
||||
programString, err := params.GetString("program")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.message.create program error: %w", err)
|
||||
}
|
||||
|
||||
programTemplate, err := template.New("program").Parse(programString)
|
||||
@@ -347,16 +296,9 @@ func init() {
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
msgType, ok := params["type"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create requires a type parameter")
|
||||
}
|
||||
|
||||
msgTypeString, ok := msgType.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.create type parameter must be a string")
|
||||
msgTypeString, err := params.GetString("type")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.message.create type error: %w", err)
|
||||
}
|
||||
|
||||
switch msgTypeString {
|
||||
|
||||
@@ -18,7 +18,7 @@ func (mme *MIDIMessageEncode) Process(ctx context.Context, payload any) (any, er
|
||||
payloadMessage, ok := payload.(midi.Message)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.encode processor only accepts an midi.Message")
|
||||
return nil, errors.New("midi.message.encode processor only accepts a midi.Message")
|
||||
}
|
||||
|
||||
return payloadMessage.Bytes(), nil
|
||||
|
||||
@@ -5,6 +5,7 @@ package processor
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
@@ -19,7 +20,7 @@ func (mmf *MIDIMessageFilter) Process(ctx context.Context, payload any) (any, er
|
||||
payloadMessage, ok := payload.(midi.Message)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.filter processor only accepts an midi.Message")
|
||||
return nil, errors.New("midi.message.filter processor only accepts a midi.Message")
|
||||
}
|
||||
|
||||
if payloadMessage.Type().String() != mmf.MIDIType {
|
||||
@@ -38,18 +39,12 @@ func init() {
|
||||
Type: "midi.message.filter",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
midiType, ok := params["type"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.filter requires a type parameter")
|
||||
}
|
||||
midiTypeString, ok := midiType.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.message.filter type must be a string")
|
||||
msgTypeString, err := params.GetString("type")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.message.filter type error: %w", err)
|
||||
}
|
||||
|
||||
return &MIDIMessageFilter{config: config, MIDIType: midiTypeString}, nil
|
||||
return &MIDIMessageFilter{config: config, MIDIType: msgTypeString}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package processor
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
@@ -22,6 +23,15 @@ type MQTTMessageCreate struct {
|
||||
Payload []byte
|
||||
}
|
||||
|
||||
func NewMQTTMessage(topic string, qos byte, retained bool, payload []byte) MQTTMessage {
|
||||
return MQTTMessage{
|
||||
topic: topic,
|
||||
qos: qos,
|
||||
retained: retained,
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
func (mm MQTTMessage) Duplicate() bool {
|
||||
// TODO(jwetzell): implement?
|
||||
return false
|
||||
@@ -72,47 +82,26 @@ func init() {
|
||||
Type: "mqtt.message.create",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
topic, ok := params["topic"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.message.create requires a topic parameter")
|
||||
topicString, err := params.GetString("topic")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mqtt.message.create topic error: %w", err)
|
||||
}
|
||||
|
||||
topicString, ok := topic.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.message.create topic must be a string")
|
||||
qosByte, err := params.GetInt("qos")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mqtt.message.create qos error: %w", err)
|
||||
}
|
||||
|
||||
qos, ok := params["qos"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.message.create requires a qos parameter")
|
||||
}
|
||||
|
||||
qosByte, ok := qos.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.message.create qos must be a number")
|
||||
}
|
||||
|
||||
retained, ok := params["retained"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.message.create requires a retained parameter")
|
||||
}
|
||||
|
||||
retainedBool, ok := retained.(bool)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.message.create retained must be a boolean")
|
||||
retainedBool, err := params.GetBool("retained")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mqtt.message.create retained error: %w", err)
|
||||
}
|
||||
|
||||
//TODO(jwetzell): convert payload into []byte or string for sending
|
||||
payload, ok := params["payload"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.message.create requires a payload parameter")
|
||||
return nil, errors.New("mqtt.message.create payload error: not found")
|
||||
}
|
||||
|
||||
if payloadBytes, ok := payload.([]byte); ok {
|
||||
@@ -122,7 +111,7 @@ func init() {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.message.create payload must be a string or byte array")
|
||||
return nil, errors.New("mqtt.message.create payload error: not a string or byte array")
|
||||
}
|
||||
|
||||
payloadBytes := []byte(payloadString)
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -57,16 +57,9 @@ func init() {
|
||||
Type: "nats.message.create",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
subject, ok := params["subject"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("nats.message.create requires a subject parameter")
|
||||
}
|
||||
|
||||
subjectString, ok := subject.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("nats.message.create subject must be a string")
|
||||
subjectString, err := params.GetString("subject")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("nats.message.create subject error: %w", err)
|
||||
}
|
||||
|
||||
subjectTemplate, err := template.New("subject").Parse(subjectString)
|
||||
@@ -75,16 +68,9 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payload, ok := params["payload"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("nats.message.create requires a payload parameter")
|
||||
}
|
||||
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("nats.message.create payload must be a string")
|
||||
payloadString, err := params.GetString("payload")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("nats.message.create payload error: %w", err)
|
||||
}
|
||||
|
||||
payloadTemplate, err := template.New("payload").Parse(payloadString)
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
type NATSMessageEncode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (nme *NATSMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadMessage, ok := payload.(*nats.Msg)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("nats.message.encode processor only accepts an nats.Msg")
|
||||
}
|
||||
|
||||
return payloadMessage.Data, nil
|
||||
}
|
||||
|
||||
func (nme *NATSMessageEncode) Type() string {
|
||||
return nme.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "nats.message.encode",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &NATSMessageEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -80,16 +80,9 @@ func init() {
|
||||
Type: "osc.message.create",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
address, ok := params["address"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("osc.message.create requires an address parameter")
|
||||
}
|
||||
|
||||
addressString, ok := address.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("osc.message.create address must be a string")
|
||||
addressString, err := params.GetString("address")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("osc.message.create address error: %w", err)
|
||||
}
|
||||
|
||||
addressTemplate, err := template.New("address").Parse(addressString)
|
||||
@@ -107,16 +100,9 @@ func init() {
|
||||
return nil, fmt.Errorf("osc.message.create address must be an array found %T", args)
|
||||
}
|
||||
|
||||
types, ok := params["types"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("osc.message.create requires a types parameter with args")
|
||||
}
|
||||
|
||||
typesString, ok := types.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("osc.message.create types must be a string")
|
||||
typesString, err := params.GetString("types")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("osc.message.create types error: %w", err)
|
||||
}
|
||||
|
||||
if len(rawArgs) != len(typesString) {
|
||||
@@ -129,7 +115,7 @@ func init() {
|
||||
argString, ok := rawArg.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("osc.message.create arg must be a string")
|
||||
return nil, errors.New("osc.message.create arg error: not a string")
|
||||
}
|
||||
|
||||
argTemplate, err := template.New("arg").Parse(argString)
|
||||
|
||||
@@ -40,16 +40,9 @@ func init() {
|
||||
Type: "osc.message.filter",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
address, ok := params["address"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("osc.message.filter requires an address parameter")
|
||||
}
|
||||
|
||||
addressString, ok := address.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("osc.message.filter address must be a string")
|
||||
addressString, err := params.GetString("address")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("osc.message.filter address error: %w", err)
|
||||
}
|
||||
|
||||
addressPattern := strings.ReplaceAll(addressString, "?", ".")
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/expr-lang/expr"
|
||||
"github.com/expr-lang/expr/vm"
|
||||
@@ -35,16 +35,9 @@ func init() {
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
expression, ok := params["expression"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("script.expr requires an expression parameter")
|
||||
}
|
||||
|
||||
expressionString, ok := expression.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("script.expr expression must be a string")
|
||||
expressionString, err := params.GetString("expression")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("script.expr expression error: %w", err)
|
||||
}
|
||||
|
||||
program, err := expr.Compile(expressionString)
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"modernc.org/quickjs"
|
||||
@@ -71,16 +71,9 @@ func init() {
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
program, ok := params["program"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("script.js requires a program parameter")
|
||||
}
|
||||
|
||||
programString, ok := program.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("script.js program must be a string")
|
||||
programString, err := params.GetString("program")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("script.js program error: %w", err)
|
||||
}
|
||||
|
||||
return &ScriptJS{config: config, Program: programString}, nil
|
||||
|
||||
@@ -2,6 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
extism "github.com/extism/go-sdk"
|
||||
@@ -44,32 +45,30 @@ func (se *ScriptWASM) Type() string {
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "script.wasm",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
New: func(processorConfig config.ProcessorConfig) (Processor, error) {
|
||||
params := processorConfig.Params
|
||||
|
||||
path, ok := params["path"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.wasm requires a path parameter")
|
||||
pathString, err := params.GetString("path")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("script.wasm path error: %w", err)
|
||||
}
|
||||
|
||||
pathString, ok := path.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.wasm path must be a string")
|
||||
}
|
||||
|
||||
functionString := "process"
|
||||
|
||||
function, ok := params["function"]
|
||||
|
||||
if ok {
|
||||
specificFunctionString, ok := function.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.wasm function must be a string")
|
||||
functionString, err := params.GetString("function")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
functionString = "process"
|
||||
} else {
|
||||
return nil, fmt.Errorf("script.wasm function error: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
enableWasiBool, err := params.GetBool("enableWasi")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
enableWasiBool = false
|
||||
} else {
|
||||
return nil, fmt.Errorf("script.wasm enableWasi error: %w", err)
|
||||
}
|
||||
functionString = specificFunctionString
|
||||
}
|
||||
|
||||
manifest := extism.Manifest{
|
||||
@@ -80,13 +79,15 @@ func init() {
|
||||
},
|
||||
}
|
||||
|
||||
program, err := extism.NewCompiledPlugin(context.Background(), manifest, extism.PluginConfig{}, []extism.HostFunction{})
|
||||
program, err := extism.NewCompiledPlugin(context.Background(), manifest, extism.PluginConfig{
|
||||
EnableWasi: enableWasiBool,
|
||||
}, []extism.HostFunction{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ScriptWASM{config: config, Program: program, Function: functionString}, nil
|
||||
return &ScriptWASM{config: processorConfig, Program: program, Function: functionString}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -50,40 +50,19 @@ func init() {
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
preWait, ok := params["preWait"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create requires a preWait parameter")
|
||||
preWaitNum, err := params.GetInt("preWait")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sip.response.audio.create preWait error: %w", err)
|
||||
}
|
||||
|
||||
preWaitNum, ok := preWait.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create preWait must be a number")
|
||||
postWaitNum, err := params.GetInt("postWait")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sip.response.audio.create postWait error: %w", err)
|
||||
}
|
||||
|
||||
postWait, ok := params["postWait"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create requires a postWait parameter")
|
||||
}
|
||||
|
||||
postWaitNum, ok := postWait.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create postWait must be a number")
|
||||
}
|
||||
|
||||
audioFile, ok := params["audioFile"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create requires a audioFile parameter")
|
||||
}
|
||||
|
||||
audioFileString, ok := audioFile.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create audioFile must be a string")
|
||||
audioFileString, err := params.GetString("audioFile")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sip.response.audio.create audioFile error: %w", err)
|
||||
}
|
||||
|
||||
audioFileTemplate, err := template.New("audioFile").Parse(audioFileString)
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"text/template"
|
||||
|
||||
@@ -56,40 +57,19 @@ func init() {
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
preWait, ok := params["preWait"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create requires a preWait parameter")
|
||||
preWaitNum, err := params.GetInt("preWait")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sip.response.dtmf.create preWait error: %w", err)
|
||||
}
|
||||
|
||||
preWaitNum, ok := preWait.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create preWait must be a number")
|
||||
postWaitNum, err := params.GetInt("postWait")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sip.response.dtmf.create postWait error: %w", err)
|
||||
}
|
||||
|
||||
postWait, ok := params["postWait"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create requires a postWait parameter")
|
||||
}
|
||||
|
||||
postWaitNum, ok := postWait.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create postWait must be a number")
|
||||
}
|
||||
|
||||
digits, ok := params["digits"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create requires a digits parameter")
|
||||
}
|
||||
|
||||
digitsString, ok := digits.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create digits must be a string")
|
||||
digitsString, err := params.GetString("digits")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sip.response.dtmf.create digits error: %w", err)
|
||||
}
|
||||
|
||||
digitsTemplate, err := template.New("digits").Parse(digitsString)
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -36,16 +36,9 @@ func init() {
|
||||
Type: "string.create",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
tmpl, ok := params["template"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("string.create requires a template parameter")
|
||||
}
|
||||
|
||||
templateString, ok := tmpl.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("string.create template must be a string")
|
||||
templateString, err := params.GetString("template")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("string.create template error: %w", err)
|
||||
}
|
||||
|
||||
templateTemplate, err := template.New("template").Parse(templateString)
|
||||
|
||||
@@ -3,6 +3,7 @@ package processor
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -37,16 +38,9 @@ func init() {
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
pattern, ok := params["pattern"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("string.filter requires a pattern parameter")
|
||||
}
|
||||
|
||||
patternString, ok := pattern.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("string.filter pattern must be a string")
|
||||
patternString, err := params.GetString("pattern")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("string.filter pattern error: %w", err)
|
||||
}
|
||||
|
||||
patternRegexp, err := regexp.Compile(patternString)
|
||||
|
||||
@@ -3,6 +3,7 @@ package processor
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -35,16 +36,9 @@ func init() {
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
separator, ok := params["separator"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("string.split requires a separator")
|
||||
}
|
||||
|
||||
separatorString, ok := separator.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("string.split separator must be a string")
|
||||
separatorString, err := params.GetString("separator")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("string.split separator error: %w", err)
|
||||
}
|
||||
|
||||
return &StringSplit{config: config, Separator: separatorString}, nil
|
||||
|
||||
49
internal/processor/struct-field-get.go
Normal file
49
internal/processor/struct-field-get.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type StructFieldGet struct {
|
||||
config config.ProcessorConfig
|
||||
Name string
|
||||
}
|
||||
|
||||
func (sf *StructFieldGet) Process(ctx context.Context, payload any) (any, error) {
|
||||
s := reflect.ValueOf(payload)
|
||||
|
||||
if s.Kind() != reflect.Struct {
|
||||
return nil, errors.New("struct.field.get processor only accepts a struct payload")
|
||||
}
|
||||
|
||||
field := s.FieldByName(sf.Name)
|
||||
if !field.IsValid() {
|
||||
return nil, fmt.Errorf("struct.field.get field '%s' does not exist", sf.Name)
|
||||
}
|
||||
|
||||
return field.Interface(), nil
|
||||
}
|
||||
|
||||
func (sf *StructFieldGet) Type() string {
|
||||
return sf.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "struct.field.get",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
nameString, err := params.GetString("name")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("struct.field.get name error: %w", err)
|
||||
}
|
||||
|
||||
return &StructFieldGet{config: config, Name: nameString}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
65
internal/processor/struct-method-get.go
Normal file
65
internal/processor/struct-method-get.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type StructMethodGet struct {
|
||||
config config.ProcessorConfig
|
||||
Name string
|
||||
}
|
||||
|
||||
func (sm *StructMethodGet) Process(ctx context.Context, payload any) (any, error) {
|
||||
s := reflect.ValueOf(payload)
|
||||
|
||||
if s.Kind() != reflect.Struct {
|
||||
return nil, errors.New("struct.method.get processor only accepts a struct payload")
|
||||
}
|
||||
|
||||
method := s.MethodByName(sm.Name)
|
||||
if !method.IsValid() {
|
||||
return nil, fmt.Errorf("struct.method.get method '%s' does not exist", sm.Name)
|
||||
}
|
||||
|
||||
value := method.Call(nil)
|
||||
|
||||
if len(value) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if len(value) == 1 {
|
||||
return value[0].Interface(), nil
|
||||
}
|
||||
|
||||
results := make([]any, len(value))
|
||||
|
||||
for i, v := range value {
|
||||
results[i] = v.Interface()
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (sm *StructMethodGet) Type() string {
|
||||
return sm.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "struct.method.get",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
nameString, err := params.GetString("name")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("struct.method.get name error: %w", err)
|
||||
}
|
||||
|
||||
return &StructMethodGet{config: config, Name: nameString}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
27
internal/processor/test/artnet-packet-decode_test.go
Normal file
27
internal/processor/test/artnet-packet-decode_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestArtnetPacketCreateFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["artnet.packet.decode"]
|
||||
if !ok {
|
||||
t.Fatalf("artnet.packet.decode processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "artnet.packet.decode",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create artnet.packet.decode processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "artnet.packet.decode" {
|
||||
t.Fatalf("artnet.packet.decode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
27
internal/processor/test/artnet-packet-encode_test.go
Normal file
27
internal/processor/test/artnet-packet-encode_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestArtnetPacketEncodeFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["artnet.packet.encode"]
|
||||
if !ok {
|
||||
t.Fatalf("artnet.packet.encode processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "artnet.packet.encode",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create artnet.packet.encode processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "artnet.packet.encode" {
|
||||
t.Fatalf("artnet.packet.encode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
@@ -71,12 +71,15 @@ func TestGoodArtnetPacketFilter(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
opCode uint16
|
||||
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,
|
||||
@@ -90,7 +93,6 @@ func TestGoodArtnetPacketFilter(t *testing.T) {
|
||||
Hours: 0,
|
||||
Type: 0,
|
||||
},
|
||||
opCode: artnet.OpTimeCode,
|
||||
expected: &artnet.ArtTimeCode{
|
||||
ID: []byte{'A', 'r', 't', '-', 'N', 'e', 't', 0x00},
|
||||
OpCode: artnet.OpTimeCode,
|
||||
@@ -106,7 +108,10 @@ func TestGoodArtnetPacketFilter(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "tiemcode packet with mismatching opCode",
|
||||
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,
|
||||
@@ -120,17 +125,27 @@ func TestGoodArtnetPacketFilter(t *testing.T) {
|
||||
Hours: 0,
|
||||
Type: 0,
|
||||
},
|
||||
opCode: artnet.OpDmx,
|
||||
expected: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
artnetPacketFilter := processor.ArtNetPacketFilter{
|
||||
OpCode: test.opCode,
|
||||
registration, ok := processor.ProcessorRegistry["artnet.packet.filter"]
|
||||
if !ok {
|
||||
t.Fatalf("artnet.packet.filter processor not registered")
|
||||
}
|
||||
got, err := artnetPacketFilter.Process(t.Context(), test.payload)
|
||||
|
||||
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 failed: %s", err)
|
||||
@@ -158,8 +173,8 @@ func TestGoodArtnetPacketFilter(t *testing.T) {
|
||||
func TestBadArtnetPacketFilter(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
payload any
|
||||
params map[string]any
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
@@ -184,7 +199,7 @@ func TestBadArtnetPacketFilter(t *testing.T) {
|
||||
Type: 0,
|
||||
},
|
||||
params: map[string]any{},
|
||||
errorString: "artnet.packet.filter requires an opCode parameter",
|
||||
errorString: "artnet.packet.filter opCode error: not found",
|
||||
},
|
||||
{
|
||||
name: "opCode not a number",
|
||||
@@ -202,7 +217,7 @@ func TestBadArtnetPacketFilter(t *testing.T) {
|
||||
Type: 0,
|
||||
},
|
||||
params: map[string]any{"opCode": "100"},
|
||||
errorString: "artnet.packet.filter opCode must be a number",
|
||||
errorString: "artnet.packet.filter opCode error: not a number",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -80,37 +80,55 @@ func TestFloatParseGoodConfig(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGoodFloatParse(t *testing.T) {
|
||||
floatParser := processor.FloatParse{}
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
bitSize int
|
||||
expected float64
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
expected float64
|
||||
}{
|
||||
{
|
||||
name: "positive number",
|
||||
name: "positive number",
|
||||
params: map[string]any{
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
payload: "12345.67",
|
||||
bitSize: 64,
|
||||
expected: 12345.67,
|
||||
},
|
||||
{
|
||||
name: "negative number",
|
||||
name: "negative number",
|
||||
params: map[string]any{
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
payload: "-12345.67",
|
||||
bitSize: 64,
|
||||
expected: -12345.67,
|
||||
},
|
||||
{
|
||||
name: "zero",
|
||||
name: "zero",
|
||||
params: map[string]any{
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
payload: "0",
|
||||
bitSize: 64,
|
||||
expected: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := floatParser.Process(t.Context(), test.payload)
|
||||
registration, ok := processor.ProcessorRegistry["float.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("float.parse processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "float.parse",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("float.parse failed to create processor: %s", err)
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
||||
|
||||
gotFloat, ok := got.(float64)
|
||||
if !ok {
|
||||
@@ -128,38 +146,50 @@ func TestGoodFloatParse(t *testing.T) {
|
||||
|
||||
func TestBadFloatParse(t *testing.T) {
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
bitSize int
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "non-string input",
|
||||
name: "non-string input",
|
||||
params: map[string]any{
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
payload: []byte{0x01},
|
||||
bitSize: 64,
|
||||
errorString: "float.parse processor only accepts a string",
|
||||
},
|
||||
{
|
||||
name: "not float string",
|
||||
name: "not float string",
|
||||
params: map[string]any{
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
payload: "abcd",
|
||||
bitSize: 64,
|
||||
errorString: "strconv.ParseFloat: parsing \"abcd\": invalid syntax",
|
||||
},
|
||||
{
|
||||
name: "bit size overflow",
|
||||
name: "bit size overflow",
|
||||
params: map[string]any{
|
||||
"bitSize": 32.0,
|
||||
},
|
||||
payload: "1.79e+64",
|
||||
bitSize: 32,
|
||||
errorString: "strconv.ParseFloat: parsing \"1.79e+64\": value out of range",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
floatParser := processor.FloatParse{
|
||||
BitSize: test.bitSize,
|
||||
registration, ok := processor.ProcessorRegistry["float.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("float.parse processor not registered")
|
||||
}
|
||||
got, err := floatParser.Process(t.Context(), test.payload)
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "float.parse",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("float.parse expected to fail but succeeded, got: %v", got)
|
||||
38
internal/processor/test/free-d-create_test.go
Normal file
38
internal/processor/test/free-d-create_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestFreeDCreateFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["freed.create"]
|
||||
if !ok {
|
||||
t.Fatalf("freed.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "freed.create",
|
||||
Params: map[string]any{
|
||||
"id": "0",
|
||||
"pan": "0",
|
||||
"tilt": "0",
|
||||
"roll": "0",
|
||||
"posX": "0",
|
||||
"posY": "0",
|
||||
"posZ": "0",
|
||||
"zoom": "0",
|
||||
"focus": "0",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create freed.create processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "freed.create" {
|
||||
t.Fatalf("freed.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
27
internal/processor/test/free-d-decode_test.go
Normal file
27
internal/processor/test/free-d-decode_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestFreeDDecodeFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["freed.decode"]
|
||||
if !ok {
|
||||
t.Fatalf("freed.decode processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "freed.decode",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create freed.decode processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "freed.decode" {
|
||||
t.Fatalf("freed.decode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
27
internal/processor/test/free-d-encode_test.go
Normal file
27
internal/processor/test/free-d-encode_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestFreeDEncodeFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["freed.encode"]
|
||||
if !ok {
|
||||
t.Fatalf("freed.encode processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "freed.encode",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create freed.encode processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "freed.encode" {
|
||||
t.Fatalf("freed.encode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
BIN
internal/processor/test/good.wasm
Executable file
BIN
internal/processor/test/good.wasm
Executable file
Binary file not shown.
31
internal/processor/test/http-request-create_test.go
Normal file
31
internal/processor/test/http-request-create_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestHTTPRequestCreateFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["http.request.create"]
|
||||
if !ok {
|
||||
t.Fatalf("http.request.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "http.request.create",
|
||||
Params: map[string]any{
|
||||
"method": "GET",
|
||||
"url": "http://example.com",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create http.request.create processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "http.request.create" {
|
||||
t.Fatalf("http.request.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
27
internal/processor/test/http-request-encode_test.go
Normal file
27
internal/processor/test/http-request-encode_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestHTTPRequestEncodeFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["http.request.encode"]
|
||||
if !ok {
|
||||
t.Fatalf("http.request.encode processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "http.request.encode",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create http.request.encode processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "http.request.encode" {
|
||||
t.Fatalf("http.request.encode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
31
internal/processor/test/http-request-filter_test.go
Normal file
31
internal/processor/test/http-request-filter_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
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())
|
||||
}
|
||||
}
|
||||
31
internal/processor/test/http-response-create_test.go
Normal file
31
internal/processor/test/http-response-create_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestHTTPResponseCreateFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["http.response.create"]
|
||||
if !ok {
|
||||
t.Fatalf("http.response.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "http.response.create",
|
||||
Params: map[string]any{
|
||||
"status": 200.0,
|
||||
"bodyTemplate": "Hello, World!",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create http.response.create processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "http.response.create" {
|
||||
t.Fatalf("http.response.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
27
internal/processor/test/http-response-encode_test.go
Normal file
27
internal/processor/test/http-response-encode_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestHTTPResponseEncodeFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["http.response.encode"]
|
||||
if !ok {
|
||||
t.Fatalf("http.response.encode processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "http.response.encode",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create http.response.encode processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "http.response.encode" {
|
||||
t.Fatalf("http.response.encode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
@@ -100,58 +100,75 @@ func TestIntParseGoodConfig(t *testing.T) {
|
||||
|
||||
func TestGoodIntParse(t *testing.T) {
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
expected int64
|
||||
base int
|
||||
bitSize int
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
expected int64
|
||||
}{
|
||||
{
|
||||
name: "positive number",
|
||||
name: "positive number",
|
||||
params: map[string]any{
|
||||
"base": 10.0,
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
payload: "12345",
|
||||
expected: 12345,
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "negative number",
|
||||
name: "negative number",
|
||||
params: map[string]any{
|
||||
"base": 10.0,
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
payload: "-12345",
|
||||
expected: -12345,
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "zero",
|
||||
name: "zero",
|
||||
params: map[string]any{
|
||||
"base": 10.0,
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
payload: "0",
|
||||
expected: 0,
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "binary",
|
||||
name: "binary",
|
||||
params: map[string]any{
|
||||
"base": 2.0,
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
payload: "1010101",
|
||||
expected: 85,
|
||||
base: 2,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "hex",
|
||||
name: "hex",
|
||||
params: map[string]any{
|
||||
"base": 16.0,
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
payload: "15F",
|
||||
expected: 351,
|
||||
base: 16,
|
||||
bitSize: 64,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
intParser := processor.IntParse{
|
||||
Base: test.base,
|
||||
BitSize: test.bitSize,
|
||||
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("int.parse processor not registered")
|
||||
}
|
||||
|
||||
got, err := intParser.Process(t.Context(), test.payload)
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "int.parse",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("int.parse failed to create processor: %s", err)
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
||||
|
||||
gotInt, ok := got.(int64)
|
||||
if !ok {
|
||||
@@ -169,43 +186,53 @@ func TestGoodIntParse(t *testing.T) {
|
||||
|
||||
func TestBadIntParse(t *testing.T) {
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
base int
|
||||
bitSize int
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "non-string input",
|
||||
name: "non-string input",
|
||||
params: map[string]any{
|
||||
"base": 10.0,
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
payload: []byte{0x01},
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
errorString: "int.parse processor only accepts a string",
|
||||
},
|
||||
{
|
||||
name: "not int string",
|
||||
name: "not int string",
|
||||
params: map[string]any{
|
||||
"base": 10.0,
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
payload: "123.46",
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
errorString: "strconv.ParseInt: parsing \"123.46\": invalid syntax",
|
||||
},
|
||||
{
|
||||
name: "bit overflow",
|
||||
name: "bit overflow",
|
||||
params: map[string]any{
|
||||
"base": 10.0,
|
||||
"bitSize": 32.0,
|
||||
},
|
||||
payload: "12345678901234567890",
|
||||
base: 10,
|
||||
bitSize: 32,
|
||||
errorString: "strconv.ParseInt: parsing \"12345678901234567890\": value out of range",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
intParser := processor.IntParse{
|
||||
Base: test.base,
|
||||
BitSize: test.bitSize,
|
||||
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("int.parse processor not registered")
|
||||
}
|
||||
got, err := intParser.Process(t.Context(), test.payload)
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "int.parse",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("int.parse expected to fail but succeeded, got: %v", got)
|
||||
@@ -67,27 +67,37 @@ func TestIntRandomGoodConfig(t *testing.T) {
|
||||
|
||||
func TestGoodIntRandom(t *testing.T) {
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
min int
|
||||
max int
|
||||
name string
|
||||
payload any
|
||||
params map[string]any
|
||||
}{
|
||||
{
|
||||
name: "1-10",
|
||||
name: "1-10",
|
||||
params: map[string]any{
|
||||
"min": 1.0,
|
||||
"max": 10.0,
|
||||
},
|
||||
payload: "12345",
|
||||
min: 1,
|
||||
max: 10,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
intRandom := processor.IntRandom{
|
||||
Min: test.min,
|
||||
Max: test.max,
|
||||
registration, ok := processor.ProcessorRegistry["int.random"]
|
||||
if !ok {
|
||||
t.Fatalf("int.random processor not registered")
|
||||
}
|
||||
got, err := intRandom.Process(t.Context(), test.payload)
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "int.random",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("int.random failed to create processor: %s", err)
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
||||
gotInt, ok := got.(int)
|
||||
if !ok {
|
||||
t.Fatalf("int.random returned a %T payload: %s", got, got)
|
||||
@@ -95,8 +105,18 @@ func TestGoodIntRandom(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("int.random failed: %s", err)
|
||||
}
|
||||
if gotInt < test.min || gotInt > test.max {
|
||||
t.Fatalf("int.random got %d, expected between %d and %d", gotInt, test.min, test.max)
|
||||
|
||||
minNum, ok := test.params["min"].(float64)
|
||||
if !ok {
|
||||
t.Fatalf("int.random test min param is not a number: %s", test.params["min"])
|
||||
}
|
||||
maxNum, ok := test.params["max"].(float64)
|
||||
if !ok {
|
||||
t.Fatalf("int.random test max param is not a number: %s", test.params["max"])
|
||||
}
|
||||
|
||||
if gotInt < int(minNum) || gotInt > int(maxNum) {
|
||||
t.Fatalf("int.random got %d, expected between %d and %d", gotInt, int(minNum), int(maxNum))
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -113,25 +133,25 @@ func TestBadIntRandom(t *testing.T) {
|
||||
name: "no min param",
|
||||
payload: "hello",
|
||||
params: map[string]any{"max": 10.0},
|
||||
errorString: "int.random requires a min parameter",
|
||||
errorString: "int.random min error: not found",
|
||||
},
|
||||
{
|
||||
name: "no max param",
|
||||
payload: "hello",
|
||||
params: map[string]any{"min": 1.0},
|
||||
errorString: "int.random requires a max parameter",
|
||||
errorString: "int.random max error: not found",
|
||||
},
|
||||
{
|
||||
name: "min param not a number",
|
||||
payload: "hello",
|
||||
params: map[string]any{"min": "1", "max": 10.0},
|
||||
errorString: "int.random min must be a number",
|
||||
errorString: "int.random min error: not a number",
|
||||
},
|
||||
{
|
||||
name: "max param not a number",
|
||||
payload: "hello",
|
||||
params: map[string]any{"min": 1.0, "max": "10"},
|
||||
errorString: "int.random max must be a number",
|
||||
errorString: "int.random max error: not a number",
|
||||
},
|
||||
{
|
||||
name: "max less than min",
|
||||
@@ -89,3 +89,36 @@ func TestGoodJsonDecode(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadJsonDecode(t *testing.T) {
|
||||
stringEncoder := processor.JsonDecode{}
|
||||
tests := []struct {
|
||||
name string
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "non-string input",
|
||||
payload: []byte("hello"),
|
||||
errorString: "json.decode processor only accepts a string",
|
||||
},
|
||||
{
|
||||
name: "invalid json",
|
||||
payload: "{\"address\":\"/hello\",\"args\":}",
|
||||
errorString: "invalid character '}' looking for beginning of value",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := stringEncoder.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("json.decode expected to fail but got payload: %+v", got)
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("json.decode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -83,3 +83,31 @@ func TestGoodJsonEncode(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadJsonEncode(t *testing.T) {
|
||||
stringEncoder := processor.JsonEncode{}
|
||||
tests := []struct {
|
||||
name string
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "unencodable type",
|
||||
payload: make(chan int),
|
||||
errorString: "json: unsupported type: chan int",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := stringEncoder.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("json.encode expected to fail but got payload: %+v", got)
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("json.encode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
33
internal/processor/test/midi-message-create_test.go
Normal file
33
internal/processor/test/midi-message-create_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestMIDIMessageCreateFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.message.create"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.message.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.message.create",
|
||||
Params: map[string]any{
|
||||
"type": "note_on",
|
||||
"channel": "1",
|
||||
"note": "60",
|
||||
"velocity": "100",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create midi.message.create processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "midi.message.create" {
|
||||
t.Fatalf("midi.message.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
27
internal/processor/test/midi-message-decode_test.go
Normal file
27
internal/processor/test/midi-message-decode_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestMIDIMessageDecodeFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.message.decode"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.message.decode processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.message.decode",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create midi.message.decode processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "midi.message.decode" {
|
||||
t.Fatalf("midi.message.decode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
89
internal/processor/test/midi-message-encode_test.go
Normal file
89
internal/processor/test/midi-message-encode_test.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
func TestMIDIMessageEncodeFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.message.encode"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.message.encode processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.message.encode",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create midi.message.encode processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "midi.message.encode" {
|
||||
t.Fatalf("midi.message.encode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodMIDIMessageEncode(t *testing.T) {
|
||||
midiMessageEncoder := processor.MIDIMessageEncode{}
|
||||
tests := []struct {
|
||||
name string
|
||||
payload any
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
name: "note on message",
|
||||
payload: midi.NoteOn(1, 60, 127),
|
||||
expected: []byte{0x91, 0x3c, 0x7f},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := midiMessageEncoder.Process(t.Context(), test.payload)
|
||||
|
||||
gotBytes, ok := got.([]byte)
|
||||
if !ok {
|
||||
t.Fatalf("midi.message.encode returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("midi.message.encode failed: %s", err)
|
||||
}
|
||||
if !slices.Equal(gotBytes, test.expected) {
|
||||
t.Fatalf("midi.message.encode got %+v, expected %+v", got, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadMIDIMessageEncode(t *testing.T) {
|
||||
midiMessageEncoder := processor.MIDIMessageEncode{}
|
||||
tests := []struct {
|
||||
name string
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "non-midi message input",
|
||||
payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
||||
errorString: "midi.message.encode processor only accepts a midi.Message",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := midiMessageEncoder.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("midi.message.encode expected to fail but got payload: %s", got)
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("midi.message.encode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
157
internal/processor/test/midi-message-filter_test.go
Normal file
157
internal/processor/test/midi-message-filter_test.go
Normal file
@@ -0,0 +1,157 @@
|
||||
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 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
27
internal/processor/test/midi-message-unpack_test.go
Normal file
27
internal/processor/test/midi-message-unpack_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestMIDIMessageUnpackFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["midi.message.unpack"]
|
||||
if !ok {
|
||||
t.Fatalf("midi.message.unpack processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "midi.message.unpack",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create midi.message.unpack processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "midi.message.unpack" {
|
||||
t.Fatalf("midi.message.unpack processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
33
internal/processor/test/mqtt-message-create_test.go
Normal file
33
internal/processor/test/mqtt-message-create_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestMQTTMessageCreateFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["mqtt.message.create"]
|
||||
if !ok {
|
||||
t.Fatalf("mqtt.message.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "mqtt.message.create",
|
||||
Params: map[string]any{
|
||||
"topic": "test/topic",
|
||||
"payload": "Hello, World!",
|
||||
"qos": 1.0,
|
||||
"retained": true,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create mqtt.message.create processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "mqtt.message.create" {
|
||||
t.Fatalf("mqtt.message.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
89
internal/processor/test/mqtt-message-encode_test.go
Normal file
89
internal/processor/test/mqtt-message-encode_test.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestMQTTMessageEncodeFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["mqtt.message.encode"]
|
||||
if !ok {
|
||||
t.Fatalf("mqtt.message.encode processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "mqtt.message.encode",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create mqtt.message.encode processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "mqtt.message.encode" {
|
||||
t.Fatalf("mqtt.message.encode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodMQTTMessageEncode(t *testing.T) {
|
||||
stringEncoder := processor.MQTTMessageEncode{}
|
||||
tests := []struct {
|
||||
name string
|
||||
payload mqtt.Message
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
name: "basic string",
|
||||
payload: processor.NewMQTTMessage("test/topic", 1, true, []byte("hello")),
|
||||
expected: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := stringEncoder.Process(t.Context(), test.payload)
|
||||
|
||||
gotBytes, ok := got.([]byte)
|
||||
if !ok {
|
||||
t.Fatalf("mqtt.message.encode returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("mqtt.message.encode failed: %s", err)
|
||||
}
|
||||
if !slices.Equal(gotBytes, test.expected) {
|
||||
t.Fatalf("mqtt.message.encode got %s, expected %s", got, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadMQTTMessageEncode(t *testing.T) {
|
||||
stringEncoder := processor.MQTTMessageEncode{}
|
||||
tests := []struct {
|
||||
name string
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "non-mqtt message input",
|
||||
payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
||||
errorString: "mqtt.message.encode processor only accepts an mqtt.Message",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := stringEncoder.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("mqtt.message.encode expected to fail but got payload: %s", got)
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("mqtt.message.encode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
31
internal/processor/test/nats-message-create_test.go
Normal file
31
internal/processor/test/nats-message-create_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestNATSMessageCreateFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["nats.message.create"]
|
||||
if !ok {
|
||||
t.Fatalf("nats.message.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "nats.message.create",
|
||||
Params: map[string]any{
|
||||
"subject": "test",
|
||||
"payload": "Hello, World!",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create nats.message.create processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "nats.message.create" {
|
||||
t.Fatalf("nats.message.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
341
internal/processor/test/osc-message-create_test.go
Normal file
341
internal/processor/test/osc-message-create_test.go
Normal file
@@ -0,0 +1,341 @@
|
||||
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 TestOSCMessageCreateFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["osc.message.create"]
|
||||
if !ok {
|
||||
t.Fatalf("osc.message.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "osc.message.create",
|
||||
Params: map[string]any{
|
||||
"address": "/test",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create osc.message.create processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "osc.message.create" {
|
||||
t.Fatalf("osc.message.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodOSCMessageCreate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
payload any
|
||||
params map[string]any
|
||||
expected any
|
||||
}{
|
||||
{
|
||||
name: "basic address and no args",
|
||||
params: map[string]any{
|
||||
"address": "/test",
|
||||
},
|
||||
payload: osc.OSCMessage{},
|
||||
expected: osc.OSCMessage{Address: "/test"},
|
||||
},
|
||||
{
|
||||
name: "address with template and no args",
|
||||
params: map[string]any{
|
||||
"address": "/test/{{.Value}}",
|
||||
},
|
||||
payload: map[string]any{"Value": "value"},
|
||||
expected: osc.OSCMessage{Address: "/test/value"},
|
||||
},
|
||||
{
|
||||
name: "address with template and string arg",
|
||||
params: map[string]any{
|
||||
"address": "/test/{{.Value}}",
|
||||
"args": []interface{}{"arg1"},
|
||||
"types": "s",
|
||||
},
|
||||
payload: map[string]any{"Value": "value"},
|
||||
expected: osc.OSCMessage{Address: "/test/value", Args: []osc.OSCArg{{Value: "arg1", Type: "s"}}},
|
||||
},
|
||||
{
|
||||
name: "address with template and mixed args",
|
||||
params: map[string]any{
|
||||
"address": "/test/{{.Value}}",
|
||||
"args": []interface{}{"arg1", "42", "3.14"},
|
||||
"types": "sif",
|
||||
},
|
||||
payload: map[string]any{"Value": "value"},
|
||||
expected: osc.OSCMessage{
|
||||
Address: "/test/value",
|
||||
Args: []osc.OSCArg{
|
||||
{Value: "arg1", Type: "s"},
|
||||
{Value: int32(42), Type: "i"},
|
||||
{Value: float32(3.14), Type: "f"},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "address with template and int64 arg",
|
||||
params: map[string]any{
|
||||
"address": "/test/{{.Value}}",
|
||||
"args": []interface{}{"42"},
|
||||
"types": "h",
|
||||
},
|
||||
payload: map[string]any{"Value": "value"},
|
||||
expected: osc.OSCMessage{Address: "/test/value", Args: []osc.OSCArg{{Value: int64(42), Type: "h"}}},
|
||||
},
|
||||
{
|
||||
name: "address with template and double arg",
|
||||
params: map[string]any{
|
||||
"address": "/test/{{.Value}}",
|
||||
"args": []interface{}{"42"},
|
||||
"types": "d",
|
||||
},
|
||||
payload: map[string]any{"Value": "value"},
|
||||
expected: osc.OSCMessage{Address: "/test/value", Args: []osc.OSCArg{{Value: float64(42), Type: "d"}}},
|
||||
},
|
||||
{
|
||||
name: "address with template and true arg",
|
||||
params: map[string]any{
|
||||
"address": "/test/{{.Value}}",
|
||||
"args": []interface{}{""},
|
||||
"types": "T",
|
||||
},
|
||||
payload: map[string]any{"Value": "value"},
|
||||
expected: osc.OSCMessage{Address: "/test/value", Args: []osc.OSCArg{{Value: true, Type: "T"}}},
|
||||
},
|
||||
{
|
||||
name: "address with template and false arg",
|
||||
params: map[string]any{
|
||||
"address": "/test/{{.Value}}",
|
||||
"args": []interface{}{""},
|
||||
"types": "F",
|
||||
},
|
||||
payload: map[string]any{"Value": "value"},
|
||||
expected: osc.OSCMessage{Address: "/test/value", Args: []osc.OSCArg{{Value: false, Type: "F"}}},
|
||||
},
|
||||
{
|
||||
name: "address with template and nil arg",
|
||||
params: map[string]any{
|
||||
"address": "/test/{{.Value}}",
|
||||
"args": []interface{}{""},
|
||||
"types": "N",
|
||||
},
|
||||
payload: map[string]any{"Value": "value"},
|
||||
expected: osc.OSCMessage{Address: "/test/value", Args: []osc.OSCArg{{Value: nil, Type: "N"}}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["osc.message.create"]
|
||||
if !ok {
|
||||
t.Fatalf("osc.message.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "osc.message.create",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("osc.message.create failed to create processor: %s", err)
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("osc.message.create process failed: %s", err)
|
||||
}
|
||||
|
||||
if test.expected == nil {
|
||||
if got != nil {
|
||||
t.Fatalf("osc.message.create got %+v, expected nil", got)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
gotMessage, ok := got.(osc.OSCMessage)
|
||||
if !ok {
|
||||
t.Fatalf("osc.message.create returned a %T payload: %s", got, got)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(gotMessage, test.expected) {
|
||||
t.Fatalf("osc.message.create got %+v, expected %+v", gotMessage, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadOSCMessageCreate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no address parameter",
|
||||
params: map[string]any{},
|
||||
payload: "test",
|
||||
errorString: "osc.message.create address error: not found",
|
||||
},
|
||||
{
|
||||
name: "non-string address parameter",
|
||||
params: map[string]any{
|
||||
"address": 123,
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "osc.message.create address error: not a string",
|
||||
},
|
||||
{
|
||||
name: "bad address template",
|
||||
params: map[string]any{
|
||||
"address": "{{",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "template: address:1: unclosed action",
|
||||
},
|
||||
{
|
||||
name: "non-array args parameter",
|
||||
params: map[string]any{
|
||||
"address": "/test",
|
||||
"args": "not an array",
|
||||
"types": "s",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "osc.message.create address must be an array found string",
|
||||
},
|
||||
{
|
||||
name: "args without types parameter",
|
||||
params: map[string]any{
|
||||
"address": "/test",
|
||||
"args": []interface{}{"arg1"},
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "osc.message.create types error: not found",
|
||||
},
|
||||
{
|
||||
name: "args and types length mismatch",
|
||||
params: map[string]any{
|
||||
"address": "/test",
|
||||
"args": []interface{}{"arg1", "arg2"},
|
||||
"types": "s",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "osc.message.create args and types must be the same length",
|
||||
},
|
||||
{
|
||||
name: "non-string arg",
|
||||
params: map[string]any{
|
||||
"address": "/test",
|
||||
"args": []interface{}{"arg1", 123},
|
||||
"types": "ss",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "osc.message.create arg error: not a string",
|
||||
},
|
||||
{
|
||||
name: "bad arg template",
|
||||
params: map[string]any{
|
||||
"address": "/test",
|
||||
"args": []interface{}{"{{"},
|
||||
"types": "s",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "template: arg:1: unclosed action",
|
||||
},
|
||||
{
|
||||
name: "non-string types parameter",
|
||||
params: map[string]any{
|
||||
"address": "/test",
|
||||
"args": []interface{}{"arg1"},
|
||||
"types": 123,
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "osc.message.create types error: not a string",
|
||||
},
|
||||
{
|
||||
name: "invalid type in types parameter",
|
||||
params: map[string]any{
|
||||
"address": "/test",
|
||||
"args": []interface{}{"arg1"},
|
||||
"types": "x",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "osc.message.create unhandled osc type: x",
|
||||
},
|
||||
{
|
||||
name: "empty address template",
|
||||
params: map[string]any{
|
||||
"address": "",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "osc.message.create address must not be empty",
|
||||
},
|
||||
{
|
||||
name: "address template with missing value",
|
||||
params: map[string]any{
|
||||
"address": "/test/{{.missing}}",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "template: address:1:8: executing \"address\" at <.missing>: can't evaluate field missing in type string",
|
||||
},
|
||||
{
|
||||
name: "address doesn't start with slash",
|
||||
params: map[string]any{
|
||||
"address": "test",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "osc.message.create address must start with '/'",
|
||||
},
|
||||
{
|
||||
name: "address template with missing field",
|
||||
params: map[string]any{
|
||||
"address": "/test",
|
||||
"args": []interface{}{"{{.missing}}"},
|
||||
"types": "s",
|
||||
},
|
||||
payload: "test",
|
||||
errorString: "template: arg:1:2: executing \"arg\" at <.missing>: can't evaluate field missing in type string",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["osc.message.create"]
|
||||
if !ok {
|
||||
t.Fatalf("osc.message.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "osc.message.create",
|
||||
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.create expected to fail but succeeded, got: %v", got)
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("osc.message.create got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
27
internal/processor/test/osc-message-decode_test.go
Normal file
27
internal/processor/test/osc-message-decode_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestOSCMessageDecodeFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["osc.message.decode"]
|
||||
if !ok {
|
||||
t.Fatalf("osc.message.decode processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "osc.message.decode",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create osc.message.decode processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "osc.message.decode" {
|
||||
t.Fatalf("osc.message.decode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user