Documentation
¶
Overview ¶
Package protoyaml renders and parses protobuf messages as YAML using the canonical protojson mapping.
Definition ¶
protobuf's JSON representation is defined by protojson. This package renders and parses that representation in YAML syntax, using goccy/go-yaml as the YAML engine.
By default the output is the canonical protojson mapping. protojson-sanctioned variants are opt-in through WithProtoJSON: passing a protojson.MarshalOptions value enables the knobs protojson itself defines (UseProtoNames, UseEnumNumbers, EmitUnpopulated, a custom type Resolver, and so on). There is no non-protojson (reflection-based) mode: every value this package emits or accepts is a protojson representation written in YAML rather than JSON syntax.
This is unrelated to github.com/bufbuild/protoyaml-go, which uses a different YAML engine and pursues different design goals.
Marshal pipeline ¶
Marshal drives protojson for semantics and goccy/go-yaml for syntax:
- protojson.Marshal(m) produces canonical JSON.
- The JSON is decoded with goccy/go-yaml using UseOrderedMap. Because JSON is valid YAML flow syntax, this yields an ordered value tree that preserves protojson's key order (field-number order, map keys sorted by protojson).
- The ordered value is rendered as YAML, optionally with flow-style leaf collections (see WithFlowLeafCollections).
Unmarshal pipeline ¶
Unmarshal converts YAML to JSON with YAMLToJSON and then decodes with protojson using DiscardUnknown, so unknown keys are ignored rather than rejected. UnmarshalJSON exposes the JSON-side decode directly.
Compatibility ¶
The exact output bytes are part of the compatibility surface: a change to the rendered bytes is a breaking change. The semantics are inherited from protojson, so protobuf's JSON mapping rules (enum names, int64 as string, well-known type encodings, and so on) apply unchanged.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal renders m as YAML using the protojson mapping. By default that is the canonical protojson mapping; WithProtoJSON opts into the protojson-sanctioned variants (UseProtoNames, UseEnumNumbers, EmitUnpopulated, a custom Resolver).
The pipeline is: protojson.Marshal(m) produces the JSON, that JSON is parsed by goccy/go-yaml with UseOrderedMap so protojson's key order is preserved (JSON is valid YAML flow syntax), and the ordered value is rendered back out as YAML. There is intentionally no non-protojson (reflection) path.
func Unmarshal ¶
Unmarshal parses YAML into m using the canonical protojson mapping.
It converts YAML to JSON (see YAMLToJSON) and then decodes with protojson using DiscardUnknown, so unknown fields are ignored rather than rejected.
func UnmarshalJSON ¶
UnmarshalJSON decodes canonical protojson bytes into m with DiscardUnknown.
func YAMLToJSON ¶
YAMLToJSON converts YAML bytes into JSON bytes suitable for protojson.Unmarshal. It decodes the YAML into a generic value with goccy/go-yaml (which normalizes mapping keys to strings) and re-encodes it with encoding/json. This mirrors the reference implementation in github.com/apstndb/spannerplan.
Types ¶
type Option ¶
type Option func(*config)
Option configures Marshal. Options are applied in order.
func WithFlowLeafCollections ¶
func WithFlowLeafCollections() Option
WithFlowLeafCollections renders every mapping whose values are all scalars in YAML flow style (for example {k: v, k2: v2}) instead of block style. Sequences always stay in block style, non-leaf mappings (those that contain a nested mapping or sequence) always stay in block style, and the document root mapping always stays in block style so the output reads as a YAML document even when the whole message is scalar-only. The result is more compact for deeply nested leaf records while keeping the outer structure readable.
func WithProtoJSON ¶
func WithProtoJSON(o protojson.MarshalOptions) Option
WithProtoJSON sets the protojson.MarshalOptions used for the protojson stage of Marshal. The zero value (the canonical protojson mapping) is used by default; passing a non-zero value opts into the protojson-sanctioned variants protojson itself defines (EmitUnpopulated, UseProtoNames, UseEnumNumbers, a custom type Resolver, and so on). Marshal always drives protojson for its semantics; this option only selects which protojson marshal options apply.
This configures the marshal side only. The unmarshal side uses a fixed protojson.UnmarshalOptions{DiscardUnknown: true} with the default (global) type resolver; there is currently no option to supply a custom resolver to Unmarshal (see UnmarshalJSON).
func WithYAMLOptions ¶
func WithYAMLOptions(opts ...yaml.EncodeOption) Option
WithYAMLOptions passes goccy/go-yaml encode options through to the YAML rendering stage. Use it for stylistic control such as yaml.Indent, yaml.IndentSequence, or yaml.UseLiteralStyleIfMultiline. These options only affect the lexical shape of the output; they never change its semantics.