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"
|
2012-03-19 20:07:17 +00:00
|
|
|
"sort"
|
2013-08-23 22:10:56 +00:00
|
|
|
"strconv"
|
2011-04-16 05:22:30 +00:00
|
|
|
"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-06-24 20:02:51 +00:00
|
|
|
// Reads json config data from the specified open file, expanding
|
2013-08-23 22:10:56 +00:00
|
|
|
// all expressions
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func ReadFile(configPath string) (Obj, error) {
|
2012-04-13 21:36:30 +00:00
|
|
|
var c ConfigParser
|
|
|
|
return c.ReadFile(configPath)
|
2011-06-24 20:02:51 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2011-06-19 20:09:05 +00:00
|
|
|
ei, ok := jc[key]
|
2011-05-09 21:17:30 +00:00
|
|
|
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-06-19 20:09:05 +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-06-04 15:45:03 +00:00
|
|
|
func (jc Obj) RequiredStringOrObject(key string) interface{} {
|
|
|
|
return jc.stringOrObject(key, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) OptionalStringOrObject(key string) interface{} {
|
|
|
|
return jc.stringOrObject(key, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) stringOrObject(key string, required bool) interface{} {
|
|
|
|
jc.noteKnownKey(key)
|
2011-06-19 20:09:05 +00:00
|
|
|
ei, ok := jc[key]
|
2011-06-04 15:45:03 +00:00
|
|
|
if !ok {
|
|
|
|
if !required {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
jc.appendError(fmt.Errorf("Missing required config key %q (string or object)", key))
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if _, ok := ei.(map[string]interface{}); ok {
|
|
|
|
return ei
|
|
|
|
}
|
|
|
|
if _, ok := ei.(string); ok {
|
|
|
|
return ei
|
|
|
|
}
|
|
|
|
jc.appendError(fmt.Errorf("Expected config key %q to be a string or object", key))
|
2011-06-19 20:09:05 +00:00
|
|
|
return ""
|
2011-06-04 15:45:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2013-08-23 22:10:56 +00:00
|
|
|
switch v := ei.(type) {
|
|
|
|
case bool:
|
|
|
|
return v
|
|
|
|
case string:
|
|
|
|
b, err := strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
|
|
|
jc.appendError(fmt.Errorf("Config key %q has bad boolean format %q", key, v))
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
default:
|
2011-04-16 05:22:30 +00:00
|
|
|
jc.appendError(fmt.Errorf("Expected config key %q to be a boolean", key))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2012-10-19 17:26:15 +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
|
|
|
}
|
|
|
|
|
2014-05-15 19:11:24 +00:00
|
|
|
func (jc Obj) RequiredInt64(key string) int64 {
|
|
|
|
return jc.int64(key, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) OptionalInt64(key string, def int64) int64 {
|
|
|
|
return jc.int64(key, &def)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) int64(key string, def *int64) int64 {
|
|
|
|
jc.noteKnownKey(key)
|
|
|
|
ei, ok := jc[key]
|
|
|
|
if !ok {
|
|
|
|
if def != nil {
|
|
|
|
return *def
|
|
|
|
}
|
|
|
|
jc.appendError(fmt.Errorf("Missing required config key %q (integer)", key))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
b, ok := ei.(float64)
|
|
|
|
if !ok {
|
|
|
|
jc.appendError(fmt.Errorf("Expected config key %q to be a number", key))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return int64(b)
|
|
|
|
}
|
|
|
|
|
2011-05-21 16:26:20 +00:00
|
|
|
func (jc Obj) RequiredList(key string) []string {
|
2011-06-19 20:09:05 +00:00
|
|
|
return jc.requiredList(key, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) OptionalList(key string) []string {
|
|
|
|
return jc.requiredList(key, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (jc Obj) requiredList(key string, required bool) []string {
|
2011-05-21 16:26:20 +00:00
|
|
|
jc.noteKnownKey(key)
|
2011-06-19 20:09:05 +00:00
|
|
|
ei, ok := jc[key]
|
2011-05-21 16:26:20 +00:00
|
|
|
if !ok {
|
2011-06-19 20:09:05 +00:00
|
|
|
if required {
|
|
|
|
jc.appendError(fmt.Errorf("Missing required config key %q (list of strings)", key))
|
|
|
|
}
|
2011-05-21 16:26:20 +00:00
|
|
|
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 {
|
2011-06-19 20:09:05 +00:00
|
|
|
jc["_knownkeys"] = make(map[string]bool)
|
2011-04-16 05:22:30 +00:00
|
|
|
}
|
|
|
|
jc["_knownkeys"].(map[string]bool)[key] = true
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (jc Obj) appendError(err error) {
|
2011-04-16 05:22:30 +00:00
|
|
|
ei, ok := jc["_errors"]
|
|
|
|
if ok {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
jc["_errors"] = append(ei.([]error), err)
|
2011-04-16 05:22:30 +00:00
|
|
|
} else {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
jc["_errors"] = []error{err}
|
2011-04-16 05:22:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 16:52:52 +00:00
|
|
|
// UnknownKeys returns the keys from the config that have not yet been discovered by one of the RequiredT or OptionalT calls.
|
|
|
|
func (jc Obj) UnknownKeys() []string {
|
2011-04-16 05:22:30 +00:00
|
|
|
ei, ok := jc["_knownkeys"]
|
|
|
|
var known map[string]bool
|
|
|
|
if ok {
|
|
|
|
known = ei.(map[string]bool)
|
|
|
|
}
|
2012-03-19 20:07:17 +00:00
|
|
|
var unknown []string
|
2011-04-16 05:22:30 +00:00
|
|
|
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
|
|
|
|
}
|
2012-03-19 20:07:17 +00:00
|
|
|
unknown = append(unknown, k)
|
|
|
|
}
|
|
|
|
sort.Strings(unknown)
|
2013-12-10 16:52:52 +00:00
|
|
|
return unknown
|
2011-04-16 05:22:30 +00:00
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (jc Obj) Validate() error {
|
2013-12-10 16:52:52 +00:00
|
|
|
unknown := jc.UnknownKeys()
|
|
|
|
for _, k := range unknown {
|
|
|
|
jc.appendError(fmt.Errorf("Unknown key %q", k))
|
|
|
|
}
|
2011-04-16 05:22:30 +00:00
|
|
|
|
|
|
|
ei, ok := jc["_errors"]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
errList := ei.([]error)
|
2011-04-16 05:22:30 +00:00
|
|
|
if len(errList) == 1 {
|
|
|
|
return errList[0]
|
|
|
|
}
|
|
|
|
strs := make([]string, 0)
|
|
|
|
for _, v := range errList {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
strs = append(strs, v.Error())
|
2011-04-16 05:22:30 +00:00
|
|
|
}
|
|
|
|
return fmt.Errorf("Multiple errors: " + strings.Join(strs, ", "))
|
|
|
|
}
|