path.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package xpj
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. )
  7. // A path names a node below another, in the form used by the format doc:
  8. // object keys separated by dots, array indexes in brackets, e.g.
  9. // "tracks[0].program.drum.instruments[4].layersv[0].sliceInfo.End".
  10. // Keys containing '.', '[' or ']' cannot be addressed this way; use Lookup.
  11. type step struct {
  12. key string
  13. index int // -1 for a key step
  14. }
  15. func parsePath(path string) ([]step, error) {
  16. var steps []step
  17. rest := path
  18. for rest != "" {
  19. if rest[0] == '[' {
  20. end := strings.IndexByte(rest, ']')
  21. if end < 0 {
  22. return nil, fmt.Errorf("xpj: path %q: missing ]", path)
  23. }
  24. i, err := strconv.Atoi(rest[1:end])
  25. if err != nil || i < 0 {
  26. return nil, fmt.Errorf("xpj: path %q: bad index %q", path, rest[1:end])
  27. }
  28. steps = append(steps, step{index: i})
  29. rest = rest[end+1:]
  30. if strings.HasPrefix(rest, ".") {
  31. rest = rest[1:]
  32. if rest == "" || rest[0] == '[' {
  33. return nil, fmt.Errorf("xpj: path %q: empty key", path)
  34. }
  35. }
  36. continue
  37. }
  38. end := strings.IndexAny(rest, ".[]")
  39. if end < 0 {
  40. end = len(rest)
  41. }
  42. if end == 0 {
  43. return nil, fmt.Errorf("xpj: path %q: empty key", path)
  44. }
  45. steps = append(steps, step{key: rest[:end], index: -1})
  46. rest = rest[end:]
  47. if strings.HasPrefix(rest, "]") {
  48. return nil, fmt.Errorf("xpj: path %q: unexpected ]", path)
  49. }
  50. if strings.HasPrefix(rest, ".") {
  51. rest = rest[1:]
  52. if rest == "" || rest[0] == '[' {
  53. return nil, fmt.Errorf("xpj: path %q: empty key", path)
  54. }
  55. }
  56. }
  57. return steps, nil
  58. }
  59. func (n *Node) walk(steps []step, path string) (*Node, error) {
  60. cur := n
  61. for i, s := range steps {
  62. if s.index < 0 {
  63. next := cur.Lookup(s.key)
  64. if next == nil {
  65. return nil, fmt.Errorf("xpj: path %q: no key %q at %s", path, s.key, describe(steps[:i]))
  66. }
  67. cur = next
  68. continue
  69. }
  70. if cur.Kind != Array {
  71. return nil, fmt.Errorf("xpj: path %q: %s is %s, not an array", path, describe(steps[:i]), cur.Kind)
  72. }
  73. if s.index >= len(cur.Items) {
  74. return nil, fmt.Errorf("xpj: path %q: index %d out of range at %s (length %d)", path, s.index, describe(steps[:i]), len(cur.Items))
  75. }
  76. cur = cur.Items[s.index]
  77. }
  78. return cur, nil
  79. }
  80. func describe(steps []step) string {
  81. if len(steps) == 0 {
  82. return "root"
  83. }
  84. var b strings.Builder
  85. for i, s := range steps {
  86. if s.index >= 0 {
  87. fmt.Fprintf(&b, "[%d]", s.index)
  88. continue
  89. }
  90. if i > 0 {
  91. b.WriteByte('.')
  92. }
  93. b.WriteString(s.key)
  94. }
  95. return b.String()
  96. }
  97. // Get returns the node at path below n. An empty path returns n.
  98. func (n *Node) Get(path string) (*Node, error) {
  99. steps, err := parsePath(path)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return n.walk(steps, path)
  104. }
  105. // Set replaces the node at path below n. The parent must exist; a missing
  106. // final key is appended to its object, but array indexes must already exist.
  107. func (n *Node) Set(path string, v *Node) error {
  108. steps, err := parsePath(path)
  109. if err != nil {
  110. return err
  111. }
  112. if len(steps) == 0 {
  113. return fmt.Errorf("xpj: cannot Set the root")
  114. }
  115. parent, err := n.walk(steps[:len(steps)-1], path)
  116. if err != nil {
  117. return err
  118. }
  119. last := steps[len(steps)-1]
  120. if last.index < 0 {
  121. return parent.SetKey(last.key, v)
  122. }
  123. if parent.Kind != Array {
  124. return fmt.Errorf("xpj: path %q: parent is %s, not an array", path, parent.Kind)
  125. }
  126. if last.index >= len(parent.Items) {
  127. return fmt.Errorf("xpj: path %q: index %d out of range (length %d)", path, last.index, len(parent.Items))
  128. }
  129. parent.Items[last.index] = v
  130. return nil
  131. }