...

Source file src/k8s.io/kubectl/pkg/explain/recursive_fields_printer.go

Documentation: k8s.io/kubectl/pkg/explain

     1  /*
     2  Copyright 2017 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 explain
    18  
    19  import "k8s.io/kube-openapi/pkg/util/proto"
    20  
    21  // indentPerLevel is the level of indentation for each field recursion.
    22  const indentPerLevel = 3
    23  
    24  // recursiveFieldsPrinter recursively prints all the fields for a given
    25  // schema.
    26  type recursiveFieldsPrinter struct {
    27  	Writer *Formatter
    28  	Error  error
    29  }
    30  
    31  var _ proto.SchemaVisitor = &recursiveFieldsPrinter{}
    32  var _ fieldsPrinter = &recursiveFieldsPrinter{}
    33  var visitedReferences = map[string]struct{}{}
    34  
    35  // VisitArray is just a passthrough.
    36  func (f *recursiveFieldsPrinter) VisitArray(a *proto.Array) {
    37  	a.SubType.Accept(f)
    38  }
    39  
    40  // VisitKind prints all its fields with their type, and then recurses
    41  // inside each of these (pre-order).
    42  func (f *recursiveFieldsPrinter) VisitKind(k *proto.Kind) {
    43  	for _, key := range k.Keys() {
    44  		v := k.Fields[key]
    45  		f.Writer.Write("%s\t<%s>", key, GetTypeName(v))
    46  		subFields := &recursiveFieldsPrinter{
    47  			Writer: f.Writer.Indent(indentPerLevel),
    48  		}
    49  		if err := subFields.PrintFields(v); err != nil {
    50  			f.Error = err
    51  			return
    52  		}
    53  	}
    54  }
    55  
    56  // VisitMap is just a passthrough.
    57  func (f *recursiveFieldsPrinter) VisitMap(m *proto.Map) {
    58  	m.SubType.Accept(f)
    59  }
    60  
    61  // VisitPrimitive does nothing, since it doesn't have sub-fields.
    62  func (f *recursiveFieldsPrinter) VisitPrimitive(p *proto.Primitive) {
    63  	// Nothing to do.
    64  }
    65  
    66  // VisitReference is just a passthrough.
    67  func (f *recursiveFieldsPrinter) VisitReference(r proto.Reference) {
    68  	if _, ok := visitedReferences[r.Reference()]; ok {
    69  		return
    70  	}
    71  	visitedReferences[r.Reference()] = struct{}{}
    72  	r.SubSchema().Accept(f)
    73  	delete(visitedReferences, r.Reference())
    74  }
    75  
    76  // PrintFields will recursively print all the fields for the given
    77  // schema.
    78  func (f *recursiveFieldsPrinter) PrintFields(schema proto.Schema) error {
    79  	schema.Accept(f)
    80  	return f.Error
    81  }
    82  

View as plain text