fix GetByteSlice for params

This commit is contained in:
Joel Wetzell
2026-03-02 14:00:47 -06:00
parent 925873a124
commit df4f0f745d

View File

@@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math" "math"
"reflect"
) )
type Config struct { type Config struct {
@@ -131,14 +132,20 @@ func (p Params) GetByteSlice(key string) ([]byte, error) {
return nil, ErrParamNotFound return nil, ErrParamNotFound
} }
byteSlice, ok := value.([]any) v := reflect.ValueOf(value)
if !ok { if v.Kind() != reflect.Slice {
return nil, ErrParamNotSlice return nil, ErrParamNotSlice
} }
result := make([]byte, len(byteSlice)) result := make([]byte, v.Len())
for i, v := range byteSlice { for i := 0; i < v.Len(); i++ {
uintValue, ok := v.(uint) elem := v.Index(i).Interface()
byteValue, ok := elem.(byte)
if ok {
result[i] = byteValue
continue
}
uintValue, ok := elem.(uint)
if ok { if ok {
if uintValue > 255 { if uintValue > 255 {
return nil, fmt.Errorf("element at index %d is out of byte range", i) return nil, fmt.Errorf("element at index %d is out of byte range", i)
@@ -146,7 +153,7 @@ func (p Params) GetByteSlice(key string) ([]byte, error) {
result[i] = byte(uintValue) result[i] = byte(uintValue)
continue continue
} }
intValue, ok := v.(int) intValue, ok := elem.(int)
if ok { if ok {
if intValue < 0 || intValue > 255 { if intValue < 0 || intValue > 255 {
return nil, fmt.Errorf("element at index %d is out of byte range", i) return nil, fmt.Errorf("element at index %d is out of byte range", i)
@@ -154,7 +161,7 @@ func (p Params) GetByteSlice(key string) ([]byte, error) {
result[i] = byte(intValue) result[i] = byte(intValue)
continue continue
} }
floatValue, ok := v.(float64) floatValue, ok := elem.(float64)
if ok { if ok {
if floatValue != math.Floor(floatValue) { if floatValue != math.Floor(floatValue) {
return nil, fmt.Errorf("element at index %d is not an integer", i) return nil, fmt.Errorf("element at index %d is not an integer", i)