From 01b1780288d1dcf3451e48bce510daae1ff5033f Mon Sep 17 00:00:00 2001 From: Chris Hamant Date: Thu, 9 Oct 2014 15:48:55 -0400 Subject: [PATCH 1/2] adding simple test for BindPFlag --- viper_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/viper_test.go b/viper_test.go index 9818138..3a9a764 100644 --- a/viper_test.go +++ b/viper_test.go @@ -7,11 +7,13 @@ package viper import ( "bytes" + "fmt" "os" "sort" "testing" "time" + "github.com/spf13/pflag" "github.com/stretchr/testify/assert" ) @@ -51,6 +53,27 @@ var jsonExample = []byte(`{ } }`) +//stubs for PFlag Values +type stringValue string + +func newStringValue(val string, p *string) *stringValue { + *p = val + return (*stringValue)(p) +} + +func (s *stringValue) Set(val string) error { + *s = stringValue(val) + return nil +} + +func (s *stringValue) Type() string { + return "string" +} + +func (s *stringValue) String() string { + return fmt.Sprintf("%s", *s) +} + func TestBasics(t *testing.T) { SetConfigFile("/tmp/config.yaml") assert.Equal(t, "/tmp/config.yaml", getConfigFile()) @@ -202,3 +225,24 @@ func TestMarshal(t *testing.T) { } assert.Equal(t, &C, &config{Name: "Steve", Port: 1234}) } + +func TestBindPFlag(t *testing.T) { + var testString = "testing" + var testValue = newStringValue(testString, &testString) + + flag := &pflag.Flag{ + Name: "testflag", + Value: testValue, + Changed: false, + } + + BindPFlag("testvalue", flag) + + assert.Equal(t, testString, Get("testvalue")) + + flag.Value.Set("testing_mutate") + flag.Changed = true //hack for pflag usage + + assert.Equal(t, "testing_mutate", Get("testvalue")) + +} From 1b8adf4854c1b66a685cbcbd6ebb202cb832a1b7 Mon Sep 17 00:00:00 2001 From: Chris Hamant Date: Thu, 9 Oct 2014 16:39:24 -0400 Subject: [PATCH 2/2] fixing problem with case sensitivity with keys in env and flags maps --- viper.go | 4 ++-- viper_test.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/viper.go b/viper.go index e658a8e..720b759 100644 --- a/viper.go +++ b/viper.go @@ -147,7 +147,7 @@ func BindPFlag(key string, flag *pflag.Flag) (err error) { if flag == nil { return fmt.Errorf("flag for %q is nil", key) } - pflags[key] = flag + pflags[strings.ToLower(key)] = flag switch flag.Value.Type() { 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") } - key = input[0] + key = strings.ToLower(input[0]) if len(input) == 1 { envkey = strings.ToUpper(key) diff --git a/viper_test.go b/viper_test.go index 3a9a764..09d8c78 100644 --- a/viper_test.go +++ b/viper_test.go @@ -27,6 +27,7 @@ clothing: jacket: leather trousers: denim age: 35 +eyes : brown beard: true `) @@ -170,9 +171,9 @@ func TestEnv(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") - 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 allkeys = AllKeys() @@ -246,3 +247,26 @@ func TestBindPFlag(t *testing.T) { 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")) + +}