tomlpatch

package
v0.12.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package tomlpatch provides line-aware TOML patch helpers for generated configuration files that must preserve unrelated comments and formatting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendBlock

func AppendBlock(output *[]string, block []string)

AppendBlock appends a block to output with one blank line separator.

func BuildKeyLine

func BuildKeyLine(base KeyLine, key string, value string, commented bool) string

BuildKeyLine renders a key/value line using indentation and inline comment from base.

func CloneLines

func CloneLines(lines []string) []string

CloneLines returns a copy of lines.

func CommentForLine

func CommentForLine(lines []string, lineIndex int) string

CommentForLine collects contiguous leading comment lines and any inline comment on a line.

func ContainsUnescapedTripleQuote

func ContainsUnescapedTripleQuote(s string) bool

ContainsUnescapedTripleQuote reports whether s contains an unescaped basic multiline-string delimiter.

func EnsureCommented

func EnsureCommented(line string) string

EnsureCommented returns line with a leading comment marker.

func ExtractBlockKeyValue

func ExtractBlockKeyValue(lines []string, key string) string

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

func FindInsertIndex(lines []string, afterKey string) int

FindInsertIndex returns the line index to insert a new key line after afterKey.

func FormatDottedKeyPath

func FormatDottedKeyPath(path []string) string

FormatDottedKeyPath renders path as a TOML dotted key with quoted segments where required.

func FormatKey

func FormatKey(key string) string

FormatKey renders key as a TOML bare key when possible, otherwise as a basic quoted key.

func FormatValue

func FormatValue(value any) string

FormatValue converts a scalar value into a TOML literal string.

func InlineCommentForLine

func InlineCommentForLine(lines []string, lineIndex int) string

InlineCommentForLine extracts a TOML inline comment on a specific line.

func MultilineValueEndIndex

func MultilineValueEndIndex(lines []string, startIdx int) int

MultilineValueEndIndex returns the index of the last line of a value that starts at startIdx.

func ParseHeader

func ParseHeader(line string) (string, bool, bool)

ParseHeader detects a TOML table header and extracts its name.

func ParseKeyPath

func ParseKeyPath(input string) ([]string, bool)

ParseKeyPath parses a TOML dotted key or table path into unescaped segments.

func ParseKeyValueWithState

func ParseKeyValueWithState(line string, key string, state StringState) (string, string, bool)

ParseKeyValueWithState extracts a simple key/value pair from a TOML line.

func RemoveKeyFromBlock

func RemoveKeyFromBlock(block *Block, key string)

RemoveKeyFromBlock removes all uncommented lines for key from block, including multiline continuation lines and dotted sub-key assignments.

func ReplaceOrInsertLine

func ReplaceOrInsertLine(block *Block, key string, newLine string, afterKey string)

ReplaceOrInsertLine replaces an existing key line or inserts a new line.

func SetCommentedKeyLine

func SetCommentedKeyLine(block *Block, templateBlock *Block, key string, afterKey string)

SetCommentedKeyLine ensures key is present as a commented line when possible.

func SetKeyValue

func SetKeyValue(block *Block, templateBlock *Block, key string, value string, afterKey string)

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

func TrimEmptyLines(lines []string) []string

TrimEmptyLines removes leading and trailing blank lines.

func TrimTrailingEmptyLines

func TrimTrailingEmptyLines(lines []string) []string

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
}

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.

func ParseDocument

func ParseDocument(content string) Document

ParseDocument splits TOML content into a line-aware document.

type KeyLine

type KeyLine struct {
	Raw           string
	Indent        string
	Commented     bool
	InlineComment string
}

KeyLine holds a parsed key/value line with comment metadata.

func FindKeyLine

func FindKeyLine(lines []string, key string) (KeyLine, bool)

FindKeyLine searches lines for a key/value assignment.

func ParseDottedPrefixLine

func ParseDottedPrefixLine(line string, key string) (KeyLine, bool)

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

type LineWalkResult struct {
	AdvanceTo int
	Stop      bool
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL