buscodec

package
v0.0.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package buscodec is the runtime's canonical message-bus codec (ADR-0036). Every thestack broker MUST encode bus payloads through this package and MUST NOT import encoding/json or call cbor.Marshal/Unmarshal directly. The default codec is CBOR (fxamacker/cbor with canonical encoding); a CodecI seam allows swapping for replay or debug builds.

Field tags: the default CBOR codec (fxamacker/cbor) honours `cbor:` tags and otherwise uses the Go field name — it does NOT read `json:` tags. Encode/Decode are symmetric through a single codec, so this is internally consistent; the `json:` tags several DTOs carry only take effect under the opt-in jsonCodec (NewJSON, for human-readable replay) and do not change the CBOR wire. Do not assume a `json:`-named key appears on the CBOR wire.

Versioning: this package does NOT impose envelope versioning on the wire. Per-message versioning is a payload concern — brokers that need it (e.g. chlocalbroker) carry a uint8 V field on the struct.

Index

Constants

This section is empty.

Variables

View Source
var PackageProps = packageprops.Props{
	WASMWASI:         packageprops.WASMCompiles,
	WASMJS:           packageprops.WASMCompiles,
	WASMFreestanding: packageprops.WASMCompiles,
}

PackageProps records this package's curated properties (ADR-0080). Seeded by `boxer code analysis golang wasmsurvey props generate`; curate by hand. The same group's `props verify` reconciles it.

Functions

func Decode

func Decode[T any](b []byte) (v T, err error)

Decode is the generic inverse of Encode.

func Encode

func Encode[T any](v T) (b []byte, err error)

Encode is the generic call-site helper. Routes through the per-type registry if T is registered, else through Default(). Centralises the eh.Errorf wrap so brokers don't repeat it per payload type.

func Register

func Register[T any](codec CodecI)

Register installs a per-type codec for T. Subsequent Encode[T] / Decode[T] / Reply[T] calls route through `codec` instead of Default(). Passing nil unregisters T (so the next call falls back to Default()).

Goroutine-safe; intended for package-init or test setup. Re-registering overwrites the previous codec without warning.

func Reply

func Reply[T any](pub PublishFunc, subject string, v T) (err error)

Reply folds the broker-side "encode v and publish on subject" pattern — replaces the per-broker replyJSON helpers.

func SetDefault

func SetDefault(c CodecI)

SetDefault swaps the process-wide codec. Intended for init-time use by tests, debug builds, or capture-replay tools. Reads after the call observe the new codec; concurrent SetDefault calls race with each other but not with Default — Default is always safe.

Types

type CodecI

type CodecI interface {
	// Name returns a short stable identifier (e.g. "cbor", "json"). Used
	// for diagnostics and content-type emission.
	Name() (n string)
	// ContentType returns the MIME type for the wire format.
	ContentType() (ct string)
	// Encode serialises v into a freshly-allocated slice.
	Encode(v any) (b []byte, err error)
	// Decode populates v from b. v MUST be a non-nil pointer.
	Decode(b []byte, v any) (err error)
}

CodecI is the wire-format contract. Implementations must be goroutine- safe. Encode may pool buffers internally but the returned slice is caller-owned: callers may retain it past the call.

func Default

func Default() (c CodecI)

Default returns the process-wide default codec.

func Lookup

func Lookup[T any]() (c CodecI)

Lookup returns the codec routed to T — the per-type registration if one exists, else Default(). Exposed so tests + diagnostics can observe the dispatch without going through Encode/Decode.

func NewCBOR

func NewCBOR() (c CodecI)

NewCBOR constructs the default CBOR codec.

func NewJSON

func NewJSON() (c CodecI)

NewJSON constructs the JSON fallback codec.

type PublishFunc

type PublishFunc func(subject string, payload []byte) (err error)

PublishFunc matches inprocbus.Client.Publish so brokers can pass their client's Publish into Reply without an adapter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL