-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmap_expression.go
More file actions
106 lines (92 loc) · 2.49 KB
/
map_expression.go
File metadata and controls
106 lines (92 loc) · 2.49 KB
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
package cypher
import (
errors "golang.org/x/xerrors"
)
type MapExpression struct {
ExpressionContainer
expressions []Expression
key string
notNil bool
err error
}
func MapExpressionCreate(newContents []Expression) MapExpression {
for _, content := range newContents {
if content != nil && content.GetError() != nil {
return MapExpressionError(content.GetError())
}
}
m := MapExpression{
expressions: newContents,
notNil: true,
}
m.key = getAddress(&m)
m.ExpressionContainer = ExpressionWrap(m)
return m
}
func MapExpressionError(err error) MapExpression {
return MapExpression{
err: err,
}
}
func (m MapExpression) GetExpressionType() ExpressionType {
return "MapExpression"
}
func (m MapExpression) GetError() error {
return m.err
}
func (m MapExpression) isNotNil() bool {
return m.notNil
}
func (m MapExpression) getKey() string {
return m.key
}
func NewMapExpression(objects ...interface{}) MapExpression {
if len(objects)%2 != 0 {
return MapExpressionError(errors.Errorf("new map expression number of object input should be product of 2 but it is %", len(objects)))
}
var newContents = make([]Expression, len(objects)/2)
var knownKeys = make(map[string]int)
for i := 0; i < len(objects); i += 2 {
key, isString := objects[i].(string)
if !isString {
return MapExpressionError(errors.Errorf("key must be string"))
}
value, isExpression := objects[i+1].(Expression)
if !isExpression {
value = LiteralOf(objects[i+1])
}
if knownKeys[key] == 1 {
return MapExpressionError(errors.Errorf("duplicate key"))
}
knownKeys[key] = 1
newContents[i/2] = EntryExpressionCreate(key, value)
}
return MapExpressionCreate(newContents)
}
func (m MapExpression) accept(visitor *CypherRenderer) {
visitor.enter(m)
for _, child := range m.expressions {
m.PrepareVisit(child).accept(visitor)
}
visitor.leave(m)
}
func (m MapExpression) enter(renderer *CypherRenderer) {
renderer.append("{")
}
func (m MapExpression) leave(renderer *CypherRenderer) {
renderer.append("}")
}
func (m MapExpression) AddEntries(entries []Expression) MapExpression {
newContent := make([]Expression, len(m.expressions)+len(entries))
for i := range m.expressions {
newContent[i] = m.expressions[i]
}
for i := range entries {
newContent[i+len(m.expressions)] = entries[i]
}
return MapExpressionCreate(newContent)
}
func (m MapExpression) PrepareVisit(visitable Visitable) Visitable {
expression := visitable.(Expression)
return NameOrExpression(expression)
}