Refactor normalizers: dedupe JSON decode + event finalize

Add shared normalizer helpers to centralize payload extraction, JSON decoding,
and event finalization/validation.

Refactor NWS, Open-Meteo, and OpenWeather observation normalizers to use the
shared spine, removing repeated boilerplate while preserving provider-specific
mapping logic.
This commit is contained in:
2026-01-15 10:36:18 -06:00
parent e92577c30e
commit 8968b6bdcd
5 changed files with 118 additions and 90 deletions

View File

@@ -0,0 +1,32 @@
// FILE: ./internal/normalizers/common/finalize.go
package common
import (
"time"
"gitea.maximumdirect.net/ejr/feedkit/event"
)
// Finalize builds the output event envelope by copying the input and applying the
// canonical schema/payload, plus (optionally) EffectiveAt.
//
// Important behavior:
// - ID/Kind/Source/EmittedAt are preserved by copying the input event.
// - EffectiveAt is only overwritten when effectiveAt is non-zero.
// If effectiveAt is zero, any existing in.EffectiveAt is preserved.
func Finalize(in event.Event, outSchema string, outPayload any, effectiveAt time.Time) (*event.Event, error) {
out := in
out.Schema = outSchema
out.Payload = outPayload
if !effectiveAt.IsZero() {
t := effectiveAt.UTC()
out.EffectiveAt = &t
}
if err := out.Validate(); err != nil {
return nil, err
}
return &out, nil
}