...

Source file src/sigs.k8s.io/kustomize/kyaml/yaml/internal/k8sgen/pkg/util/validation/field/path.go

Documentation: sigs.k8s.io/kustomize/kyaml/yaml/internal/k8sgen/pkg/util/validation/field

     1  // Code generated by k8scopy from k8s.io/apimachinery@v0.19.8; DO NOT EDIT.
     2  // File content copied from k8s.io/apimachinery@v0.19.8/pkg/util/validation/field/path.go
     3  
     4  /*
     5  Copyright 2015 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package field
    21  
    22  import (
    23  	"bytes"
    24  	"fmt"
    25  	"strconv"
    26  )
    27  
    28  // Path represents the path from some root to a particular field.
    29  type Path struct {
    30  	name   string // the name of this field or "" if this is an index
    31  	index  string // if name == "", this is a subscript (index or map key) of the previous element
    32  	parent *Path  // nil if this is the root element
    33  }
    34  
    35  // NewPath creates a root Path object.
    36  func NewPath(name string, moreNames ...string) *Path {
    37  	r := &Path{name: name, parent: nil}
    38  	for _, anotherName := range moreNames {
    39  		r = &Path{name: anotherName, parent: r}
    40  	}
    41  	return r
    42  }
    43  
    44  // Root returns the root element of this Path.
    45  func (p *Path) Root() *Path {
    46  	for ; p.parent != nil; p = p.parent {
    47  		// Do nothing.
    48  	}
    49  	return p
    50  }
    51  
    52  // Child creates a new Path that is a child of the method receiver.
    53  func (p *Path) Child(name string, moreNames ...string) *Path {
    54  	r := NewPath(name, moreNames...)
    55  	r.Root().parent = p
    56  	return r
    57  }
    58  
    59  // Index indicates that the previous Path is to be subscripted by an int.
    60  // This sets the same underlying value as Key.
    61  func (p *Path) Index(index int) *Path {
    62  	return &Path{index: strconv.Itoa(index), parent: p}
    63  }
    64  
    65  // Key indicates that the previous Path is to be subscripted by a string.
    66  // This sets the same underlying value as Index.
    67  func (p *Path) Key(key string) *Path {
    68  	return &Path{index: key, parent: p}
    69  }
    70  
    71  // String produces a string representation of the Path.
    72  func (p *Path) String() string {
    73  	// make a slice to iterate
    74  	elems := []*Path{}
    75  	for ; p != nil; p = p.parent {
    76  		elems = append(elems, p)
    77  	}
    78  
    79  	// iterate, but it has to be backwards
    80  	buf := bytes.NewBuffer(nil)
    81  	for i := range elems {
    82  		p := elems[len(elems)-1-i]
    83  		if p.parent != nil && len(p.name) > 0 {
    84  			// This is either the root or it is a subscript.
    85  			buf.WriteString(".")
    86  		}
    87  		if len(p.name) > 0 {
    88  			buf.WriteString(p.name)
    89  		} else {
    90  			fmt.Fprintf(buf, "[%s]", p.index)
    91  		}
    92  	}
    93  	return buf.String()
    94  }
    95  

View as plain text