package tomlcode

import "golazy.dev/lazycode/tomlcode"

Package tomlcode provides conservative, comment-preserving TOML edits as lazycode operations.

A typed operation can be combined with other edits in one lazycode plan:

workspace, err := lazycode.Load(".", "lazy.toml")
if err != nil {
	return err
}

_, err = workspace.Plan(
	tomlcode.SetString(
		"lazy.toml",
		"database",
		"url_variable",
		"DATABASE_URL",
	),
)
if err != nil {
	return err
}
if result.Changed() {
	fmt.Println("lazy.toml would change")
}

The editor preserves unrelated text and comments. It supports ordinary tables and single keys; ambiguous or unsupported TOML returns an error instead of causing a broad reformat.

Functions

func Edit

Edit returns an operation that parses name, applies edit, validates the result, and replaces the file in memory only when it changed.

func Edit(name string, edit EditFunc) lazycode.Operation

func EncodeString

EncodeString renders value as a TOML basic string.

func EncodeString(value string) string

func EncodeStrings

EncodeStrings renders values as a single-line TOML string array.

func EncodeStrings(values []string) string

Types

type Document

Document is a parsed TOML document that retains its original line endings, comments, and unrelated source text.

type Document struct {
	// contains filtered or unexported fields
}
func Parse

Parse validates data as a document supported by this conservative editor.

func Parse(data []byte) (*Document, error)
func (d *Document) Bytes

Bytes returns the document's current source representation.

func (d *Document) Bytes() []byte
func (d *Document) EnsureTable

EnsureTable creates an ordinary table when it is absent and reports whether the document changed.

func (d *Document) EnsureTable(table string) (bool, error)
func (d *Document) HasTable

HasTable reports whether an ordinary table exists. The root table always exists. Array tables do not satisfy an ordinary-table lookup.

func (d *Document) HasTable(table string) (bool, error)
func (d *Document) Raw

Raw returns the source value assigned to table.key in a normalized single-line form. It is intended for ownership-aware planners that must restore an application's previous value without interpreting arbitrary TOML types. Comments inside a multi-line value are omitted from the normalized result while the value itself remains valid TOML.

func (d *Document) Raw(table, key string) (string, bool, error)
func (d *Document) Remove

Remove removes table.key and reports whether it existed.

func (d *Document) Remove(table, key string) (bool, error)
func (d *Document) RemoveTable

RemoveTable removes an ordinary table and all of its keys.

func (d *Document) RemoveTable(table string) (bool, error)
func (d *Document) RemoveTableIfEmpty

RemoveTableIfEmpty removes an ordinary table only when it contains no keys. It is useful when an ownership-aware edit created a table but must preserve application keys added there later.

func (d *Document) RemoveTableIfEmpty(table string) (bool, error)
func (d *Document) SetBool

SetBool sets table.key to a TOML boolean.

func (d *Document) SetBool(table, key string, value bool) (bool, error)
func (d *Document) SetInteger

SetInteger sets table.key to a TOML integer.

func (d *Document) SetInteger(table, key string, value int64) (bool, error)
func (d *Document) SetRaw

SetRaw sets a single-line TOML value, creating an ordinary table if needed.

func (d *Document) SetRaw(table, key, value string) (bool, error)
func (d *Document) SetString

SetString sets table.key to a TOML string.

func (d *Document) SetString(table, key, value string) (bool, error)
func (d *Document) SetStrings

SetStrings sets table.key to a single-line array of TOML strings.

func (d *Document) SetStrings(table, key string, values []string) (bool, error)