Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ func newConfig() *Config {
return result
}

func newConfigFromValue(v map[string]interface{}) *Config {
result := new(Config)
result.data = v
return result
}

// Loads config information from a JSON file
func LoadConfig(filename string) *Config {
result := newConfig()
Expand Down Expand Up @@ -126,3 +132,12 @@ func (c *Config) GetArray(key string) []interface{} {
}
return result.([]interface{})
}

// Returns an object value as Config
func (c *Config) GetConfig(key string) *Config {
v, exists := c.data[key]
if !exists {
return nil
}
return newConfigFromValue(v.(map[string]interface{}))
}
17 changes: 17 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,20 @@ func TestConfigMerge(t *testing.T) {
t.Errorf("expected vier, got %s:", c.GetString("four"))
}
}

func TestGetConfig(t *testing.T) {
c := LoadConfigString(`{"one":{"two":1,"three":"zwei"}}`)
if c == nil {
t.Fatalf("expected a config object")
}
subc := c.GetConfig("one")
if subc == nil {
t.Fatalf("expected sub-config object")
}
if subc.GetInt("two") != 1 {
t.Fatalf("expected int in sub-config")
}
if subc.GetString("three") != "zwei" {
t.Fatalf("expected string in sub-config")
}
}