molt/ops
molt/ops: Operation types for document manipulation.
The types declared in this module offer document manipulation options for
use in molt.run batches.
Path Resolution Targets
The path string provided to operations resolves against a logical version
of the document’s concrete syntax tree so that implicit nodes and value
nodes may be modified. The shape of the targeted document node determines
which operations are legal and what side-effects occur.
-
Concrete tables: tables defined by explicit headers, such as
[header]or[database.connection]. -
Implicit tables: tables whose existence are implied by dotted key or sub-header references. In
[database.connection],databaseis an implicit table, as ispackagefor a dotted key definition of:package.type = "gleam". -
Array of tables: a collection of tables identified by
[[name]]headers. Paths may resolve to the collection by omitting index references (custom.pluginsreferencing[[custom.plugins]]) and individual table entries within the collection with index references (custom.plugins[1]resolving to the second table in the collection). -
Key/value nodes: a leaf assignment (
key = value). The value may be a scalar value (integer, float, string, etc.), an inline table, or an array. -
Inline table nodes: a table written using the inline table syntax (
key = { … }) that is part of a key/value node. Path strings may extend into the inline table. -
Arrays: a value node that may represent heterogenous TOML values. Path strings allow resolution to the array or elements within an array using the same syntax as array of table references.
If an operation indicates that it works on a table-like path, it means that it may operate on concrete tables, implicit tables, or an array of tables entry.
Operation Values
Many operations work with Values from molt/value, a comprehensive
representation of TOML values that does not preserve comments or
formatting.
If an operation indicates that it works with a table-like value, it means that it will work with a Value representing an inline table or a concrete table’s values.
Types
Comments attached to a node.
pub type Comments {
Comments(
leading: List(String),
trailing: option.Option(String),
)
}
Constructors
-
Comments(leading: List(String), trailing: option.Option(String))
Strategy for handling key conflicts during move/merge operations.
pub type ConflictStrategy {
OnConflictError
OnConflictOverwrite
OnConflictSkip
}
Constructors
-
OnConflictErrorError if destination key already exists.
-
OnConflictOverwriteOverwrite destination with source value.
-
OnConflictSkipSkip keys that already exist in destination.
pub type Form {
Block
Inline
}
Constructors
-
BlockThe table or array of tables is in a “block” form:
[a] b = 1 c = 2 [[q]] r = 1 [[q]] r = 2 -
InlineThe table or array of tables is in an inline form:
a = { b = 1, c = 2 } q = [{ r = 1 }, { r = 2 }]
pub type Operation {
Append(path: String, value: value.Value)
Concat(path: String, values: List(value.Value))
EnsureExists(path: String, kind: types.TomlKind)
Insert(path: String, before: Int, value: value.Value)
InsertKey(
path: String,
before: String,
key: String,
value: value.Value,
)
MergeValues(
path: String,
entries: List(#(String, value.Value)),
on_conflict: ConflictStrategy,
)
Move(from: String, to: String)
MoveComments(from: String, to: String)
MoveKeys(
from: String,
to: String,
keys: List(String),
on_conflict: ConflictStrategy,
)
Place(path: String, value: value.Value)
Remove(path: String)
Rename(path: String, to: String)
Representation(path: String, form: Form)
Set(path: String, value: value.Value)
SetComments(path: String, comments: Comments)
Transfer(
from: String,
to: String,
on_conflict: ConflictStrategy,
)
Update(
path: String,
with: fn(value.Value) -> Result(value.Value, error.MoltError),
)
}
Constructors
-
Append(path: String, value: value.Value)Appends to an array at
path.pathmust resolve to either a value node containing an array or an array of tables. Whenpathresolves to an array of tables, thevalueprovided must be table-like. -
Concat(path: String, values: List(value.Value))Concatenate multiple values to an array at
path.pathmust resolve to either a value node containing an array or an array of tables. Whenpathresolves to an array of tables, all entries invaluesmust be table-like. -
EnsureExists(path: String, kind: types.TomlKind)Ensures that a table or array of tables exists at
path.kindmust betypes.Tableortypes.ArrayOfTables; other kinds are rejected.If the matching structure already exists, nothing is done. If
pathdoes not resolve, an empty entry is created with implicit table ancestors as required. Ifpathresolves to an implicit table andkindistypes.Table, the implicit table is concretized into an emitted header using the key segments ofpath.A
TypeMismatcherror is returned ifpathresolves to any other value shape, or if any ancestor in thepathis anything other than an implicit table, concrete table, or array of tables entry. -
Insert(path: String, before: Int, value: value.Value)Inserts a value before index
beforein an array atpath.pathmust resolve to a value node containing an array or an array of tables. Whenpathresolves to an array of tables,valuemust be table-like. -
InsertKey( path: String, before: String, key: String, value: value.Value, )Inserts a key/value pair before an existing key in the table at
path.pathmust resolve to a concrete table, array of tables entry, or implicit table. Bothbeforeandkeyare literal key names (immediate children ofpath). Ifbeforeis not found, the entry is appended.When
pathis an implicit table, the new entry is emitted as a root-level dotted key. -
MergeValues( path: String, entries: List(#(String, value.Value)), on_conflict: ConflictStrategy, )Merge key/value entries into the resolved table at
path.pathmust resolve to a concrete table or an array of tables entry; implicit tables and other shapes are rejected with aTypeMismatcherror.Each key in
entriesis parsed as a path value relative to the table atpath. Index segments in entry keys will be rejected with anInvalidPatherror, and entry keys redefining any existing concrete table or value are rejected.The
on_conflictparameter controls how collisions with existing leaf keys are resolved. -
Move(from: String, to: String)Moves the node at
fromtoto.frommust resolve to an existing node of any kind except the root.tomust not already exist, and its last segment must be a key (not an index). The node is removed fromfromand re-inserted atto, preserving its structure. -
MoveComments(from: String, to: String)Moves comments from the node at
fromto the node atto.Both
fromandtomust resolve to concrete nodes or the root of the document (""). -
MoveKeys( from: String, to: String, keys: List(String), on_conflict: ConflictStrategy, )Moves a subset of keys from the table at
frominto the table atto.frommust resolve to a concrete table, implicit table, or array of tables entry.keysare literal key names naming the immediate children of thefromtable. Keys not present in thefromtable are ignored.If
todoes not exist or is an implicit table, a concrete table header will be created. Theon_conflictparameter controls how collisions with existing keys in thetotable are resolved. -
Place(path: String, value: value.Value)Unconditionally places
valueatpath.If
pathalready exists, it is removed before writing thevalue. StructuralValues (table, array of tables, etc.) are permitted. -
Remove(path: String)Removes the node at
pathfrom the document.If
pathresolves to an implicit table, the implicit table and all concrete nodes beneath it are removed. -
Rename(path: String, to: String)Renames the last segment of
pathtoto.The last segment of
pathmust be a key.tois a literal key name and must not already exist as a sibling.Renaming an implicit table renames all concrete descendants that reference it.
-
Representation(path: String, form: Form)Converts the structure at
pathbetween inline and block forms.pathmust resolve to a table or array of tables. The data is preserved; only the representation changes (inline table ↔ table section, array of inline tables ↔ array of tables entries).A
paththat does not reference a convertible structure is aTypeMismatch. Conversions that would produce invalid TOML (e.g., inlining a table with sub-table descendants) are rejected. -
Set(path: String, value: value.Value)Sets a value at
pathin the document.Creates or overwrites a key/value node. If
pathdoes not exist, it is created (with implicit ancestors as needed). Ifpathresolves to an existing value node, the value is replaced.If
pathresolves to a structural node (section tables, array of tables, implicit tables) orvaluewould render a structural node (section tables, array of tables),Setwill return aTypeMismatcherror. -
SetComments(path: String, comments: Comments)Sets comments on the node at
path.pathmust resolve to a concrete node (not an implicit table) or the root of the document (""). Replaces any existing comments on the node with the providedcomments. -
Transfer(from: String, to: String, on_conflict: ConflictStrategy)Transfers all keys from
fromtoto, then removesfrom.frommust resolve to a concrete or implicit table.towill be created as a concrete table if it does not exist. Theon_conflictparameter controls how collisions with existing keys intoare resolved. -
Update( path: String, with: fn(value.Value) -> Result(value.Value, error.MoltError), )Transforms a value in place via callback.
pathmust resolve to a scalar, array, or inline table value node. Structural types (concrete tables, implicit tables, array of tables) are rejected withTypeMismatch.Transforming inline tables or arrays round-trips through
Value, which loses internal comments and multiline formatting.