Adding AllKeys and AllSettings functions and tests

This commit is contained in:
spf13 2014-09-27 14:00:51 -07:00
parent 181a3b5f3b
commit aacc3049e2
2 changed files with 48 additions and 0 deletions

View File

@ -393,6 +393,38 @@ func insensativiseMap(m map[string]interface{}) {
}
}
func AllKeys() []string {
m := map[string]struct{}{}
for key, _ := range defaults {
m[key] = struct{}{}
}
for key, _ := range config {
m[key] = struct{}{}
}
for key, _ := range override {
m[key] = struct{}{}
}
a := []string{}
for x, _ := range m {
a = append(a, x)
}
return a
}
func AllSettings() map[string]interface{} {
m := map[string]interface{}{}
for _, x := range AllKeys() {
m[x] = Get(x)
}
return m
}
// Name for the config file.
// Does not include extension.
func SetConfigName(in string) {

View File

@ -8,7 +8,9 @@ package viper
import (
"bytes"
"os"
"sort"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
@ -138,6 +140,20 @@ func TestEnv(t *testing.T) {
assert.Equal(t, "apple", Get("f"))
}
func TestAllKeys(t *testing.T) {
ks := sort.StringSlice{"title", "owner", "name", "beard", "ppu", "batters", "hobbies", "clothing", "age", "hacker", "id", "type"}
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"}
var allkeys sort.StringSlice
allkeys = AllKeys()
allkeys.Sort()
ks.Sort()
assert.Equal(t, ks, allkeys)
assert.Equal(t, all, AllSettings())
}
func TestCaseInSensitive(t *testing.T) {
assert.Equal(t, true, Get("hacker"))
Set("Title", "Checking Case")