...

Source file src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/visitor.go

Documentation: k8s.io/apiextensions-apiserver/pkg/apiserver/schema

     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 schema
    18  
    19  // Visitor recursively walks through a structural schema.
    20  type Visitor struct {
    21  	// Structural is called on each Structural node in the schema, before recursing into
    22  	// the subtrees. It is allowed to mutate s. Return true if something has been changed.
    23  	// +optional
    24  	Structural func(s *Structural) bool
    25  	// NestedValueValidation is called on each NestedValueValidation node in the schema,
    26  	// before recursing into subtrees. It is allowed to mutate vv. Return true if something
    27  	// has been changed.
    28  	// +optional
    29  	NestedValueValidation func(vv *NestedValueValidation) bool
    30  }
    31  
    32  // Visit recursively walks through the structural schema and calls the given callbacks
    33  // at each node of those types.
    34  func (m *Visitor) Visit(s *Structural) {
    35  	m.visitStructural(s)
    36  }
    37  
    38  func (m *Visitor) visitStructural(s *Structural) bool {
    39  	ret := false
    40  	if m.Structural != nil {
    41  		ret = m.Structural(s)
    42  	}
    43  
    44  	if s.Items != nil {
    45  		m.visitStructural(s.Items)
    46  	}
    47  	for k, v := range s.Properties {
    48  		if changed := m.visitStructural(&v); changed {
    49  			ret = true
    50  			s.Properties[k] = v
    51  		}
    52  	}
    53  	if s.Generic.AdditionalProperties != nil && s.Generic.AdditionalProperties.Structural != nil {
    54  		m.visitStructural(s.Generic.AdditionalProperties.Structural)
    55  	}
    56  	if s.ValueValidation != nil {
    57  		for i := range s.ValueValidation.AllOf {
    58  			m.visitNestedValueValidation(&s.ValueValidation.AllOf[i])
    59  		}
    60  		for i := range s.ValueValidation.AnyOf {
    61  			m.visitNestedValueValidation(&s.ValueValidation.AnyOf[i])
    62  		}
    63  		for i := range s.ValueValidation.OneOf {
    64  			m.visitNestedValueValidation(&s.ValueValidation.OneOf[i])
    65  		}
    66  		if s.ValueValidation.Not != nil {
    67  			m.visitNestedValueValidation(s.ValueValidation.Not)
    68  		}
    69  	}
    70  
    71  	return ret
    72  }
    73  
    74  func (m *Visitor) visitNestedValueValidation(vv *NestedValueValidation) bool {
    75  	ret := false
    76  	if m.NestedValueValidation != nil {
    77  		ret = m.NestedValueValidation(vv)
    78  	}
    79  
    80  	if vv.Items != nil {
    81  		m.visitNestedValueValidation(vv.Items)
    82  	}
    83  	for k, v := range vv.Properties {
    84  		if changed := m.visitNestedValueValidation(&v); changed {
    85  			ret = true
    86  			vv.Properties[k] = v
    87  		}
    88  	}
    89  	if vv.ForbiddenGenerics.AdditionalProperties != nil && vv.ForbiddenGenerics.AdditionalProperties.Structural != nil {
    90  		m.visitStructural(vv.ForbiddenGenerics.AdditionalProperties.Structural)
    91  	}
    92  	for i := range vv.ValueValidation.AllOf {
    93  		m.visitNestedValueValidation(&vv.ValueValidation.AllOf[i])
    94  	}
    95  	for i := range vv.ValueValidation.AnyOf {
    96  		m.visitNestedValueValidation(&vv.ValueValidation.AnyOf[i])
    97  	}
    98  	for i := range vv.ValueValidation.OneOf {
    99  		m.visitNestedValueValidation(&vv.ValueValidation.OneOf[i])
   100  	}
   101  	if vv.ValueValidation.Not != nil {
   102  		m.visitNestedValueValidation(vv.ValueValidation.Not)
   103  	}
   104  
   105  	return ret
   106  }
   107  

View as plain text