Documentation
¶
Overview ¶
Package rest-nested-binary demonstrates two extensions to the merge-field "one struct, one call" pattern that are easy to assume DON'T work without checking:
- Non-JSON body formats — Gob here — compose with the merge-field convenience exactly like JSON/YAML/TOML, because body decode/encode is completely orthogonal to var-merge (codex.DecodeVars/EncodeVars only ever touch a map[string]string, never body bytes).
- Nested struct composition — Req/Resp built from sub-structs (Meta for header/query fields, Payload for the body) instead of flat top-level fields — works out of the box, because merge-field get/set are plain Go closures, not reflection over T's direct fields.
A subtlety worth calling out explicitly: format.Gob(codec) — the convenience constructor — serialises the WHOLE typed value via encoding/gob's own reflection, bypassing the codec's Encode/Decode entirely for the wire bytes (the codec is only used for Validate). That means format.Gob(reqCodec) on a NESTED UploadReq would gob-encode ID and Meta too, not just Payload — harmless (DecodeMerged always merges path/header/query AFTER body decode, so the authoritative HTTP values win regardless), but wasteful. When you want the wire bytes to represent ONLY the nested Payload sub-field, use format.NewTyped with a custom marshal/unmarshal that projects onto/from that sub-field manually — that is exactly what this example does.
Run with: go run ./examples/rest-nested-binary