59 - add properties file support to viper

This commit is contained in:
Wayne Walker 2015-04-14 13:15:02 -05:00
parent 39ab3ca72e
commit ba3382dd23
3 changed files with 40 additions and 3 deletions

12
util.go
View file

@ -22,6 +22,7 @@ import (
"unicode"
"github.com/BurntSushi/toml"
"github.com/magiconair/properties"
"github.com/spf13/cast"
jww "github.com/spf13/jwalterweatherman"
"gopkg.in/yaml.v2"
@ -137,6 +138,17 @@ func marshallConfigReader(in io.Reader, c map[string]interface{}, configType str
if _, err := toml.Decode(buf.String(), &c); err != nil {
jww.ERROR.Fatalf("Error parsing config: %s", err)
}
case "properties", "props", "prop":
var p *properties.Properties
var err error
if p, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil {
jww.ERROR.Fatalf("Error parsing config: %s", err)
}
for _, key := range p.Keys() {
value, _ := p.Get(key)
c[key] = value
}
}
insensitiviseMap(c)

View file

@ -174,7 +174,7 @@ type remoteProvider struct {
}
// Universally supported extensions.
var SupportedExts []string = []string{"json", "toml", "yaml", "yml"}
var SupportedExts []string = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop"}
// Universally supported remote providers.
var SupportedRemoteProviders []string = []string{"etcd", "consul"}

View file

@ -55,6 +55,14 @@ var jsonExample = []byte(`{
}
}`)
var propertiesExample = []byte(`
p_id: 0001
p_type: donut
p_name: Cake
p_ppu: 0.55
p_batters.batter.type: Regular
`)
var remoteExample = []byte(`{
"id":"0002",
"type":"cronut",
@ -71,6 +79,10 @@ func initConfigs() {
r = bytes.NewReader(jsonExample)
marshalReader(r, v.config)
SetConfigType("properties")
r = bytes.NewReader(propertiesExample)
marshalReader(r, v.config)
SetConfigType("toml")
r = bytes.NewReader(tomlExample)
marshalReader(r, v.config)
@ -96,6 +108,14 @@ func initJSON() {
marshalReader(r, v.config)
}
func initProperties() {
Reset()
SetConfigType("properties")
r := bytes.NewReader(propertiesExample)
marshalReader(r, v.config)
}
func initTOML() {
Reset()
SetConfigType("toml")
@ -185,6 +205,11 @@ func TestJSON(t *testing.T) {
assert.Equal(t, "0001", Get("id"))
}
func TestProperties(t *testing.T) {
initProperties()
assert.Equal(t, "0001", Get("p_id"))
}
func TestTOML(t *testing.T) {
initTOML()
assert.Equal(t, "TOML Example", Get("title"))
@ -277,9 +302,9 @@ func TestSetEnvReplacer(t *testing.T) {
func TestAllKeys(t *testing.T) {
initConfigs()
ks := sort.StringSlice{"title", "newkey", "owner", "name", "beard", "ppu", "batters", "hobbies", "clothing", "age", "hacker", "id", "type", "eyes"}
ks := sort.StringSlice{"title", "newkey", "owner", "name", "beard", "ppu", "batters", "hobbies", "clothing", "age", "hacker", "id", "type", "eyes", "p_id", "p_ppu", "p_batters.batter.type", "p_type", "p_name"}
dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
all := map[string]interface{}{"owner": map[string]interface{}{"organization": "MongoDB", "Bio": "MongoDB Chief Developer Advocate & Hacker at Large", "dob": dob}, "title": "TOML Example", "ppu": 0.55, "eyes": "brown", "clothing": map[interface{}]interface{}{"trousers": "denim", "jacket": "leather"}, "id": "0001", "batters": map[string]interface{}{"batter": []interface{}{map[string]interface{}{"type": "Regular"}, map[string]interface{}{"type": "Chocolate"}, map[string]interface{}{"type": "Blueberry"}, map[string]interface{}{"type": "Devil's Food"}}}, "hacker": true, "beard": true, "hobbies": []interface{}{"skateboarding", "snowboarding", "go"}, "age": 35, "type": "donut", "newkey": "remote", "name": "Cake"}
all := map[string]interface{}{"owner": map[string]interface{}{"organization": "MongoDB", "Bio": "MongoDB Chief Developer Advocate & Hacker at Large", "dob": dob}, "title": "TOML Example", "ppu": 0.55, "eyes": "brown", "clothing": map[interface{}]interface{}{"trousers": "denim", "jacket": "leather"}, "id": "0001", "batters": map[string]interface{}{"batter": []interface{}{map[string]interface{}{"type": "Regular"}, map[string]interface{}{"type": "Chocolate"}, map[string]interface{}{"type": "Blueberry"}, map[string]interface{}{"type": "Devil's Food"}}}, "hacker": true, "beard": true, "hobbies": []interface{}{"skateboarding", "snowboarding", "go"}, "age": 35, "type": "donut", "newkey": "remote", "name": "Cake", "p_id": "0001", "p_ppu": "0.55", "p_name": "Cake", "p_batters.batter.type": "Regular", "p_type": "donut"}
var allkeys sort.StringSlice
allkeys = AllKeys()