-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
230 lines (209 loc) · 5.16 KB
/
config.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package neo4j
import (
"strconv"
"strings"
"github.com/araddon/dateparse"
"github.com/cloudprivacylabs/lsa/pkg/ls"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/nleeper/goment"
)
type Config struct {
TermMappings map[string]string `json:"termMappings" yaml:"termMappings"`
NamespaceMappings map[string]string `json:"namespaceMappings" yaml:"namespaceMappings"`
PropertyTypes map[string]string `json:"propertyTypes" yaml:"propertyTypes"`
EntityMergeActions map[string]EntityMergeAction `json:"entityMergeActions" yaml:"entityMergeActions"`
trie *Trie
}
type EntityMergeAction struct {
Merge *bool `json:"merge" yaml:"merge"`
Create *bool `json:"create" yaml:"create"`
}
func (e EntityMergeAction) GetMerge() bool {
if e.Merge == nil {
return true
}
return *e.Merge
}
func (e EntityMergeAction) GetCreate() bool {
if e.Create == nil {
return true
}
return *e.Create
}
type withProperty interface {
ForEachProperty(func(string, interface{}) bool) bool
GetProperty(string) (interface{}, bool)
}
type mapWithProperty map[string]interface{}
func (m mapWithProperty) ForEachProperty(f func(string, interface{}) bool) bool {
for k, v := range m {
if !f(k, v) {
return false
}
}
return true
}
func (m mapWithProperty) GetProperty(key string) (interface{}, bool) {
x, ok := m[key]
return x, ok
}
func InitNamespaceTrie(cfg *Config) *Trie {
root := InitTrie()
for k, v := range cfg.NamespaceMappings {
root.Insert(k, v)
}
cfg.trie = root
return root
}
// IsMergeEntity returns if the entity has a merge config
func (cfg Config) IsMergeEntity(name string) bool {
_, ok := cfg.EntityMergeActions[name]
return ok
}
func (cfg Config) MakeProperties(x withProperty, txVars map[string]any) string {
propMap := make(map[string]any)
x.ForEachProperty(func(k string, v any) bool {
_, ok := v.(*ls.PropertyValue)
if WriteableType(v) || ok {
short := cfg.Shorten(k)
if short != "" {
propMap[short] = v
}
}
return true
})
props := buildDBPropertiesForSave(cfg, x, txVars, propMap)
return props
}
func (cfg Config) ShortenProperties(props map[string]any) map[string]any {
propMap := make(map[string]any)
for k, v := range props {
short := cfg.Shorten(k)
if short != "" {
propMap[short] = v
}
}
return propMap
}
func (cfg Config) MakePropertiesObj(x withProperty) map[string]any {
propMap := make(map[string]any)
x.ForEachProperty(func(k string, v any) bool {
_, ok := v.(*ls.PropertyValue)
if WriteableType(v) || ok {
short := cfg.Shorten(k)
if short != "" {
propMap[short] = v
}
}
return true
})
return buildDBPropertiesForSaveObj(cfg, x, propMap)
}
func (cfg Config) MakeLabels(types []string) string {
var mapped []string
for _, t := range types {
short := cfg.Shorten(t)
if short != "" {
mapped = append(mapped, short)
}
}
labels := makeLabels(nil, mapped)
return labels
}
// GetNativePropertyValue is called during building properties for save and when the expanded property key exists in the config.
func (cfg Config) GetNeo4jPropertyValue(expandedPropertyKey string, val string) (interface{}, error) {
prop, exists := cfg.PropertyTypes[expandedPropertyKey]
if !exists {
return val, nil
}
var propType string
var format string
prefix := strings.Index(prop, ",")
if prefix == -1 {
propType = prop
} else {
propType = strings.TrimSpace(prop[:prefix])
format = strings.TrimSpace(prop[prefix+1:])
}
var v interface{}
var err error
switch propType {
case "Integer":
v, err = strconv.Atoi(val)
if err != nil {
return nil, err
}
case "Float":
v, err = strconv.ParseFloat(val, 64)
if err != nil {
return nil, err
}
case "Boolean":
v, err = strconv.ParseBool(val)
if err != nil {
return nil, err
}
case "Date":
if format != "" {
gmt, err := goment.New(val, format)
if err != nil {
return nil, err
}
v = neo4j.DateOf(gmt.ToTime())
} else {
t, err := dateparse.ParseAny(val)
if err != nil {
return nil, err
}
v = neo4j.DateOf(t)
}
case "DateTime":
if format != "" {
gmt, err := goment.New(val, format)
if err != nil {
return nil, err
}
v = neo4j.LocalDateTimeOf(gmt.ToTime())
} else {
t, err := dateparse.ParseAny(val)
if err != nil {
return nil, err
}
v = neo4j.LocalDateTimeOf(t)
}
}
return nativeValueToNeo4jValue(v), nil
}
func (cfg Config) Shorten(fullName string) string {
if _, exists := cfg.TermMappings[fullName]; exists {
return cfg.TermMappings[fullName]
}
prefix, alias, found := cfg.trie.Search(fullName)
if found && (prefix != "" && alias != "") {
shortName := alias + ":" + fullName[len(prefix):]
return shortName
}
return fullName
}
func reverseLookup(m map[string]string, key string) (string, bool) {
for k, v := range m {
if key == v {
return k, true
}
}
return "", false
}
func (cfg *Config) Expand(short string) string {
if x, exists := reverseLookup(cfg.TermMappings, short); exists {
return x
}
col := strings.Index(short, ":")
if col == -1 {
return short
}
if prefix, exists := reverseLookup(cfg.NamespaceMappings, short[:col]); exists {
suffix := short[col+1:]
return prefix + suffix
}
return short
}