add more convenience functions for getting slices from params and add tests

This commit is contained in:
Joel Wetzell
2026-03-02 12:03:35 -06:00
parent 7d2a3225a7
commit af39c16f30
2 changed files with 298 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ package config
import (
"errors"
"fmt"
"math"
)
type Config struct {
@@ -85,6 +86,90 @@ func (p Params) GetStringSlice(key string) ([]string, error) {
return stringSlice, nil
}
func (p Params) GetIntSlice(key string) ([]int, error) {
value, ok := p[key]
if !ok {
return nil, ErrParamNotFound
}
interfaceSlice, ok := value.([]any)
if !ok {
return nil, ErrParamNotSlice
}
intSlice := make([]int, len(interfaceSlice))
for i, v := range interfaceSlice {
intValue, ok := v.(int)
if ok {
intSlice[i] = intValue
continue
}
uintValue, ok := v.(uint)
if ok {
intSlice[i] = int(uintValue)
continue
}
floatValue, ok := v.(float64)
if ok {
if floatValue != math.Floor(floatValue) {
return nil, fmt.Errorf("element at index %d is not an integer", i)
}
intSlice[i] = int(floatValue)
continue
}
return nil, fmt.Errorf("element at index %d is not a number", i)
}
return intSlice, nil
}
func (p Params) GetByteSlice(key string) ([]byte, error) {
value, ok := p[key]
if !ok {
return nil, ErrParamNotFound
}
byteSlice, ok := value.([]any)
if !ok {
return nil, ErrParamNotSlice
}
result := make([]byte, len(byteSlice))
for i, v := range byteSlice {
uintValue, ok := v.(uint)
if ok {
if uintValue > 255 {
return nil, fmt.Errorf("element at index %d is out of byte range", i)
}
result[i] = byte(uintValue)
continue
}
intValue, ok := v.(int)
if ok {
if intValue < 0 || intValue > 255 {
return nil, fmt.Errorf("element at index %d is out of byte range", i)
}
result[i] = byte(intValue)
continue
}
floatValue, ok := v.(float64)
if ok {
if floatValue != math.Floor(floatValue) {
return nil, fmt.Errorf("element at index %d is not an integer", i)
}
if floatValue < 0 || floatValue > 255 {
return nil, fmt.Errorf("element at index %d is out of byte range", i)
}
result[i] = byte(floatValue)
continue
}
return nil, fmt.Errorf("element at index %d is not a number", i)
}
return result, nil
}
type ModuleConfig struct {
Id string `json:"id"`
Type string `json:"type"`