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