OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / pelletier / go-toml / doc_test.go
1 // code examples for godoc
2
3 package toml_test
4
5 import (
6         "fmt"
7         "log"
8
9         toml "github.com/pelletier/go-toml"
10 )
11
12 func Example_tree() {
13         config, err := toml.LoadFile("config.toml")
14
15         if err != nil {
16                 fmt.Println("Error ", err.Error())
17         } else {
18                 // retrieve data directly
19                 user := config.Get("postgres.user").(string)
20                 password := config.Get("postgres.password").(string)
21
22                 // or using an intermediate object
23                 configTree := config.Get("postgres").(*toml.Tree)
24                 user = configTree.Get("user").(string)
25                 password = configTree.Get("password").(string)
26                 fmt.Println("User is", user, " and password is", password)
27
28                 // show where elements are in the file
29                 fmt.Printf("User position: %v\n", configTree.GetPosition("user"))
30                 fmt.Printf("Password position: %v\n", configTree.GetPosition("password"))
31         }
32 }
33
34 func Example_unmarshal() {
35         type Employer struct {
36                 Name  string
37                 Phone string
38         }
39         type Person struct {
40                 Name     string
41                 Age      int64
42                 Employer Employer
43         }
44
45         document := []byte(`
46         name = "John"
47         age = 30
48         [employer]
49                 name = "Company Inc."
50                 phone = "+1 234 567 89012"
51         `)
52
53         person := Person{}
54         toml.Unmarshal(document, &person)
55         fmt.Println(person.Name, "is", person.Age, "and works at", person.Employer.Name)
56         // Output:
57         // John is 30 and works at Company Inc.
58 }
59
60 func ExampleMarshal() {
61         type Postgres struct {
62                 User     string `toml:"user"`
63                 Password string `toml:"password"`
64                 Database string `toml:"db" commented:"true" comment:"not used anymore"`
65         }
66         type Config struct {
67                 Postgres Postgres `toml:"postgres" comment:"Postgres configuration"`
68         }
69
70         config := Config{Postgres{User: "pelletier", Password: "mypassword", Database: "old_database"}}
71         b, err := toml.Marshal(config)
72         if err != nil {
73                 log.Fatal(err)
74         }
75         fmt.Println(string(b))
76         // Output:
77         // # Postgres configuration
78         // [postgres]
79         //
80         //   # not used anymore
81         //   # db = "old_database"
82         //   password = "mypassword"
83         //   user = "pelletier"
84 }
85
86 func ExampleUnmarshal() {
87         type Postgres struct {
88                 User     string
89                 Password string
90         }
91         type Config struct {
92                 Postgres Postgres
93         }
94
95         doc := []byte(`
96         [postgres]
97         user = "pelletier"
98         password = "mypassword"`)
99
100         config := Config{}
101         toml.Unmarshal(doc, &config)
102         fmt.Println("user=", config.Postgres.User)
103         // Output:
104         // user= pelletier
105 }