blob: ac57a32793ec3c7fb2fd214d0e512e912aaff451 [file] [log] [blame]
Paul Duffin18122942021-08-24 19:01:25 +01001// 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"
Paul Duffin42a49f12022-08-17 22:09:55 +000019 "math"
Paul Duffin0c3acbf2021-08-24 19:01:25 +010020 "reflect"
Paul Duffin18122942021-08-24 19:01:25 +010021 "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//
Paul Duffin13648912022-07-15 13:12:35 +000028// The name of the release, is the same as the code for the dessert release, e.g. S, Tiramisu, etc.
Paul Duffin18122942021-08-24 19:01:25 +010029type buildRelease struct {
Paul Duffin13648912022-07-15 13:12:35 +000030 // The name of the release, e.g. S, Tiramisu, etc.
Paul Duffin18122942021-08-24 19:01:25 +010031 name string
32
Paul Duffin42a49f12022-08-17 22:09:55 +000033 // 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.
Paul Duffin18122942021-08-24 19:01:25 +010037 ordinal int
38}
39
Paul Duffin13648912022-07-15 13:12:35 +000040func (br *buildRelease) EarlierThan(other *buildRelease) bool {
41 return br.ordinal < other.ordinal
42}
43
Paul Duffin18122942021-08-24 19:01:25 +010044// String returns the name of the build release.
Paul Duffin13648912022-07-15 13:12:35 +000045func (br *buildRelease) String() string {
46 return br.name
Paul Duffin18122942021-08-24 19:01:25 +010047}
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 {
Paul Duffin42a49f12022-08-17 22:09:55 +000063 s.addItem(dessertBuildReleases[i])
Paul Duffin18122942021-08-24 19:01:25 +010064 }
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{}
Paul Duffin42a49f12022-08-17 22:09:55 +000076 addRelease := func(release *buildRelease) {
Paul Duffin18122942021-08-24 19:01:25 +010077 if _, ok := s.contents[release]; ok {
78 list = append(list, release.name)
79 }
80 }
Paul Duffin42a49f12022-08-17 22:09:55 +000081 // 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)
Paul Duffin18122942021-08-24 19:01:25 +010087 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
Paul Duffin42a49f12022-08-17 22:09:55 +000094 // dessertBuildReleases lists all the available dessert build releases, i.e. excluding current.
95 dessertBuildReleases = []*buildRelease{}
Paul Duffin18122942021-08-24 19:01:25 +010096
97 // allBuildReleaseSet is the set of all build releases.
98 allBuildReleaseSet = &buildReleaseSet{contents: map[*buildRelease]struct{}{}}
99
Paul Duffin42a49f12022-08-17 22:09:55 +0000100 // Add the dessert build releases from oldest to newest.
Paul Duffin18122942021-08-24 19:01:25 +0100101 buildReleaseS = initBuildRelease("S")
Paul Duffine7babdb2022-02-10 13:06:54 +0000102 buildReleaseT = initBuildRelease("Tiramisu")
Paul Duffin42a49f12022-08-17 22:09:55 +0000103
104 // Add the current build release which is always treated as being more recent than any other
105 // build release, including those added in tests.
106 buildReleaseCurrent = initBuildRelease("current")
Paul Duffin18122942021-08-24 19:01:25 +0100107)
108
109// initBuildRelease creates a new build release with the specified name.
110func initBuildRelease(name string) *buildRelease {
Paul Duffin42a49f12022-08-17 22:09:55 +0000111 ordinal := len(dessertBuildReleases)
112 if name == "current" {
113 // The current build release is more recent than all other build releases, including those
114 // created in tests so use the max int value. It cannot just rely on being created after all
115 // the other build releases as some are created in tests which run after the current build
116 // release has been created.
117 ordinal = math.MaxInt
118 }
Paul Duffin18122942021-08-24 19:01:25 +0100119 release := &buildRelease{name: name, ordinal: ordinal}
120 nameToBuildRelease[name] = release
Paul Duffin18122942021-08-24 19:01:25 +0100121 allBuildReleaseSet.addItem(release)
Paul Duffin42a49f12022-08-17 22:09:55 +0000122 if name != "current" {
123 // As the current build release has an ordinal value that does not correspond to its position
124 // in the dessertBuildReleases list do not add it to the list.
125 dessertBuildReleases = append(dessertBuildReleases, release)
126 }
Paul Duffin18122942021-08-24 19:01:25 +0100127 return release
128}
129
Paul Duffin42a49f12022-08-17 22:09:55 +0000130// latestDessertBuildRelease returns the latest dessert release build name, i.e. the last dessert
131// release added to the list, which does not include current.
132func latestDessertBuildRelease() *buildRelease {
133 return dessertBuildReleases[len(dessertBuildReleases)-1]
Paul Duffin18122942021-08-24 19:01:25 +0100134}
135
136// nameToRelease maps from build release name to the corresponding build release (if it exists) or
137// the error if it does not.
138func nameToRelease(name string) (*buildRelease, error) {
139 if r, ok := nameToBuildRelease[name]; ok {
140 return r, nil
141 }
142
143 return nil, fmt.Errorf("unknown release %q, expected one of %s", name, allBuildReleaseSet)
144}
145
146// parseBuildReleaseSet parses a build release set string specification into a build release set.
147//
148// The specification consists of one of the following:
149// * a single build release name, e.g. S, T, etc.
150// * a closed range (inclusive to inclusive), e.g. S-T
151// * an open range, e.g. T+.
152//
153// This returns the set if the specification was valid or an error.
154func parseBuildReleaseSet(specification string) (*buildReleaseSet, error) {
155 set := &buildReleaseSet{contents: map[*buildRelease]struct{}{}}
156
157 if strings.HasSuffix(specification, "+") {
158 rangeStart := strings.TrimSuffix(specification, "+")
159 start, err := nameToRelease(rangeStart)
160 if err != nil {
161 return nil, err
162 }
Paul Duffin42a49f12022-08-17 22:09:55 +0000163 end := latestDessertBuildRelease()
Paul Duffin18122942021-08-24 19:01:25 +0100164 set.addRange(start, end)
Paul Duffin42a49f12022-08-17 22:09:55 +0000165 // An open-ended range always includes the current release.
166 set.addItem(buildReleaseCurrent)
Paul Duffin18122942021-08-24 19:01:25 +0100167 } else if strings.Contains(specification, "-") {
168 limits := strings.SplitN(specification, "-", 2)
169 start, err := nameToRelease(limits[0])
170 if err != nil {
171 return nil, err
172 }
173
174 end, err := nameToRelease(limits[1])
175 if err != nil {
176 return nil, err
177 }
178
179 if start.ordinal > end.ordinal {
180 return nil, fmt.Errorf("invalid closed range, start release %q is later than end release %q", start.name, end.name)
181 }
182
183 set.addRange(start, end)
184 } else {
185 release, err := nameToRelease(specification)
186 if err != nil {
187 return nil, err
188 }
189 set.addItem(release)
190 }
191
192 return set, nil
193}
Paul Duffin0c3acbf2021-08-24 19:01:25 +0100194
195// Given a set of properties (struct value), set the value of a field within that struct (or one of
196// its embedded structs) to its zero value.
197type fieldPrunerFunc func(structValue reflect.Value)
198
199// A property that can be cleared by a propertyPruner.
200type prunerProperty struct {
201 // The name of the field for this property. It is a "."-separated path for fields in non-anonymous
202 // sub-structs.
203 name string
204
205 // Sets the associated field to its zero value.
206 prunerFunc fieldPrunerFunc
207}
208
209// propertyPruner provides support for pruning (i.e. setting to their zero value) properties from
210// a properties structure.
211type propertyPruner struct {
212 // The properties that the pruner will clear.
213 properties []prunerProperty
214}
215
216// gatherFields recursively processes the supplied structure and a nested structures, selecting the
217// fields that require pruning and populates the propertyPruner.properties with the information
218// needed to prune those fields.
219//
220// containingStructAccessor is a func that if given an object will return a field whose value is
221// of the supplied structType. It is nil on initial entry to this method but when this method is
222// called recursively on a field that is a nested structure containingStructAccessor is set to a
223// func that provides access to the field's value.
224//
225// namePrefix is the prefix to the fields that are being visited. It is "" on initial entry to this
226// method but when this method is called recursively on a field that is a nested structure
227// namePrefix is the result of appending the field name (plus a ".") to the previous name prefix.
228// Unless the field is anonymous in which case it is passed through unchanged.
229//
230// selector is a func that will select whether the supplied field requires pruning or not. If it
231// returns true then the field will be added to those to be pruned, otherwise it will not.
232func (p *propertyPruner) gatherFields(structType reflect.Type, containingStructAccessor fieldAccessorFunc, namePrefix string, selector fieldSelectorFunc) {
233 for f := 0; f < structType.NumField(); f++ {
234 field := structType.Field(f)
235 if field.PkgPath != "" {
236 // Ignore unexported fields.
237 continue
238 }
239
240 // Save a copy of the field index for use in the function.
241 fieldIndex := f
242
243 name := namePrefix + field.Name
244
245 fieldGetter := func(container reflect.Value) reflect.Value {
246 if containingStructAccessor != nil {
247 // This is an embedded structure so first access the field for the embedded
248 // structure.
249 container = containingStructAccessor(container)
250 }
251
252 // Skip through interface and pointer values to find the structure.
253 container = getStructValue(container)
254
255 defer func() {
256 if r := recover(); r != nil {
257 panic(fmt.Errorf("%s for fieldIndex %d of field %s of container %#v", r, fieldIndex, name, container.Interface()))
258 }
259 }()
260
261 // Return the field.
262 return container.Field(fieldIndex)
263 }
264
Paul Duffin545c5922022-01-27 16:51:51 +0000265 fieldType := field.Type
266 if selector(name, field) {
267 zeroValue := reflect.Zero(fieldType)
268 fieldPruner := func(container reflect.Value) {
269 if containingStructAccessor != nil {
270 // This is an embedded structure so first access the field for the embedded
271 // structure.
272 container = containingStructAccessor(container)
273 }
274
275 // Skip through interface and pointer values to find the structure.
276 container = getStructValue(container)
277
278 defer func() {
279 if r := recover(); r != nil {
280 panic(fmt.Errorf("%s\n\tfor field (index %d, name %s)", r, fieldIndex, name))
281 }
282 }()
283
284 // Set the field.
285 container.Field(fieldIndex).Set(zeroValue)
Paul Duffin0c3acbf2021-08-24 19:01:25 +0100286 }
287
Paul Duffin0c3acbf2021-08-24 19:01:25 +0100288 property := prunerProperty{
289 name,
290 fieldPruner,
291 }
292 p.properties = append(p.properties, property)
Paul Duffin545c5922022-01-27 16:51:51 +0000293 } else {
294 switch fieldType.Kind() {
295 case reflect.Struct:
296 // Gather fields from the nested or embedded structure.
297 var subNamePrefix string
298 if field.Anonymous {
299 subNamePrefix = namePrefix
300 } else {
301 subNamePrefix = name + "."
302 }
303 p.gatherFields(fieldType, fieldGetter, subNamePrefix, selector)
Paul Duffin106a3a42022-01-27 16:39:06 +0000304
305 case reflect.Map:
306 // Get the type of the values stored in the map.
307 valueType := fieldType.Elem()
308 // Skip over * types.
309 if valueType.Kind() == reflect.Ptr {
310 valueType = valueType.Elem()
311 }
312 if valueType.Kind() == reflect.Struct {
313 // If this is not referenced by a pointer then it is an error as it is impossible to
314 // modify a struct that is stored directly as a value in a map.
315 if fieldType.Elem().Kind() != reflect.Ptr {
316 panic(fmt.Errorf("Cannot prune struct %s stored by value in map %s, map values must"+
317 " be pointers to structs",
318 fieldType.Elem(), name))
319 }
320
321 // Create a new pruner for the values of the map.
322 valuePruner := newPropertyPrunerForStructType(valueType, selector)
323
324 // Create a new fieldPruner that will iterate over all the items in the map and call the
325 // pruner on them.
326 fieldPruner := func(container reflect.Value) {
327 mapValue := fieldGetter(container)
328
329 for _, keyValue := range mapValue.MapKeys() {
330 itemValue := mapValue.MapIndex(keyValue)
331
332 defer func() {
333 if r := recover(); r != nil {
334 panic(fmt.Errorf("%s\n\tfor key %q", r, keyValue))
335 }
336 }()
337
338 valuePruner.pruneProperties(itemValue.Interface())
339 }
340 }
341
342 // Add the map field pruner to the list of property pruners.
343 property := prunerProperty{
344 name + "[*]",
345 fieldPruner,
346 }
347 p.properties = append(p.properties, property)
348 }
Paul Duffin0c3acbf2021-08-24 19:01:25 +0100349 }
Paul Duffin0c3acbf2021-08-24 19:01:25 +0100350 }
351 }
352}
353
Paul Duffin545c5922022-01-27 16:51:51 +0000354// pruneProperties will prune (set to zero value) any properties in the struct referenced by the
355// supplied struct pointer.
Paul Duffin0c3acbf2021-08-24 19:01:25 +0100356//
357// The struct must be of the same type as was originally passed to newPropertyPruner to create this
358// propertyPruner.
359func (p *propertyPruner) pruneProperties(propertiesStruct interface{}) {
Paul Duffin545c5922022-01-27 16:51:51 +0000360
361 defer func() {
362 if r := recover(); r != nil {
363 panic(fmt.Errorf("%s\n\tof container %#v", r, propertiesStruct))
364 }
365 }()
366
Paul Duffin0c3acbf2021-08-24 19:01:25 +0100367 structValue := reflect.ValueOf(propertiesStruct)
368 for _, property := range p.properties {
369 property.prunerFunc(structValue)
370 }
371}
372
373// fieldSelectorFunc is called to select whether a specific field should be pruned or not.
374// name is the name of the field, including any prefixes from containing str
375type fieldSelectorFunc func(name string, field reflect.StructField) bool
376
377// newPropertyPruner creates a new property pruner for the structure type for the supplied
378// properties struct.
379//
380// The returned pruner can be used on any properties structure of the same type as the supplied set
381// of properties.
382func newPropertyPruner(propertiesStruct interface{}, selector fieldSelectorFunc) *propertyPruner {
383 structType := getStructValue(reflect.ValueOf(propertiesStruct)).Type()
Paul Duffin106a3a42022-01-27 16:39:06 +0000384 return newPropertyPrunerForStructType(structType, selector)
385}
386
387// newPropertyPruner creates a new property pruner for the supplied properties struct type.
388//
389// The returned pruner can be used on any properties structure of the supplied type.
390func newPropertyPrunerForStructType(structType reflect.Type, selector fieldSelectorFunc) *propertyPruner {
Paul Duffin0c3acbf2021-08-24 19:01:25 +0100391 pruner := &propertyPruner{}
392 pruner.gatherFields(structType, nil, "", selector)
393 return pruner
394}
395
396// newPropertyPrunerByBuildRelease creates a property pruner that will clear any properties in the
397// structure which are not supported by the specified target build release.
398//
399// A property is pruned if its field has a tag of the form:
Colin Crossd079e0b2022-08-16 10:27:33 -0700400//
401// `supported_build_releases:"<build-release-set>"`
402//
Paul Duffin0c3acbf2021-08-24 19:01:25 +0100403// and the resulting build release set does not contain the target build release. Properties that
404// have no such tag are assumed to be supported by all releases.
405func newPropertyPrunerByBuildRelease(propertiesStruct interface{}, targetBuildRelease *buildRelease) *propertyPruner {
406 return newPropertyPruner(propertiesStruct, func(name string, field reflect.StructField) bool {
407 if supportedBuildReleases, ok := field.Tag.Lookup("supported_build_releases"); ok {
408 set, err := parseBuildReleaseSet(supportedBuildReleases)
409 if err != nil {
410 panic(fmt.Errorf("invalid `supported_build_releases` tag on %s of %T: %s", name, propertiesStruct, err))
411 }
412
413 // If the field does not support tha target release then prune it.
414 return !set.contains(targetBuildRelease)
415
416 } else {
417 // Any untagged fields are assumed to be supported by all build releases so should never be
418 // pruned.
419 return false
420 }
421 })
422}