-
Notifications
You must be signed in to change notification settings - Fork 67
/
hydrator.go
353 lines (325 loc) · 9.85 KB
/
hydrator.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright (c) 2002-2020 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bolt
import (
"errors"
"fmt"
"time"
"github.com/neo4j/neo4j-go-driver/v4/neo4j/db"
"github.com/neo4j/neo4j-go-driver/v4/neo4j/dbtype"
"github.com/neo4j/neo4j-go-driver/v4/neo4j/internal/packstream"
)
// Called by packstream unpacker to hydrate a packstream struct into something
// more usable by the consumer.
func hydrate(tag packstream.StructTag, fields []interface{}) (interface{}, error) {
switch tag {
case msgSuccess:
return hydrateSuccess(fields)
case msgIgnored:
return hydrateIgnored(fields)
case msgFailure:
return hydrateFailure(fields)
case msgRecord:
return hydrateRecord(fields)
case 'N':
return hydrateNode(fields)
case 'R':
return hydrateRelationship(fields)
case 'r':
return hydrateRelNode(fields)
case 'P':
return hydratePath(fields)
case 'X':
return hydratePoint2d(fields)
case 'Y':
return hydratePoint3d(fields)
case 'F':
return hydrateDateTimeOffset(fields)
case 'f':
return hydrateDateTimeNamedZone(fields)
case 'd':
return hydrateLocalDateTime(fields)
case 'D':
return hydrateDate(fields)
case 'T':
return hydrateTime(fields)
case 't':
return hydrateLocalTime(fields)
case 'E':
return hydrateDuration(fields)
default:
return nil, errors.New(fmt.Sprintf("Unknown tag: %02x", tag))
}
}
func hydrateNode(fields []interface{}) (interface{}, error) {
if len(fields) != 3 {
return nil, errors.New("Node hydrate error")
}
id, idok := fields[0].(int64)
tagsx, tagsok := fields[1].([]interface{})
props, propsok := fields[2].(map[string]interface{})
if !idok || !tagsok || !propsok {
return nil, errors.New("Node hydrate error")
}
n := dbtype.Node{Id: id, Props: props, Labels: make([]string, len(tagsx))}
// Convert tags to strings
for i, x := range tagsx {
t, tok := x.(string)
if !tok {
return nil, errors.New("Node hydrate error")
}
n.Labels[i] = t
}
return n, nil
}
func hydrateRelationship(fields []interface{}) (interface{}, error) {
if len(fields) != 5 {
return nil, errors.New("Relationship hydrate error")
}
id, idok := fields[0].(int64)
sid, sidok := fields[1].(int64)
eid, eidok := fields[2].(int64)
lbl, lblok := fields[3].(string)
props, propsok := fields[4].(map[string]interface{})
if !idok || !sidok || !eidok || !lblok || !propsok {
return nil, errors.New("Relationship hydrate error")
}
return dbtype.Relationship{Id: id, StartId: sid, EndId: eid, Type: lbl, Props: props}, nil
}
func hydrateRelNode(fields []interface{}) (interface{}, error) {
if len(fields) != 3 {
return nil, errors.New("RelNode hydrate error")
}
id, idok := fields[0].(int64)
lbl, lblok := fields[1].(string)
props, propsok := fields[2].(map[string]interface{})
if !idok || !lblok || !propsok {
return nil, errors.New("RelNode hydrate error")
}
s := &relNode{id: id, name: lbl, props: props}
return s, nil
}
func hydratePath(fields []interface{}) (interface{}, error) {
if len(fields) != 3 {
return nil, errors.New("Path hydrate error")
}
nodesx, nok := fields[0].([]interface{})
relnodesx, rok := fields[1].([]interface{})
indsx, iok := fields[2].([]interface{})
if !nok || !rok || !iok {
return nil, errors.New("Path hydrate error")
}
nodes := make([]dbtype.Node, len(nodesx))
for i, nx := range nodesx {
n, ok := nx.(dbtype.Node)
if !ok {
return nil, errors.New("Path hydrate error")
}
nodes[i] = n
}
relnodes := make([]*relNode, len(relnodesx))
for i, rx := range relnodesx {
r, ok := rx.(*relNode)
if !ok {
return nil, errors.New("Path hydrate error")
}
relnodes[i] = r
}
indexes := make([]int, len(indsx))
for i, ix := range indsx {
p, ok := ix.(int64)
if !ok {
return nil, errors.New("Path hydrate error")
}
indexes[i] = int(p)
}
// Must be even number
if (len(indexes) & 0x01) == 1 {
return nil, errors.New("Path hydrate error")
}
return buildPath(nodes, relnodes, indexes), nil
}
func hydrateSuccess(fields []interface{}) (interface{}, error) {
if len(fields) != 1 {
return nil, errors.New("Success hydrate error")
}
meta, metaok := fields[0].(map[string]interface{})
if !metaok {
return nil, errors.New("Success hydrate error")
}
return &successResponse{meta: meta}, nil
}
func hydrateRecord(fields []interface{}) (interface{}, error) {
if len(fields) == 0 {
return nil, errors.New("Record hydrate error")
}
v, vok := fields[0].([]interface{})
if !vok {
return nil, errors.New("Record hydrate error")
}
return &db.Record{Values: v}, nil
}
func hydrateIgnored(fields []interface{}) (interface{}, error) {
if len(fields) != 0 {
return nil, errors.New("Ignored hydrate error")
}
return &ignoredResponse{}, nil
}
func hydrateFailure(fields []interface{}) (interface{}, error) {
if len(fields) != 1 {
return nil, errors.New("Failure hydrate error")
}
m, mok := fields[0].(map[string]interface{})
if !mok {
return nil, errors.New("Failure hydrate error")
}
code, cok := m["code"].(string)
msg, mok := m["message"].(string)
if !cok || !mok {
return nil, errors.New("Failure hydrate error")
}
// Hydrate right into error defined in connection package to avoid remapping at a later
// state.
return &db.DatabaseError{Code: code, Msg: msg}, nil
}
func hydratePoint2d(fields []interface{}) (interface{}, error) {
if len(fields) != 3 {
return nil, errors.New("Point2d hydrate error")
}
srId, sok := fields[0].(int64)
x, xok := fields[1].(float64)
y, yok := fields[2].(float64)
if !sok || !xok || !yok {
return nil, errors.New("Point2d hydrate error")
}
return dbtype.Point2D{SpatialRefId: uint32(srId), X: x, Y: y}, nil
}
func hydratePoint3d(fields []interface{}) (interface{}, error) {
if len(fields) != 4 {
return nil, errors.New("Point3d hydrate error")
}
srId, sok := fields[0].(int64)
x, xok := fields[1].(float64)
y, yok := fields[2].(float64)
z, zok := fields[3].(float64)
if !sok || !xok || !yok || !zok {
return nil, errors.New("Point3d hydrate error")
}
return dbtype.Point3D{SpatialRefId: uint32(srId), X: x, Y: y, Z: z}, nil
}
func hydrateDateTimeOffset(fields []interface{}) (interface{}, error) {
if len(fields) != 3 {
return nil, errors.New("DateTime hydrate error")
}
secs, sok := fields[0].(int64)
nans, nok := fields[1].(int64)
offs, ook := fields[2].(int64)
if !sok || !nok || !ook {
return nil, errors.New("DateTime hydrate error")
}
t := time.Unix(secs, nans).UTC()
l := time.FixedZone("Offset", int(offs))
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), l), nil
}
func hydrateDateTimeNamedZone(fields []interface{}) (interface{}, error) {
if len(fields) != 3 {
return nil, errors.New("DateTime hydrate error")
}
secs, sok := fields[0].(int64)
nans, nok := fields[1].(int64)
zone, zok := fields[2].(string)
if !sok || !nok || !zok {
return nil, errors.New("DateTime hydrate error")
}
t := time.Unix(secs, nans).UTC()
l, err := time.LoadLocation(zone)
if err != nil {
// TODO: Wrap error
return nil, err
}
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), l), nil
}
func hydrateLocalDateTime(fields []interface{}) (interface{}, error) {
if len(fields) != 2 {
return nil, errors.New("LocalDateTime hydrate error")
}
secs, sok := fields[0].(int64)
nans, nok := fields[1].(int64)
if !sok || !nok {
return nil, errors.New("LocalDateTime hydrate error")
}
t := time.Unix(secs, nans).UTC()
t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.Local)
return dbtype.LocalDateTime(t), nil
}
func hydrateDate(fields []interface{}) (interface{}, error) {
if len(fields) != 1 {
return nil, errors.New("Date hydrate error")
}
days, dok := fields[0].(int64)
if !dok {
return nil, errors.New("Date hydrate error")
}
secs := days * 86400
return dbtype.Date(time.Unix(secs, 0).UTC()), nil
}
func hydrateTime(fields []interface{}) (interface{}, error) {
if len(fields) != 2 {
return nil, errors.New("Time hydrate error")
}
nans, nok := fields[0].(int64)
offs, ook := fields[1].(int64)
if !nok || !ook {
return nil, errors.New("Time hydrate error")
}
secs := nans / int64(time.Second)
nans -= secs * int64(time.Second)
l := time.FixedZone("Offset", int(offs))
t := time.Date(0, 0, 0, 0, 0, int(secs), int(nans), l)
return dbtype.Time(t), nil
}
func hydrateLocalTime(fields []interface{}) (interface{}, error) {
if len(fields) != 1 {
return nil, errors.New("LocalTime hydrate error")
}
nans, nok := fields[0].(int64)
if !nok {
return nil, errors.New("LocalTime hydrate error")
}
secs := nans / int64(time.Second)
nans -= secs * int64(time.Second)
t := time.Date(0, 0, 0, 0, 0, int(secs), int(nans), time.Local)
return dbtype.LocalTime(t), nil
}
func hydrateDuration(fields []interface{}) (interface{}, error) {
// Always hydrate to dbtype.Duration since that allows for longer durations than the
// standard time.Duration even though it's probably very unusual with the need to
// express durations for hundreds of years.
if len(fields) != 4 {
return nil, errors.New("Duration hydrate error")
}
mon, mok := fields[0].(int64)
day, dok := fields[1].(int64)
sec, sok := fields[2].(int64)
nan, nok := fields[3].(int64)
if !mok || !dok || !sok || !nok {
return nil, errors.New("Duration hydrate error")
}
return dbtype.Duration{Months: mon, Days: day, Seconds: sec, Nanos: int(nan)}, nil
}