forked from mindstand/go-cypherdsl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.go
133 lines (103 loc) · 2.56 KB
/
merge.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
package go_cypherdsl
import (
"errors"
"reflect"
"strings"
)
type MergeConfig struct {
// the path its merging on
Path string
// what it does if its creating the node
OnCreate *MergeSetConfig
// what it does if its matching the node
OnMatch *MergeSetConfig
}
func (m *MergeConfig) ToString() (string, error) {
var sb strings.Builder
if m.Path == "" {
return "", errors.New("path can not be empty")
}
sb.WriteString(m.Path)
if m.OnCreate != nil {
str, err := m.OnCreate.ToString()
if err != nil {
return "", err
}
sb.WriteString(" ON CREATE SET ")
sb.WriteString(str)
}
if m.OnMatch != nil {
str, err := m.OnMatch.ToString()
if err != nil {
return "", err
}
sb.WriteString(" ON MATCH SET ")
sb.WriteString(str)
}
return sb.String(), nil
}
type MergeSetConfig struct {
// variable name
Name string
// member variable of node
Member string
// new value
Target interface{}
// new value if its a function, do not include
TargetFunction *FunctionConfig
// Operator is the operator used in the SET clause. Must be "=" or "+=".
// If not set, defaults to "=".
Operator BooleanOperator
}
func (m *MergeSetConfig) ToString() (string, error) {
var sb strings.Builder
if m.Name == "" {
return "", errors.New("name can not be empty")
}
if m.Target == nil && m.TargetFunction == nil {
return "", errors.New("target or target function has to be defined")
}
if m.Target != nil && m.TargetFunction != nil {
return "", errors.New("target and target function can not both be defined")
}
if m.Target != nil && (reflect.TypeOf(m.Target) == reflect.TypeOf(ParamString(""))) {
sb.WriteString(m.Name)
sb.WriteRune(' ')
operator := m.Operator
if operator == "" {
operator = EqualToOperator
} else if operator != EqualToOperator && operator != PlusEqualOperator {
return "", errors.New("invalid SET operator")
}
sb.WriteString(operator.String())
sb.WriteRune(' ')
} else {
if m.Member == "" {
return "", errors.New("member can not be empty")
}
operator := m.Operator
if operator == "" {
operator = EqualToOperator
} else if operator != EqualToOperator {
return "", errors.New("invalid SET operator for member")
}
sb.WriteString(m.Name)
sb.WriteRune('.')
sb.WriteString(m.Member)
sb.WriteRune(' ')
sb.WriteString(EqualToOperator.String())
sb.WriteRune(' ')
}
var str string
var err error
if m.Target != nil {
str, err = cypherizeInterface(m.Target)
} else {
str, err = m.TargetFunction.ToString()
}
if err != nil {
return "", err
}
sb.WriteString(str)
return sb.String(), nil
}