1// code examples for godoc 2 3package toml_test 4 5import ( 6 "fmt" 7 "log" 8 "os" 9 10 toml "github.com/pelletier/go-toml" 11) 12 13func Example_tree() { 14 config, err := toml.LoadFile("config.toml") 15 16 if err != nil { 17 fmt.Println("Error ", err.Error()) 18 } else { 19 // retrieve data directly 20 directUser := config.Get("postgres.user").(string) 21 directPassword := config.Get("postgres.password").(string) 22 fmt.Println("User is", directUser, " and password is", directPassword) 23 24 // or using an intermediate object 25 configTree := config.Get("postgres").(*toml.Tree) 26 user := configTree.Get("user").(string) 27 password := configTree.Get("password").(string) 28 fmt.Println("User is", user, " and password is", password) 29 30 // show where elements are in the file 31 fmt.Printf("User position: %v\n", configTree.GetPosition("user")) 32 fmt.Printf("Password position: %v\n", configTree.GetPosition("password")) 33 } 34} 35 36func Example_unmarshal() { 37 type Employer struct { 38 Name string 39 Phone string 40 } 41 type Person struct { 42 Name string 43 Age int64 44 Employer Employer 45 } 46 47 document := []byte(` 48 name = "John" 49 age = 30 50 [employer] 51 name = "Company Inc." 52 phone = "+1 234 567 89012" 53 `) 54 55 person := Person{} 56 toml.Unmarshal(document, &person) 57 fmt.Println(person.Name, "is", person.Age, "and works at", person.Employer.Name) 58 // Output: 59 // John is 30 and works at Company Inc. 60} 61 62func ExampleMarshal() { 63 type Postgres struct { 64 User string `toml:"user"` 65 Password string `toml:"password"` 66 Database string `toml:"db" commented:"true" comment:"not used anymore"` 67 } 68 type Config struct { 69 Postgres Postgres `toml:"postgres" comment:"Postgres configuration"` 70 } 71 72 config := Config{Postgres{User: "pelletier", Password: "mypassword", Database: "old_database"}} 73 b, err := toml.Marshal(config) 74 if err != nil { 75 log.Fatal(err) 76 } 77 fmt.Println(string(b)) 78 // Output: 79 // # Postgres configuration 80 // [postgres] 81 // 82 // # not used anymore 83 // # db = "old_database" 84 // password = "mypassword" 85 // user = "pelletier" 86} 87 88func ExampleUnmarshal() { 89 type Postgres struct { 90 User string 91 Password string 92 } 93 type Config struct { 94 Postgres Postgres 95 } 96 97 doc := []byte(` 98 [postgres] 99 user = "pelletier" 100 password = "mypassword"`) 101 102 config := Config{} 103 toml.Unmarshal(doc, &config) 104 fmt.Println("user=", config.Postgres.User) 105 // Output: 106 // user= pelletier 107} 108 109func ExampleEncoder_anonymous() { 110 type Credentials struct { 111 User string `toml:"user"` 112 Password string `toml:"password"` 113 } 114 115 type Protocol struct { 116 Name string `toml:"name"` 117 } 118 119 type Config struct { 120 Version int `toml:"version"` 121 Credentials 122 Protocol `toml:"Protocol"` 123 } 124 config := Config{ 125 Version: 2, 126 Credentials: Credentials{ 127 User: "pelletier", 128 Password: "mypassword", 129 }, 130 Protocol: Protocol{ 131 Name: "tcp", 132 }, 133 } 134 fmt.Println("Default:") 135 fmt.Println("---------------") 136 137 def := toml.NewEncoder(os.Stdout) 138 if err := def.Encode(config); err != nil { 139 log.Fatal(err) 140 } 141 142 fmt.Println("---------------") 143 fmt.Println("With promotion:") 144 fmt.Println("---------------") 145 146 prom := toml.NewEncoder(os.Stdout).PromoteAnonymous(true) 147 if err := prom.Encode(config); err != nil { 148 log.Fatal(err) 149 } 150 // Output: 151 // Default: 152 // --------------- 153 // password = "mypassword" 154 // user = "pelletier" 155 // version = 2 156 // 157 // [Protocol] 158 // name = "tcp" 159 // --------------- 160 // With promotion: 161 // --------------- 162 // version = 2 163 // 164 // [Credentials] 165 // password = "mypassword" 166 // user = "pelletier" 167 // 168 // [Protocol] 169 // name = "tcp" 170} 171