merge upstream/master - viper - [fork] go viper port for 9front
(HTM) git clone git@git.drkhsh.at/viper.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 0562ef4c8bf927bbf1e77bf6321563f1282ec2da
(DIR) parent e55c96ecc93e092a32a7739d7542a4b29621cf5e
(HTM) Author: Brian Ketelsen <bketelsen@gmail.com>
Date: Tue, 28 Oct 2014 22:09:30 -0400
merge upstream/master
Diffstat:
M viper.go | 4 ++--
M viper_test.go | 72 ++++++++++++++++++++++++++++++-
2 files changed, 72 insertions(+), 4 deletions(-)
---
(DIR) diff --git a/viper.go b/viper.go
@@ -241,7 +241,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":
@@ -263,7 +263,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)
(DIR) diff --git 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"
)
@@ -25,6 +27,7 @@ clothing:
jacket: leather
trousers: denim
age: 35
+eyes : brown
beard: true
`)
@@ -57,6 +60,27 @@ var remoteExample = []byte(`{
"newkey":"remote"
}`)
+//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())
@@ -169,9 +193,9 @@ func TestEnv(t *testing.T) {
}
func TestAllKeys(t *testing.T) {
- ks := sort.StringSlice{"title", "newkey", "owner", "name", "beard", "ppu", "batters", "hobbies", "clothing", "age", "hacker", "id", "type"}
+ ks := sort.StringSlice{"title", "newkey", "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, "newkey": "remote", "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, "newkey": "remote", "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()
@@ -224,3 +248,47 @@ 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"))
+
+}
+
+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"))
+
+}