github.com/caseydavenport/controller-tools@v0.2.6-0.20200519183242-e8a18b1a6750/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
    48  // visitor (this is as simple as passing a pointer in most cases,
    49  // but special care needs to be taken to persist with maps).
    50  func (w schemaWalker) walkSchema(schema *apiext.JSONSchemaProps) {
    51  	subVisitor := w.visitor.Visit(schema)
    52  	if subVisitor == nil {
    53  		return
    54  	}
    55  	defer subVisitor.Visit(nil)
    56  
    57  	subWalker := schemaWalker{visitor: subVisitor}
    58  	if schema.Items != nil {
    59  		subWalker.walkPtr(schema.Items.Schema)
    60  		subWalker.walkSlice(schema.Items.JSONSchemas)
    61  	}
    62  	subWalker.walkSlice(schema.AllOf)
    63  	subWalker.walkSlice(schema.OneOf)
    64  	subWalker.walkSlice(schema.AnyOf)
    65  	subWalker.walkPtr(schema.Not)
    66  	subWalker.walkMap(schema.Properties)
    67  	if schema.AdditionalProperties != nil {
    68  		subWalker.walkPtr(schema.AdditionalProperties.Schema)
    69  	}
    70  	subWalker.walkMap(schema.PatternProperties)
    71  	for name, dep := range schema.Dependencies {
    72  		subWalker.walkPtr(dep.Schema)
    73  		schema.Dependencies[name] = dep
    74  	}
    75  	if schema.AdditionalItems != nil {
    76  		subWalker.walkPtr(schema.AdditionalItems.Schema)
    77  	}
    78  	subWalker.walkMap(schema.Definitions)
    79  }
    80  
    81  // walkMap walks over values of the given map, saving changes to them.
    82  func (w schemaWalker) walkMap(defs map[string]apiext.JSONSchemaProps) {
    83  	for name, def := range defs {
    84  		w.walkSchema(&def)
    85  		// make sure the edits actually go through since we can't
    86  		// take a reference to the value in the map
    87  		defs[name] = def
    88  	}
    89  }
    90  
    91  // walkSlice walks over items of the given slice.
    92  func (w schemaWalker) walkSlice(defs []apiext.JSONSchemaProps) {
    93  	for i := range defs {
    94  		w.walkSchema(&defs[i])
    95  	}
    96  }
    97  
    98  // walkPtr walks over the contents of the given pointer, if it's not nil.
    99  func (w schemaWalker) walkPtr(def *apiext.JSONSchemaProps) {
   100  	if def == nil {
   101  		return
   102  	}
   103  	w.walkSchema(def)
   104  }