From d3244fbe905674d041f584802fa98b4d09957d59 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Mon, 1 Dec 2025 20:31:56 -0600 Subject: [PATCH] add processes to parse strings into various number --- internal/processing/float-parse.go | 39 ++++++++++++++++++++++++++++++ internal/processing/int-parse.go | 39 ++++++++++++++++++++++++++++++ internal/processing/uint-parse.go | 39 ++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 internal/processing/float-parse.go create mode 100644 internal/processing/int-parse.go create mode 100644 internal/processing/uint-parse.go diff --git a/internal/processing/float-parse.go b/internal/processing/float-parse.go new file mode 100644 index 0000000..d325c2e --- /dev/null +++ b/internal/processing/float-parse.go @@ -0,0 +1,39 @@ +package processing + +import ( + "context" + "fmt" + "strconv" +) + +type FloatParse struct { + config ProcessorConfig +} + +func (fp *FloatParse) Process(ctx context.Context, payload any) (any, error) { + payloadString, ok := payload.(string) + + if !ok { + return nil, fmt.Errorf("float.parse processor only accepts a string") + } + + // TODO(jwetzell): make bitSize configurable + payloadFloat, err := strconv.ParseFloat(payloadString, 64) + if err != nil { + return nil, err + } + return payloadFloat, nil +} + +func (fp *FloatParse) Type() string { + return fp.config.Type +} + +func init() { + RegisterProcessor(ProcessorRegistration{ + Type: "float.parse", + New: func(config ProcessorConfig) (Processor, error) { + return &FloatParse{config: config}, nil + }, + }) +} diff --git a/internal/processing/int-parse.go b/internal/processing/int-parse.go new file mode 100644 index 0000000..9702ddb --- /dev/null +++ b/internal/processing/int-parse.go @@ -0,0 +1,39 @@ +package processing + +import ( + "context" + "fmt" + "strconv" +) + +type IntParse struct { + config ProcessorConfig +} + +func (ip *IntParse) Process(ctx context.Context, payload any) (any, error) { + payloadString, ok := payload.(string) + + if !ok { + return nil, fmt.Errorf("int.parse processor only accepts a string") + } + + // TODO(jwetzell): make base and bitSize configurable + payloadInt, err := strconv.ParseInt(payloadString, 10, 64) + if err != nil { + return nil, err + } + return payloadInt, nil +} + +func (ip *IntParse) Type() string { + return ip.config.Type +} + +func init() { + RegisterProcessor(ProcessorRegistration{ + Type: "int.parse", + New: func(config ProcessorConfig) (Processor, error) { + return &IntParse{config: config}, nil + }, + }) +} diff --git a/internal/processing/uint-parse.go b/internal/processing/uint-parse.go new file mode 100644 index 0000000..d5476ff --- /dev/null +++ b/internal/processing/uint-parse.go @@ -0,0 +1,39 @@ +package processing + +import ( + "context" + "fmt" + "strconv" +) + +type UintParse struct { + config ProcessorConfig +} + +func (up *UintParse) Process(ctx context.Context, payload any) (any, error) { + payloadString, ok := payload.(string) + + if !ok { + return nil, fmt.Errorf("uint.parse processor only accepts a string") + } + + // TODO(jwetzell): make base and bitSize configurable + payloadUint, err := strconv.ParseUint(payloadString, 10, 64) + 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(config ProcessorConfig) (Processor, error) { + return &UintParse{config: config}, nil + }, + }) +}