forked from neo4j-drivers/gobolt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
301 lines (256 loc) · 8.54 KB
/
value.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
/*
* Copyright (c) 2002-2018 "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 gobolt
/*
#include <stdlib.h>
#include "bolt/bolt.h"
*/
import "C"
import (
"reflect"
"unsafe"
)
type boltValueSystem struct {
valueHandlers []ValueHandler
valueHandlersBySignature map[int16]ValueHandler
valueHandlersByType map[reflect.Type]ValueHandler
connectorErrorFactory func(state, code int, codeText, context, description string) ConnectorError
databaseErrorFactory func(classification, code, message string) DatabaseError
genericErrorFactory func(format string, args ...interface{}) GenericError
}
func (valueSystem *boltValueSystem) valueAsGo(value *C.struct_BoltValue) (interface{}, error) {
valueType := C.BoltValue_type(value)
switch {
case valueType == C.BOLT_NULL:
return nil, nil
case valueType == C.BOLT_BOOLEAN:
return valueSystem.valueAsBoolean(value), nil
case valueType == C.BOLT_INTEGER:
return valueSystem.valueAsInt(value), nil
case valueType == C.BOLT_FLOAT:
return valueSystem.valueAsFloat(value), nil
case valueType == C.BOLT_STRING:
return valueSystem.valueAsString(value), nil
case valueType == C.BOLT_DICTIONARY:
return valueSystem.valueAsDictionary(value)
case valueType == C.BOLT_LIST:
return valueSystem.valueAsList(value)
case valueType == C.BOLT_BYTES:
return valueSystem.valueAsBytes(value), nil
case valueType == C.BOLT_STRUCTURE:
signature := int16(C.BoltStructure_code(value))
if handler, ok := valueSystem.valueHandlersBySignature[signature]; ok {
listValue, err := valueSystem.structAsList(value)
if err != nil {
return nil, err
}
return handler.Read(signature, listValue)
}
return nil, newGenericError("unsupported struct type received: %#x", signature)
}
return nil, newGenericError("unsupported data type")
}
func (valueSystem *boltValueSystem) valueAsBoolean(value *C.struct_BoltValue) bool {
val := C.BoltBoolean_get(value)
return val == 1
}
func (valueSystem *boltValueSystem) valueAsInt(value *C.struct_BoltValue) int64 {
val := C.BoltInteger_get(value)
return int64(val)
}
func (valueSystem *boltValueSystem) valueAsFloat(value *C.struct_BoltValue) float64 {
val := C.BoltFloat_get(value)
return float64(val)
}
func (valueSystem *boltValueSystem) valueAsString(value *C.struct_BoltValue) string {
val := C.BoltString_get(value)
return C.GoStringN(val, C.BoltValue_size(value))
}
func (valueSystem *boltValueSystem) valueAsDictionary(value *C.struct_BoltValue) (map[string]interface{}, error) {
size := int(C.BoltValue_size(value))
dict := make(map[string]interface{}, size)
for i := 0; i < size; i++ {
index := C.int32_t(i)
key := valueSystem.valueAsString(C.BoltDictionary_key(value, index))
value, err := valueSystem.valueAsGo(C.BoltDictionary_value(value, index))
if err != nil {
return nil, err
}
dict[key] = value
}
return dict, nil
}
func (valueSystem *boltValueSystem) valueAsList(value *C.struct_BoltValue) ([]interface{}, error) {
size := int(C.BoltValue_size(value))
list := make([]interface{}, size)
for i := 0; i < size; i++ {
index := C.int32_t(i)
value, err := valueSystem.valueAsGo(C.BoltList_value(value, index))
if err != nil {
return nil, err
}
list[i] = value
}
return list, nil
}
func (valueSystem *boltValueSystem) structAsList(value *C.struct_BoltValue) ([]interface{}, error) {
size := int(C.BoltValue_size(value))
list := make([]interface{}, size)
for i := 0; i < size; i++ {
index := C.int32_t(i)
value, err := valueSystem.valueAsGo(C.BoltStructure_value(value, index))
if err != nil {
return nil, err
}
list[i] = value
}
return list, nil
}
func (valueSystem *boltValueSystem) valueAsBytes(value *C.struct_BoltValue) []byte {
val := C.BoltBytes_get_all(value)
return C.GoBytes(unsafe.Pointer(val), C.BoltValue_size(value))
}
func (valueSystem *boltValueSystem) valueToConnector(value interface{}) *C.struct_BoltValue {
res := C.BoltValue_create()
valueSystem.valueAsConnector(res, value)
return res
}
func (valueSystem *boltValueSystem) valueAsConnector(target *C.struct_BoltValue, value interface{}) error {
if value == nil {
C.BoltValue_format_as_Null(target)
} else {
handled := true
switch v := value.(type) {
case bool:
valueSystem.boolAsValue(target, v)
case int8:
valueSystem.intAsValue(target, int64(v))
case int16:
valueSystem.intAsValue(target, int64(v))
case int:
valueSystem.intAsValue(target, int64(v))
case int32:
valueSystem.intAsValue(target, int64(v))
case int64:
valueSystem.intAsValue(target, v)
case uint8:
valueSystem.intAsValue(target, int64(v))
case uint16:
valueSystem.intAsValue(target, int64(v))
case uint:
valueSystem.intAsValue(target, int64(v))
case uint32:
valueSystem.intAsValue(target, int64(v))
case uint64:
valueSystem.intAsValue(target, int64(v))
case float32:
valueSystem.floatAsValue(target, float64(v))
case float64:
valueSystem.floatAsValue(target, v)
case string:
valueSystem.stringAsValue(target, v)
case []byte:
valueSystem.bytesAsValue(target, v)
default:
handled = false
}
if !handled {
v := reflect.TypeOf(value)
handled = true
switch v.Kind() {
case reflect.Ptr:
valueSystem.valueAsConnector(target, reflect.ValueOf(value).Elem().Interface())
case reflect.Slice:
valueSystem.listAsValue(target, value)
case reflect.Map:
valueSystem.mapAsValue(target, value)
default:
// ask for value handlers
if handler, ok := valueSystem.valueHandlersByType[v]; ok {
signature, fields, err := handler.Write(value)
if err != nil {
return err
}
C.BoltValue_format_as_Structure(target, C.int16_t(signature), C.int32_t(len(fields)))
for index, fieldValue := range fields {
valueSystem.valueAsConnector(C.BoltStructure_value(target, C.int32_t(index)), fieldValue)
}
} else {
handled = false
}
}
}
if !handled {
return newGenericError("unsupported value for conversion: %v", value)
}
}
return nil
}
func (valueSystem *boltValueSystem) boolAsValue(target *C.struct_BoltValue, value bool) {
data := C.char(0)
if value {
data = C.char(1)
}
C.BoltValue_format_as_Boolean(target, data)
}
func (valueSystem *boltValueSystem) intAsValue(target *C.struct_BoltValue, value int64) {
C.BoltValue_format_as_Integer(target, C.int64_t(value))
}
func (valueSystem *boltValueSystem) floatAsValue(target *C.struct_BoltValue, value float64) {
C.BoltValue_format_as_Float(target, C.double(value))
}
func (valueSystem *boltValueSystem) stringAsValue(target *C.struct_BoltValue, value string) {
str := C.CString(value)
C.BoltValue_format_as_String(target, str, C.int32_t(len(value)))
C.free(unsafe.Pointer(str))
}
func (valueSystem *boltValueSystem) bytesAsValue(target *C.struct_BoltValue, value []byte) {
bytes := C.CBytes(value)
str := (*C.char)(bytes)
C.BoltValue_format_as_Bytes(target, str, C.int32_t(len(value)))
C.free(bytes)
}
func (valueSystem *boltValueSystem) listAsValue(target *C.struct_BoltValue, value interface{}) error {
slice := reflect.ValueOf(value)
if slice.Kind() != reflect.Slice {
return newGenericError("listAsValue invoked with a non-slice type: %v", value)
}
C.BoltValue_format_as_List(target, C.int32_t(slice.Len()))
for i := 0; i < slice.Len(); i++ {
elTarget := C.BoltList_value(target, C.int32_t(i))
valueSystem.valueAsConnector(elTarget, slice.Index(i).Interface())
}
return nil
}
func (valueSystem *boltValueSystem) mapAsValue(target *C.struct_BoltValue, value interface{}) error {
dict := reflect.ValueOf(value)
if dict.Kind() != reflect.Map {
return newGenericError("mapAsValue invoked with a non-map type: %v", value)
}
C.BoltValue_format_as_Dictionary(target, C.int32_t(dict.Len()))
index := C.int32_t(0)
for _, key := range dict.MapKeys() {
keyTarget := C.BoltDictionary_key(target, index)
elTarget := C.BoltDictionary_value(target, index)
valueSystem.valueAsConnector(keyTarget, key.Interface())
valueSystem.valueAsConnector(elTarget, dict.MapIndex(key).Interface())
index++
}
return nil
}