Documentation
¶
Overview ¶
Package configtoml reads TOML configuration through the config codec seam.
It is a sibling of the config core, not part of it: a consumer who needs TOML takes this module, and one who does not pays nothing for it. The core reads and writes only YAML; every other file format is a module like this one.
config.NewStore(ctx, config.WithFiles(fsys, "/etc/app.yaml"), // YAML, the default config.WithBackend(configtoml.New(fsys, "/etc/app.toml")), )
A TOML layer takes part in precedence, per-key merge, provenance and write routing exactly as a YAML file does.
Writing preserves the file ¶
Re-serialising a decoded document destroys it — comments gone, sections alphabetised, indentation injected — the merged-view write the whole toolkit exists to prevent. So writes edit the document in place: reading is go-toml/v2's, and editing is a targeted byte replacement located by go-toml/v2's own source ranges, so an edit changes one value or adds one key and leaves comments, table headers and key order exactly as they were.
Setting or removing a key, and adding a key to an existing section or the top level, are supported. Writing into a sub-path of an inline-table value, or adding a key to an empty section, is refused with config.ErrInvalidTarget rather than guessed.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Codec ¶ added in v0.3.0
type Codec struct{}
Codec reads a TOML document and edits it in place. New uses it, and it is exported so a backend adapter can reuse it as a value decoder — a Consul or etcd key whose value is a TOML document, decoded into a subtree via that adapter's WithValueCodec option. It is a config.Codec (and config.EditingCodec).
Reading (Decode) is go-toml/v2's; editing (Check, Apply, Empty, in write.go) is a targeted byte replacement located by go-toml/v2's source ranges. The two never cross: decoded values feed the read side, and edits are applied to bytes, and the seam re-decodes what was written.
A TOML document's top level is always a table, so — unlike JSON — there is no non-object shape to refuse.
func (Codec) Apply ¶ added in v0.3.0
Apply folds the edits into the document one at a time, replacing a value's bytes in place or inserting a key, and leaving comments, table headers and key order untouched.
func (Codec) Check ¶ added in v0.3.0
Check reports whether a TOML source can be edited safely. Every edit is a targeted byte replacement over the parsed document, so anything that parses can be edited — Check surfaces the same parse error Decode would, nothing more.
func (Codec) PreservesComments ¶ added in v0.3.2
PreservesComments reports that a TOML edit retains comments and formatting. Editing is a targeted byte replacement over the parsed document — comments, table headers and key order are left exactly as they were — so this is true, and it backs the file backend's PreservesComments capability. It satisfies config.CommentPreservingCodec.