allow json.decode to take byte slice and string

This commit is contained in:
Joel Wetzell
2026-03-08 13:09:22 -05:00
parent 9859aa8fd4
commit 9502201261
3 changed files with 59 additions and 6 deletions

View File

@@ -3,6 +3,8 @@ package processor
import (
"context"
"fmt"
"math"
"reflect"
"sync"
"github.com/jwetzell/showbridge-go/internal/common"
@@ -47,6 +49,52 @@ func GetAnyAs[T any](p any) (T, bool) {
return typed, ok
}
func GetAnyAsByteSlice(p any) ([]byte, bool) {
v := reflect.ValueOf(p)
if v.Kind() != reflect.Slice {
return nil, false
}
result := make([]byte, v.Len())
for i := 0; i < v.Len(); i++ {
elem := v.Index(i).Interface()
byteValue, ok := elem.(byte)
if ok {
result[i] = byteValue
continue
}
uintValue, ok := elem.(uint)
if ok {
if uintValue > 255 {
return nil, false
}
result[i] = byte(uintValue)
continue
}
intValue, ok := elem.(int)
if ok {
if intValue < 0 || intValue > 255 {
return nil, false
}
result[i] = byte(intValue)
continue
}
floatValue, ok := elem.(float64)
if ok {
if floatValue != math.Floor(floatValue) {
return nil, false
}
if floatValue < 0 || floatValue > 255 {
return nil, false
}
result[i] = byte(floatValue)
continue
}
return nil, false
}
return result, true
}
type TemplateData struct {
Payload any
Modules any