mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add processor to pull field out of struct
This commit is contained in:
49
internal/processor/struct-field-get.go
Normal file
49
internal/processor/struct-field-get.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type StructFieldGet struct {
|
||||
config config.ProcessorConfig
|
||||
Name string
|
||||
}
|
||||
|
||||
func (sf *StructFieldGet) Process(ctx context.Context, payload any) (any, error) {
|
||||
s := reflect.ValueOf(payload)
|
||||
|
||||
if s.Kind() != reflect.Struct {
|
||||
return nil, errors.New("struct.field.get processor only accepts a struct payload")
|
||||
}
|
||||
|
||||
field := s.FieldByName(sf.Name)
|
||||
if !field.IsValid() {
|
||||
return nil, fmt.Errorf("struct.field.get field '%s' does not exist", sf.Name)
|
||||
}
|
||||
|
||||
return field.Interface(), nil
|
||||
}
|
||||
|
||||
func (sf *StructFieldGet) Type() string {
|
||||
return sf.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "struct.field.get",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
nameString, err := params.GetString("name")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("struct.field.get name error: %w", err)
|
||||
}
|
||||
|
||||
return &StructFieldGet{config: config, Name: nameString}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user