mirror of
https://github.com/spf13/viper
synced 2025-05-06 12:17:18 +00:00
Merge fdd64c6697
into b53595fb56
This commit is contained in:
commit
5da108f6b9
6 changed files with 114 additions and 23 deletions
3
util.go
3
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}
|
||||
}
|
||||
|
||||
|
|
17
util_notoml.go
Normal file
17
util_notoml.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
// +build !toml
|
||||
|
||||
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
||||
//
|
||||
// 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
|
||||
}
|
24
util_toml.go
Normal file
24
util_toml.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
// +build toml
|
||||
|
||||
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
||||
//
|
||||
// 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
|
||||
}
|
32
viper_notoml_test.go
Normal file
32
viper_notoml_test.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
// +build !toml
|
||||
|
||||
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
||||
//
|
||||
// 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) {
|
||||
}
|
|
@ -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) {
|
||||
|
|
37
viper_toml_test.go
Normal file
37
viper_toml_test.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
// +build toml
|
||||
|
||||
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
||||
//
|
||||
// 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`))
|
||||
}
|
Loading…
Add table
Reference in a new issue