Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix examples #178

Merged
merged 2 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions doc_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// code examples for godoc

package toml
package toml_test

import (
"fmt"
"log"

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

func Example_tree() {
config, err := LoadFile("config.toml")
config, err := toml.LoadFile("config.toml")

if err != nil {
fmt.Println("Error ", err.Error())
Expand All @@ -17,7 +20,7 @@ func Example_tree() {
password := config.Get("postgres.password").(string)

// or using an intermediate object
configTree := config.Get("postgres").(*Tree)
configTree := config.Get("postgres").(*toml.Tree)
user = configTree.Get("user").(string)
password = configTree.Get("password").(string)
fmt.Println("User is", user, " and password is", password)
Expand Down Expand Up @@ -48,6 +51,50 @@ func Example_unmarshal() {
`)

person := Person{}
Unmarshal(document, &person)
toml.Unmarshal(document, &person)
fmt.Println(person.Name, "is", person.Age, "and works at", person.Employer.Name)
// Output:
// John is 30 and works at Company Inc.
}

func ExampleMarshal() {
type Postgres struct {
User string `toml:"user"`
Password string `toml:"password"`
}
type Config struct {
Postgres Postgres `toml:"postgres"`
}

config := Config{Postgres{User: "pelletier", Password: "mypassword"}}
b, err := toml.Marshal(config)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
// Output:
// [postgres]
// password = "mypassword"
// user = "pelletier"
}

func ExampleUnmarshal() {
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)
// Output:
// user= pelletier
}
19 changes: 0 additions & 19 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,25 +177,6 @@ func TestDocUnmarshal(t *testing.T) {
}
}

func ExampleUnmarshal() {
type Postgres struct {
User string
Password string
}
type Config struct {
Postgres Postgres
}

doc := []byte(`
[postgres]
user = "pelletier"
password = "mypassword"`)

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

func TestDocPartialUnmarshal(t *testing.T) {
result := testDocSubs{}

Expand Down