From d170958d9ba89bb7283566e357c955ba9dee4873 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sun, 28 Dec 2025 09:05:01 -0600 Subject: [PATCH] add processors to create random int/uint --- internal/processor/int-random.go | 61 +++++++++++++++++++++++++++++++ internal/processor/uint-random.go | 61 +++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 internal/processor/int-random.go create mode 100644 internal/processor/uint-random.go diff --git a/internal/processor/int-random.go b/internal/processor/int-random.go new file mode 100644 index 0000000..734ce6f --- /dev/null +++ b/internal/processor/int-random.go @@ -0,0 +1,61 @@ +package processor + +import ( + "context" + "errors" + "math/rand/v2" + + "github.com/jwetzell/showbridge-go/internal/config" +) + +type IntRandom struct { + Min int + Max int + config config.ProcessorConfig +} + +func (up *IntRandom) Process(ctx context.Context, payload any) (any, error) { + payloadInt := rand.IntN(up.Max-up.Min+1) + up.Min + return payloadInt, nil +} + +func (up *IntRandom) Type() string { + return up.config.Type +} + +func init() { + RegisterProcessor(ProcessorRegistration{ + Type: "int.random", + 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") + } + + minFloat, ok := min.(float64) + + if !ok { + return nil, errors.New("int.random min must be a number") + } + + 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 { + return nil, errors.New("int.random max must be greater than min") + } + + return &IntRandom{config: config, Min: int(minFloat), Max: int(maxFloat)}, nil + }, + }) +} diff --git a/internal/processor/uint-random.go b/internal/processor/uint-random.go new file mode 100644 index 0000000..412823a --- /dev/null +++ b/internal/processor/uint-random.go @@ -0,0 +1,61 @@ +package processor + +import ( + "context" + "errors" + "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 + + min, ok := params["min"] + if !ok { + return nil, errors.New("uint.random requires a min parameter") + } + + minFloat, ok := min.(float64) + + if !ok { + return nil, errors.New("uint.random min must be a number") + } + + max, ok := params["max"] + if !ok { + return nil, errors.New("uint.random requires a max parameter") + } + + maxFloat, ok := max.(float64) + + if !ok { + return nil, errors.New("uint.random max must be a number") + } + + if maxFloat < minFloat { + return nil, errors.New("uint.random max must be greater than min") + } + + return &UintRandom{config: config, Min: uint(minFloat), Max: uint(maxFloat)}, nil + }, + }) +}