better convert

This commit is contained in:
mats committed 2026-06-29 13:17:43 +02:00
1 parent 2a66754f9e
commit b128608316
7 files changed
+591 -20

No files matched your search

+23
View File
@@ -1,5 +1,7 @@
package projection
import "encoding/json"
// Bus framing contract (shared by producer + consumer).
//
// A projection stream carries two channels distinguished only by the event's
@@ -55,3 +57,24 @@ type Transform[W, T any] func(W) T
// wire value W — the store applies the same Transform to it. Return (_, false)
// for a genuine miss. May be nil.
type Fallback[W any] func(key string) (W, bool)
// JSONDecode is a ready-made Decoder for domains whose wire schema is just a
// JSON array of Update[W] (the generic shape — Key/Value/Deleted). Catalog does
// NOT use this (its wire is a flat Projection for back-compat); identity
// consumers like tracking popularity can, pairing it with JSONEncode on the
// emitter side. Marshal errors propagate so HandleFrame can NACK.
func JSONDecode[W any](payload []byte) ([]Update[W], error) {
var out []Update[W]
if err := json.Unmarshal(payload, &out); err != nil {
return nil, err
}
return out, nil
}
// JSONEncode is the inverse of JSONDecode — the EncodeFunc an Emitter uses when
// the wire schema is a JSON array of Update[W]. A nil/empty slice marshals to a
// payload JSONDecode reads back as zero updates, which is what begin/end frames
// need.
func JSONEncode[W any](items []Update[W]) ([]byte, error) {
return json.Marshal(items)
}