mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add processors to create random int/uint
This commit is contained in:
61
internal/processor/int-random.go
Normal file
61
internal/processor/int-random.go
Normal file
@@ -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
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user