Colin Cross | 8e0c511 | 2015-01-23 14:15:10 -0800 | [diff] [blame] | 1 | // Copyright 2014 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 15 | package blueprint |
| 16 | |
| 17 | import ( |
| 18 | "blueprint/parser" |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 19 | "blueprint/proptools" |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 20 | "fmt" |
| 21 | "reflect" |
Colin Cross | 11a114f | 2014-12-17 16:46:09 -0800 | [diff] [blame] | 22 | "strings" |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | type packedProperty struct { |
| 26 | property *parser.Property |
| 27 | unpacked bool |
| 28 | } |
| 29 | |
| 30 | func unpackProperties(propertyDefs []*parser.Property, |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 31 | propertiesStructs ...interface{}) (map[string]*parser.Property, []error) { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 32 | |
| 33 | propertyMap := make(map[string]*packedProperty) |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 34 | errs := buildPropertyMap("", propertyDefs, propertyMap) |
| 35 | if len(errs) > 0 { |
| 36 | return nil, errs |
| 37 | } |
| 38 | |
| 39 | for _, properties := range propertiesStructs { |
| 40 | propertiesValue := reflect.ValueOf(properties) |
| 41 | if propertiesValue.Kind() != reflect.Ptr { |
| 42 | panic("properties must be a pointer to a struct") |
| 43 | } |
| 44 | |
| 45 | propertiesValue = propertiesValue.Elem() |
| 46 | if propertiesValue.Kind() != reflect.Struct { |
| 47 | panic("properties must be a pointer to a struct") |
| 48 | } |
| 49 | |
| 50 | newErrs := unpackStructValue("", propertiesValue, propertyMap) |
| 51 | errs = append(errs, newErrs...) |
| 52 | |
| 53 | if len(errs) >= maxErrors { |
| 54 | return nil, errs |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Report any properties that didn't have corresponding struct fields as |
| 59 | // errors. |
| 60 | result := make(map[string]*parser.Property) |
| 61 | for name, packedProperty := range propertyMap { |
| 62 | result[name] = packedProperty.property |
| 63 | if !packedProperty.unpacked { |
| 64 | err := &Error{ |
| 65 | Err: fmt.Errorf("unrecognized property %q", name), |
| 66 | Pos: packedProperty.property.Pos, |
| 67 | } |
| 68 | errs = append(errs, err) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if len(errs) > 0 { |
| 73 | return nil, errs |
| 74 | } |
| 75 | |
| 76 | return result, nil |
| 77 | } |
| 78 | |
| 79 | func buildPropertyMap(namePrefix string, propertyDefs []*parser.Property, |
| 80 | propertyMap map[string]*packedProperty) (errs []error) { |
| 81 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 82 | for _, propertyDef := range propertyDefs { |
Colin Cross | d1facc1 | 2015-01-08 14:56:03 -0800 | [diff] [blame] | 83 | name := namePrefix + propertyDef.Name.Name |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 84 | if first, present := propertyMap[name]; present { |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 85 | if first.property == propertyDef { |
| 86 | // We've already added this property. |
| 87 | continue |
| 88 | } |
| 89 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 90 | errs = append(errs, &Error{ |
| 91 | Err: fmt.Errorf("property %q already defined", name), |
| 92 | Pos: propertyDef.Pos, |
| 93 | }) |
| 94 | errs = append(errs, &Error{ |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 95 | Err: fmt.Errorf("<-- previous definition here"), |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 96 | Pos: first.property.Pos, |
| 97 | }) |
| 98 | if len(errs) >= maxErrors { |
| 99 | return errs |
| 100 | } |
| 101 | continue |
| 102 | } |
| 103 | |
| 104 | propertyMap[name] = &packedProperty{ |
| 105 | property: propertyDef, |
| 106 | unpacked: false, |
| 107 | } |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 108 | |
| 109 | // We intentionally do not rescursively add MapValue properties to the |
| 110 | // property map here. Instead we add them when we encounter a struct |
| 111 | // into which they can be unpacked. We do this so that if we never |
| 112 | // encounter such a struct then the "unrecognized property" error will |
| 113 | // be reported only once for the map property and not for each of its |
| 114 | // sub-properties. |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 117 | return |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 120 | func unpackStructValue(namePrefix string, structValue reflect.Value, |
Jamie Gennis | 1174c69 | 2014-10-05 07:41:44 -0700 | [diff] [blame] | 121 | propertyMap map[string]*packedProperty) []error { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 122 | |
| 123 | structType := structValue.Type() |
| 124 | |
| 125 | var errs []error |
| 126 | for i := 0; i < structValue.NumField(); i++ { |
| 127 | fieldValue := structValue.Field(i) |
| 128 | field := structType.Field(i) |
| 129 | |
Jamie Gennis | d4c53d8 | 2014-06-22 17:02:55 -0700 | [diff] [blame] | 130 | if field.PkgPath != "" { |
| 131 | // This is an unexported field, so just skip it. |
| 132 | continue |
| 133 | } |
| 134 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 135 | if !fieldValue.CanSet() { |
| 136 | panic(fmt.Errorf("field %s is not settable", field.Name)) |
| 137 | } |
| 138 | |
| 139 | // To make testing easier we validate the struct field's type regardless |
| 140 | // of whether or not the property was specified in the parsed string. |
| 141 | switch kind := fieldValue.Kind(); kind { |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 142 | case reflect.Bool, reflect.String, reflect.Struct: |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 143 | // Do nothing |
| 144 | case reflect.Slice: |
| 145 | elemType := field.Type.Elem() |
| 146 | if elemType.Kind() != reflect.String { |
| 147 | panic(fmt.Errorf("field %s is a non-string slice", field.Name)) |
| 148 | } |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 149 | case reflect.Interface: |
| 150 | if fieldValue.IsNil() { |
| 151 | panic(fmt.Errorf("field %s contains a nil interface", |
| 152 | field.Name)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 153 | } |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 154 | fieldValue = fieldValue.Elem() |
| 155 | elemType := fieldValue.Type() |
| 156 | if elemType.Kind() != reflect.Ptr { |
| 157 | panic(fmt.Errorf("field %s contains a non-pointer interface", |
| 158 | field.Name)) |
| 159 | } |
| 160 | fallthrough |
| 161 | case reflect.Ptr: |
| 162 | if fieldValue.IsNil() { |
| 163 | panic(fmt.Errorf("field %s contains a nil pointer", |
| 164 | field.Name)) |
| 165 | } |
| 166 | fieldValue = fieldValue.Elem() |
| 167 | elemType := fieldValue.Type() |
| 168 | if elemType.Kind() != reflect.Struct { |
| 169 | panic(fmt.Errorf("field %s contains a non-struct pointer", |
| 170 | field.Name)) |
| 171 | } |
Colin Cross | 11a114f | 2014-12-17 16:46:09 -0800 | [diff] [blame] | 172 | case reflect.Int, reflect.Uint: |
| 173 | if !hasTag(field, "blueprint", "mutated") { |
| 174 | panic(fmt.Errorf(`int field %s must be tagged blueprint:"mutated"`, field.Name)) |
| 175 | } |
| 176 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 177 | default: |
| 178 | panic(fmt.Errorf("unsupported kind for field %s: %s", |
| 179 | field.Name, kind)) |
| 180 | } |
| 181 | |
| 182 | // Get the property value if it was specified. |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 183 | propertyName := namePrefix + proptools.PropertyNameForField(field.Name) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 184 | packedProperty, ok := propertyMap[propertyName] |
| 185 | if !ok { |
| 186 | // This property wasn't specified. |
| 187 | continue |
| 188 | } |
| 189 | |
Colin Cross | 11a114f | 2014-12-17 16:46:09 -0800 | [diff] [blame] | 190 | var newErrs []error |
| 191 | |
| 192 | if hasTag(field, "blueprint", "mutated") { |
| 193 | errs = append(errs, |
| 194 | fmt.Errorf("mutated field %s cannot be set in a Blueprint file", propertyName)) |
| 195 | if len(errs) >= maxErrors { |
| 196 | return errs |
| 197 | } |
| 198 | continue |
| 199 | } |
| 200 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 201 | packedProperty.unpacked = true |
| 202 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 203 | switch kind := fieldValue.Kind(); kind { |
| 204 | case reflect.Bool: |
Jamie Gennis | 1174c69 | 2014-10-05 07:41:44 -0700 | [diff] [blame] | 205 | newErrs = unpackBool(fieldValue, packedProperty.property) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 206 | case reflect.String: |
Jamie Gennis | 1174c69 | 2014-10-05 07:41:44 -0700 | [diff] [blame] | 207 | newErrs = unpackString(fieldValue, packedProperty.property) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 208 | case reflect.Slice: |
Jamie Gennis | 1174c69 | 2014-10-05 07:41:44 -0700 | [diff] [blame] | 209 | newErrs = unpackSlice(fieldValue, packedProperty.property) |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 210 | case reflect.Struct: |
| 211 | newErrs = unpackStruct(propertyName+".", fieldValue, |
| 212 | packedProperty.property, propertyMap) |
| 213 | case reflect.Ptr, reflect.Interface: |
| 214 | structValue := fieldValue.Elem() |
| 215 | newErrs = unpackStruct(propertyName+".", structValue, |
| 216 | packedProperty.property, propertyMap) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 217 | } |
| 218 | errs = append(errs, newErrs...) |
| 219 | if len(errs) >= maxErrors { |
| 220 | return errs |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return errs |
| 225 | } |
| 226 | |
Jamie Gennis | 1174c69 | 2014-10-05 07:41:44 -0700 | [diff] [blame] | 227 | func unpackBool(boolValue reflect.Value, property *parser.Property) []error { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 228 | if property.Value.Type != parser.Bool { |
| 229 | return []error{ |
| 230 | fmt.Errorf("%s: can't assign %s value to %s property %q", |
| 231 | property.Value.Pos, property.Value.Type, parser.Bool, |
| 232 | property.Name), |
| 233 | } |
| 234 | } |
Jamie Gennis | 1174c69 | 2014-10-05 07:41:44 -0700 | [diff] [blame] | 235 | boolValue.SetBool(property.Value.BoolValue) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 236 | return nil |
| 237 | } |
| 238 | |
| 239 | func unpackString(stringValue reflect.Value, |
Jamie Gennis | 1174c69 | 2014-10-05 07:41:44 -0700 | [diff] [blame] | 240 | property *parser.Property) []error { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 241 | |
| 242 | if property.Value.Type != parser.String { |
| 243 | return []error{ |
| 244 | fmt.Errorf("%s: can't assign %s value to %s property %q", |
| 245 | property.Value.Pos, property.Value.Type, parser.String, |
| 246 | property.Name), |
| 247 | } |
| 248 | } |
Jamie Gennis | 1174c69 | 2014-10-05 07:41:44 -0700 | [diff] [blame] | 249 | stringValue.SetString(property.Value.StringValue) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 250 | return nil |
| 251 | } |
| 252 | |
Jamie Gennis | 1174c69 | 2014-10-05 07:41:44 -0700 | [diff] [blame] | 253 | func unpackSlice(sliceValue reflect.Value, property *parser.Property) []error { |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 254 | if property.Value.Type != parser.List { |
| 255 | return []error{ |
| 256 | fmt.Errorf("%s: can't assign %s value to %s property %q", |
| 257 | property.Value.Pos, property.Value.Type, parser.List, |
| 258 | property.Name), |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | var list []string |
| 263 | for _, value := range property.Value.ListValue { |
| 264 | if value.Type != parser.String { |
| 265 | // The parser should not produce this. |
| 266 | panic("non-string value found in list") |
| 267 | } |
| 268 | list = append(list, value.StringValue) |
| 269 | } |
| 270 | |
Jamie Gennis | 1174c69 | 2014-10-05 07:41:44 -0700 | [diff] [blame] | 271 | sliceValue.Set(reflect.ValueOf(list)) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 272 | return nil |
| 273 | } |
| 274 | |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 275 | func unpackStruct(namePrefix string, structValue reflect.Value, |
| 276 | property *parser.Property, |
| 277 | propertyMap map[string]*packedProperty) []error { |
| 278 | |
| 279 | if property.Value.Type != parser.Map { |
| 280 | return []error{ |
| 281 | fmt.Errorf("%s: can't assign %s value to %s property %q", |
| 282 | property.Value.Pos, property.Value.Type, parser.Map, |
| 283 | property.Name), |
| 284 | } |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 285 | } |
Jamie Gennis | 8762292 | 2014-09-30 11:38:25 -0700 | [diff] [blame] | 286 | |
| 287 | errs := buildPropertyMap(namePrefix, property.Value.MapValue, propertyMap) |
| 288 | if len(errs) > 0 { |
| 289 | return errs |
| 290 | } |
| 291 | |
| 292 | return unpackStructValue(namePrefix, structValue, propertyMap) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 293 | } |
Colin Cross | 11a114f | 2014-12-17 16:46:09 -0800 | [diff] [blame] | 294 | |
| 295 | func hasTag(field reflect.StructField, name, value string) bool { |
| 296 | tag := field.Tag.Get(name) |
| 297 | for _, entry := range strings.Split(tag, ",") { |
| 298 | if entry == value { |
| 299 | return true |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | return false |
| 304 | } |