MCPcopy
hub / github.com/BurntSushi/toml

github.com/BurntSushi/toml @v1.6.0 sqlite

repository ↗ · DeepWiki ↗ · release v1.6.0 ↗
539 symbols 1,648 edges 30 files 146 documented · 27%
README

TOML stands for Tom's Obvious, Minimal Language. This Go package provides a reflection interface similar to Go's standard library json and xml packages.

Compatible with TOML version v1.1.0.

Documentation: https://pkg.go.dev/github.com/BurntSushi/toml

See the releases page for a changelog; this information is also in the git tag annotations (e.g. git show v0.4.0).

This library requires Go 1.18 or newer; add it to your go.mod with:

% go get github.com/BurntSushi/toml@latest

It also comes with a TOML validator CLI tool:

% go install github.com/BurntSushi/toml/cmd/tomlv@latest
% tomlv some-toml-file.toml

Examples

For the simplest example, consider some TOML file as just a list of keys and values:

Age = 25
Cats = [ "Cauchy", "Plato" ]
Pi = 3.14
Perfection = [ 6, 28, 496, 8128 ]
DOB = 1987-07-05T05:45:00Z

Which can be decoded with:

type Config struct {
    Age        int
    Cats       []string
    Pi         float64
    Perfection []int
    DOB        time.Time
}

var conf Config
_, err := toml.Decode(tomlData, &conf)

You can also use struct tags if your struct field name doesn't map to a TOML key value directly:

some_key_NAME = "wat"
type TOML struct {
    ObscureKey string `toml:"some_key_NAME"`
}

Beware that like other decoders only exported fields are considered when encoding and decoding; private fields are silently ignored.

Using the Marshaler and encoding.TextUnmarshaler interfaces

Here's an example that automatically parses values in a mail.Address:

contacts = [
    "Donald Duck <donald@duckburg.com>",
    "Scrooge McDuck <scrooge@duckburg.com>",
]

Can be decoded with:

// Create address type which satisfies the encoding.TextUnmarshaler interface.
type address struct {
    *mail.Address
}

func (a *address) UnmarshalText(text []byte) error {
    var err error
    a.Address, err = mail.ParseAddress(string(text))
    return err
}

// Decode it.
func decode() {
    blob := `
        contacts = [
            "Donald Duck <donald@duckburg.com>",
            "Scrooge McDuck <scrooge@duckburg.com>",
        ]
    `

    var contacts struct {
        Contacts []address
    }

    _, err := toml.Decode(blob, &contacts)
    if err != nil {
        log.Fatal(err)
    }

    for _, c := range contacts.Contacts {
        fmt.Printf("%#v\n", c.Address)
    }

    // Output:
    // &mail.Address{Name:"Donald Duck", Address:"donald@duckburg.com"}
    // &mail.Address{Name:"Scrooge McDuck", Address:"scrooge@duckburg.com"}
}

To target TOML specifically you can implement UnmarshalTOML TOML interface in a similar way.

More complex usage

See the _example/ directory for a more complex example.

Extension points exported contracts — how you extend this code

Unmarshaler (Interface)
Unmarshaler is the interface implemented by objects that can unmarshal a TOML description of themselves. [6 implementers]
decode.go
Marshaler (Interface)
Marshaler is the interface implemented by types that can marshal themselves into valid TOML. [5 implementers]
encode.go
Parser (Interface)
A Parser instance is used to call the TOML parser we test. By default this is done through an external command. [2 implementers]
internal/toml-test/runner.go
Inner (Interface)
(no doc) [1 implementers]
encode_test.go

Core symbols most depended-on inside this repo

String
called by 69
meta.go
Decode
called by 49
decode.go
next
called by 45
lex.go
errorf
called by 39
lex.go
backup
called by 36
lex.go
Run
called by 33
internal/toml-test/runner.go
Type
called by 32
meta.go
fail
called by 32
internal/toml-test/runner.go

Shape

Method 202
Function 198
Struct 100
TypeAlias 29
Interface 7
FuncType 3

Languages

Go100%

Modules by API surface

encode_test.go99 symbols
lex.go78 symbols
decode_test.go62 symbols
parse.go36 symbols
error.go34 symbols
encode.go34 symbols
decode.go31 symbols
internal/toml-test/runner.go30 symbols
example_test.go24 symbols
type_fields.go12 symbols
meta.go11 symbols
internal/toml-test/json.go11 symbols

For agents

$ claude mcp add toml \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact