Viper is now case insensitive

This commit is contained in:
spf13 2014-04-05 01:19:39 -04:00
parent 98be0718d2
commit bcb02e2472
2 changed files with 71 additions and 23 deletions

View File

@ -14,6 +14,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings"
"time" "time"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
@ -75,19 +76,24 @@ func GetTime(key string) time.Time {
return cast.ToTime(Get(key)) return cast.ToTime(Get(key))
} }
func GetStringArray(key string) []string { func GetStringSlice(key string) []string {
return cast.ToStringArray(Get(key)) return cast.ToStringSlice(Get(key))
}
func GetStringMap(key string) map[string]interface{} {
return cast.ToStringMap(Get(key))
}
func GetStringMapString(key string) map[string]string {
return cast.ToStringMapString(Get(key))
} }
func find(key string) interface{} { func find(key string) interface{} {
var val interface{} var val interface{}
var exists bool var exists bool
// If the requested key is an alias, then return the proper key // if the requested key is an alias, then return the proper key
newkey, exists := aliases[key] key = realKey(key)
if exists {
return find(newkey)
}
val, exists = override[key] val, exists = override[key]
if exists { if exists {
@ -111,6 +117,7 @@ func find(key string) interface{} {
} }
func Get(key string) interface{} { func Get(key string) interface{} {
key = strings.ToLower(key)
v := find(key) v := find(key)
if v == nil { if v == nil {
@ -140,33 +147,51 @@ func IsSet(key string) bool {
} }
func RegisterAlias(alias string, key string) { func RegisterAlias(alias string, key string) {
registerAlias(alias, strings.ToLower(key))
}
func registerAlias(alias string, key string) {
alias = strings.ToLower(alias)
if alias != key && alias != realKey(key) {
_, exists := aliases[alias]
if !exists {
aliases[alias] = key aliases[alias] = key
} }
} else {
jww.WARN.Println("Creating circular reference alias", alias, key, realKey(key))
}
}
func realKey(key string) string {
newkey, exists := aliases[key]
if exists {
jww.DEBUG.Println("Alias", key, "to", newkey)
return realKey(newkey)
} else {
return key
}
}
func InConfig(key string) bool { func InConfig(key string) bool {
// if the requested key is an alias, then return the proper key
key = realKey(key)
_, exists := config[key] _, exists := config[key]
return exists return exists
} }
func SetDefault(key string, value interface{}) { func SetDefault(key string, value interface{}) {
// If alias passed in, then set the proper default // If alias passed in, then set the proper default
newkey, exists := aliases[key] key = realKey(strings.ToLower(key))
if exists {
defaults[newkey] = value
} else {
defaults[key] = value defaults[key] = value
} }
}
func Set(key string, value interface{}) { func Set(key string, value interface{}) {
// If alias passed in, then set the proper override // If alias passed in, then set the proper override
newkey, exists := aliases[key] key = realKey(strings.ToLower(key))
if exists { key = strings.ToLower(key)
override[newkey] = value
} else {
override[key] = value override[key] = value
} }
}
func ReadInConfig() { func ReadInConfig() {
jww.INFO.Println("Attempting to read in config file") jww.INFO.Println("Attempting to read in config file")
@ -200,6 +225,16 @@ func MarshallReader(in io.Reader) {
jww.ERROR.Fatalf("Error parsing config: %s", err) jww.ERROR.Fatalf("Error parsing config: %s", err)
} }
} }
insensativiseMap()
}
func insensativiseMap() {
for key, _ := range config {
if key != strings.ToLower(key) {
registerAlias(key, key)
}
}
} }
func SetConfigName(in string) { func SetConfigName(in string) {

View File

@ -25,7 +25,7 @@ title = "TOML Example"
[owner] [owner]
organization = "MongoDB" organization = "MongoDB"
bio = "MongoDB Chief Developer Advocate & Hacker at Large" Bio = "MongoDB Chief Developer Advocate & Hacker at Large"
dob = 1979-05-27T07:32:00Z # First class dates? Why not?`) dob = 1979-05-27T07:32:00Z # First class dates? Why not?`)
var jsonExample = []byte(`{ var jsonExample = []byte(`{
@ -114,6 +114,19 @@ func TestTOML(t *testing.T) {
assert.Equal(t, "TOML Example", Get("title")) assert.Equal(t, "TOML Example", Get("title"))
} }
func TestCaseSensitive(t *testing.T) { func TestCaseInSensitive(t *testing.T) {
assert.Equal(t, true, Get("Hacker")) assert.Equal(t, true, Get("hacker"))
Set("Title", "Checking Case")
assert.Equal(t, "Checking Case", Get("tItle"))
}
func TestAliasesOfAliases(t *testing.T) {
RegisterAlias("Foo", "Bar")
RegisterAlias("Bar", "Title")
assert.Equal(t, "Checking Case", Get("FOO"))
}
func TestRecursiveAliases(t *testing.T) {
RegisterAlias("Baz", "Roo")
RegisterAlias("Roo", "baz")
} }