schema_visitor.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package crd
  14. import (
  15. apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  16. )
  17. // SchemaVisitor walks the nodes of a schema.
  18. type SchemaVisitor interface {
  19. // Visit is called for each schema node. If it returns a visitor,
  20. // the visitor will be called on each direct child node, and then
  21. // this visitor will be called again with `nil` to indicate that
  22. // all children have been visited. If a nil visitor is returned,
  23. // children are not visited.
  24. //
  25. // It is *NOT* safe to save references to the given schema.
  26. // Make deepcopies if you need to keep things around beyond
  27. // the lifetime of the call.
  28. Visit(schema *apiext.JSONSchemaProps) SchemaVisitor
  29. }
  30. // EditSchema walks the given schema using the given visitor. Actual
  31. // pointers to each schema node are passed to the visitor, so any changes
  32. // made by the visitor will be reflected to the passed-in schema.
  33. func EditSchema(schema *apiext.JSONSchemaProps, visitor SchemaVisitor) {
  34. walker := schemaWalker{visitor: visitor}
  35. walker.walkSchema(schema)
  36. }
  37. // schemaWalker knows how to walk the schema, saving modifications
  38. // made by the given visitor.
  39. type schemaWalker struct {
  40. visitor SchemaVisitor
  41. }
  42. // walkSchema walks the given schema, saving modifications made by the visitor
  43. // (this is as simple as passing a pointer in most cases, but special care
  44. // needs to be taken to persist with maps). It also visits referenced
  45. // schemata, dealing with circular references appropriately. The returned
  46. // visitor will be used to visit all "children" of the current schema, followed
  47. // by a nil schema with the returned visitor to mark completion. If a nil visitor
  48. // is returned, traversal will no continue into the children of the current schema.
  49. func (w schemaWalker) walkSchema(schema *apiext.JSONSchemaProps) {
  50. // Walk a potential chain of schema references, keeping track of seen
  51. // references to avoid circular references
  52. subVisitor := w.visitor
  53. seenRefs := map[string]bool{}
  54. if schema.Ref != nil {
  55. seenRefs[*schema.Ref] = true
  56. }
  57. for {
  58. subVisitor = subVisitor.Visit(schema)
  59. if subVisitor == nil {
  60. return
  61. }
  62. // mark completion of the visitor
  63. defer subVisitor.Visit(nil)
  64. // Break if schema is not a reference or a cycle is detected
  65. if schema.Ref == nil || len(*schema.Ref) == 0 || seenRefs[*schema.Ref] {
  66. break
  67. }
  68. seenRefs[*schema.Ref] = true
  69. }
  70. // walk sub-schemata
  71. subWalker := schemaWalker{visitor: subVisitor}
  72. if schema.Items != nil {
  73. subWalker.walkPtr(schema.Items.Schema)
  74. subWalker.walkSlice(schema.Items.JSONSchemas)
  75. }
  76. subWalker.walkSlice(schema.AllOf)
  77. subWalker.walkSlice(schema.OneOf)
  78. subWalker.walkSlice(schema.AnyOf)
  79. subWalker.walkPtr(schema.Not)
  80. subWalker.walkMap(schema.Properties)
  81. if schema.AdditionalProperties != nil {
  82. subWalker.walkPtr(schema.AdditionalProperties.Schema)
  83. }
  84. subWalker.walkMap(schema.PatternProperties)
  85. for name, dep := range schema.Dependencies {
  86. subWalker.walkPtr(dep.Schema)
  87. schema.Dependencies[name] = dep
  88. }
  89. if schema.AdditionalItems != nil {
  90. subWalker.walkPtr(schema.AdditionalItems.Schema)
  91. }
  92. subWalker.walkMap(schema.Definitions)
  93. }
  94. // walkMap walks over values of the given map, saving changes to them.
  95. func (w schemaWalker) walkMap(defs map[string]apiext.JSONSchemaProps) {
  96. for name, def := range defs {
  97. // this is iter var reference is because we immediately preseve it below
  98. //nolint:gosec
  99. w.walkSchema(&def)
  100. // make sure the edits actually go through since we can't
  101. // take a reference to the value in the map
  102. defs[name] = def
  103. }
  104. }
  105. // walkSlice walks over items of the given slice.
  106. func (w schemaWalker) walkSlice(defs []apiext.JSONSchemaProps) {
  107. for i := range defs {
  108. w.walkSchema(&defs[i])
  109. }
  110. }
  111. // walkPtr walks over the contents of the given pointer, if it's not nil.
  112. func (w schemaWalker) walkPtr(def *apiext.JSONSchemaProps) {
  113. if def == nil {
  114. return
  115. }
  116. w.walkSchema(def)
  117. }