From 50f755f9146b549362bc1a6db4f7246624165e56 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Wed, 24 Dec 2025 10:34:06 -0600 Subject: [PATCH] make base and bitsize configurable for number parsers --- internal/processor/float-parse.go | 20 ++++++++++++++---- internal/processor/int-parse.go | 34 +++++++++++++++++++++++++++---- internal/processor/uint-parse.go | 33 ++++++++++++++++++++++++++---- 3 files changed, 75 insertions(+), 12 deletions(-) diff --git a/internal/processor/float-parse.go b/internal/processor/float-parse.go index d2b99c8..c2e71d0 100644 --- a/internal/processor/float-parse.go +++ b/internal/processor/float-parse.go @@ -9,7 +9,8 @@ import ( ) type FloatParse struct { - config config.ProcessorConfig + BitSize int + config config.ProcessorConfig } func (fp *FloatParse) Process(ctx context.Context, payload any) (any, error) { @@ -19,8 +20,7 @@ func (fp *FloatParse) Process(ctx context.Context, payload any) (any, error) { return nil, errors.New("float.parse processor only accepts a string") } - // TODO(jwetzell): make bitSize configurable - payloadFloat, err := strconv.ParseFloat(payloadString, 64) + payloadFloat, err := strconv.ParseFloat(payloadString, fp.BitSize) if err != nil { return nil, err } @@ -35,7 +35,19 @@ func init() { RegisterProcessor(ProcessorRegistration{ Type: "float.parse", New: func(config config.ProcessorConfig) (Processor, error) { - return &FloatParse{config: config}, nil + params := config.Params + bitSizeNum := 64 + bitSize, ok := params["bitSize"] + if ok { + bitSizeFloat, ok := bitSize.(float64) + + if !ok { + return nil, errors.New("float.parse bitSize must be a number") + } + + bitSizeNum = int(bitSizeFloat) + } + return &FloatParse{config: config, BitSize: bitSizeNum}, nil }, }) } diff --git a/internal/processor/int-parse.go b/internal/processor/int-parse.go index eb4ae45..47e3549 100644 --- a/internal/processor/int-parse.go +++ b/internal/processor/int-parse.go @@ -9,7 +9,9 @@ import ( ) type IntParse struct { - config config.ProcessorConfig + Base int + BitSize int + config config.ProcessorConfig } func (ip *IntParse) Process(ctx context.Context, payload any) (any, error) { @@ -19,8 +21,7 @@ func (ip *IntParse) Process(ctx context.Context, payload any) (any, error) { return nil, errors.New("int.parse processor only accepts a string") } - // TODO(jwetzell): make base and bitSize configurable - payloadInt, err := strconv.ParseInt(payloadString, 10, 64) + payloadInt, err := strconv.ParseInt(payloadString, ip.Base, ip.BitSize) if err != nil { return nil, err } @@ -35,7 +36,32 @@ func init() { RegisterProcessor(ProcessorRegistration{ Type: "int.parse", New: func(config config.ProcessorConfig) (Processor, error) { - return &IntParse{config: config}, nil + params := config.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 = 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 = int(bitSizeFloat) + } + return &IntParse{config: config, Base: baseNum, BitSize: bitSizeNum}, nil }, }) } diff --git a/internal/processor/uint-parse.go b/internal/processor/uint-parse.go index 2348a47..c4ee872 100644 --- a/internal/processor/uint-parse.go +++ b/internal/processor/uint-parse.go @@ -9,7 +9,9 @@ import ( ) type UintParse struct { - config config.ProcessorConfig + Base int + BitSize int + config config.ProcessorConfig } func (up *UintParse) Process(ctx context.Context, payload any) (any, error) { @@ -19,8 +21,7 @@ func (up *UintParse) Process(ctx context.Context, payload any) (any, error) { return nil, errors.New("uint.parse processor only accepts a string") } - // TODO(jwetzell): make base and bitSize configurable - payloadUint, err := strconv.ParseUint(payloadString, 10, 64) + payloadUint, err := strconv.ParseUint(payloadString, up.Base, up.BitSize) if err != nil { return nil, err } @@ -35,7 +36,31 @@ func init() { RegisterProcessor(ProcessorRegistration{ Type: "uint.parse", New: func(config config.ProcessorConfig) (Processor, error) { - return &UintParse{config: config}, nil + params := config.Params + baseNum := 10 + base, ok := params["base"] + if ok { + baseFloat, ok := base.(float64) + + if !ok { + return nil, errors.New("uint.parse base must be a number") + } + + baseNum = int(baseFloat) + } + + bitSizeNum := 64 + bitSize, ok := params["bitSize"] + if ok { + bitSizeFloat, ok := bitSize.(float64) + + if !ok { + return nil, errors.New("uint.parse bitSize must be a number") + } + + bitSizeNum = int(bitSizeFloat) + } + return &UintParse{config: config, Base: baseNum, BitSize: bitSizeNum}, nil }, }) }