func TestMarshal(t *testing.T) { for i, test := range marshalTests { b, err := toml.Marshal(test.in) if test.print && err == nil { t.Errorf("\n# %d: marshaled TOML document:\n%s# EOF\n", i, string(b)) } if test.err != nil { if !reflect.DeepEqual(test.err, err) { t.Errorf("#%d: error got %s\n, want %s", i, err, test.err) } continue } if err != nil { t.Errorf("#%d: got error: %s", i, err) continue } err = toml.Unmarshal(b, test.out) if err != nil { t.Errorf("#%d: unmarshal error: %s\ntext:\n%s\nfrom: %+v", i, err, string(b), test.in) continue } got := reflect.ValueOf(test.out).Elem().Interface() if !reflect.DeepEqual(test.in, got) { t.Errorf("#%d:\ngot %+v,\nwant %+v\n,\ntext:\n%s", i, got, test.in, string(b)) continue } } }
func ExampleUnmarshal_integer() { data := []byte(`key = 12345`) var out struct{ Key int } err := toml.Unmarshal(data, &out) if err != nil { panic(err) } fmt.Println(out.Key) // Output: 12345 }
func ExampleUnmarshal_array() { data := []byte(`key = [1, 2, 3,4]`) var out struct{ Key []int } err := toml.Unmarshal(data, &out) if err != nil { panic(err) } fmt.Println(out.Key) // Output: [1 2 3 4] }
func ExampleUnmarshal_datetimeTextUnmarshaler() { data := []byte(`key = "2016-01-07T15:30:30.123456789Z"`) var out struct{ Key time.Time } err := toml.Unmarshal(data, &out) if err != nil { panic(err) } fmt.Println(out.Key.Format(time.RFC3339Nano)) // Output: 2016-01-07T15:30:30.123456789Z }
func ExampleUnmarshal_string() { data := []byte(`key = "value"`) var out struct{ Key string } err := toml.Unmarshal(data, &out) if err != nil { panic(err) } fmt.Println(out.Key) // Output: value }
func ExampleUnmarshal_boolean() { data := []byte(`key = true`) var out struct{ Key bool } err := toml.Unmarshal(data, &out) if err != nil { panic(err) } fmt.Println(out.Key) // Output: true }
func ExampleUnmarshal_float() { data := []byte(`key = 3.14`) var out struct{ Key float64 } err := toml.Unmarshal(data, &out) if err != nil { panic(err) } fmt.Println(out.Key) // Output: 3.14 }
func ExampleUnmarshal_textUnmarshaler() { data := []byte(`timeout = "300ms"`) var out struct{ Timeout duration } err := toml.Unmarshal(data, &out) if err != nil { panic(err) } fmt.Println(time.Duration(out.Timeout)) // Output: 300ms }
func ExampleUnmarshal_tagString() { data := []byte(`key = "12345"`) var out struct { Key int `toml:",string"` } err := toml.Unmarshal(data, &out) if err != nil { panic(err) } fmt.Println(out.Key) // Output: 12345 }
func ExampleUnmarshal_tagName() { data := []byte(`KKKK = "value"`) var out struct { Key string `toml:"KKKK"` } err := toml.Unmarshal(data, &out) if err != nil { panic(err) } fmt.Println(out.Key) // Output: value }
func ExampleUnmarshal_tagOmitempty() { data := []byte(``) var out struct { Key string `toml:",omitempty"` } out.Key = "Not empty, for now." err := toml.Unmarshal(data, &out) if err != nil { panic(err) } fmt.Println(out.Key) // Output: }
func ExampleUnmarshal_tableArray() { data := []byte(` [[array]] description = "Table In Array" `) var out struct { Array []struct{ Description string } } err := toml.Unmarshal(data, &out) if err != nil { panic(err) } fmt.Println(out.Array[0].Description) // Output: Table In Array }
func ExampleUnmarshal_inlineTable() { data := []byte(`key = { name = "name", value = "value" }`) var out struct { Key struct { Name string Value string } } err := toml.Unmarshal(data, &out) if err != nil { panic(err) } fmt.Printf("key.name = %q\n", out.Key.Name) fmt.Printf("key.value = %q\n", out.Key.Value) // Output: // key.name = "name" // key.value = "value" }
func TestUnmarshal(t *testing.T) { for i, test := range unmarshalTests { err := toml.Unmarshal([]byte(test.in), test.ptr) if test.err != nil { if !reflect.DeepEqual(test.err, err) { t.Errorf("#%d: error got %s\n, want %s", i, err, test.err) } continue } if err != nil { t.Errorf("#%d: got error: %s", i, err) continue } got := reflect.ValueOf(test.ptr).Elem().Interface() if !reflect.DeepEqual(got, test.out) { t.Errorf("#%d: got %+v\n, want %+v", i, got, test.out) continue } } }
func (cfg *Config) Load(c *cli.Context) { // Enable debug. if c.GlobalBool("debug") { log.SetDebug() } // Default path for config file. u, _ := user.Current() cfgs := []string{ u.HomeDir + "/.etcdrest.json", u.HomeDir + "/.etcdrest.yaml", u.HomeDir + "/.etcdrest.yml", u.HomeDir + "/.etcdrest.toml", u.HomeDir + "/.etcdrest.tml", "/etc/etcdrest.json", "/etc/etcdrest.yaml", "/etc/etcdrest.yml", "/etc/etcdrest.toml", "/etc/etcdrest.tml", "/app/etc/etcdrest.json", "/app/etc/etcdrest.yaml", "/app/etc/etcdrest.yml", "/app/etc/etcdrest.toml", "/app/etc/etcdrest.tml", } // Check if we have an arg. for config file and that it exist's. if c.GlobalString("config") != "" { if _, err := os.Stat(c.GlobalString("config")); os.IsNotExist(err) { log.Fatalf("Config file doesn't exist: %s", c.GlobalString("config")) } cfgs = append([]string{c.GlobalString("config")}, cfgs...) } for _, fn := range cfgs { if _, err := os.Stat(fn); os.IsNotExist(err) { continue } log.Infof("Using config file: %s", fn) // Load config file. b, err := ioutil.ReadFile(fn) if err != nil { log.Fatal(err.Error()) } switch filepath.Ext(fn) { case ".json": if err := json.Unmarshal(b, cfg); err != nil { log.Fatal(err.Error()) } case ".yaml", ".yml": if err := yaml.Unmarshal(b, cfg); err != nil { log.Fatal(err.Error()) } case ".toml", ".tml": if err := toml.Unmarshal(b, cfg); err != nil { log.Fatal(err.Error()) } default: log.Fatal("unsupported data format") } // Validate config using JSON schema. break } // Override configuration. if c.GlobalString("templ-dir") != "" { cfg.TemplDir = c.GlobalString("templ-dir") } if c.GlobalString("schema-uri") != "" { cfg.SchemaURI = c.GlobalString("schema-uri") } if c.GlobalString("bind") != "" { cfg.Bind = c.GlobalString("bind") } if c.GlobalString("server-uri") != "" { cfg.ServerURI = c.GlobalString("server-uri") } if c.GlobalBool("envelope") { cfg.Envelope = true } if c.GlobalBool("no-indent") { cfg.Indent = true } // Override etcd configuration. if c.GlobalString("peers") != "" { cfg.Etcd.Peers = c.GlobalString("peers") } if c.GlobalString("cert") != "" { cfg.Etcd.Cert = c.GlobalString("cert") } if c.GlobalString("key") != "" { cfg.Etcd.Key = c.GlobalString("key") } if c.GlobalString("ca") != "" { cfg.Etcd.CA = c.GlobalString("ca") } if c.GlobalString("user") != "" { cfg.Etcd.User = c.GlobalString("user") } if c.GlobalDuration("timeout") != 0 { cfg.Etcd.Timeout = c.GlobalDuration("timeout") } if c.GlobalDuration("command-timeout") != 0 { cfg.Etcd.CmdTimeout = c.GlobalDuration("command-timeout") } }