Documentation
¶
Overview ¶
Package yaml is a thin wrapper around go.yaml.in/yaml/v3 for consuming the RawYAML bodies that internal/parsers/grammar/ isolates between `---` fences, plus the typed-extensions service the grammar lexer calls for `extensions:` raw blocks.
Importers:
- The builder layer — bridge taggers that decide when to parse a given RawYAML body (operations, meta, schema).
- internal/parsers/grammar — calls TypedExtensions from its extensions raw-block lexer so Extension.Value ships typed.
The grammar import is the one carve-out from the "grammar stays YAML-free" architecture rule.
See README.md for the long-form rationale (typed-extensions pipeline, dedent strategies, sibling-sub-parser seam).
Index ¶
- func Parse(body string) (any, error)
- func ParseInto(body string, dst any) error
- func RemoveIndent(lines []string) []string
- func TypedExtensions(body string) (map[string]any, error)
- func UnmarshalBody(body string, unmarshal func([]byte) error) error
- func UnmarshalListBody(body string, unmarshal func([]byte) error) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
Parse unmarshals the given raw YAML body into a generic value (typically a map[string]any or []any). YAML library errors carry their own line/column numbers relative to the body, not to the Go source — callers that need source-relative positions wrap the error with their own annotation position.
Returns (nil, nil) for an empty body so callers can handle "annotation had a fence but no content" without branching on error-vs-nil.
func ParseInto ¶
ParseInto unmarshals body into the given destination, typically a pointer to a struct the caller defined to match an expected YAML shape (e.g., operation-body or extension-value). Wraps the underlying error for uniform error reporting.
func RemoveIndent ¶
RemoveIndent normalises the common leading indentation of a YAML body lifted from a godoc comment block. It runs in two passes:
- Expand the leading tabs of every (non-blank) line to two spaces. YAML refuses tab indentation, and gofmt renders an indented doc-comment line (a code block) with a leading tab; expanding up front makes tab- and space-indented lines comparable.
- Treat the first non-blank line's indent length as the canonical strip width and strip it from every line.
The first-(non-blank-)line dedent (vs "shortest leading-whitespace run across every non-blank line") is the operations / meta path's contract — the existing operation goldens depend on it. The typed-extensions path uses common-prefix dedent instead; see README.md §dedent.
Why expand BEFORE stripping (not the post-strip retab it replaced): gofmt rewrites a column-0 doc-comment key as a prose line (one leading space) and its indented value block as a code block (a leading tab), separated by a blank "//" line. A swagger:meta body is uniformly tab-prefixed (quirk F7), but a swagger:operation body interleaves 1-space prose keys (`responses:`, `x-*:`) with tab-prefixed value blocks. Stripping the prose-keyed width (1) off a child's lone leading tab would consume the child's whole indent and flatten the nesting; the YAML parser then rejects or mis-nests the body. Expanding first turns each tab into two spaces, so the strip leaves the relative nesting intact for both shapes. Leading blank lines are skipped when choosing the canonical line (gofmt inserts one under a column-0 key).
Whitespace tokens recognised here are space (' '), tab ('\t'), and the leading `/` characters that survive when the lexer hasn't stripped a godoc comment marker yet. Unicode space separators (\p{Zs}) are NOT recognised: real Go source code uses ASCII whitespace. If a corpus surfaces that depends on it, reintroduce the Unicode branch.
func TypedExtensions ¶
TypedExtensions parses the body of an `extensions:` raw block and returns its top-level entries as JSON-typed values (bool / float64 / string / []any / map[string]any).
The body is dedented before parsing — the grammar lexer preserves godoc-level indentation per line, but YAML refuses tab indentation and treats leading whitespace as structural. The YAML → JSON normalisation enforces map[string]any with concrete leaf types via swag/yamlutils.YAMLToJSON; downstream consumers (vendor-extension targets, code generators, AddExtension surfaces) rely on that shape.
No name filtering is applied here: the caller decides whether to accept only x-* keys (via classify.IsAllowedExtension) or to consume the full mapping. Empty body returns (nil, nil).
See README.md §typed-extensions for the full contract.
func UnmarshalBody ¶
UnmarshalBody runs a raw godoc-comment YAML body through the standard godoc → YAML → JSON pipeline expected by every Swagger target that consumes JSON-shape input:
- RemoveIndent — strip the common indent godoc adds to every line and turn leading tabs into two-space sequences (YAML refuses tab indentation).
- yaml.Unmarshal into a generic map[any]any.
- yamlutils.YAMLToJSON — coerce the map[any]any soup into JSON-shaped values with concrete leaf types.
- Hand the resulting JSON bytes to the caller's callback, typically a *spec.<Target>.UnmarshalJSON or a json.Unmarshal into a caller-provided struct.
Empty body returns nil — the caller's target is left untouched.
Used by the operations bridge (swagger:operation YAML body), the meta bridge (securityDefinitions, externalDocs), and any future mapping target that needs the same shape. Sequence-shaped bodies (e.g. meta `Tags:`) use UnmarshalListBody.
func UnmarshalListBody ¶ added in v0.35.0
UnmarshalListBody is the sequence-shaped counterpart to UnmarshalBody: it runs the same godoc → YAML → JSON pipeline but decodes the body as a YAML list ([]any) rather than a mapping. Used by the meta bridge for the top-level `Tags:` block (a list of tag objects). Empty body returns nil.
Types ¶
This section is empty.