jsonconfig: add optional object

This commit is contained in:
Brad Fitzpatrick 2011-05-09 14:17:30 -07:00
parent 73e4da3b08
commit ab2a1e1fb4
1 changed files with 15 additions and 0 deletions

View File

@ -39,6 +39,21 @@ func (jc Obj) RequiredObject(key string) Obj {
return Obj(m)
}
// Optional object doesn't take a default; it always returns an empty map.
func (jc Obj) OptionalObject(key string) Obj {
jc.noteKnownKey(key)
ei, ok := jc[key]
if !ok {
return make(Obj)
}
m, ok := ei.(map[string]interface{})
if !ok {
jc.appendError(fmt.Errorf("Expected config key %q to be an object, not %T", key, ei))
return make(Obj)
}
return Obj(m)
}
func (jc Obj) RequiredString(key string) string {
jc.noteKnownKey(key)
ei, ok := jc[key]