Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(269)

Side by Side Diff: charm/config.go

Issue 7405049: charm: Add config.Convert() (Closed)
Patch Set: Created 12 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « [revision details] ('k') | charm/config_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 package charm 1 package charm
2 2
3 import ( 3 import (
4 "errors" 4 "errors"
5 "fmt" 5 "fmt"
6 "io" 6 "io"
7 "io/ioutil" 7 "io/ioutil"
8 "launchpad.net/goyaml" 8 "launchpad.net/goyaml"
9 "launchpad.net/juju-core/schema" 9 "launchpad.net/juju-core/schema"
10 "reflect" 10 "reflect"
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 } 110 }
111 } 111 }
112 for k, opt := range c.Options { 112 for k, opt := range c.Options {
113 if _, ok := out[k]; !ok && opt.Default != nil { 113 if _, ok := out[k]; !ok && opt.Default != nil {
114 out[k] = opt.Default 114 out[k] = opt.Default
115 } 115 }
116 } 116 }
117 return out, nil 117 return out, nil
118 } 118 }
119 119
120 // Convert converts the given config values such that they match the
121 // existing schema. Unknown values are silently ignored.
122 func (c *Config) Convert(values map[string]interface{}) (converted map[string]in terface{}, err error) {
rog 2013/02/26 18:15:10 no need to name the return params, i think.
dimitern 2013/02/26 18:24:57 Done.
123 converted = make(map[string]interface{})
124 for k, v := range values {
125 opt, ok := c.Options[k]
126 if !ok {
127 continue
128 }
129 if reflect.TypeOf(v).Kind() != validTypes[opt.Type] {
130 return nil, fmt.Errorf("cannot convert: type of %q has c hanged", k)
rog 2013/02/26 18:15:10 It might be nice to include a bit more information
dimitern 2013/02/26 18:24:57 Done.
131 }
132 converted[k] = v
133 }
134 return converted, nil
135 }
136
120 var validTypes = map[string]reflect.Kind{ 137 var validTypes = map[string]reflect.Kind{
121 "string": reflect.String, 138 "string": reflect.String,
122 "int": reflect.Int64, 139 "int": reflect.Int64,
123 "boolean": reflect.Bool, 140 "boolean": reflect.Bool,
124 "float": reflect.Float64, 141 "float": reflect.Float64,
125 } 142 }
126 143
127 var optionSchema = schema.FieldMap( 144 var optionSchema = schema.FieldMap(
128 schema.Fields{ 145 schema.Fields{
129 "type": schema.OneOf(schema.Const("string"), schema.Const ("int"), schema.Const("float"), schema.Const("boolean")), 146 "type": schema.OneOf(schema.Const("string"), schema.Const ("int"), schema.Const("float"), schema.Const("boolean")),
130 "default": schema.OneOf(schema.String(), schema.Int(), schem a.Float(), schema.Bool()), 147 "default": schema.OneOf(schema.String(), schema.Int(), schem a.Float(), schema.Bool()),
131 "description": schema.String(), 148 "description": schema.String(),
132 }, 149 },
133 schema.Defaults{ 150 schema.Defaults{
134 "default": schema.Omit, 151 "default": schema.Omit,
135 "description": schema.Omit, 152 "description": schema.Omit,
136 }, 153 },
137 ) 154 )
138 155
139 var configSchema = schema.FieldMap( 156 var configSchema = schema.FieldMap(
140 schema.Fields{ 157 schema.Fields{
141 "options": schema.StringMap(optionSchema), 158 "options": schema.StringMap(optionSchema),
142 }, 159 },
143 nil, 160 nil,
144 ) 161 )
OLDNEW
« no previous file with comments | « [revision details] ('k') | charm/config_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b