aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/pelletier/go-toml/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/pelletier/go-toml/README.md')
-rw-r--r--vendor/github.com/pelletier/go-toml/README.md85
1 files changed, 42 insertions, 43 deletions
diff --git a/vendor/github.com/pelletier/go-toml/README.md b/vendor/github.com/pelletier/go-toml/README.md
index b8137e0..2681690 100644
--- a/vendor/github.com/pelletier/go-toml/README.md
+++ b/vendor/github.com/pelletier/go-toml/README.md
@@ -16,65 +16,64 @@ This library supports TOML version
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 TomlTree
+* Easily navigate TOML structure using Tree
+* Mashaling and unmarshaling to and from data structures
* Line & column position data for all parsed elements
-* Query support similar to JSON-Path
+* [Query support similar to JSON-Path](query/)
* Syntax errors contain line and column numbers
-Go-toml is designed to help cover use-cases not covered by reflection-based TOML parsing:
+## Import
-* Semantic evaluation of parsed TOML
-* Informing a user of mistakes in the source document, after it has been parsed
-* Programatic handling of default values on a case-by-case basis
-* Using a TOML document as a flexible data-store
+```go
+import "github.com/pelletier/go-toml"
+```
-## Import
+## Usage example
- import "github.com/pelletier/go-toml"
+Read a TOML document:
-## Usage
+```go
+config, _ := toml.Load(`
+[postgres]
+user = "pelletier"
+password = "mypassword"`)
+// retrieve data directly
+user := config.Get("postgres.user").(string)
-### Example
+// or using an intermediate object
+postgresConfig := config.Get("postgres").(*toml.Tree)
+password := postgresConfig.Get("password").(string)
+```
+
+Or use Unmarshal:
-Say you have a TOML file that looks like this:
+```go
+type Postgres struct {
+ User string
+ Password string
+}
+type Config struct {
+ Postgres Postgres
+}
-```toml
+doc := []byte(`
[postgres]
user = "pelletier"
-password = "mypassword"
+password = "mypassword"`)
+
+config := Config{}
+toml.Unmarshal(doc, &config)
+fmt.Println("user=", config.Postgres.User)
```
-Read the username and password like this:
+Or use a query:
```go
-import (
- "fmt"
- "github.com/pelletier/go-toml"
-)
-
-config, err := toml.LoadFile("config.toml")
-if err != nil {
- fmt.Println("Error ", err.Error())
-} else {
- // retrieve data directly
- user := config.Get("postgres.user").(string)
- password := config.Get("postgres.password").(string)
-
- // or using an intermediate object
- configTree := config.Get("postgres").(*toml.TomlTree)
- user = configTree.Get("user").(string)
- password = configTree.Get("password").(string)
- fmt.Println("User is ", user, ". Password is ", password)
-
- // show where elements are in the file
- fmt.Println("User position: %v", configTree.GetPosition("user"))
- fmt.Println("Password position: %v", configTree.GetPosition("password"))
-
- // use a query to gather elements without walking the tree
- results, _ := config.Query("$..[user,password]")
- for ii, item := range results.Values() {
- fmt.Println("Query result %d: %v", ii, item)
- }
+// 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.Println("Query result %d: %v", ii, item)
}
```