github.com/waynz0r/controller-tools@v0.4.1-0.20200916220028-16254aeef2d7/pkg/crd/schema_visitor.go (about)

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