github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/validation/field/path.go (about) 1 /* 2 Copyright 2015 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 field 18 19 import ( 20 "bytes" 21 "fmt" 22 "strconv" 23 ) 24 25 type pathOptions struct { 26 path *Path 27 } 28 29 // PathOption modifies a pathOptions 30 type PathOption func(o *pathOptions) 31 32 // WithPath generates a PathOption 33 func WithPath(p *Path) PathOption { 34 return func(o *pathOptions) { 35 o.path = p 36 } 37 } 38 39 // ToPath produces *Path from a set of PathOption 40 func ToPath(opts ...PathOption) *Path { 41 c := &pathOptions{} 42 for _, opt := range opts { 43 opt(c) 44 } 45 return c.path 46 } 47 48 // Path represents the path from some root to a particular field. 49 type Path struct { 50 name string // the name of this field or "" if this is an index 51 index string // if name == "", this is a subscript (index or map key) of the previous element 52 parent *Path // nil if this is the root element 53 } 54 55 // NewPath creates a root Path object. 56 func NewPath(name string, moreNames ...string) *Path { 57 r := &Path{name: name, parent: nil} 58 for _, anotherName := range moreNames { 59 r = &Path{name: anotherName, parent: r} 60 } 61 return r 62 } 63 64 // Root returns the root element of this Path. 65 func (p *Path) Root() *Path { 66 for ; p.parent != nil; p = p.parent { 67 // Do nothing. 68 } 69 return p 70 } 71 72 // Child creates a new Path that is a child of the method receiver. 73 func (p *Path) Child(name string, moreNames ...string) *Path { 74 r := NewPath(name, moreNames...) 75 r.Root().parent = p 76 return r 77 } 78 79 // Index indicates that the previous Path is to be subscripted by an int. 80 // This sets the same underlying value as Key. 81 func (p *Path) Index(index int) *Path { 82 return &Path{index: strconv.Itoa(index), parent: p} 83 } 84 85 // Key indicates that the previous Path is to be subscripted by a string. 86 // This sets the same underlying value as Index. 87 func (p *Path) Key(key string) *Path { 88 return &Path{index: key, parent: p} 89 } 90 91 // String produces a string representation of the Path. 92 func (p *Path) String() string { 93 if p == nil { 94 return "<nil>" 95 } 96 // make a slice to iterate 97 elems := []*Path{} 98 for ; p != nil; p = p.parent { 99 elems = append(elems, p) 100 } 101 102 // iterate, but it has to be backwards 103 buf := bytes.NewBuffer(nil) 104 for i := range elems { 105 p := elems[len(elems)-1-i] 106 if p.parent != nil && len(p.name) > 0 { 107 // This is either the root or it is a subscript. 108 buf.WriteString(".") 109 } 110 if len(p.name) > 0 { 111 buf.WriteString(p.name) 112 } else { 113 fmt.Fprintf(buf, "[%s]", p.index) 114 } 115 } 116 return buf.String() 117 }