path.go 2.9 KB

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