context.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package raymond
  2. import (
  3. "strings"
  4. )
  5. type contextMember struct {
  6. path string
  7. asMapping string
  8. }
  9. type handlebarsContext struct {
  10. contextMembers []contextMember
  11. }
  12. func newHandlebarsContext() *handlebarsContext {
  13. var cm []contextMember
  14. return &handlebarsContext{contextMembers: cm}
  15. }
  16. func (h *handlebarsContext) AddMemberContext(path, asMapping string) {
  17. cmp := contextMember{path: path, asMapping: asMapping}
  18. h.contextMembers = append(h.contextMembers, cmp)
  19. }
  20. func (h *handlebarsContext) GetCurrentContext() []string {
  21. return h.GetParentContext(0)
  22. }
  23. func (h *handlebarsContext) GetCurrentContextString() string {
  24. return h.GetParentContextString(0)
  25. }
  26. func (h *handlebarsContext) GetParentContext(num_ancestors int) []string {
  27. if len(h.contextMembers) == 0 {
  28. return []string{}
  29. }
  30. return strings.Split(h.GetParentContextString(num_ancestors), ".")
  31. }
  32. func (h *handlebarsContext) GetParentContextString(num_ancestors int) string {
  33. if len(h.contextMembers) == 0 {
  34. return ""
  35. }
  36. if num_ancestors > len(h.contextMembers) {
  37. num_ancestors = 0
  38. }
  39. var res string
  40. for _, val := range h.contextMembers[:len(h.contextMembers)-num_ancestors] {
  41. if len(res) == 0 {
  42. res = val.path
  43. } else {
  44. res = res + "." + val.path
  45. }
  46. }
  47. return res
  48. }
  49. func (h *handlebarsContext) MoveUpContext() {
  50. if len(h.contextMembers) > 0 {
  51. h.contextMembers = h.contextMembers[:len(h.contextMembers)-1]
  52. }
  53. }
  54. func (h *handlebarsContext) HaveAsContexts(num_ancestors int) bool {
  55. if num_ancestors > len(h.contextMembers) {
  56. num_ancestors = 0
  57. }
  58. for val := range h.contextMembers[:len(h.contextMembers)-num_ancestors] {
  59. if h.contextMembers[val].asMapping != "" {
  60. return true
  61. }
  62. }
  63. return false
  64. }
  65. func (h *handlebarsContext) GetMappedContext(path []string, num_ancestors int) []string {
  66. if len(path) == 0 {
  67. return []string{}
  68. }
  69. return strings.Split(h.GetMappedContextString(path, num_ancestors), ".")
  70. }
  71. func (h *handlebarsContext) GetMappedContextString(path []string, num_ancestors int) string {
  72. if len(h.contextMembers) == 0 {
  73. return strings.Join(path, ".")
  74. }
  75. if num_ancestors > len(h.contextMembers) {
  76. num_ancestors = 0
  77. }
  78. if !h.HaveAsContexts(num_ancestors) {
  79. var res string
  80. if path[0] == "" {
  81. res = h.GetParentContextString(num_ancestors)
  82. } else {
  83. res = h.GetParentContextString(num_ancestors) + "." + strings.Join(path, ".")
  84. }
  85. return strings.Trim(res, ".")
  86. }
  87. var res string
  88. copiedMembers := make([]contextMember, 0)
  89. if num_ancestors > 0 {
  90. copiedMembers = append(copiedMembers, h.contextMembers[:len(h.contextMembers)-num_ancestors]...)
  91. } else {
  92. copiedMembers = append(copiedMembers, h.contextMembers...)
  93. }
  94. for p := len(path) - 1; p >= 0; p-- {
  95. var val contextMember
  96. var found string
  97. if len(copiedMembers) == 0 {
  98. found = path[p]
  99. } else {
  100. val = copiedMembers[len(copiedMembers)-1]
  101. if val.asMapping == path[p] {
  102. found = val.path
  103. if len(copiedMembers) > 1 {
  104. val2 := copiedMembers[len(copiedMembers)-2]
  105. tmp := strings.Split(val.path, ".")
  106. if tmp[0] == val2.asMapping {
  107. found = strings.Join(tmp[1:], ".")
  108. }
  109. }
  110. copiedMembers = copiedMembers[:len(copiedMembers)-1]
  111. } else {
  112. if len(val.asMapping) == 0 {
  113. found = val.path + "." + path[p]
  114. copiedMembers = copiedMembers[:len(copiedMembers)-1]
  115. } else {
  116. if len(copiedMembers) == 0 {
  117. ss := strings.Split(val.asMapping, ".")
  118. if ss[0] == path[p] {
  119. found = val.path
  120. } else {
  121. }
  122. } else {
  123. if len(copiedMembers) > 1 {
  124. cv := copiedMembers[len(copiedMembers)-2]
  125. mappedPath := strings.Split(cv.path, ".")
  126. if len(mappedPath) > 1 {
  127. tmp := strings.Join(mappedPath[1:], ".")
  128. if tmp == val.asMapping {
  129. found = val.path
  130. copiedMembers = copiedMembers[:len(copiedMembers)-1]
  131. } else {
  132. found = path[p]
  133. }
  134. } else {
  135. found = path[p]
  136. }
  137. } else {
  138. found = path[p]
  139. }
  140. }
  141. }
  142. }
  143. }
  144. res = found + "." + res
  145. }
  146. if len(copiedMembers) > 0 {
  147. for p := len(copiedMembers) - 1; p >= 0; p-- {
  148. res = copiedMembers[p].path + "." + res
  149. }
  150. }
  151. return strings.Trim(res, ".")
  152. }