diff --git a/internal/processor/test/uint-parse_test.go b/internal/processor/test/uint-parse_test.go deleted file mode 100644 index 164a501..0000000 --- a/internal/processor/test/uint-parse_test.go +++ /dev/null @@ -1,171 +0,0 @@ -package processor_test - -import ( - "testing" - - "github.com/jwetzell/showbridge-go/internal/config" - "github.com/jwetzell/showbridge-go/internal/processor" -) - -func TestUintParseFromRegistry(t *testing.T) { - registration, ok := processor.ProcessorRegistry["uint.parse"] - if !ok { - t.Fatalf("uint.parse processor not registered") - } - - processorInstance, err := registration.New(config.ProcessorConfig{ - Type: "uint.parse", - }) - - if err != nil { - t.Fatalf("failed to create uint.parse processor: %s", err) - } - - if processorInstance.Type() != "uint.parse" { - t.Fatalf("uint.parse processor has wrong type: %s", processorInstance.Type()) - } -} - -func TestGoodUintParse(t *testing.T) { - - tests := []struct { - name string - params map[string]any - payload any - expected uint64 - }{ - { - name: "positive number", - params: map[string]any{"base": 10, "bitSize": 64}, - payload: "12345", - expected: 12345, - }, - { - name: "zero", - params: map[string]any{"base": 10, "bitSize": 64}, - payload: "0", - expected: 0, - }, - { - name: "binary", - params: map[string]any{"base": 2, "bitSize": 64}, - payload: "1010101", - expected: 85, - }, - { - name: "hex", - params: map[string]any{"base": 16, "bitSize": 64}, - payload: "15F", - expected: 351, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - registration, ok := processor.ProcessorRegistry["uint.parse"] - if !ok { - t.Fatalf("uint.parse processor not registered") - } - - processorInstance, err := registration.New(config.ProcessorConfig{ - Type: "uint.parse", - Params: test.params, - }) - - if err != nil { - t.Fatalf("uint.parse failed to create processor: %s", err) - } - - got, err := processorInstance.Process(t.Context(), test.payload) - if err != nil { - t.Fatalf("uint.parse processing failed: %s", err) - } - - gotUint, ok := got.(uint64) - if !ok { - t.Fatalf("uint.parse returned a %T payload: %s", got, got) - } - - if gotUint != test.expected { - t.Fatalf("uint.parse got %d, expected %d", gotUint, test.expected) - } - }) - } -} - -func TestBadUintParse(t *testing.T) { - tests := []struct { - name string - params map[string]any - payload any - errorString string - }{ - { - name: "non-string base", - params: map[string]any{ - "base": "10", - "bitSize": 64, - }, - payload: "12345", - errorString: "uint.parse base error: not a number", - }, - { - name: "non-string bitSize", - params: map[string]any{ - "base": 10, - "bitSize": "64", - }, - payload: "12345", - errorString: "uint.parse bitSize error: not a number", - }, - { - name: "non-string input", - params: map[string]any{"base": 10, "bitSize": 64}, - payload: []byte{0x01}, - errorString: "uint.parse processor only accepts a string", - }, - { - name: "not uint string", - params: map[string]any{"base": 10, "bitSize": 64}, - payload: "-1234", - errorString: "strconv.ParseUint: parsing \"-1234\": invalid syntax", - }, - { - name: "bit overflow", - params: map[string]any{"base": 10, "bitSize": 32}, - payload: "123456789012345678901234567", - errorString: "strconv.ParseUint: parsing \"123456789012345678901234567\": value out of range", - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - registration, ok := processor.ProcessorRegistry["uint.parse"] - if !ok { - t.Fatalf("uint.parse processor not registered") - } - - processorInstance, err := registration.New(config.ProcessorConfig{ - Type: "uint.parse", - Params: test.params, - }) - - if err != nil { - if err.Error() != test.errorString { - t.Fatalf("uint.parse got error '%s', expected '%s'", err.Error(), test.errorString) - } - return - } - - got, err := processorInstance.Process(t.Context(), test.payload) - - if err == nil { - t.Fatalf("uint.parse expected to fail but succeeded, got: %v", got) - - } - if err.Error() != test.errorString { - t.Fatalf("uint.parse got error '%s', expected '%s'", err.Error(), test.errorString) - } - }) - } -} diff --git a/internal/processor/test/uint-random_test.go b/internal/processor/test/uint-random_test.go deleted file mode 100644 index 9a6216b..0000000 --- a/internal/processor/test/uint-random_test.go +++ /dev/null @@ -1,193 +0,0 @@ -package processor_test - -import ( - "testing" - - "github.com/jwetzell/showbridge-go/internal/config" - "github.com/jwetzell/showbridge-go/internal/processor" -) - -func TestUintRandomFromRegistry(t *testing.T) { - registration, ok := processor.ProcessorRegistry["uint.random"] - if !ok { - t.Fatalf("uint.random processor not registered") - } - - processorInstance, err := registration.New(config.ProcessorConfig{ - Type: "uint.random", - Params: map[string]any{ - "min": 1, - "max": 10, - }, - }) - - if err != nil { - t.Fatalf("failed to create uint.random processor: %s", err) - } - - if processorInstance.Type() != "uint.random" { - t.Fatalf("uint.random processor has wrong type: %s", processorInstance.Type()) - } -} - -func TestUintRandomGoodConfig(t *testing.T) { - registration, ok := processor.ProcessorRegistry["uint.random"] - if !ok { - t.Fatalf("uint.random processor not registered") - } - - processorInstance, err := registration.New(config.ProcessorConfig{ - Type: "uint.random", - Params: map[string]any{ - "min": 1, - "max": 10, - }, - }) - - if err != nil { - t.Fatalf("uint.random should have created processor but got error: %s", err) - } - - payload := "12345" - - got, err := processorInstance.Process(t.Context(), payload) - if err != nil { - t.Fatalf("uint.random processing failed: %s", err) - } - - gotUint, ok := got.(uint) - if !ok { - t.Fatalf("uint.random returned a %T payload: %s", got, got) - } - - if gotUint < 1 || gotUint > 10 { - t.Fatalf("uint.random got %d, expected between %d and %d", gotUint, 1, 10) - } -} - -func TestGoodUintRandom(t *testing.T) { - - tests := []struct { - name string - params map[string]any - payload any - }{ - { - name: "1-10", - params: map[string]any{"min": 1, "max": 10}, - payload: "12345", - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - registration, ok := processor.ProcessorRegistry["uint.random"] - if !ok { - t.Fatalf("uint.random processor not registered") - } - - processorInstance, err := registration.New(config.ProcessorConfig{ - Type: "uint.random", - Params: test.params, - }) - - if err != nil { - t.Fatalf("uint.random failed to create processor: %s", err) - } - - got, err := processorInstance.Process(t.Context(), test.payload) - if err != nil { - t.Fatalf("uint.random processing failed: %s", err) - } - - gotUint, ok := got.(uint) - if !ok { - t.Fatalf("uint.random returned a %T payload: %s", got, got) - } - - minNum, ok := test.params["min"].(int) - if !ok { - t.Fatalf("uint.random test min param is not a number") - } - maxNum, ok := test.params["max"].(int) - if !ok { - t.Fatalf("uint.random test max param is not a number") - } - if gotUint < uint(minNum) || gotUint > uint(maxNum) { - t.Fatalf("uint.random got %d, expected between %d and %d", gotUint, uint(minNum), uint(maxNum)) - } - }) - } -} - -func TestBadUintRandom(t *testing.T) { - tests := []struct { - name string - params map[string]any - payload any - errorString string - }{ - { - name: "no min param", - payload: "hello", - params: map[string]any{"max": 10}, - errorString: "uint.random min error: not found", - }, - { - name: "no max param", - payload: "hello", - params: map[string]any{"min": 1}, - errorString: "uint.random max error: not found", - }, - { - name: "min param not a number", - payload: "hello", - params: map[string]any{"min": "1", "max": 10}, - errorString: "uint.random min error: not a number", - }, - { - name: "max param not a number", - payload: "hello", - params: map[string]any{"min": 1, "max": "10"}, - errorString: "uint.random max error: not a number", - }, - { - name: "max less than min", - payload: "hello", - params: map[string]any{"min": 1, "max": 0}, - errorString: "uint.random max must be greater than min", - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - - registration, ok := processor.ProcessorRegistry["uint.random"] - if !ok { - t.Fatalf("uint.random processor not registered") - } - - processorInstance, err := registration.New(config.ProcessorConfig{ - Type: "uint.random", - Params: test.params, - }) - - if err != nil { - if test.errorString != err.Error() { - t.Fatalf("uint.random got error '%s', expected '%s'", err.Error(), test.errorString) - } - return - } - - got, err := processorInstance.Process(t.Context(), test.payload) - - if err == nil { - t.Fatalf("uint.random expected to fail but got payload: %s", got) - } - - if err.Error() != test.errorString { - t.Fatalf("uint.random got error '%s', expected '%s'", err.Error(), test.errorString) - } - }) - } -} diff --git a/internal/processor/uint-parse.go b/internal/processor/uint-parse.go deleted file mode 100644 index cbcbeb5..0000000 --- a/internal/processor/uint-parse.go +++ /dev/null @@ -1,61 +0,0 @@ -package processor - -import ( - "context" - "errors" - "fmt" - "strconv" - - "github.com/jwetzell/showbridge-go/internal/config" -) - -type UintParse struct { - Base int - BitSize int - config config.ProcessorConfig -} - -func (up *UintParse) Process(ctx context.Context, payload any) (any, error) { - payloadString, ok := GetAnyAs[string](payload) - - if !ok { - return nil, errors.New("uint.parse processor only accepts a string") - } - - payloadUint, err := strconv.ParseUint(payloadString, up.Base, up.BitSize) - if err != nil { - return nil, err - } - return payloadUint, nil -} - -func (up *UintParse) Type() string { - return up.config.Type -} - -func init() { - RegisterProcessor(ProcessorRegistration{ - Type: "uint.parse", - New: func(moduleConfig config.ProcessorConfig) (Processor, error) { - params := moduleConfig.Params - baseNum, err := params.GetInt("base") - if err != nil { - if errors.Is(err, config.ErrParamNotFound) { - baseNum = 10 - } else { - return nil, fmt.Errorf("uint.parse base error: %w", err) - } - } - - bitSizeNum, err := params.GetInt("bitSize") - if err != nil { - if errors.Is(err, config.ErrParamNotFound) { - bitSizeNum = 64 - } else { - return nil, fmt.Errorf("uint.parse bitSize error: %w", err) - } - } - return &UintParse{config: moduleConfig, Base: baseNum, BitSize: bitSizeNum}, nil - }, - }) -} diff --git a/internal/processor/uint-random.go b/internal/processor/uint-random.go deleted file mode 100644 index ebc3f32..0000000 --- a/internal/processor/uint-random.go +++ /dev/null @@ -1,50 +0,0 @@ -package processor - -import ( - "context" - "errors" - "fmt" - "math/rand/v2" - - "github.com/jwetzell/showbridge-go/internal/config" -) - -type UintRandom struct { - Min uint - Max uint - config config.ProcessorConfig -} - -func (ur *UintRandom) Process(ctx context.Context, payload any) (any, error) { - payloadInt := rand.UintN(ur.Max-ur.Min+1) + ur.Min - return payloadInt, nil -} - -func (ur *UintRandom) Type() string { - return ur.config.Type -} - -func init() { - RegisterProcessor(ProcessorRegistration{ - Type: "uint.random", - New: func(config config.ProcessorConfig) (Processor, error) { - params := config.Params - - minInt, err := params.GetInt("min") - if err != nil { - return nil, fmt.Errorf("uint.random min error: %w", err) - } - - maxInt, err := params.GetInt("max") - if err != nil { - return nil, fmt.Errorf("uint.random max error: %w", err) - } - - if maxInt < minInt { - return nil, errors.New("uint.random max must be greater than min") - } - - return &UintRandom{config: config, Min: uint(minInt), Max: uint(maxInt)}, nil - }, - }) -} diff --git a/schema/processors.schema.json b/schema/processors.schema.json index 8e8164a..00c218b 100644 --- a/schema/processors.schema.json +++ b/schema/processors.schema.json @@ -838,45 +838,6 @@ }, "required": ["type", "params"], "additionalProperties": false - }, - { - "type": "object", - "title": "Parse Unsigned Int", - "properties": { - "type": { - "type": "string", - "const": "uint.parse" - } - }, - "required": ["type"], - "additionalProperties": false - }, - { - "type": "object", - "title": "Random Unsigned Int", - "properties": { - "type": { - "type": "string", - "const": "uint.random" - }, - "params": { - "type": "object", - "properties": { - "min": { - "type": "integer", - "minimum": 0 - }, - "max": { - "type": "integer", - "minimum": 0 - } - }, - "required": ["min", "max"], - "additionalProperties": false - } - }, - "required": ["type", "params"], - "additionalProperties": false } ] }