build_release.go 15 KB

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