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 CommentForLine(lines []string, lineIndex int) string
- func ContainsUnescapedTripleQuote(s string) bool
- func EnsureCommented(line string) string
- func ExtractBlockKeyValue(lines []string, key string) string
- func ExtractInlineCommentWithState(line string, state StringState) string
- func FindInsertIndex(lines []string, afterKey string) int
- func FormatDottedKeyPath(path []string) string
- func FormatKey(key string) string
- func FormatValue(value any) string
- func InlineCommentForLine(lines []string, lineIndex int) 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 CommentForLine ¶
CommentForLine collects contiguous leading comment lines and any inline comment on a line.
func ContainsUnescapedTripleQuote ¶
ContainsUnescapedTripleQuote reports whether s contains an unescaped basic multiline-string delimiter.
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 ExtractInlineCommentWithState ¶
func ExtractInlineCommentWithState(line string, state StringState) string
ExtractInlineCommentWithState returns the inline comment portion.
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 FormatValue ¶
FormatValue converts a scalar value into a TOML literal string.
func InlineCommentForLine ¶
InlineCommentForLine extracts a TOML inline comment on a specific line.
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 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.
func ParseDocument ¶
ParseDocument splits TOML content into a line-aware document.
type KeyLine ¶
KeyLine holds a parsed key/value line with comment metadata.
func FindKeyLine ¶
FindKeyLine searches lines for a key/value assignment.
func ParseDottedPrefixLine ¶
ParseDottedPrefixLine checks if line defines a dotted sub-key of key.
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.