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)
|
||||
}
|
||||
|
||||
|
||||
39
internal/normalizers/common/json_test.go
Normal file
39
internal/normalizers/common/json_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
)
|
||||
|
||||
func TestDecodeJSONPayloadAcceptsTypedPayload(t *testing.T) {
|
||||
type rawPayload struct {
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
got, err := DecodeJSONPayload[rawPayload](event.Event{
|
||||
Payload: rawPayload{Value: "ok"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeJSONPayload() error = %v", err)
|
||||
}
|
||||
if got.Value != "ok" {
|
||||
t.Fatalf("Value = %q, want ok", got.Value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeJSONPayloadAcceptsTypedPointerPayload(t *testing.T) {
|
||||
type rawPayload struct {
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
got, err := DecodeJSONPayload[rawPayload](event.Event{
|
||||
Payload: &rawPayload{Value: "ok"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeJSONPayload() error = %v", err)
|
||||
}
|
||||
if got.Value != "ok" {
|
||||
t.Fatalf("Value = %q, want ok", got.Value)
|
||||
}
|
||||
}
|
||||
@@ -107,6 +107,21 @@ func TestConvectiveOutlookNormalizerProducesCanonicalSchemaAndMapsSample(t *test
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvectiveOutlookNormalizerAcceptsTypedSourcePayload(t *testing.T) {
|
||||
bundle := spcBundle(t, 38.5, -90.5)
|
||||
in := spcRawEvent(t, bundle)
|
||||
in.Payload = bundle
|
||||
|
||||
out, err := (ConvectiveOutlookNormalizer{}).Normalize(nil, in)
|
||||
if err != nil {
|
||||
t.Fatalf("Normalize() error = %v", err)
|
||||
}
|
||||
run := out.Payload.(model.WeatherOutlookRun)
|
||||
if len(run.Outlooks) != 9 {
|
||||
t.Fatalf("Outlooks length = %d, want 9", len(run.Outlooks))
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvectiveOutlookNormalizerOrdersProductsByDayAndType(t *testing.T) {
|
||||
bundle := spcBundle(t, 0, 0)
|
||||
for i, j := 0, len(bundle.Products)-1; i < j; i, j = i+1, j-1 {
|
||||
|
||||
Reference in New Issue
Block a user