Help us shape the future of Go! Take the 2026 Go Developer Survey

tomlpatch

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 6 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 AssignmentKeyPath added in v1.1.0

func AssignmentKeyPath(line string, state StringState) ([]string, bool)

AssignmentKeyPath returns the key path assigned by an uncommented line.

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 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 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 FormatString added in v0.16.0

func FormatString(value string) (string, error)

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

func FormatValue(value any) string

FormatValue converts a scalar value into a TOML literal string.

func InlineComment added in v1.1.0

func InlineComment(line string, state StringState) string

InlineComment returns the trimmed inline comment portion of line, or "".

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
	// 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

func ParseDocument(content string) Document

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

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 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