mirror of
https://github.com/spf13/viper
synced 2024-11-04 20:27:02 +00:00
59 - add properties file support to viper
This commit is contained in:
parent
39ab3ca72e
commit
ba3382dd23
3 changed files with 40 additions and 3 deletions
12
util.go
12
util.go
|
@ -22,6 +22,7 @@ import (
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
|
"github.com/magiconair/properties"
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
jww "github.com/spf13/jwalterweatherman"
|
jww "github.com/spf13/jwalterweatherman"
|
||||||
"gopkg.in/yaml.v2"
|
"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 {
|
if _, err := toml.Decode(buf.String(), &c); err != nil {
|
||||||
jww.ERROR.Fatalf("Error parsing config: %s", err)
|
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)
|
insensitiviseMap(c)
|
||||||
|
|
2
viper.go
2
viper.go
|
@ -174,7 +174,7 @@ type remoteProvider struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Universally supported extensions.
|
// 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.
|
// Universally supported remote providers.
|
||||||
var SupportedRemoteProviders []string = []string{"etcd", "consul"}
|
var SupportedRemoteProviders []string = []string{"etcd", "consul"}
|
||||||
|
|
|
@ -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(`{
|
var remoteExample = []byte(`{
|
||||||
"id":"0002",
|
"id":"0002",
|
||||||
"type":"cronut",
|
"type":"cronut",
|
||||||
|
@ -71,6 +79,10 @@ func initConfigs() {
|
||||||
r = bytes.NewReader(jsonExample)
|
r = bytes.NewReader(jsonExample)
|
||||||
marshalReader(r, v.config)
|
marshalReader(r, v.config)
|
||||||
|
|
||||||
|
SetConfigType("properties")
|
||||||
|
r = bytes.NewReader(propertiesExample)
|
||||||
|
marshalReader(r, v.config)
|
||||||
|
|
||||||
SetConfigType("toml")
|
SetConfigType("toml")
|
||||||
r = bytes.NewReader(tomlExample)
|
r = bytes.NewReader(tomlExample)
|
||||||
marshalReader(r, v.config)
|
marshalReader(r, v.config)
|
||||||
|
@ -96,6 +108,14 @@ func initJSON() {
|
||||||
marshalReader(r, v.config)
|
marshalReader(r, v.config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initProperties() {
|
||||||
|
Reset()
|
||||||
|
SetConfigType("properties")
|
||||||
|
r := bytes.NewReader(propertiesExample)
|
||||||
|
|
||||||
|
marshalReader(r, v.config)
|
||||||
|
}
|
||||||
|
|
||||||
func initTOML() {
|
func initTOML() {
|
||||||
Reset()
|
Reset()
|
||||||
SetConfigType("toml")
|
SetConfigType("toml")
|
||||||
|
@ -185,6 +205,11 @@ func TestJSON(t *testing.T) {
|
||||||
assert.Equal(t, "0001", Get("id"))
|
assert.Equal(t, "0001", Get("id"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProperties(t *testing.T) {
|
||||||
|
initProperties()
|
||||||
|
assert.Equal(t, "0001", Get("p_id"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestTOML(t *testing.T) {
|
func TestTOML(t *testing.T) {
|
||||||
initTOML()
|
initTOML()
|
||||||
assert.Equal(t, "TOML Example", Get("title"))
|
assert.Equal(t, "TOML Example", Get("title"))
|
||||||
|
@ -277,9 +302,9 @@ func TestSetEnvReplacer(t *testing.T) {
|
||||||
func TestAllKeys(t *testing.T) {
|
func TestAllKeys(t *testing.T) {
|
||||||
initConfigs()
|
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")
|
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
|
var allkeys sort.StringSlice
|
||||||
allkeys = AllKeys()
|
allkeys = AllKeys()
|
||||||
|
|
Loading…
Reference in a new issue