From aacc3049e2e1b6d6f3288aa65ba6f78160596835 Mon Sep 17 00:00:00 2001 From: spf13 Date: Sat, 27 Sep 2014 14:00:51 -0700 Subject: [PATCH] Adding AllKeys and AllSettings functions and tests --- viper.go | 32 ++++++++++++++++++++++++++++++++ viper_test.go | 16 ++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/viper.go b/viper.go index c788770..c45abc8 100644 --- a/viper.go +++ b/viper.go @@ -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) { diff --git a/viper_test.go b/viper_test.go index 311c3ef..fba5a18 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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")