codec

package
v0.0.1-alpha.9 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package codec defines the wire-protocol abstraction used by Overcast's typed operation dispatcher.

A Codec is a stateless (de)serialiser for one AWS wire protocol. It does not know the operation, the service, or any business logic — it only turns request bodies into typed input structs and typed output structs (or AWSErrors) into response bytes.

This package is part of Phase 0 of the Smithy-aligned wire-protocol plan. See docs/plans/smithy.md.

IMPORTANT: codecs in this package wrap the existing protocol.WriteJSON / protocol.WriteXML / protocol.WriteQueryXML helpers. They MUST produce byte-identical responses to the legacy code paths so that migrating a service to the typed dispatcher is a no-op on the wire.

Index

Constants

View Source
const (
	NameAWSJSON10 = "aws.protocols#awsJson1_0"
	NameAWSJSON11 = "aws.protocols#awsJson1_1"
	NameAWSQuery  = "aws.protocols#awsQuery"
	NameRESTXML   = "aws.protocols#restXml"
	NameRPCv2CBOR = "smithy.protocols#rpcv2Cbor"
)

Smithy protocol shape IDs. These are the canonical names used by the Smithy 2.0 spec and are stable identifiers we use in logs, telemetry, and for the Smithy-Protocol response header (rpcv2Cbor only).

Source:

Variables

This section is empty.

Functions

func Supports

func Supports(supported []Codec, c Codec) bool

Supports reports whether c is in supported, treating JSON 1.0 and 1.1 as equivalent (the emulator accepts either for any JSON-tier service — see docs/smithy.md). Every service's Dispatch method should use this instead of writing its own supportsCodec loop.

func WithDispatch

func WithDispatch(ctx context.Context, c Codec, op string) context.Context

WithDispatch returns a derived context carrying the picked codec and operation name. Used by the protocol-detection middleware.

Types

type Codec

type Codec interface {
	// Name returns the Smithy protocol shape ID, e.g. NameAWSJSON10.
	// Used for diagnostics, logging, and the Smithy-Protocol response
	// header (rpcv2Cbor only).
	Name() string

	// Decode reads r.Body into into. into MUST be a non-nil pointer to a
	// typed input struct. Returns a *protocol.AWSError populated with a
	// 4xx code on malformed input, or nil on success.
	//
	// Decode is responsible for draining and closing r.Body so the
	// HTTP/1.1 connection can be reused. Callers MUST NOT touch r.Body
	// after calling Decode.
	Decode(r *http.Request, into any) *protocol.AWSError

	// WriteResponse serialises v as a successful response body using the
	// codec's native format and writes it to w with status. v may be nil
	// or a typed pointer; nil is rendered as the codec's empty-response
	// representation (e.g. "{}" for JSON).
	WriteResponse(w http.ResponseWriter, r *http.Request, status int, v any)

	// WriteError serialises aerr in the codec's native error envelope.
	// The HTTP status comes from aerr.HTTPStatus.
	WriteError(w http.ResponseWriter, r *http.Request, aerr *protocol.AWSError)
}

Codec serialises requests and responses for one AWS wire protocol.

Implementations MUST be stateless and safe for concurrent use. They never inspect the operation name, never know which service they're serving, and never invoke business logic.

Methods on Codec are intentionally narrow:

  • Decode reads the request body into a pointer-to-struct.
  • WriteResponse serialises a successful response.
  • WriteError serialises an AWSError in the codec's native envelope.

All concrete codecs in Phase 0 are thin wrappers over the existing helpers in package protocol; the wire bytes are byte-identical.

var JSON10 Codec = json10{}

JSON10 is the singleton AWS JSON 1.0 codec.

var JSON11 Codec = json11{}

JSON11 is the singleton AWS JSON 1.1 codec.

var QueryXML Codec = queryXML{}

QueryXML is the singleton AWS Query codec.

var RESTXML Codec = restXML{}

RESTXML is the singleton AWS REST-XML codec.

var RPCv2CBOR Codec = rpcv2CBOR{}

func FromContext

func FromContext(ctx context.Context) (Codec, string)

FromContext returns the codec and operation name stashed by the protocol-detection middleware, or (nil, "") if no codec was identified for this request (e.g. legacy services, or protocol dispatch middleware).

type Identifier

type Identifier interface {
	// Claim returns (codec, operationName, true) on a match, or
	// (nil, "", false) if the request does not match this protocol.
	Claim(r *http.Request) (Codec, string, bool)
}

Identifier inspects a request and, if it matches the protocol's identification rules, returns the matching Codec and the AWS operation name encoded in the request.

Identifiers MUST NOT consume the request body — only headers, method, path, and (for query-protocol form bodies) the parsed form. Body consumption belongs to the codec's Decode pass.

The order in which identifiers are tried matters; see the Smithy AWS service protocol precision rules: https://smithy.io/2.0/guides/wire-protocol-selection.html#aws-service-protocol-precision

func DefaultIdentifiers

func DefaultIdentifiers() []Identifier

DefaultIdentifiers returns the built-in identifiers in precision order. CBOR is intentionally omitted in Phase 1 (introduced in Phase 4 with the cbor codec).

REST-XML has no reliable header/path-only identification heuristic — services using it (S3, CloudFront) are routed by their own router today and stay on the bespoke path per docs/plans/smithy.md §10. It is therefore not in this list.

Jump to

Keyboard shortcuts

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