Documentation
¶
Overview ¶
Package tomledit reads and edits TOML documents while preserving their formatting: comments, whitespace, key quoting and the spelling of values that are not changed survive every edit byte for byte.
Parse accepts TOML 1.1 by default, or only TOML 1.0 with the Strict10 option. A parsed Document returns its source unchanged from String until it is edited with Set, Insert or Delete. Every edit is validated; an edit that would make the document invalid is rolled back and reported.
Values are read with Get or the typed getters (GetInt, GetString, ...). Integers are int64, floats float64, offset date-times time.Time, and local dates and times the types of package eval. New values are written from Go strings, booleans, integers, floats, time.Time, the eval date/time types, slices (as arrays) and maps with string keys (as inline tables).
Example ¶
package main
import (
"fmt"
tomledit "github.com/npikall/toml-edit"
)
const exampleConfig = `# server settings
[server]
host = "localhost" # bind address
port = 8080
debug = true
`
func main() {
doc, err := tomledit.Parse(exampleConfig)
if err != nil {
panic(err)
}
port, _ := doc.GetInt("server", "port")
fmt.Println("port:", port)
_ = doc.Set([]string{"server", "port"}, 9090)
_ = doc.Insert([]string{"server", "workers"}, 4)
_ = doc.Delete("server", "debug")
fmt.Print(doc)
}
Output: port: 8080 # server settings [server] host = "localhost" # bind address port = 9090 workers = 4
Index ¶
- Variables
- type Document
- func (d *Document) Delete(keys ...string) error
- func (d *Document) Get(keys ...string) (any, bool)
- func (d *Document) GetBool(keys ...string) (bool, error)
- func (d *Document) GetFloat(keys ...string) (float64, error)
- func (d *Document) GetInt(keys ...string) (int64, error)
- func (d *Document) GetString(keys ...string) (string, error)
- func (d *Document) Insert(path []string, value any, opts ...EditOption) error
- func (d *Document) Set(path []string, value any) error
- func (d *Document) String() string
- type EditOption
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound is returned when a key path does not exist. ErrNotFound = errors.New("key not found") // ErrNotValue is returned by Set when a key path names a table defined // by a header or dotted keys, or an array of tables. ErrNotValue = errors.New("key does not hold a value") // ErrType is returned by the typed getters when the value has a // different type. ErrType = errors.New("wrong value type") // ErrExists is returned by Insert when the key path already exists. ErrExists = errors.New("key already exists") // ErrNotTable is returned by Insert when the parent of the key path is // not a table. ErrNotTable = errors.New("key does not hold a table") )
Functions ¶
This section is empty.
Types ¶
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document is a parsed TOML document. Its String method returns the source with all edits applied.
func Parse ¶
Parse parses and validates src as TOML 1.1, or 1.0 with Strict10. The result's String method returns src unchanged until it is edited.
func (*Document) Delete ¶
Delete removes the key at the key path together with the comments and blank lines before it. Deleting a table removes its header, its key/values and all its sub-tables.
Example ¶
package main
import (
"fmt"
tomledit "github.com/npikall/toml-edit"
)
func main() {
doc, _ := tomledit.Parse(`point = { x = 1, y = 2 }
# old section
[legacy]
a = 1
`)
_ = doc.Delete("point", "x")
_ = doc.Delete("legacy")
fmt.Print(doc)
}
Output: point = { y = 2 }
func (*Document) Get ¶
Get returns the value at the key path: a string, int64, float64, bool, time.Time, eval.LocalDateTime, eval.LocalDate, eval.LocalTime, []any or *eval.Table. No keys return the root table.
Example ¶
package main
import (
"fmt"
tomledit "github.com/npikall/toml-edit"
)
func main() {
doc, _ := tomledit.Parse("tags = [\"a\", \"b\"]\npoint = { x = 1 }\n")
tags, _ := doc.Get("tags")
x, _ := doc.Get("point", "x")
_, ok := doc.Get("missing")
fmt.Println(tags, x, ok)
}
Output: [a b] 1 false
func (*Document) Insert ¶
func (d *Document) Insert(path []string, value any, opts ...EditOption) error
Insert adds a new key with value at the end of the table section that owns it, indented like the key/value before it.
Example ¶
package main
import (
"errors"
"fmt"
tomledit "github.com/npikall/toml-edit"
)
func main() {
doc, _ := tomledit.Parse("[a]\nx = 1\n")
_ = doc.Insert([]string{"a", "point"}, map[string]any{"x": 1, "y": 2})
_ = doc.Insert([]string{"b", "c", "d"}, []int{1, 2})
err := doc.Insert([]string{"a", "x"}, 2)
fmt.Print(doc)
fmt.Println(errors.Is(err, tomledit.ErrExists))
}
Output: [a] x = 1 point = { x = 1, y = 2 } [b.c] d = [1, 2] true
func (*Document) Set ¶
Set replaces the value of an existing key. Only the value's text changes; the key, the whitespace around the value and a trailing comment are kept. An array written across several lines stays one element per line with a trailing comma; elements that remain keep their spelling and comments.
Example ¶
package main
import (
"fmt"
tomledit "github.com/npikall/toml-edit"
)
func main() {
doc, _ := tomledit.Parse("name = 'old' # keep me\n")
_ = doc.Set([]string{"name"}, "new")
fmt.Print(doc)
}
Output: name = "new" # keep me
type EditOption ¶
type EditOption func(*editConfig)
EditOption configures Insert. Set takes none: it follows the layout of the value it replaces.
func Multiline ¶
func Multiline() EditOption
Multiline makes Insert write an array value one element per line, each followed by a comma and indented two spaces deeper than the line of its key.
type Option ¶
Option configures Parse.
func Strict10 ¶
func Strict10() Option
Strict10 makes Parse accept TOML 1.0 only, rejecting the syntax that 1.1 added. Edits never add 1.1 syntax, so an edited document stays valid 1.0.
Example ¶
package main
import (
"fmt"
tomledit "github.com/npikall/toml-edit"
)
func main() {
_, err := tomledit.Parse("t = 07:32\n", tomledit.Strict10())
fmt.Println(err)
}
Output: parse: 1:5: expected a value, found invalid "07:32"
Directories
¶
| Path | Synopsis |
|---|---|
|
Package ast defines the concrete syntax tree (CST) of a TOML document.
|
Package ast defines the concrete syntax tree (CST) of a TOML document. |
|
cmd
|
|
|
toml-test-decoder
command
Command toml-test-decoder reads TOML on stdin and writes toml-test's tagged JSON on stdout.
|
Command toml-test-decoder reads TOML on stdin and writes toml-test's tagged JSON on stdout. |
|
Package eval turns a CST into semantic values and enforces the TOML rules the parser cannot check: duplicate keys, table redefinition, value ranges.
|
Package eval turns a CST into semantic values and enforces the TOML rules the parser cannot check: duplicate keys, table redefinition, value ranges. |
|
Package format turns Go values into CST nodes with canonical TOML text.
|
Package format turns Go values into CST nodes with canonical TOML text. |
|
internal
|
|
|
tagged
Package tagged converts evaluated TOML into toml-test's tagged JSON form, where every scalar is {"type": ..., "value": ...}.
|
Package tagged converts evaluated TOML into toml-test's tagged JSON form, where every scalar is {"type": ..., "value": ...}. |
|
Package parser turns TOML source into a lossless concrete syntax tree.
|
Package parser turns TOML source into a lossless concrete syntax tree. |