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 ¶
const ( NameAWSJSON10 = "aws.protocols#awsJson1_0" NameAWSJSON11 = "aws.protocols#awsJson1_1" NameAWSQuery = "aws.protocols#awsQuery" NameRESTXML = "aws.protocols#restXml" NameRPCv2CBOR = "smithy.protocols#rpcv2Cbor" NameRPCv2JSON = "smithy.protocols#rpcv2Json" )
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 (Smithy RPC v2 protocols only).
Source:
- https://smithy.io/2.0/aws/protocols/aws-json-1_0-protocol.html
- https://smithy.io/2.0/aws/protocols/aws-json-1_1-protocol.html
- https://smithy.io/2.0/aws/protocols/aws-query-protocol.html
- https://smithy.io/2.0/aws/protocols/aws-restxml-protocol.html
- https://smithy.io/2.0/additional-specs/protocols/smithy-rpc-v2.html
- https://smithy.io/2.0/additional-specs/protocols/smithy-rpc-v2-json.html
Variables ¶
This section is empty.
Functions ¶
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 (Smithy RPC v2 protocols 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{}
var RPCv2JSON Codec = rpcv2JSON{}
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 generally MUST NOT consume the request body — only headers, method, path, and (for query-protocol form bodies) the URL query string. Body consumption belongs to the codec's Decode pass.
identifyQuery is the sole, documented exception: the AWS Query protocol encodes Action into the POST body for the overwhelming majority of real SDK traffic, so resolving an operation name at all requires reading the body. It reads it through protocol.ParseFormPreservingBody, which caches the fields on r.Form — every later read of Action/other fields, whether by the router's own QueryDispatcher resolution, a legacy handler's r.FormValue calls, or the typed codec's Decode pass, reuses that cached r.Form — and puts the bytes back on r.Body so a request that only *looked* like Query traffic still reaches its own handler with its payload. Identifiers run ahead of routing, so "this content type means Query" is a guess, and a wrong guess must not cost the request its body. See docs/plans/level2-codegen.md Track 1.1.
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. Explicit Smithy RPC protocol markers take precedence over the AWS JSON and Query heuristics.
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.