10 "github.com/mitchellh/mapstructure"
13 // MapFieldWriter writes data into a single map[string]string structure.
14 type MapFieldWriter struct {
15 Schema map[string]*Schema
18 result map[string]string
21 // Map returns the underlying map that is being written to.
22 func (w *MapFieldWriter) Map() map[string]string {
26 w.result = make(map[string]string)
32 func (w *MapFieldWriter) unsafeWriteField(addr string, value string) {
36 w.result = make(map[string]string)
39 w.result[addr] = value
42 // clearTree clears a field and any sub-fields of the given address out of the
43 // map. This should be used to reset some kind of complex structures (namely
44 // sets) before writing to make sure that any conflicting data is removed (for
45 // example, if the set was previously written to the writer's layer).
46 func (w *MapFieldWriter) clearTree(addr []string) {
47 prefix := strings.Join(addr, ".") + "."
48 for k := range w.result {
49 if strings.HasPrefix(k, prefix) {
55 func (w *MapFieldWriter) WriteField(addr []string, value interface{}) error {
59 w.result = make(map[string]string)
62 schemaList := addrToSchema(addr, w.Schema)
63 if len(schemaList) == 0 {
64 return fmt.Errorf("Invalid address to set: %#v", addr)
67 // If we're setting anything other than a list root or set root,
69 for _, schema := range schemaList[:len(schemaList)-1] {
70 if schema.Type == TypeList {
72 "%s: can only set full list",
73 strings.Join(addr, "."))
76 if schema.Type == TypeMap {
78 "%s: can only set full map",
79 strings.Join(addr, "."))
82 if schema.Type == TypeSet {
84 "%s: can only set full set",
85 strings.Join(addr, "."))
89 return w.set(addr, value)
92 func (w *MapFieldWriter) set(addr []string, value interface{}) error {
93 schemaList := addrToSchema(addr, w.Schema)
94 if len(schemaList) == 0 {
95 return fmt.Errorf("Invalid address to set: %#v", addr)
98 schema := schemaList[len(schemaList)-1]
100 case TypeBool, TypeInt, TypeFloat, TypeString:
101 return w.setPrimitive(addr, value, schema)
103 return w.setList(addr, value, schema)
105 return w.setMap(addr, value, schema)
107 return w.setSet(addr, value, schema)
109 return w.setObject(addr, value, schema)
111 panic(fmt.Sprintf("Unknown type: %#v", schema.Type))
115 func (w *MapFieldWriter) setList(
118 schema *Schema) error {
119 k := strings.Join(addr, ".")
120 setElement := func(idx string, value interface{}) error {
121 addrCopy := make([]string, len(addr), len(addr)+1)
123 return w.set(append(addrCopy, idx), value)
127 if err := mapstructure.Decode(v, &vs); err != nil {
128 return fmt.Errorf("%s: %s", k, err)
131 // Wipe the set from the current writer prior to writing if it exists.
132 // Multiple writes to the same layer is a lot safer for lists than sets due
133 // to the fact that indexes are always deterministic and the length will
134 // always be updated with the current length on the last write, but making
135 // sure we have a clean namespace removes any chance for edge cases to pop up
136 // and ensures that the last write to the set is the correct value.
139 // Set the entire list.
141 for i, elem := range vs {
142 is := strconv.FormatInt(int64(i), 10)
143 err = setElement(is, elem)
149 for i, _ := range vs {
150 is := strconv.FormatInt(int64(i), 10)
157 w.result[k+".#"] = strconv.FormatInt(int64(len(vs)), 10)
161 func (w *MapFieldWriter) setMap(
164 schema *Schema) error {
165 k := strings.Join(addr, ".")
166 v := reflect.ValueOf(value)
167 vs := make(map[string]interface{})
170 // The empty string here means the map is removed.
175 if v.Kind() != reflect.Map {
176 return fmt.Errorf("%s: must be a map", k)
178 if v.Type().Key().Kind() != reflect.String {
179 return fmt.Errorf("%s: keys must strings", k)
181 for _, mk := range v.MapKeys() {
183 vs[mk.String()] = mv.Interface()
186 // Wipe this address tree. The contents of the map should always reflect the
187 // last write made to it.
190 // Remove the pure key since we're setting the full map value
194 addrCopy := make([]string, len(addr), len(addr)+1)
196 for subKey, v := range vs {
197 if err := w.set(append(addrCopy, subKey), v); err != nil {
203 w.result[k+".%"] = strconv.Itoa(len(vs))
208 func (w *MapFieldWriter) setObject(
211 schema *Schema) error {
212 // Set the entire object. First decode into a proper structure
213 var v map[string]interface{}
214 if err := mapstructure.Decode(value, &v); err != nil {
215 return fmt.Errorf("%s: %s", strings.Join(addr, "."), err)
218 // Make space for additional elements in the address
219 addrCopy := make([]string, len(addr), len(addr)+1)
222 // Set each element in turn
224 for k1, v1 := range v {
225 if err = w.set(append(addrCopy, k1), v1); err != nil {
230 for k1, _ := range v {
231 w.set(append(addrCopy, k1), nil)
238 func (w *MapFieldWriter) setPrimitive(
241 schema *Schema) error {
242 k := strings.Join(addr, ".")
245 // The empty string here means the value is removed.
254 if err := mapstructure.Decode(v, &b); err != nil {
255 return fmt.Errorf("%s: %s", k, err)
258 set = strconv.FormatBool(b)
260 if err := mapstructure.Decode(v, &set); err != nil {
261 return fmt.Errorf("%s: %s", k, err)
265 if err := mapstructure.Decode(v, &n); err != nil {
266 return fmt.Errorf("%s: %s", k, err)
268 set = strconv.FormatInt(int64(n), 10)
271 if err := mapstructure.Decode(v, &n); err != nil {
272 return fmt.Errorf("%s: %s", k, err)
274 set = strconv.FormatFloat(float64(n), 'G', -1, 64)
276 return fmt.Errorf("Unknown type: %#v", schema.Type)
283 func (w *MapFieldWriter) setSet(
286 schema *Schema) error {
287 addrCopy := make([]string, len(addr), len(addr)+1)
289 k := strings.Join(addr, ".")
292 w.result[k+".#"] = "0"
296 // If it is a slice, then we have to turn it into a *Set so that
297 // we get the proper order back based on the hash code.
298 if v := reflect.ValueOf(value); v.Kind() == reflect.Slice {
299 // Build a temp *ResourceData to use for the conversion
300 tempAddr := addr[len(addr)-1:]
301 tempSchema := *schema
302 tempSchema.Type = TypeList
303 tempSchemaMap := map[string]*Schema{tempAddr[0]: &tempSchema}
304 tempW := &MapFieldWriter{Schema: tempSchemaMap}
306 // Set the entire list, this lets us get sane values out of it
307 if err := tempW.WriteField(tempAddr, value); err != nil {
311 // Build the set by going over the list items in order and
312 // hashing them into the set. The reason we go over the list and
313 // not the `value` directly is because this forces all types
314 // to become []interface{} (generic) instead of []string, which
315 // most hash functions are expecting.
316 s := schema.ZeroValue().(*Set)
317 tempR := &MapFieldReader{
318 Map: BasicMapReader(tempW.Map()),
319 Schema: tempSchemaMap,
321 for i := 0; i < v.Len(); i++ {
322 is := strconv.FormatInt(int64(i), 10)
323 result, err := tempR.ReadField(append(tempAddr, is))
328 panic("set item just set doesn't exist")
337 // Clear any keys that match the set address first. This is necessary because
338 // it's always possible and sometimes may be necessary to write to a certain
339 // writer layer more than once with different set data each time, which will
340 // lead to different keys being inserted, which can lead to determinism
341 // problems when the old data isn't wiped first.
344 if value.(*Set) == nil {
345 w.result[k+".#"] = "0"
349 for code, elem := range value.(*Set).m {
350 if err := w.set(append(addrCopy, code), elem); err != nil {
355 w.result[k+".#"] = strconv.Itoa(value.(*Set).Len())