package processor import ( "context" "encoding/json" "errors" "fmt" "math/rand/v2" "github.com/google/jsonschema-go/jsonschema" "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" ) type FloatRandom struct { BitSize int Min float64 Max float64 config config.ProcessorConfig } func (fr *FloatRandom) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) { if fr.BitSize == 32 { payloadFloat := rand.Float32()*(float32(fr.Max)-float32(fr.Min)) + float32(fr.Min) wrappedPayload.Payload = payloadFloat return wrappedPayload, nil } if fr.BitSize == 64 { payloadFloat := rand.Float64()*(fr.Max-fr.Min) + fr.Min wrappedPayload.Payload = payloadFloat return wrappedPayload, nil } wrappedPayload.End = true return wrappedPayload, errors.New("float.random bitSize error: must be 32 or 64") } func (fr *FloatRandom) Type() string { return fr.config.Type } func init() { RegisterProcessor(ProcessorRegistration{ Type: "float.random", Title: "Random Float", ParamsSchema: &jsonschema.Schema{ Type: "object", Properties: map[string]*jsonschema.Schema{ "bitSize": { Title: "Bit Size", Type: "integer", Enum: []any{32, 64}, Default: json.RawMessage("32"), }, "min": { Title: "Minimum", Type: "number", }, "max": { Title: "Maximum", Type: "number", }, }, Required: []string{"min", "max"}, AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}}, }, New: func(processorConfig config.ProcessorConfig) (Processor, error) { params := processorConfig.Params bitSizeInt, err := params.GetInt("bitSize") if err != nil { if errors.Is(err, config.ErrParamNotFound) { bitSizeInt = 32 } else { return nil, fmt.Errorf("float.random bitSize error: %w", err) } } if bitSizeInt != 32 && bitSizeInt != 64 { return nil, errors.New("float.random bitSize error: must be 32 or 64") } minFloat, err := params.GetFloat64("min") if err != nil { return nil, fmt.Errorf("float.random min error: %w", err) } maxFloat, err := params.GetFloat64("max") if err != nil { return nil, fmt.Errorf("float.random max error: %w", err) } if maxFloat < minFloat { return nil, errors.New("float.random max must be greater than min") } return &FloatRandom{config: processorConfig, Min: minFloat, Max: maxFloat, BitSize: bitSizeInt}, nil }, }) }