mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
allow json.decode to take byte slice and string
This commit is contained in:
@@ -13,15 +13,20 @@ type JsonDecode struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (jd *JsonDecode) Process(ctx context.Context, payload any) (any, error) {
|
func (jd *JsonDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||||
payloadString, ok := GetAnyAs[string](payload)
|
|
||||||
|
payloadBytes, ok := GetAnyAsByteSlice(payload)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("json.decode processor only accepts a string")
|
payloadString, ok := GetAnyAs[string](payload)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("json.decode can only process a string or []byte")
|
||||||
|
}
|
||||||
|
payloadBytes = []byte(payloadString)
|
||||||
}
|
}
|
||||||
|
|
||||||
payloadJson := make(map[string]any)
|
payloadJson := make(map[string]any)
|
||||||
|
|
||||||
err := json.Unmarshal([]byte(payloadString), &payloadJson)
|
err := json.Unmarshal(payloadBytes, &payloadJson)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package processor
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/common"
|
"github.com/jwetzell/showbridge-go/internal/common"
|
||||||
@@ -47,6 +49,52 @@ func GetAnyAs[T any](p any) (T, bool) {
|
|||||||
return typed, ok
|
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 {
|
type TemplateData struct {
|
||||||
Payload any
|
Payload any
|
||||||
Modules any
|
Modules any
|
||||||
|
|||||||
@@ -99,9 +99,9 @@ func TestBadJsonDecode(t *testing.T) {
|
|||||||
errorString string
|
errorString string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "non-string input",
|
name: "non-string or byte input",
|
||||||
payload: []byte("hello"),
|
payload: 123,
|
||||||
errorString: "json.decode processor only accepts a string",
|
errorString: "json.decode can only process a string or []byte",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid json",
|
name: "invalid json",
|
||||||
|
|||||||
Reference in New Issue
Block a user