Skip to content
Go library for the TOML language
Go Other
  1. Go 99.2%
  2. Other 0.8%
Branch: master
Clone or download

Latest commit

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/ISSUE_TEMPLATE Set up CI with Azure Pipelines (#304) Sep 24, 2019
cmd Replace CIs by Github Actions (#294) Sep 10, 2019
query Fix query test May 17, 2020
.dockerignore Dockerfile (#269) Apr 10, 2019
.gitignore Dockerfile (#269) Apr 10, 2019
CONTRIBUTING.md Port toml-test to pure Go (#264) Mar 20, 2019
Dockerfile jsontoml tool (#296) Sep 6, 2019
LICENSE Update license (#128) Feb 9, 2017
Makefile CI Build Binaries (#345) Mar 30, 2020
PULL_REQUEST_TEMPLATE.md Setup Codecov (#235) Jul 24, 2018
README.md Mention support to v1.0.0-rc.1 May 17, 2020
azure-pipelines.yml CI computes manifest for built binaries (#346) Mar 30, 2020
benchmark.json Benchmark against other libraries (#175) Jun 25, 2017
benchmark.sh Set up CI with Azure Pipelines (#304) Sep 24, 2019
benchmark.toml Benchmark against other libraries (#175) Jun 25, 2017
benchmark.yml Benchmark against other libraries (#175) Jun 25, 2017
benchmark_test.go Benchmark against other libraries (#175) Jun 25, 2017
doc.go TOML 0.5.0 (#320) Oct 25, 2019
doc_test.go marshal: do not encode embedded structs as sub-table (#368) Apr 25, 2020
example-crlf.toml Add float to test case to check leading zeroes in exponent parts (#363) Apr 22, 2020
example.toml Add float to test case to check leading zeroes in exponent parts (#363) Apr 22, 2020
fuzz.go Fix fuzz (#199) Oct 22, 2017
fuzz.sh Add fuzz.sh to do fuzzing with go-fuzz (#194) Oct 21, 2017
fuzzit.sh Update fuzzit.dev script to latest (#301) Sep 18, 2019
go.mod Bump gopkg.in/yaml.v2 from 2.2.8 to 2.3.0 (#408) May 14, 2020
go.sum Bump gopkg.in/yaml.v2 from 2.2.8 to 2.3.0 (#408) May 14, 2020
keysparsing.go Fix various quoted keys bugs (#400) May 7, 2020
keysparsing_test.go Fix goreportcard issues (#271) Apr 11, 2019
lexer.go Allow spaces when using dotted keys in assignment (#402) May 7, 2020
lexer_test.go Allow spaces when using dotted keys in assignment (#402) May 7, 2020
localtime.go Short-date support (#298) Oct 25, 2019
localtime_test.go Short-date support (#298) Oct 25, 2019
marshal.go fix issue #406 May 8, 2020
marshal_OrderPreserve_test.toml Fix float64 truncation error (#293) Aug 27, 2019
marshal_test.go fix issue #406 May 8, 2020
marshal_test.toml Fix float64 truncation error (#293) Aug 27, 2019
parser.go Add support of mixed-type array (#376) May 7, 2020
parser_test.go Fix marshaling nested arrays of tables (#395) May 7, 2020
position.go Pointer cleanup (#78) Jul 21, 2016
position_test.go Simplify redundant types in literals (#95) Aug 23, 2016
token.go Fix various quoted keys bugs (#400) May 7, 2020
token_test.go Short-date support (#298) Oct 25, 2019
toml.go Forbid adding keys to exist inline table (#378) May 4, 2020
toml_test.go Delete function to Tree (#256) Mar 1, 2019
toml_testgen_support_test.go Port toml-test to pure Go (#264) Mar 20, 2019
toml_testgen_test.go Add support of mixed-type array (#376) May 7, 2020
tomltree_create.go Comment annotation for Marshal (#185) Oct 1, 2017
tomltree_create_test.go Fix marshaling nested arrays of tables (#395) May 7, 2020
tomltree_write.go Fix marshaling nested arrays of tables (#395) May 7, 2020
tomltree_write_test.go Add float to test case to check leading zeroes in exponent parts (#363) Apr 22, 2020

README.md

go-toml

Go library for the TOML format.

This library supports TOML version v1.0.0-rc.1

GoDoc license Build Status codecov Go Report Card FOSSA Status

Features

Go-toml provides the following features for using data parsed from TOML documents:

  • Load TOML documents from files and string data
  • Easily navigate TOML structure using Tree
  • Marshaling and unmarshaling to and from data structures
  • Line & column position data for all parsed elements
  • Query support similar to JSON-Path
  • Syntax errors contain line and column numbers

Import

import "github.com/pelletier/go-toml"

Usage example

Read a TOML document:

config, _ := toml.Load(`
[postgres]
user = "pelletier"
password = "mypassword"`)
// retrieve data directly
user := config.Get("postgres.user").(string)

// or using an intermediate object
postgresConfig := config.Get("postgres").(*toml.Tree)
password := postgresConfig.Get("password").(string)

Or use Unmarshal:

type Postgres struct {
    User     string
    Password string
}
type Config struct {
    Postgres Postgres
}

doc := []byte(`
[Postgres]
User = "pelletier"
Password = "mypassword"`)

config := Config{}
toml.Unmarshal(doc, &config)
fmt.Println("user=", config.Postgres.User)

Or use a query:

// use a query to gather elements without walking the tree
q, _ := query.Compile("$..[user,password]")
results := q.Execute(config)
for ii, item := range results.Values() {
    fmt.Printf("Query result %d: %v\n", ii, item)
}

Documentation

The documentation and additional examples are available at godoc.org.

Tools

Go-toml provides two handy command line tools:

  • tomll: Reads TOML files and lints them.

    go install github.com/pelletier/go-toml/cmd/tomll
    tomll --help
    
  • tomljson: Reads a TOML file and outputs its JSON representation.

    go install github.com/pelletier/go-toml/cmd/tomljson
    tomljson --help
    
  • jsontoml: Reads a JSON file and outputs a TOML representation.

    go install github.com/pelletier/go-toml/cmd/jsontoml
    jsontoml --help
    

Docker image

Those tools are also availble as a Docker image from dockerhub. For example, to use tomljson:

docker run -v $PWD:/workdir pelletier/go-toml tomljson /workdir/example.toml

Only master (latest) and tagged versions are published to dockerhub. You can build your own image as usual:

docker build -t go-toml .

Contribute

Feel free to report bugs and patches using GitHub's pull requests system on pelletier/go-toml. Any feedback would be much appreciated!

Run tests

go test ./...

Fuzzing

The script ./fuzz.sh is available to run go-fuzz on go-toml.

Versioning

Go-toml follows Semantic Versioning. The supported version of TOML is indicated at the beginning of this document. The last two major versions of Go are supported (see Go Release Policy).

License

The MIT License (MIT). Read LICENSE.

You can’t perform that action at this time.