mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
45 lines
823 B
Go
45 lines
823 B
Go
package processor
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"github.com/jwetzell/showbridge-go/internal/config"
|
|
)
|
|
|
|
type JsonDecode struct {
|
|
config config.ProcessorConfig
|
|
}
|
|
|
|
func (jd *JsonDecode) Process(ctx context.Context, payload any) (any, error) {
|
|
payloadString, ok := GetAnyAs[string](payload)
|
|
|
|
if !ok {
|
|
return nil, errors.New("json.decode processor only accepts a string")
|
|
}
|
|
|
|
payloadJson := make(map[string]any)
|
|
|
|
err := json.Unmarshal([]byte(payloadString), &payloadJson)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return payloadJson, nil
|
|
|
|
}
|
|
|
|
func (jd *JsonDecode) Type() string {
|
|
return jd.config.Type
|
|
}
|
|
|
|
func init() {
|
|
RegisterProcessor(ProcessorRegistration{
|
|
Type: "json.decode",
|
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
|
return &JsonDecode{config: config}, nil
|
|
},
|
|
})
|
|
}
|