Format-preserving TOML editing.
parse/2 keeps the original source text, so a document renders back byte for byte,
including comments, whitespace, key order, and value formatting such as hex integers or
literal strings. Edits only rewrite the value they change; the rest of the document is
left as written.
{:ok, doc} = TomlElixir.Document.parse(File.read!("config.toml"))
{:ok, doc} = TomlElixir.Document.put(doc, ["server", "port"], 9090)
File.write!("config.toml", TomlElixir.Document.to_iodata(doc))Paths list keys from the document root as strings, with integer indexes for array
elements and arrays of tables, such as ["products", 0, "name"].
Where new values go
put/3 places a value that does not exist yet next to related content:
- A key in a table defined by a header, or in the root table, goes after the table's last key/value line, with the same indentation.
- A key in a table defined by dotted keys, such as
a.b = 1, becomes another dotted key after its siblings. - A key in an inline table becomes a new entry, and index
lengthof an array appends an element, keeping the collection's layout. - A new table or array of tables entry gets its own header after the last section of
its nearest existing parent table, or at the end of the document. A table that only
exists through subtables, like
ain[a.b], gets a new[a]header.
New lines are inserted before trailing comments and blank lines, since those usually
introduce what follows them. New text uses the default TomlElixir.encode/2 style.
Current limits
- Replaced values are written in the default encoder style, so a replaced hex integer becomes decimal and a map replacing a non-table value becomes an inline table.
- Arrays that are values, like
ports = [80, 443], are replaced whole rather than element by element.
Every edit re-validates the whole document, so an edit cannot produce invalid TOML. This re-decodes the document on each edit; batch edits on very large documents may be slow.
Summary
Functions
Deletes the value at path.
Same as delete/2, but raises on failure.
Fetches the value at path.
Gets the value at path, or default when it does not exist.
Parses a TOML document, keeping its source formatting.
Same as parse/2, but raises on failure.
Puts value at path.
Same as put/3, but raises on failure.
Renders the document as iodata.
Returns the document's data, as TomlElixir.decode/2 would return it with default
options.
Renders the document as a binary.
Replaces the existing value at path with the result of calling fun with it.
Same as update/3, but raises on failure.
Types
@type path() :: [String.t() | non_neg_integer()]
@opaque t()
Functions
@spec delete(t(), path()) :: {:ok, t()} | {:error, TomlElixir.Document.Error.t()}
Deletes the value at path.
Deleting a key/value line removes that line, including its end-of-line comment. Deleting a table removes its header sections, subtables, and dotted keys; deleting an array of tables entry removes that entry's sections. Comment lines above deleted content are kept, since they may describe more than what was deleted. A path that does not exist leaves the document unchanged.
Same as delete/2, but raises on failure.
Fetches the value at path.
Gets the value at path, or default when it does not exist.
@spec parse(binary(), keyword()) :: {:ok, t()} | {:error, Exception.t()}
Parses a TOML document, keeping its source formatting.
Validation matches TomlElixir.decode/2, and failures return the same
TomlElixir.Parser.Error.
Options
:spec-:"1.1.0"(default) or:"1.0.0". Edits are validated against the same version.
Same as parse/2, but raises on failure.
@spec put(t(), path(), term()) :: {:ok, t()} | {:error, TomlElixir.Document.Error.t()}
Puts value at path.
Replacing an existing value keeps the text around it, such as an end-of-line comment.
Putting a value equal to the current one leaves the document unchanged, so values read
with fetch/2 can be written back without losing their format.
Putting a map on an existing table, or a list of maps on an existing array of tables, updates it key by key: unchanged keys keep their text and comments, changed keys are replaced, missing keys are deleted, and new keys are added as described in Where new values go. Arrays of tables are matched by index. Missing parent tables are created.
Same as put/3, but raises on failure.
Renders the document as iodata.
Returns the document's data, as TomlElixir.decode/2 would return it with default
options.
Renders the document as a binary.
@spec update(t(), path(), (term() -> term())) :: {:ok, t()} | {:error, TomlElixir.Document.Error.t()}
Replaces the existing value at path with the result of calling fun with it.
Same as update/3, but raises on failure.