Files
warmbly/internal/infrastructure/codec/codec.go
T
Matthew Meszaros 4080258606 infra(codec): transport-agnostic Codec (Avro + JSON)
Codec interface (Serialize / Deserialize / Name) lets payload encoding
decouple from the transport choice. Two implementations:

  AvroCodec - wraps the existing kafka.Avrov2 Schema Registry client
              via NewAvroFromClient. Preserves identical wire format
              for production deployments already on Kafka + SR.
  JSONCodec - encoding/json based. No external dependency, suitable
              for self-hosters who don't want a Schema Registry.

Factory FromEnv selects via CODEC_PROVIDER (default avro).

Once codec.Codec is in the worker boot and publisher, self-hosters can
pick EVENTBUS_PROVIDER=nats CODEC_PROVIDER=json for a Schema-Registry-
free deployment.

11 tests cover JSON round-trip, nil guards, factory paths, and Avro
interface conformance.
2026-05-27 14:42:47 +00:00

45 lines
1.8 KiB
Go

// Package codec is the abstraction over event-payload serialization.
//
// Two implementations:
// - AvroCodec (Confluent Schema Registry + Avro v2 — historical default,
// wraps internal/infrastructure/kafka.Avrov2)
// - JSONCodec (encoding/json — self-hostable alternative with no external
// schema registry dependency)
//
// The interface is intentionally small so the EventBus abstraction in
// internal/infrastructure/eventbus can stay transport-agnostic: the bus
// carries opaque bytes, the codec decides how those bytes are framed.
//
// Switching codecs requires that publishers and consumers agree: a JSONCodec
// consumer cannot decode an Avro-framed message and vice versa. There is no
// in-band marker, this is an operator-level configuration choice.
package codec
import "context"
// Codec serializes and deserializes event payloads. Implementations may
// require external services (Schema Registry for Avro) or be standalone
// (JSON). Implementations must be safe for concurrent use.
type Codec interface {
// Serialize encodes value into a transport-ready byte slice. topic is
// passed through because some codecs (Avro with subject naming strategy)
// need it to look up the right schema; codecs that don't care may ignore
// it.
Serialize(ctx context.Context, topic string, value any) ([]byte, error)
// Deserialize decodes payload into target, which must be a non-nil
// pointer to a value of the expected type. Same topic contract as
// Serialize.
Deserialize(ctx context.Context, topic string, payload []byte, target any) error
// Name returns a short identifier ("avro", "json") used in admin UI,
// startup logs, and audit trails.
Name() string
}
// Compile-time interface checks.
var (
_ Codec = (*AvroCodec)(nil)
_ Codec = (*JSONCodec)(nil)
)