From fdd64c6697936b7233eed8b94bc8ccf9ae3c3536 Mon Sep 17 00:00:00 2001 From: akutz Date: Mon, 18 Jul 2016 18:08:45 -0500 Subject: [PATCH] Optional TOML Support (disabled by default) This patch makes TOML support optional, disabling it by default. To enable support include the Golang build tag `toml`. For example, the following command builds Viper without TOML support: $ go build Whereas this command will build Viper, along with support for parsing TOML: $ go build -tags toml The reason for making TOML optional is due to the dependency upon the BurntSushi package that is used to unmarshal TOML content. The package is licensed under the WTFPL license (http://www.wtfpl.net/) and incompatible with the Kubernetes project. Pull requests submitted to K8 that transitively include a dependency upon packages licensed under the WTFPL license are denied. To this end, until such time an alternative package is deemed acceptable for parsing TOML content, this patch would make TOML an optional component of Viper, enabled only when explicitly requested via the build tag `toml`. --- util.go | 3 +-- util_notoml.go | 17 +++++++++++++++++ util_toml.go | 24 ++++++++++++++++++++++++ viper_notoml_test.go | 32 ++++++++++++++++++++++++++++++++ viper_test.go | 24 +++--------------------- viper_toml_test.go | 37 +++++++++++++++++++++++++++++++++++++ 6 files changed, 114 insertions(+), 23 deletions(-) create mode 100644 util_notoml.go create mode 100644 util_toml.go create mode 100644 viper_notoml_test.go create mode 100644 viper_toml_test.go diff --git a/util.go b/util.go index 0cc4553..acc2e0a 100644 --- a/util.go +++ b/util.go @@ -21,7 +21,6 @@ import ( "strings" "unicode" - "github.com/BurntSushi/toml" "github.com/hashicorp/hcl" "github.com/magiconair/properties" "github.com/spf13/cast" @@ -155,7 +154,7 @@ func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType s } case "toml": - if _, err := toml.Decode(buf.String(), &c); err != nil { + if err := unmarshalTOML(buf.String(), &c); err != nil { return ConfigParseError{err} } diff --git a/util_notoml.go b/util_notoml.go new file mode 100644 index 0000000..2c5151b --- /dev/null +++ b/util_notoml.go @@ -0,0 +1,17 @@ +// +build !toml + +// Copyright © 2014 Steve Francia . +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file. + +// Viper is a application configuration system. +// It believes that applications can be configured a variety of ways +// via flags, ENVIRONMENT variables, configuration files retrieved +// from the file system, or a remote key/value store. + +package viper + +func unmarshalTOML(data string, v interface{}) error { + return nil +} diff --git a/util_toml.go b/util_toml.go new file mode 100644 index 0000000..c1c8efa --- /dev/null +++ b/util_toml.go @@ -0,0 +1,24 @@ +// +build toml + +// Copyright © 2014 Steve Francia . +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file. + +// Viper is a application configuration system. +// It believes that applications can be configured a variety of ways +// via flags, ENVIRONMENT variables, configuration files retrieved +// from the file system, or a remote key/value store. + +package viper + +import ( + "github.com/BurntSushi/toml" +) + +func unmarshalTOML(data string, v interface{}) error { + if _, err := toml.Decode(data, v); err != nil { + return err + } + return nil +} diff --git a/viper_notoml_test.go b/viper_notoml_test.go new file mode 100644 index 0000000..75f9e53 --- /dev/null +++ b/viper_notoml_test.go @@ -0,0 +1,32 @@ +// +build !toml + +// Copyright © 2014 Steve Francia . +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file. + +package viper + +import ( + "testing" + "time" +) + +func initTOML(reset bool) { + + if reset { + Reset() + } + + v.config["title"] = "TOML Example" + + dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z") + v.config["owner"] = map[string]interface{}{ + "organization": "MongoDB", + "Bio": "MongoDB Chief Developer Advocate & Hacker at Large", + "dob": dob, + } +} + +func assertConfigValue(t *testing.T, v *Viper) { +} diff --git a/viper_test.go b/viper_test.go index 858caff..0085f4d 100644 --- a/viper_test.go +++ b/viper_test.go @@ -45,14 +45,6 @@ type testUnmarshalExtra struct { Existing bool } -var tomlExample = []byte(` -title = "TOML Example" - -[owner] -organization = "MongoDB" -Bio = "MongoDB Chief Developer Advocate & Hacker at Large" -dob = 1979-05-27T07:32:00Z # First class dates? Why not?`) - var jsonExample = []byte(`{ "id": "0001", "type": "donut", @@ -120,9 +112,7 @@ func initConfigs() { r = bytes.NewReader(propertiesExample) unmarshalReader(r, v.config) - SetConfigType("toml") - r = bytes.NewReader(tomlExample) - unmarshalReader(r, v.config) + initTOML(false) SetConfigType("json") remote := bytes.NewReader(remoteExample) @@ -153,14 +143,6 @@ func initProperties() { unmarshalReader(r, v.config) } -func initTOML() { - Reset() - SetConfigType("toml") - r := bytes.NewReader(tomlExample) - - unmarshalReader(r, v.config) -} - func initHcl() { Reset() SetConfigType("hcl") @@ -318,7 +300,7 @@ func TestProperties(t *testing.T) { } func TestTOML(t *testing.T) { - initTOML() + initTOML(true) assert.Equal(t, "TOML Example", Get("title")) } @@ -722,7 +704,7 @@ func TestDirsSearch(t *testing.T) { err = v.ReadInConfig() assert.Nil(t, err) - assert.Equal(t, `value is `+path.Base(v.configPaths[0]), v.GetString(`key`)) + assertConfigValue(t, v) } func TestWrongDirsSearchNotFound(t *testing.T) { diff --git a/viper_toml_test.go b/viper_toml_test.go new file mode 100644 index 0000000..789508d --- /dev/null +++ b/viper_toml_test.go @@ -0,0 +1,37 @@ +// +build toml + +// Copyright © 2014 Steve Francia . +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file. + +package viper + +import ( + "bytes" + "path" + "testing" + + "github.com/stretchr/testify/assert" +) + +var tomlExample = []byte(` +title = "TOML Example" + +[owner] +organization = "MongoDB" +Bio = "MongoDB Chief Developer Advocate & Hacker at Large" +dob = 1979-05-27T07:32:00Z # First class dates? Why not?`) + +func initTOML(reset bool) { + if reset { + Reset() + } + SetConfigType("toml") + r := bytes.NewReader(tomlExample) + unmarshalReader(r, v.config) +} + +func assertConfigValue(t *testing.T, v *Viper) { + assert.Equal(t, `value is `+path.Base(v.configPaths[0]), v.GetString(`key`)) +}