1// Copyright (C) 2021 The Android Open Source Project 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 15package sdk 16 17import ( 18 "fmt" 19 "math" 20 "reflect" 21 "strings" 22) 23 24// Supports customizing sdk snapshot output based on target build release. 25 26// buildRelease represents the version of a build system used to create a specific release. 27// 28// The name of the release, is the same as the code for the dessert release, e.g. S, Tiramisu, etc. 29type buildRelease struct { 30 // The name of the release, e.g. S, Tiramisu, etc. 31 name string 32 33 // The index of this structure within the dessertBuildReleases list. 34 // 35 // The buildReleaseCurrent does not appear in the dessertBuildReleases list as it has an ordinal value 36 // that is larger than the size of the dessertBuildReleases. 37 ordinal int 38} 39 40func (br *buildRelease) EarlierThan(other *buildRelease) bool { 41 return br.ordinal < other.ordinal 42} 43 44// String returns the name of the build release. 45func (br *buildRelease) String() string { 46 return br.name 47} 48 49// buildReleaseSet represents a set of buildRelease objects. 50type buildReleaseSet struct { 51 // Set of *buildRelease represented as a map from *buildRelease to struct{}. 52 contents map[*buildRelease]struct{} 53} 54 55// addItem adds a build release to the set. 56func (s *buildReleaseSet) addItem(release *buildRelease) { 57 s.contents[release] = struct{}{} 58} 59 60// addRange adds all the build releases from start (inclusive) to end (inclusive). 61func (s *buildReleaseSet) addRange(start *buildRelease, end *buildRelease) { 62 for i := start.ordinal; i <= end.ordinal; i += 1 { 63 s.addItem(dessertBuildReleases[i]) 64 } 65} 66 67// contains returns true if the set contains the specified build release. 68func (s *buildReleaseSet) contains(release *buildRelease) bool { 69 _, ok := s.contents[release] 70 return ok 71} 72 73// String returns a string representation of the set, sorted from earliest to latest release. 74func (s *buildReleaseSet) String() string { 75 list := []string{} 76 addRelease := func(release *buildRelease) { 77 if _, ok := s.contents[release]; ok { 78 list = append(list, release.name) 79 } 80 } 81 // Add the names of the build releases in this set in the order in which they were created. 82 for _, release := range dessertBuildReleases { 83 addRelease(release) 84 } 85 // Always add "current" to the list of names last if it is present in the set. 86 addRelease(buildReleaseCurrent) 87 return fmt.Sprintf("[%s]", strings.Join(list, ",")) 88} 89 90var ( 91 // nameToBuildRelease contains a map from name to build release. 92 nameToBuildRelease = map[string]*buildRelease{} 93 94 // dessertBuildReleases lists all the available dessert build releases, i.e. excluding current. 95 dessertBuildReleases = []*buildRelease{} 96 97 // allBuildReleaseSet is the set of all build releases. 98 allBuildReleaseSet = &buildReleaseSet{contents: map[*buildRelease]struct{}{}} 99 100 // Add the dessert build releases from oldest to newest. 101 buildReleaseS = initBuildRelease("S") 102 buildReleaseT = initBuildRelease("Tiramisu") 103 buildReleaseU = initBuildRelease("UpsideDownCake") 104 buildReleaseV = initBuildRelease("VanillaIceCream") 105 buildReleaseB = initBuildRelease("Baklava") 106 107 // Add the current build release which is always treated as being more recent than any other 108 // build release, including those added in tests. 109 buildReleaseCurrent = initBuildRelease("current") 110) 111 112// initBuildRelease creates a new build release with the specified name. 113func initBuildRelease(name string) *buildRelease { 114 ordinal := len(dessertBuildReleases) 115 if name == "current" { 116 // The current build release is more recent than all other build releases, including those 117 // created in tests so use the max int value. It cannot just rely on being created after all 118 // the other build releases as some are created in tests which run after the current build 119 // release has been created. 120 ordinal = math.MaxInt 121 } 122 release := &buildRelease{name: name, ordinal: ordinal} 123 nameToBuildRelease[name] = release 124 allBuildReleaseSet.addItem(release) 125 if name != "current" { 126 // As the current build release has an ordinal value that does not correspond to its position 127 // in the dessertBuildReleases list do not add it to the list. 128 dessertBuildReleases = append(dessertBuildReleases, release) 129 } 130 return release 131} 132 133// latestDessertBuildRelease returns the latest dessert release build name, i.e. the last dessert 134// release added to the list, which does not include current. 135func latestDessertBuildRelease() *buildRelease { 136 return dessertBuildReleases[len(dessertBuildReleases)-1] 137} 138 139// nameToRelease maps from build release name to the corresponding build release (if it exists) or 140// the error if it does not. 141func nameToRelease(name string) (*buildRelease, error) { 142 if r, ok := nameToBuildRelease[name]; ok { 143 return r, nil 144 } 145 146 return nil, fmt.Errorf("unknown release %q, expected one of %s", name, allBuildReleaseSet) 147} 148 149// parseBuildReleaseSet parses a build release set string specification into a build release set. 150// 151// The specification consists of one of the following: 152// * a single build release name, e.g. S, T, etc. 153// * a closed range (inclusive to inclusive), e.g. S-T 154// * an open range, e.g. T+. 155// 156// This returns the set if the specification was valid or an error. 157func parseBuildReleaseSet(specification string) (*buildReleaseSet, error) { 158 set := &buildReleaseSet{contents: map[*buildRelease]struct{}{}} 159 160 if strings.HasSuffix(specification, "+") { 161 rangeStart := strings.TrimSuffix(specification, "+") 162 start, err := nameToRelease(rangeStart) 163 if err != nil { 164 return nil, err 165 } 166 end := latestDessertBuildRelease() 167 set.addRange(start, end) 168 // An open-ended range always includes the current release. 169 set.addItem(buildReleaseCurrent) 170 } else if strings.Contains(specification, "-") { 171 limits := strings.SplitN(specification, "-", 2) 172 start, err := nameToRelease(limits[0]) 173 if err != nil { 174 return nil, err 175 } 176 177 end, err := nameToRelease(limits[1]) 178 if err != nil { 179 return nil, err 180 } 181 182 if start.ordinal > end.ordinal { 183 return nil, fmt.Errorf("invalid closed range, start release %q is later than end release %q", start.name, end.name) 184 } 185 186 set.addRange(start, end) 187 } else { 188 release, err := nameToRelease(specification) 189 if err != nil { 190 return nil, err 191 } 192 set.addItem(release) 193 } 194 195 return set, nil 196} 197 198// Given a set of properties (struct value), set the value of a field within that struct (or one of 199// its embedded structs) to its zero value. 200type fieldPrunerFunc func(structValue reflect.Value) 201 202// A property that can be cleared by a propertyPruner. 203type prunerProperty struct { 204 // The name of the field for this property. It is a "."-separated path for fields in non-anonymous 205 // sub-structs. 206 name string 207 208 // Sets the associated field to its zero value. 209 prunerFunc fieldPrunerFunc 210} 211 212// propertyPruner provides support for pruning (i.e. setting to their zero value) properties from 213// a properties structure. 214type propertyPruner struct { 215 // The properties that the pruner will clear. 216 properties []prunerProperty 217} 218 219// gatherFields recursively processes the supplied structure and a nested structures, selecting the 220// fields that require pruning and populates the propertyPruner.properties with the information 221// needed to prune those fields. 222// 223// containingStructAccessor is a func that if given an object will return a field whose value is 224// of the supplied structType. It is nil on initial entry to this method but when this method is 225// called recursively on a field that is a nested structure containingStructAccessor is set to a 226// func that provides access to the field's value. 227// 228// namePrefix is the prefix to the fields that are being visited. It is "" on initial entry to this 229// method but when this method is called recursively on a field that is a nested structure 230// namePrefix is the result of appending the field name (plus a ".") to the previous name prefix. 231// Unless the field is anonymous in which case it is passed through unchanged. 232// 233// selector is a func that will select whether the supplied field requires pruning or not. If it 234// returns true then the field will be added to those to be pruned, otherwise it will not. 235func (p *propertyPruner) gatherFields(structType reflect.Type, containingStructAccessor fieldAccessorFunc, namePrefix string, selector fieldSelectorFunc) { 236 for f := 0; f < structType.NumField(); f++ { 237 field := structType.Field(f) 238 if field.PkgPath != "" { 239 // Ignore unexported fields. 240 continue 241 } 242 243 // Save a copy of the field index for use in the function. 244 fieldIndex := f 245 246 name := namePrefix + field.Name 247 248 fieldGetter := func(container reflect.Value) reflect.Value { 249 if containingStructAccessor != nil { 250 // This is an embedded structure so first access the field for the embedded 251 // structure. 252 container = containingStructAccessor(container) 253 } 254 255 // Skip through interface and pointer values to find the structure. 256 container = getStructValue(container) 257 258 defer func() { 259 if r := recover(); r != nil { 260 panic(fmt.Errorf("%s for fieldIndex %d of field %s of container %#v", r, fieldIndex, name, container.Interface())) 261 } 262 }() 263 264 // Return the field. 265 return container.Field(fieldIndex) 266 } 267 268 fieldType := field.Type 269 if selector(name, field) { 270 zeroValue := reflect.Zero(fieldType) 271 fieldPruner := func(container reflect.Value) { 272 if containingStructAccessor != nil { 273 // This is an embedded structure so first access the field for the embedded 274 // structure. 275 container = containingStructAccessor(container) 276 } 277 278 // Skip through interface and pointer values to find the structure. 279 container = getStructValue(container) 280 281 defer func() { 282 if r := recover(); r != nil { 283 panic(fmt.Errorf("%s\n\tfor field (index %d, name %s)", r, fieldIndex, name)) 284 } 285 }() 286 287 // Set the field. 288 container.Field(fieldIndex).Set(zeroValue) 289 } 290 291 property := prunerProperty{ 292 name, 293 fieldPruner, 294 } 295 p.properties = append(p.properties, property) 296 } else { 297 switch fieldType.Kind() { 298 case reflect.Struct: 299 // Gather fields from the nested or embedded structure. 300 var subNamePrefix string 301 if field.Anonymous { 302 subNamePrefix = namePrefix 303 } else { 304 subNamePrefix = name + "." 305 } 306 p.gatherFields(fieldType, fieldGetter, subNamePrefix, selector) 307 308 case reflect.Map: 309 // Get the type of the values stored in the map. 310 valueType := fieldType.Elem() 311 // Skip over * types. 312 if valueType.Kind() == reflect.Ptr { 313 valueType = valueType.Elem() 314 } 315 if valueType.Kind() == reflect.Struct { 316 // If this is not referenced by a pointer then it is an error as it is impossible to 317 // modify a struct that is stored directly as a value in a map. 318 if fieldType.Elem().Kind() != reflect.Ptr { 319 panic(fmt.Errorf("Cannot prune struct %s stored by value in map %s, map values must"+ 320 " be pointers to structs", 321 fieldType.Elem(), name)) 322 } 323 324 // Create a new pruner for the values of the map. 325 valuePruner := newPropertyPrunerForStructType(valueType, selector) 326 327 // Create a new fieldPruner that will iterate over all the items in the map and call the 328 // pruner on them. 329 fieldPruner := func(container reflect.Value) { 330 mapValue := fieldGetter(container) 331 332 for _, keyValue := range mapValue.MapKeys() { 333 itemValue := mapValue.MapIndex(keyValue) 334 335 defer func() { 336 if r := recover(); r != nil { 337 panic(fmt.Errorf("%s\n\tfor key %q", r, keyValue)) 338 } 339 }() 340 341 valuePruner.pruneProperties(itemValue.Interface()) 342 } 343 } 344 345 // Add the map field pruner to the list of property pruners. 346 property := prunerProperty{ 347 name + "[*]", 348 fieldPruner, 349 } 350 p.properties = append(p.properties, property) 351 } 352 } 353 } 354 } 355} 356 357// pruneProperties will prune (set to zero value) any properties in the struct referenced by the 358// supplied struct pointer. 359// 360// The struct must be of the same type as was originally passed to newPropertyPruner to create this 361// propertyPruner. 362func (p *propertyPruner) pruneProperties(propertiesStruct interface{}) { 363 364 defer func() { 365 if r := recover(); r != nil { 366 panic(fmt.Errorf("%s\n\tof container %#v", r, propertiesStruct)) 367 } 368 }() 369 370 structValue := reflect.ValueOf(propertiesStruct) 371 for _, property := range p.properties { 372 property.prunerFunc(structValue) 373 } 374} 375 376// fieldSelectorFunc is called to select whether a specific field should be pruned or not. 377// name is the name of the field, including any prefixes from containing str 378type fieldSelectorFunc func(name string, field reflect.StructField) bool 379 380// newPropertyPruner creates a new property pruner for the structure type for the supplied 381// properties struct. 382// 383// The returned pruner can be used on any properties structure of the same type as the supplied set 384// of properties. 385func newPropertyPruner(propertiesStruct interface{}, selector fieldSelectorFunc) *propertyPruner { 386 structType := getStructValue(reflect.ValueOf(propertiesStruct)).Type() 387 return newPropertyPrunerForStructType(structType, selector) 388} 389 390// newPropertyPruner creates a new property pruner for the supplied properties struct type. 391// 392// The returned pruner can be used on any properties structure of the supplied type. 393func newPropertyPrunerForStructType(structType reflect.Type, selector fieldSelectorFunc) *propertyPruner { 394 pruner := &propertyPruner{} 395 pruner.gatherFields(structType, nil, "", selector) 396 return pruner 397} 398 399// newPropertyPrunerByBuildRelease creates a property pruner that will clear any properties in the 400// structure which are not supported by the specified target build release. 401// 402// A property is pruned if its field has a tag of the form: 403// 404// `supported_build_releases:"<build-release-set>"` 405// 406// and the resulting build release set does not contain the target build release. Properties that 407// have no such tag are assumed to be supported by all releases. 408func newPropertyPrunerByBuildRelease(propertiesStruct interface{}, targetBuildRelease *buildRelease) *propertyPruner { 409 return newPropertyPruner(propertiesStruct, func(name string, field reflect.StructField) bool { 410 if supportedBuildReleases, ok := field.Tag.Lookup("supported_build_releases"); ok { 411 set, err := parseBuildReleaseSet(supportedBuildReleases) 412 if err != nil { 413 panic(fmt.Errorf("invalid `supported_build_releases` tag on %s of %T: %s", name, propertiesStruct, err)) 414 } 415 416 // If the field does not support tha target release then prune it. 417 return !set.contains(targetBuildRelease) 418 419 } else { 420 // Any untagged fields are assumed to be supported by all build releases so should never be 421 // pruned. 422 return false 423 } 424 }) 425} 426