mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 12:55:29 +00:00
Merge pull request #86 from jwetzell/feat/struct-get-pointer
allow struct based processors to also operate on pointers to structs
This commit is contained in:
@@ -18,7 +18,11 @@ 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")
|
||||
if s.Kind() == reflect.Pointer && s.Elem().Kind() == reflect.Struct {
|
||||
s = s.Elem()
|
||||
} else {
|
||||
return nil, errors.New("struct.field.get processor only accepts a struct payload")
|
||||
}
|
||||
}
|
||||
|
||||
field := s.FieldByName(sf.Name)
|
||||
|
||||
@@ -18,7 +18,11 @@ func (sm *StructMethodGet) Process(ctx context.Context, payload any) (any, error
|
||||
s := reflect.ValueOf(payload)
|
||||
|
||||
if s.Kind() != reflect.Struct {
|
||||
return nil, errors.New("struct.method.get processor only accepts a struct payload")
|
||||
if s.Kind() == reflect.Pointer && s.Elem().Kind() == reflect.Struct {
|
||||
s = s.Elem()
|
||||
} else {
|
||||
return nil, errors.New("struct.method.get processor only accepts a struct payload")
|
||||
}
|
||||
}
|
||||
|
||||
method := s.MethodByName(sm.Name)
|
||||
|
||||
@@ -72,6 +72,12 @@ func TestGoodStructFieldGet(t *testing.T) {
|
||||
payload: TestStruct{Bool: true},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "pointer to struct payload",
|
||||
params: map[string]any{"name": "Data"},
|
||||
payload: &TestStruct{Data: "hello"},
|
||||
expected: "hello",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
@@ -89,6 +89,14 @@ func TestGoodStructMethodGet(t *testing.T) {
|
||||
payload: TestStruct{},
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "pointer to struct payload",
|
||||
params: map[string]any{
|
||||
"name": "GetData",
|
||||
},
|
||||
payload: &TestStruct{Data: "hello"},
|
||||
expected: "hello",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
Reference in New Issue
Block a user