cst

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: MIT Imports: 5 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteAllValues

func DeleteAllValues(container *Node)

DeleteAllValues removes all key-value children from a container, preserving table/array-table headers and other non-KV nodes.

func DeleteValue

func DeleteValue(container *Node, key string)

DeleteValue removes a key-value from the container.

func EscapeString

func EscapeString(s string) string

EscapeString escapes special characters for a TOML basic string.

func ExtractBool

func ExtractBool(kv *Node) (bool, bool)

ExtractBool extracts a bool value from a NodeKeyValue.

func ExtractFloat64

func ExtractFloat64(kv *Node) (float64, bool)

ExtractFloat64 extracts a float64 value from a NodeKeyValue.

func ExtractInt

func ExtractInt(kv *Node) (int, bool)

ExtractInt extracts an int value from a NodeKeyValue.

func ExtractInt64

func ExtractInt64(kv *Node) (int64, bool)

ExtractInt64 extracts an int64 value from a NodeKeyValue.

func ExtractIntSlice

func ExtractIntSlice(kv *Node) ([]int, bool)

ExtractIntSlice extracts a []int from a NodeKeyValue whose value is a NodeArray.

func ExtractRaw

func ExtractRaw(kv *Node) (any, bool)

ExtractRaw extracts the value from a NodeKeyValue as a natural Go type (string, int64, float64, bool, or []any). Used for custom unmarshalers.

func ExtractString

func ExtractString(kv *Node) (string, bool)

ExtractString extracts a string value from a NodeKeyValue.

func ExtractStringMap

func ExtractStringMap(container *Node) map[string]string

ExtractStringMap reads all key-value pairs from a container node (typically a NodeTable) as map[string]string.

func ExtractStringSlice

func ExtractStringSlice(kv *Node) ([]string, bool)

ExtractStringSlice extracts a []string from a NodeKeyValue whose value is a NodeArray.

func ExtractUint64

func ExtractUint64(kv *Node) (uint64, bool)

ExtractUint64 extracts a uint64 value from a NodeKeyValue.

func HasValue

func HasValue(container *Node, key string) bool

HasValue checks if a key-value exists in the container.

func KeyValueName

func KeyValueName(kv *Node) string

KeyValueName returns the key name from a NodeKeyValue node. For simple keys like `name = "value"`, returns "name". For dotted keys like `a.b.c = "value"`, returns "a.b.c".

func SetAny

func SetAny(container *Node, key string, value any) error

SetAny encodes a Go value and sets it as a key-value in the container.

func SetMultilineString

func SetMultilineString(container *Node, key, value string) error

SetMultilineString sets a string value using TOML multiline basic string syntax.

func SetValue

func SetValue(container *Node, key string, encoded []byte, kind NodeKind) error

SetValue finds or creates a key-value in container and sets its value.

func StripQuotes

func StripQuotes(s string) string

StripQuotes removes TOML quotes from a raw string value. Handles basic (""), literal (”), multiline ("""/”'), and bare strings.

func TableHeaderKey

func TableHeaderKey(table *Node) string

TableHeaderKey returns the dotted key from a NodeTable or NodeArrayTable header. For `[a.b.c]`, returns "a.b.c".

func UnescapeString

func UnescapeString(s string) string

UnescapeString processes TOML escape sequences in a basic string.

Types

type Node

type Node struct {
	Kind     NodeKind
	Raw      []byte
	Children []*Node
}

func AppendArrayTableEntryAfter

func AppendArrayTableEntryAfter(root *Node, key string) *Node

AppendArrayTableEntryAfter appends a new [[key]] array-table entry, inserting it after the last existing [[key]] or at the end.

func EnsureChildSubTable

func EnsureChildSubTable(root *Node, parent *Node, prefix, key string) *Node

EnsureChildSubTable finds or creates a [prefix.key] sub-table scoped to parent.

func EnsureChildTable

func EnsureChildTable(root *Node, parent *Node, key string) *Node

EnsureChildTable finds or creates a [key] table as a child of parent. Unlike the document API's EnsureTable which always appends at root end, this inserts the new table immediately after parent in root.Children, fixing scoping for tables inside array-table entries.

func FindArrayTableNodes

func FindArrayTableNodes(root *Node, key string) []*Node

FindArrayTableNodes returns all [[key]] array-table nodes from root children.

func KeyValueValue

func KeyValueValue(kv *Node) *Node

KeyValueValue returns the value node from a NodeKeyValue node. The value is the first non-whitespace child after the NodeEquals.

func Parse

func Parse(input []byte) (*Node, error)

Parse consumes raw TOML input and returns a CST document node. Every token becomes a leaf node; structural nodes group their children. Concatenating all leaf Raw bytes reproduces the original input byte-for-byte.

func ParseReader

func ParseReader(r io.Reader) (*Node, error)

ParseReader consumes TOML from an io.Reader and returns a CST document node.

func (*Node) Bytes

func (n *Node) Bytes() []byte

type NodeKind

type NodeKind int
const (
	NodeDocument   NodeKind = iota
	NodeTable               // [table]
	NodeArrayTable          // [[array-of-tables]]
	NodeKeyValue            // key = value
	NodeKey                 // bare or quoted key
	NodeDottedKey           // a.b.c
	NodeEquals              // =

	// Values
	NodeString
	NodeInteger
	NodeFloat
	NodeBool
	NodeDateTime
	NodeArray       // [1, 2, 3]
	NodeInlineTable // {a = 1, b = 2}

	// Trivia
	NodeComment    // # ...
	NodeWhitespace // spaces, tabs
	NodeNewline    // \n, \r\n

	// Punctuation
	NodeBracketOpen  // [
	NodeBracketClose // ]
	NodeBraceOpen    // {
	NodeBraceClose   // }
	NodeComma        // ,
	NodeDot          // .
)

func EncodeMultilineString

func EncodeMultilineString(value string) ([]byte, NodeKind)

EncodeMultilineString encodes a string as TOML multiline basic string.

func EncodeValue

func EncodeValue(value any) ([]byte, NodeKind, error)

EncodeValue converts a Go value to TOML bytes and node kind.

Jump to

Keyboard shortcuts

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