Documentation
¶
Overview ¶
Package tomlpatch provides line-aware TOML patch helpers for generated configuration files that must preserve unrelated comments and formatting.
Index ¶
- func AppendBlock(output *[]string, block []string)
- func BuildKeyLine(base KeyLine, key string, value string, commented bool) string
- func CloneLines(lines []string) []string
- func EnsureCommented(line string) string
- func ExtractBlockKeyValue(lines []string, key string) string
- func FindInsertIndex(lines []string, afterKey string) int
- func FormatDottedKeyPath(path []string) string
- func FormatKey(key string) string
- func FormatString(value string) (string, error)
- func FormatValue(value any) string
- func MultilineValueEndIndex(lines []string, startIdx int) int
- func ParseHeader(line string) (string, bool, bool)
- func ParseKeyPath(input string) ([]string, bool)
- func ParseKeyValueWithState(line string, key string, state StringState) (string, string, bool)
- func RemoveKeyFromBlock(block *Block, key string)
- func ReplaceOrInsertLine(block *Block, key string, newLine string, afterKey string)
- func SetCommentedKeyLine(block *Block, templateBlock *Block, key string, afterKey string)
- func SetKeyValue(block *Block, templateBlock *Block, key string, value string, afterKey string)
- func StateInMultiline(state StringState) bool
- func TrimEmptyLines(lines []string) []string
- func TrimTrailingEmptyLines(lines []string) []string
- func WalkLinesOutsideMultiline(lines []string, fn func(i int, line string, state StringState) LineWalkResult)
- type Block
- type Document
- type KeyLine
- type LineWalkResult
- type StringState
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendBlock ¶
AppendBlock appends a block to output with one blank line separator.
func BuildKeyLine ¶
BuildKeyLine renders a key/value line using indentation and inline comment from base.
func EnsureCommented ¶
EnsureCommented returns line with a leading comment marker.
func ExtractBlockKeyValue ¶
ExtractBlockKeyValue returns the unquoted value for a key in a TOML block.
func FindInsertIndex ¶
FindInsertIndex returns the line index to insert a new key line after afterKey.
func FormatDottedKeyPath ¶
FormatDottedKeyPath renders path as a TOML dotted key with quoted segments where required.
func FormatKey ¶
FormatKey renders key as a TOML bare key when possible, otherwise as a basic quoted key.
func FormatString ¶ added in v0.16.0
FormatString renders one string as a TOML value using the repository's TOML encoder. Invalid UTF-8 and values the encoder cannot represent are rejected instead of being written with Go-specific escaping rules.
func FormatValue ¶
FormatValue converts a scalar value into a TOML literal string.
func MultilineValueEndIndex ¶
MultilineValueEndIndex returns the index of the last line of a value that starts at startIdx.
func ParseHeader ¶
ParseHeader detects a TOML table header and extracts its name.
func ParseKeyPath ¶
ParseKeyPath parses a TOML dotted key or table path into unescaped segments.
func ParseKeyValueWithState ¶
ParseKeyValueWithState extracts a simple key/value pair from a TOML line.
func RemoveKeyFromBlock ¶
RemoveKeyFromBlock removes all uncommented lines for key from block, including multiline continuation lines and dotted sub-key assignments.
func ReplaceOrInsertLine ¶
ReplaceOrInsertLine replaces an existing key line or inserts a new line.
func SetCommentedKeyLine ¶
SetCommentedKeyLine ensures key is present as a commented line when possible.
func SetKeyValue ¶
SetKeyValue updates or inserts key = value in a section block.
func StateInMultiline ¶
func StateInMultiline(state StringState) bool
StateInMultiline returns true if state is inside a multiline string.
func TrimEmptyLines ¶
TrimEmptyLines removes leading and trailing blank lines.
func TrimTrailingEmptyLines ¶
TrimTrailingEmptyLines removes trailing blank lines.
func WalkLinesOutsideMultiline ¶
func WalkLinesOutsideMultiline(lines []string, fn func(i int, line string, state StringState) LineWalkResult)
WalkLinesOutsideMultiline walks lines while skipping callbacks inside multiline TOML string bodies.
Types ¶
type Block ¶
type Block struct {
Name string
Lines []string
// SubTables holds the table and array-of-table headers that TOML nests
// under this array-of-tables element, in source order, each with its own
// header line and body. Only top-level array elements carry sub-tables, and
// the list is flat: every descendant attaches to the owning element.
SubTables []*Block
}
Block is a contiguous TOML table or array-of-table block.
type Document ¶
type Document struct {
Preamble []string
Sections map[string]*Block
Arrays map[string][]*Block
Order []string
}
Document is a lightweight line-based TOML split into preamble, table blocks, and array-of-table blocks. Sections, Arrays, and Order exclude headers nested under an array-of-tables element; those appear only in the owning element's SubTables.
func ParseDocument ¶
ParseDocument splits TOML content into a line-aware document. A header nested under an array-of-tables path (for example [mcp.servers.env] after [[mcp.servers]]) belongs to the most recent element of that array, as in TOML, even when unrelated tables appear in between, so it is attached to that element's SubTables.
type KeyLine ¶
KeyLine holds a parsed key/value line with comment metadata.
func FindKeyLine ¶
FindKeyLine searches lines for a key/value assignment.
func ParseKeyLineWithState ¶
func ParseKeyLineWithState(line string, key string, state StringState) (KeyLine, bool)
ParseKeyLineWithState parses a key/value assignment line with explicit state.
type LineWalkResult ¶
LineWalkResult controls TOML line walking.
type StringState ¶
type StringState int
StringState tracks parser position relative to TOML string literals.
const ( // StateNone means parsing is outside any TOML string. StateNone StringState = iota // StateBasic means parsing is inside a basic string. StateBasic // StateLiteral means parsing is inside a literal string. StateLiteral // StateMultiBasic means parsing is inside a multiline basic string. StateMultiBasic // StateMultiLiteral means parsing is inside a multiline literal string. StateMultiLiteral )
func ScanLineForComment ¶
func ScanLineForComment(line string, state StringState) (commentPos int, nextState StringState)
ScanLineForComment scans a TOML line and returns the position of any inline comment, or -1 if none, plus the next parser state for multiline strings.