-
Notifications
You must be signed in to change notification settings - Fork 5
/
cypher_renderer.go
211 lines (185 loc) · 5.79 KB
/
cypher_renderer.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package cypher
import (
"github.com/rs/xid"
"golang.org/x/xerrors"
"strings"
"unicode"
)
type CypherRenderer struct {
visitableToAliased map[string]AliasedExpression
separatorOnLevel map[int]string
resolvedSymbolicNames map[SymbolicName]string
visitedNamed map[string]int
currentVisitedElements []string
currentAliasedElements []AliasedExpression
currentLevel int
skipNodeContent bool
builder strings.Builder
guid xid.ID
}
func (renderer *CypherRenderer) append(content string) *CypherRenderer {
renderer.builder.WriteString(content)
return renderer
}
func NewRenderer() *CypherRenderer {
return &CypherRenderer{
visitableToAliased: make(map[string]AliasedExpression),
separatorOnLevel: make(map[int]string),
resolvedSymbolicNames: make(map[SymbolicName]string),
visitedNamed: make(map[string]int),
currentVisitedElements: make([]string, 0),
currentAliasedElements: make([]AliasedExpression, 0),
skipNodeContent: false,
builder: strings.Builder{},
guid: xid.New(),
}
}
func (renderer CypherRenderer) Render(statement Statement) (string, error) {
if statement == nil {
return "", xerrors.New("can not render nil statement")
}
if statement.GetError() != nil {
return "", statement.GetError()
}
statement.accept(&renderer)
return strings.TrimSpace(renderer.builder.String()), nil
}
func (renderer *CypherRenderer) enableSeparator(level int, on bool) {
if on {
renderer.separatorOnLevel[level] = ""
} else {
delete(renderer.separatorOnLevel, level)
}
}
func (renderer *CypherRenderer) separatorOnCurrentLevel() (string, bool) {
value, isExist := renderer.separatorOnLevel[renderer.currentLevel]
return value, isExist
}
func (renderer *CypherRenderer) resetSeparatorOnCurrentLevel() {
renderer.separatorOnLevel[renderer.currentLevel] = ""
}
func (renderer *CypherRenderer) resolve(name SymbolicName) string {
if _, isExist := renderer.resolvedSymbolicNames[name]; !isExist {
value := name.value
if len(value) > 0 {
return EscapeIfNecessary(value)
}
return renderer.guid.String()
}
return renderer.resolvedSymbolicNames[name]
}
func (renderer *CypherRenderer) enter(visitable Visitable) {
used := renderer.getAliasedIfSeen(visitable)
if renderer.PreEnter(&used) {
renderer.currentVisitedElements = push(renderer.currentVisitedElements, used.getKey())
used.enter(renderer)
}
}
func (renderer *CypherRenderer) leave(visitable Visitable) {
used := renderer.getAliasedIfSeen(visitable)
current := peek(renderer.currentVisitedElements)
if current == (used).getKey() {
used.leave(renderer)
renderer.postLeave(used)
renderer.currentVisitedElements = pop(renderer.currentVisitedElements)
}
aliasedExpression, isAliased := (visitable).(AliasedExpression)
if isAliased {
renderer.visitableToAliased[aliasedExpression.delegate.getKey()] = aliasedExpression
}
}
func (renderer *CypherRenderer) PreEnter(visitable *Visitable) bool {
lastAliased := peekAliased(renderer.currentAliasedElements)
visited := false
if lastAliased != nil {
_, visited = renderer.visitableToAliased[lastAliased.delegate.getKey()]
}
if renderer.skipNodeContent || visited {
return false
}
aliasedExpression, isAliased := (*visitable).(AliasedExpression)
if isAliased {
renderer.currentAliasedElements = pushAliased(renderer.currentAliasedElements, aliasedExpression)
}
renderer.currentLevel = renderer.currentLevel + 1
nextLevel := renderer.currentLevel + 1
if _, isSub := (*visitable).(SubVisitable); isSub {
renderer.enableSeparator(nextLevel, true)
}
if separator, isSeparator := renderer.separatorOnCurrentLevel(); isSeparator {
renderer.append(separator)
renderer.resetSeparatorOnCurrentLevel()
}
return !renderer.skipNodeContent
}
func (renderer *CypherRenderer) postLeave(visitable Visitable) {
if _, isSeparator := renderer.separatorOnCurrentLevel(); isSeparator {
renderer.separatorOnLevel[renderer.currentLevel] = ", "
}
if _, isSub := visitable.(SubVisitable); isSub {
renderer.enableSeparator(renderer.currentLevel+1, false)
}
if peekAliased(renderer.currentAliasedElements) != nil &&
peekAliased(renderer.currentAliasedElements).getKey() == visitable.getKey() {
renderer.currentAliasedElements = popAliased(renderer.currentAliasedElements)
}
renderer.currentLevel = renderer.currentLevel - 1
}
func (renderer *CypherRenderer) getAliasedIfSeen(visitable Visitable) Visitable {
if expression, isExist := renderer.visitableToAliased[visitable.getKey()]; isExist {
return expression
}
return visitable
}
const RelTypSeparator = "|"
const RelTypeStart = ":"
const NodeLabelStart = ":"
func push(queue []string, element string) []string {
queue = append(queue, element)
return queue
}
func pop(queue []string) []string {
if len(queue) > 0 {
return queue[:len(queue)-1]
}
return queue
}
func peek(queue []string) string {
if len(queue) > 0 {
return queue[len(queue)-1]
}
return ""
}
func pushAliased(queue []AliasedExpression, element AliasedExpression) []AliasedExpression {
queue = append(queue, element)
return queue
}
func popAliased(queue []AliasedExpression) []AliasedExpression {
if len(queue) > 0 {
return queue[:len(queue)-1]
}
return queue
}
func peekAliased(queue []AliasedExpression) *AliasedExpression {
if len(queue) > 0 {
return &queue[len(queue)-1]
}
return nil
}
func escapeName(name string) string {
return "`" + strings.ReplaceAll(name, "`", "``") + "`"
}
func EscapeIfNecessary(name string) string {
if len(strings.TrimSpace(name)) == 0 || isIdentifier(name) {
return name
}
return "`" + strings.ReplaceAll(name, "`", "``") + "`"
}
func isIdentifier(name string) bool {
for i, c := range name {
if !unicode.IsLetter(c) && c != '_' && (i == 0 || !unicode.IsDigit(c)) {
return false
}
}
return name != ""
}