mirror of
https://github.com/spf13/viper
synced 2024-11-05 20:57:01 +00:00
Viper is now case insensitive
This commit is contained in:
parent
98be0718d2
commit
bcb02e2472
2 changed files with 71 additions and 23 deletions
75
viper.go
75
viper.go
|
@ -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,32 +147,50 @@ func IsSet(key string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterAlias(alias string, key string) {
|
func RegisterAlias(alias string, key string) {
|
||||||
aliases[alias] = key
|
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
|
||||||
|
}
|
||||||
|
} 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[key] = value
|
||||||
defaults[newkey] = value
|
|
||||||
} else {
|
|
||||||
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
|
override[key] = value
|
||||||
} else {
|
|
||||||
override[key] = value
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadInConfig() {
|
func ReadInConfig() {
|
||||||
|
@ -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) {
|
||||||
|
|
|
@ -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")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue