~rogpeppe/juju-core/438-local-instance-Addresses

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package formula

import (
	"io"
	"io/ioutil"
	"launchpad.net/ensemble/go/schema"
	"launchpad.net/goyaml"
	"os"
	"strconv"
)

// Option represents a single configuration option that is declared
// as supported by a formula in its config.yaml file.
type Option struct {
	Title       string
	Description string
	Type        string
	Default     interface{}
}

// Config represents the supported configuration options for a formula,
// as declared in its config.yaml file.
type Config struct {
	Options map[string]Option
}

// ReadConfig reads a config.yaml file and returns its representation.
func ReadConfig(r io.Reader) (config *Config, err os.Error) {
	data, err := ioutil.ReadAll(r)
	if err != nil {
		return
	}
	raw := make(map[interface{}]interface{})
	err = goyaml.Unmarshal(data, raw)
	if err != nil {
		return
	}
	v, err := configSchema.Coerce(raw, nil)
	if err != nil {
		return nil, os.NewError("config: " + err.String())
	}
	config = &Config{}
	config.Options = make(map[string]Option)
	m := v.(schema.MapType)
	for name, infov := range m["options"].(schema.MapType) {
		opt := infov.(schema.MapType)
		optTitle, _ := opt["title"].(string)
		optType, _ := opt["type"].(string)
		optDescr, _ := opt["description"].(string)
		optDefault, _ := opt["default"]
		config.Options[name.(string)] = Option{
			Title:       optTitle,
			Type:        optType,
			Description: optDescr,
			Default:     optDefault,
		}
	}
	return
}

// Validate processes the values in the input map according to the
// configuration in config, doing the following operations:
//
// - Values are converted from strings to the types defined
// - Options with default values are introduced for missing keys
// - Unknown keys and badly typed values are reported as errors
// 
func (c *Config) Validate(values map[string]string) (processed map[string]interface{}, err os.Error) {
	out := make(map[string]interface{})
	for k, v := range values {
		opt, ok := c.Options[k]
		if !ok {
			return nil, errorf("Unknown configuration option: %q", k)
		}
		switch opt.Type {
		case "string":
			out[k] = v
		case "int":
			i, err := strconv.Atoi64(v)
			if err != nil {
				return nil, errorf("Value for %q is not an int: %q", k, v)
			}
			out[k] = i
		case "float":
			f, err := strconv.Atof64(v)
			if err != nil {
				return nil, errorf("Value for %q is not a float: %q", k, v)
			}
			out[k] = f
		default:
			panic(errorf("Internal error: option type %q is unknown to Validate", opt.Type))
		}
	}
	for k, opt := range c.Options {
		if _, ok := out[k]; !ok && opt.Default != nil {
			out[k] = opt.Default
		}
	}
	return out, nil
}

var optionSchema = schema.FieldMap(
	schema.Fields{
		"type":        schema.OneOf(schema.Const("string"), schema.Const("int"), schema.Const("float")),
		"default":     schema.OneOf(schema.String(), schema.Int(), schema.Float()),
		"description": schema.String(),
	},
	schema.Optional{"default", "description"},
)

var configSchema = schema.FieldMap(
	schema.Fields{
		"options": schema.Map(schema.String(), optionSchema),
	},
	nil,
)