mirror of
https://github.com/spf13/viper
synced 2024-11-16 10:07:00 +00:00
fixing problem with case sensitivity with keys in env and flags maps
This commit is contained in:
parent
01b1780288
commit
1b8adf4854
2 changed files with 28 additions and 4 deletions
4
viper.go
4
viper.go
|
@ -147,7 +147,7 @@ func BindPFlag(key string, flag *pflag.Flag) (err error) {
|
||||||
if flag == nil {
|
if flag == nil {
|
||||||
return fmt.Errorf("flag for %q is nil", key)
|
return fmt.Errorf("flag for %q is nil", key)
|
||||||
}
|
}
|
||||||
pflags[key] = flag
|
pflags[strings.ToLower(key)] = flag
|
||||||
|
|
||||||
switch flag.Value.Type() {
|
switch flag.Value.Type() {
|
||||||
case "int", "int8", "int16", "int32", "int64":
|
case "int", "int8", "int16", "int32", "int64":
|
||||||
|
@ -169,7 +169,7 @@ func BindEnv(input ...string) (err error) {
|
||||||
return fmt.Errorf("BindEnv missing key to bind to")
|
return fmt.Errorf("BindEnv missing key to bind to")
|
||||||
}
|
}
|
||||||
|
|
||||||
key = input[0]
|
key = strings.ToLower(input[0])
|
||||||
|
|
||||||
if len(input) == 1 {
|
if len(input) == 1 {
|
||||||
envkey = strings.ToUpper(key)
|
envkey = strings.ToUpper(key)
|
||||||
|
|
|
@ -27,6 +27,7 @@ clothing:
|
||||||
jacket: leather
|
jacket: leather
|
||||||
trousers: denim
|
trousers: denim
|
||||||
age: 35
|
age: 35
|
||||||
|
eyes : brown
|
||||||
beard: true
|
beard: true
|
||||||
`)
|
`)
|
||||||
|
|
||||||
|
@ -170,9 +171,9 @@ func TestEnv(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAllKeys(t *testing.T) {
|
func TestAllKeys(t *testing.T) {
|
||||||
ks := sort.StringSlice{"title", "owner", "name", "beard", "ppu", "batters", "hobbies", "clothing", "age", "hacker", "id", "type"}
|
ks := sort.StringSlice{"title", "owner", "name", "beard", "ppu", "batters", "hobbies", "clothing", "age", "hacker", "id", "type", "eyes"}
|
||||||
dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
|
dob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
|
||||||
all := map[string]interface{}{"hacker": true, "beard": true, "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"}}}, "hobbies": []interface{}{"skateboarding", "snowboarding", "go"}, "ppu": 0.55, "clothing": map[interface{}]interface{}{"jacket": "leather", "trousers": "denim"}, "name": "crunk", "owner": map[string]interface{}{"organization": "MongoDB", "Bio": "MongoDB Chief Developer Advocate & Hacker at Large", "dob": dob}, "id": "13", "title": "TOML Example", "age": 35, "type": "donut"}
|
all := map[string]interface{}{"hacker": true, "beard": true, "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"}}}, "hobbies": []interface{}{"skateboarding", "snowboarding", "go"}, "ppu": 0.55, "clothing": map[interface{}]interface{}{"jacket": "leather", "trousers": "denim"}, "name": "crunk", "owner": map[string]interface{}{"organization": "MongoDB", "Bio": "MongoDB Chief Developer Advocate & Hacker at Large", "dob": dob}, "id": "13", "title": "TOML Example", "age": 35, "type": "donut", "eyes": "brown"}
|
||||||
|
|
||||||
var allkeys sort.StringSlice
|
var allkeys sort.StringSlice
|
||||||
allkeys = AllKeys()
|
allkeys = AllKeys()
|
||||||
|
@ -246,3 +247,26 @@ func TestBindPFlag(t *testing.T) {
|
||||||
assert.Equal(t, "testing_mutate", Get("testvalue"))
|
assert.Equal(t, "testing_mutate", Get("testvalue"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBoundCaseSensitivity(t *testing.T) {
|
||||||
|
|
||||||
|
assert.Equal(t, "brown", Get("eyes"))
|
||||||
|
|
||||||
|
BindEnv("eYEs", "TURTLE_EYES")
|
||||||
|
os.Setenv("TURTLE_EYES", "blue")
|
||||||
|
|
||||||
|
assert.Equal(t, "blue", Get("eyes"))
|
||||||
|
|
||||||
|
var testString = "green"
|
||||||
|
var testValue = newStringValue(testString, &testString)
|
||||||
|
|
||||||
|
flag := &pflag.Flag{
|
||||||
|
Name: "eyeballs",
|
||||||
|
Value: testValue,
|
||||||
|
Changed: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
BindPFlag("eYEs", flag)
|
||||||
|
assert.Equal(t, "green", Get("eyes"))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue