merge upstream/master

This commit is contained in:
Brian Ketelsen 2014-10-28 22:09:30 -04:00
commit 0562ef4c8b
2 changed files with 72 additions and 4 deletions

View file

@ -241,7 +241,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":
@ -263,7 +263,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)

View file

@ -7,11 +7,13 @@ package viper
import ( import (
"bytes" "bytes"
"fmt"
"os" "os"
"sort" "sort"
"testing" "testing"
"time" "time"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -25,6 +27,7 @@ clothing:
jacket: leather jacket: leather
trousers: denim trousers: denim
age: 35 age: 35
eyes : brown
beard: true beard: true
`) `)
@ -57,6 +60,27 @@ var remoteExample = []byte(`{
"newkey":"remote" "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) { func TestBasics(t *testing.T) {
SetConfigFile("/tmp/config.yaml") SetConfigFile("/tmp/config.yaml")
assert.Equal(t, "/tmp/config.yaml", getConfigFile()) assert.Equal(t, "/tmp/config.yaml", getConfigFile())
@ -169,9 +193,9 @@ func TestEnv(t *testing.T) {
} }
func TestAllKeys(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") 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 var allkeys sort.StringSlice
allkeys = AllKeys() allkeys = AllKeys()
@ -224,3 +248,47 @@ func TestMarshal(t *testing.T) {
} }
assert.Equal(t, &C, &config{Name: "Steve", Port: 1234}) 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"))
}