2011-04-16 05:22:30 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2011-05-23 04:20:54 +00:00
|
|
|
// Package jsonconfig defines a helper type for JSON objects to be
|
|
|
|
// used for configuration.
|
2011-04-16 05:22:30 +00:00
|
|
|
package jsonconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2011-05-23 04:20:54 +00:00
|
|
|
// Obj is a JSON configuration map.
|
2011-04-16 05:22:30 +00:00
|
|
|
type Obj map[string]interface{}
|
|
|
|
|
2011-05-09 18:49:02 +00:00
|
|
|
func (jc Obj) RequiredObject(key string) Obj {
|
2011-05-23 04:20:54 +00:00
|
|
|
return jc.obj(key, false)
|
2011-05-09 18:49:02 +00:00
|
|
|
}
|
|
|
|
|
2011-05-09 21:17:30 +00:00
|
|
|
func (jc Obj) OptionalObject(key string) Obj {
|
2011-05-23 04:20:54 +00:00
|
|
|
return jc.obj(key, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) obj(key string, optional bool) Obj {
|
2011-05-09 21:17:30 +00:00
|
|
|
jc.noteKnownKey(key)
|
|
|
|
ei, ok := jc[key]
|
|
|
|
if !ok {
|
2011-05-23 04:20:54 +00:00
|
|
|
if optional {
|
|
|
|
return make(Obj)
|
|
|
|
}
|
|
|
|
jc.appendError(fmt.Errorf("Missing required config key %q (object)", key))
|
2011-05-09 21:17:30 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2011-04-16 05:22:30 +00:00
|
|
|
func (jc Obj) RequiredString(key string) string {
|
2011-05-23 04:20:54 +00:00
|
|
|
return jc.string(key, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) OptionalString(key, def string) string {
|
|
|
|
return jc.string(key, &def)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) string(key string, def *string) string {
|
2011-04-16 05:22:30 +00:00
|
|
|
jc.noteKnownKey(key)
|
|
|
|
ei, ok := jc[key]
|
2011-05-09 18:49:02 +00:00
|
|
|
if !ok {
|
2011-05-23 04:20:54 +00:00
|
|
|
if def != nil {
|
|
|
|
return *def
|
|
|
|
}
|
2011-04-16 05:22:30 +00:00
|
|
|
jc.appendError(fmt.Errorf("Missing required config key %q (string)", key))
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
s, ok := ei.(string)
|
|
|
|
if !ok {
|
|
|
|
jc.appendError(fmt.Errorf("Expected config key %q to be a string", key))
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2011-05-23 04:20:54 +00:00
|
|
|
func (jc Obj) RequiredBool(key string) bool {
|
|
|
|
return jc.bool(key, nil)
|
2011-04-16 05:22:30 +00:00
|
|
|
}
|
|
|
|
|
2011-05-23 04:20:54 +00:00
|
|
|
func (jc Obj) OptionalBool(key string, def bool) bool {
|
|
|
|
return jc.bool(key, &def)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) bool(key string, def *bool) bool {
|
2011-04-16 05:22:30 +00:00
|
|
|
jc.noteKnownKey(key)
|
|
|
|
ei, ok := jc[key]
|
|
|
|
if !ok {
|
2011-05-23 04:20:54 +00:00
|
|
|
if def != nil {
|
|
|
|
return *def
|
|
|
|
}
|
2011-04-16 05:22:30 +00:00
|
|
|
jc.appendError(fmt.Errorf("Missing required config key %q (boolean)", key))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
b, ok := ei.(bool)
|
|
|
|
if !ok {
|
|
|
|
jc.appendError(fmt.Errorf("Expected config key %q to be a boolean", key))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2011-05-23 04:20:54 +00:00
|
|
|
func (jc Obj) RequiredInt(key string) int {
|
|
|
|
return jc.int(key, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) OptionalInt(key string, def int) int {
|
|
|
|
return jc.int(key, &def)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) int(key string, def *int) int {
|
2011-04-16 05:22:30 +00:00
|
|
|
jc.noteKnownKey(key)
|
|
|
|
ei, ok := jc[key]
|
|
|
|
if !ok {
|
2011-05-23 04:20:54 +00:00
|
|
|
if def != nil {
|
|
|
|
return *def
|
|
|
|
}
|
|
|
|
jc.appendError(fmt.Errorf("Missing required config key %q (integer)", key))
|
|
|
|
return 0
|
2011-04-16 05:22:30 +00:00
|
|
|
}
|
2011-05-23 04:20:54 +00:00
|
|
|
b, ok := ei.(float64)
|
2011-04-16 05:22:30 +00:00
|
|
|
if !ok {
|
2011-05-23 04:20:54 +00:00
|
|
|
jc.appendError(fmt.Errorf("Expected config key %q to be a number", key))
|
|
|
|
return 0
|
2011-04-16 05:22:30 +00:00
|
|
|
}
|
2011-05-23 04:20:54 +00:00
|
|
|
return int(b)
|
|
|
|
|
2011-04-16 05:22:30 +00:00
|
|
|
}
|
|
|
|
|
2011-05-21 16:26:20 +00:00
|
|
|
func (jc Obj) RequiredList(key string) []string {
|
|
|
|
jc.noteKnownKey(key)
|
|
|
|
ei, ok := jc[key]
|
|
|
|
if !ok {
|
|
|
|
jc.appendError(fmt.Errorf("Missing required config key %q (list of strings)", key))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
eil, ok := ei.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
jc.appendError(fmt.Errorf("Expected config key %q to be a list, not %T", key, ei))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sl := make([]string, len(eil))
|
|
|
|
for i, ei := range eil {
|
|
|
|
s, ok := ei.(string)
|
|
|
|
if !ok {
|
|
|
|
jc.appendError(fmt.Errorf("Expected config key %q index %d to be a string, not %T", key, i, ei))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sl[i] = s
|
|
|
|
}
|
|
|
|
return sl
|
|
|
|
}
|
|
|
|
|
2011-04-16 05:22:30 +00:00
|
|
|
func (jc Obj) noteKnownKey(key string) {
|
|
|
|
_, ok := jc["_knownkeys"]
|
|
|
|
if !ok {
|
|
|
|
jc["_knownkeys"] = make(map[string]bool)
|
|
|
|
}
|
|
|
|
jc["_knownkeys"].(map[string]bool)[key] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) appendError(err os.Error) {
|
|
|
|
ei, ok := jc["_errors"]
|
|
|
|
if ok {
|
|
|
|
jc["_errors"] = append(ei.([]os.Error), err)
|
|
|
|
} else {
|
|
|
|
jc["_errors"] = []os.Error{err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) lookForUnknownKeys() {
|
|
|
|
ei, ok := jc["_knownkeys"]
|
|
|
|
var known map[string]bool
|
|
|
|
if ok {
|
|
|
|
known = ei.(map[string]bool)
|
|
|
|
}
|
|
|
|
for k, _ := range jc {
|
|
|
|
if ok && known[k] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(k, "_") {
|
|
|
|
// Permit keys with a leading underscore as a
|
|
|
|
// form of comments.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
jc.appendError(fmt.Errorf("Unknown key %q", k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) Validate() os.Error {
|
|
|
|
jc.lookForUnknownKeys()
|
|
|
|
|
|
|
|
ei, ok := jc["_errors"]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
errList := ei.([]os.Error)
|
|
|
|
if len(errList) == 1 {
|
|
|
|
return errList[0]
|
|
|
|
}
|
|
|
|
strs := make([]string, 0)
|
|
|
|
for _, v := range errList {
|
|
|
|
strs = append(strs, v.String())
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Multiple errors: " + strings.Join(strs, ", "))
|
|
|
|
}
|