mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
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)
|
s := reflect.ValueOf(payload)
|
||||||
|
|
||||||
if s.Kind() != reflect.Struct {
|
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)
|
field := s.FieldByName(sf.Name)
|
||||||
|
|||||||
@@ -18,7 +18,11 @@ func (sm *StructMethodGet) Process(ctx context.Context, payload any) (any, error
|
|||||||
s := reflect.ValueOf(payload)
|
s := reflect.ValueOf(payload)
|
||||||
|
|
||||||
if s.Kind() != reflect.Struct {
|
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)
|
method := s.MethodByName(sm.Name)
|
||||||
|
|||||||
@@ -72,6 +72,12 @@ func TestGoodStructFieldGet(t *testing.T) {
|
|||||||
payload: TestStruct{Bool: true},
|
payload: TestStruct{Bool: true},
|
||||||
expected: true,
|
expected: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "pointer to struct payload",
|
||||||
|
params: map[string]any{"name": "Data"},
|
||||||
|
payload: &TestStruct{Data: "hello"},
|
||||||
|
expected: "hello",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|||||||
@@ -89,6 +89,14 @@ func TestGoodStructMethodGet(t *testing.T) {
|
|||||||
payload: TestStruct{},
|
payload: TestStruct{},
|
||||||
expected: nil,
|
expected: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "pointer to struct payload",
|
||||||
|
params: map[string]any{
|
||||||
|
"name": "GetData",
|
||||||
|
},
|
||||||
|
payload: &TestStruct{Data: "hello"},
|
||||||
|
expected: "hello",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|||||||
Reference in New Issue
Block a user