Enhance JSON payload decoding to accept both typed and pointer payloads, and add corresponding tests
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
This commit is contained in:
@@ -18,6 +18,18 @@ import (
|
||||
// Errors include a small amount of operation context ("extract payload", "decode raw payload").
|
||||
// Callers typically wrap these with a provider/kind label.
|
||||
func DecodeJSONPayload[T any](in event.Event) (T, error) {
|
||||
var zero T
|
||||
|
||||
if typed, ok := in.Payload.(T); ok {
|
||||
return typed, nil
|
||||
}
|
||||
if ptr, ok := in.Payload.(*T); ok {
|
||||
if ptr == nil {
|
||||
return zero, fmt.Errorf("extract payload: payload pointer is nil")
|
||||
}
|
||||
return *ptr, nil
|
||||
}
|
||||
|
||||
return fknormalize.DecodeJSONPayload[T](in)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user