Documentation
¶
Overview ¶
Package godotyaml is the reference Go library for reading and writing go.yaml, a centralized config and metadata file for Go projects.
It is the parser/helper layer other tools depend on so they do not each roll their own YAML handling for go.yaml. A go.yaml file has two zones: a closed set of root project metadata (name, version, schema_version, executables, ...) exposed here as typed accessors, and an open external namespace where each tool stores arbitrary config under external.<toolname>. External sections are treated as opaque: the library reads them, hands them back to the caller to decode, and writes a single tool's section back without disturbing the rest of the file.
The yaml.v3 node tree is kept as the internal source of truth so that comments, key ordering, and unknown root keys survive a load/save cycle, and so that updating one tool's section never re-serializes (and never corrupts) the rest of the document.
Non-goals ¶
godotyaml is deliberately small and unopinionated. It does NOT:
- validate the semantic correctness of any value (URLs, SPDX license identifiers, version strings, and so on are returned verbatim);
- enforce any schema on the external namespace;
- refuse to parse files with unknown root keys or unknown schema_version values — both are preserved and surfaced rather than rejected;
- expose helpers specific to any individual tool. No consuming tool is privileged; every tool's config lives at external.<toolname> on equal footing.
It is not a CLI, not a validator, and not a schema enforcer.
Index ¶
- type Author
- type Document
- func (d *Document) Authors() ([]Author, error)
- func (d *Document) DecodeExternalConfig(name string, out any) (bool, error)
- func (d *Document) Description() string
- func (d *Document) Documentation() string
- func (d *Document) Executables() (Executables, error)
- func (d *Document) ExternalConfigNames() []string
- func (d *Document) GetRawExternalConfig(name string) (*yaml.Node, bool)
- func (d *Document) Homepage() string
- func (d *Document) IssueTracker() string
- func (d *Document) License() string
- func (d *Document) Name() string
- func (d *Document) Repository() string
- func (d *Document) Save(path string) error
- func (d *Document) SchemaVersion() (int, error)
- func (d *Document) SetExternalConfig(name string, value any) error
- func (d *Document) Version() string
- func (d *Document) Write(w io.Writer) error
- type Executable
- type Executables
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Author ¶
type Author struct {
Name string `yaml:"name"`
Email string `yaml:"email,omitempty"`
Organization string `yaml:"organization,omitempty"`
URL string `yaml:"url,omitempty"`
}
Author describes a single entry in the root authors field. Only Name is expected to be present and the remaining fields are optional and tools may carry additional information the spec adds later by decoding the raw node directly.
type Document ¶
type Document struct {
Doc *yaml.Node // the document node returned by the decoder
Root *yaml.Node // the root mapping node (doc.Content[0])
}
Document is a parsed go.yaml file.
The underlying yaml.v3 node tree is the source of truth: typed accessors read from it on demand, and mutations edit it in place. Keeping the node tree canonical (rather than unmarshaling into a struct) is what lets the library preserve comments, key ordering, and unknown keys across a load/save cycle.
func Parse ¶
reads a go.yaml document from the given io.Reader
An empty input yields an empty document with a writable root mapping rather than an error, so callers can build a file from scratch.
func (*Document) Authors ¶
Authors returns the root authors field as a list.
The field may appear in the file as a bare string, a single mapping, or a sequence of strings and/or mappings; all forms are normalized to []Author. A bare string becomes an Author with only Name set. It returns nil when the field is absent and an error only if a mapping entry cannot be decoded.
func (*Document) DecodeExternalConfig ¶
decodes external.<name> into out, reporting whether the section exists. It is a thin convenience over GetRawExternalConfig followed by node.Decode.
func (*Document) Description ¶
Description returns the root description field, or "" if absent.
func (*Document) Documentation ¶
Documentation returns the root documentation field (the project's docs URL), or "" if absent.
func (*Document) Executables ¶
func (d *Document) Executables() (Executables, error)
Executables returns the root executables field, or nil if absent. Absence and an empty map are equivalent. It returns an error only if the section does not match the Executables shape.
func (*Document) ExternalConfigNames ¶
returns the config names present under external, in document order.
func (*Document) GetRawExternalConfig ¶
returns the raw yaml.Node for external.<name>, reporting whether the section exists.
The node is returned so the caller can decode it into its own types (node.Decode(&out)) without godotyaml imposing a structure on it. The node is the live tree node; treat it as read-only and use SetExternalConfig to write changes.
func (*Document) Homepage ¶
Homepage returns the root homepage field (the project's website), or "" if absent.
func (*Document) IssueTracker ¶
IssueTracker returns the root issue_tracker field, or "" if absent.
func (*Document) Repository ¶
Repository returns the root repository field, or "" if absent.
func (*Document) Save ¶
writes the document back to the file at path.
The file is rendered fully in memory first so a serialization error cannot leave a truncated file on disk.
func (*Document) SchemaVersion ¶
SchemaVersion returns the root schema_version as an integer.
The schema version is a single incrementing integer (0, 1, 2, ...). There are no minor versions such as 1.1. It returns (0, nil) when the key is absent. If the value is present but not a valid integer it returns a non-nil error: the file still parses (Load/Parse never reject it) and the malformed value is surfaced here rather than silently coerced. Quoted scalars (e.g. "1") are accepted.
func (*Document) SetExternalConfig ¶
writes external.<name>, creating the external section if needed.
value may be any value yaml.v3 can marshal, or an existing *yaml.Node for callers that manage their own subtree (e.g. to preserve their own comments).
This function only touches the target section of `external` and sibling objects and the remainder of the file remains intact and untouched
type Executable ¶
type Executable struct {
Entrypoint string `yaml:"entrypoint"` // directory holding package main, relative to project root
Description string `yaml:"description,omitempty"` // optional human-readable purpose
}
Executable describes one executable entry point the project produces. It is project metadata only as it carries no output paths, OS/arch targets, build flags, or per-executable versions (every executable inherits the single project version). Build-specific concerns belong in a build tool's external.<toolname> section, not here.
type Executables ¶
type Executables map[string]Executable
Executables maps an author-chosen executable name to its definition. Absence of the root key and an empty map are equivalent (both have length 0): the project produces no executables, i.e. it is a library.