add support for hcl

This commit is contained in:
patdhlk 2015-12-11 23:51:11 +01:00
parent ce08532bfd
commit 9cd0a9f663
3 changed files with 72 additions and 2 deletions

10
util.go
View File

@ -22,6 +22,7 @@ import (
"unicode" "unicode"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/hashicorp/hcl"
"github.com/magiconair/properties" "github.com/magiconair/properties"
"github.com/spf13/cast" "github.com/spf13/cast"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
@ -144,6 +145,15 @@ func unmarshallConfigReader(in io.Reader, c map[string]interface{}, configType s
return ConfigParseError{err} return ConfigParseError{err}
} }
case "hcl":
obj, err := hcl.Parse(string(buf.Bytes()))
if err != nil {
return ConfigParseError{err}
}
if err = hcl.DecodeObject(&c, obj); err != nil {
return ConfigParseError{err}
}
case "toml": case "toml":
if _, err := toml.Decode(buf.String(), &c); err != nil { if _, err := toml.Decode(buf.String(), &c); err != nil {
return ConfigParseError{err} return ConfigParseError{err}

View File

@ -179,7 +179,7 @@ func New() *Viper {
// can use it in their testing as well. // can use it in their testing as well.
func Reset() { func Reset() {
v = New() v = New()
SupportedExts = []string{"json", "toml", "yaml", "yml"} SupportedExts = []string{"json", "toml", "yaml", "yml", "hcl"}
SupportedRemoteProviders = []string{"etcd", "consul"} SupportedRemoteProviders = []string{"etcd", "consul"}
} }
@ -218,7 +218,7 @@ type RemoteProvider interface {
} }
// Universally supported extensions. // Universally supported extensions.
var SupportedExts []string = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop"} var SupportedExts []string = []string{"json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl"}
// Universally supported remote providers. // Universally supported remote providers.
var SupportedRemoteProviders []string = []string{"etcd", "consul"} var SupportedRemoteProviders []string = []string{"etcd", "consul"}

View File

@ -60,6 +60,24 @@ var jsonExample = []byte(`{
} }
}`) }`)
var hclExample = []byte(`
id = "0001"
type = "donut"
name = "Cake"
ppu = 0.55
batter {
type = "Regular"
}
batter {
type = "Chocolate"
}
batter {
type = "Blueberry"
}
batter {
type = "Devil's Food"
}`)
var propertiesExample = []byte(` var propertiesExample = []byte(`
p_id: 0001 p_id: 0001
p_type: donut p_type: donut
@ -95,6 +113,10 @@ func initConfigs() {
SetConfigType("json") SetConfigType("json")
remote := bytes.NewReader(remoteExample) remote := bytes.NewReader(remoteExample)
unmarshalReader(remote, v.kvstore) unmarshalReader(remote, v.kvstore)
SetConfigType("hcl")
r = bytes.NewReader(hclExample)
unmarshalReader(r, v.config)
} }
func initYAML() { func initYAML() {
@ -129,6 +151,14 @@ func initTOML() {
unmarshalReader(r, v.config) unmarshalReader(r, v.config)
} }
func initHcl() {
Reset()
SetConfigType("hcl")
r := bytes.NewReader(hclExample)
unmarshalReader(r, v.config)
}
// make directories for testing // make directories for testing
func initDirs(t *testing.T) (string, string, func()) { func initDirs(t *testing.T) (string, string, func()) {
@ -270,6 +300,36 @@ func TestTOML(t *testing.T) {
assert.Equal(t, "TOML Example", Get("title")) assert.Equal(t, "TOML Example", Get("title"))
} }
func TestHCL(t *testing.T) {
initHcl()
assert.Equal(t, "0001", Get("id"))
assert.Equal(t, 0.55, Get("ppu"))
assert.Equal(t, "donut", Get("type"))
assert.Equal(t, "Cake", Get("name"))
Set("id", "0002")
assert.Equal(t, "0002", Get("id"))
assert.NotEqual(t, "cronut", Get("type"))
}
func TestHCLList(t *testing.T) {
initHcl()
batters := []map[string]interface{}{
map[string]interface{}{
"type": "Regular",
},
map[string]interface{}{
"type": "Chocolate",
},
map[string]interface{}{
"type": "Blueberry",
},
map[string]interface{}{
"type": "Devil's Food",
},
}
assert.Equal(t, batters, Get("batter"))
}
func TestRemotePrecedence(t *testing.T) { func TestRemotePrecedence(t *testing.T) {
initJSON() initJSON()