-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathconversion.go
542 lines (525 loc) · 12.4 KB
/
conversion.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
* Copyright 2016-2018 Dgraph Labs, Inc. and Contributors
*
* 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 types
import (
"bytes"
"encoding/binary"
"encoding/json"
"math"
"strconv"
"time"
"unsafe"
"github.com/pkg/errors"
geom "github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/geojson"
"github.com/twpayne/go-geom/encoding/wkb"
"github.com/dgraph-io/dgo/v210/protos/api"
)
// Convert converts the value to given scalar type.
func Convert(from Val, toID TypeID) (Val, error) {
to := Val{Tid: toID}
// sanity: we expect a value
data, ok := from.Value.([]byte)
if !ok {
return to, errors.Errorf("Invalid data to convert to %s", toID.Name())
}
fromID := from.Tid
res := &to.Value
// Convert from-type to to-type and store in the result interface.
switch fromID {
case BinaryID:
{
// Unmarshal from Binary to type interfaces.
switch toID {
case BinaryID:
*res = data
case StringID, DefaultID:
// We never modify from Val, so this should be safe.
*res = *(*string)(unsafe.Pointer(&data))
case IntID:
if len(data) < 8 {
return to, errors.Errorf("Invalid data for int64 %v", data)
}
*res = int64(binary.LittleEndian.Uint64(data))
case FloatID:
if len(data) < 8 {
return to, errors.Errorf("Invalid data for float %v", data)
}
i := binary.LittleEndian.Uint64(data)
*res = math.Float64frombits(i)
case BoolID:
if len(data) == 0 || data[0] == 0 {
*res = false
return to, nil
} else if data[0] == 1 {
*res = true
return to, nil
}
return to, errors.Errorf("Invalid value for bool %v", data[0])
case DateTimeID:
var t time.Time
if err := t.UnmarshalBinary(data); err != nil {
return to, err
}
*res = t
case GeoID:
w, err := wkb.Unmarshal(data)
if err != nil {
return to, err
}
*res = w
case PasswordID:
*res = string(data)
default:
return to, cantConvert(fromID, toID)
}
}
case StringID, DefaultID:
{
vc := string(data)
switch toID {
case BinaryID:
*res = []byte(vc)
case IntID:
val, err := strconv.ParseInt(vc, 10, 64)
if err != nil {
return to, err
}
*res = val
case FloatID:
val, err := strconv.ParseFloat(vc, 64)
if err != nil {
return to, err
}
if math.IsNaN(val) {
return to, errors.Errorf("Got invalid value: NaN")
}
*res = val
case StringID, DefaultID:
*res = vc
case BoolID:
val, err := strconv.ParseBool(vc)
if err != nil {
return to, err
}
*res = val
case DateTimeID:
t, err := ParseTime(vc)
if err != nil {
return to, err
}
*res = t
case GeoID:
var g geom.T
text := bytes.Replace([]byte(vc), []byte("'"), []byte("\""), -1)
if err := geojson.Unmarshal(text, &g); err != nil {
return to,
errors.Wrapf(err, "Error while unmarshalling: [%s] as geojson", vc)
}
*res = g
case PasswordID:
p, err := Encrypt(vc)
if err != nil {
return to, err
}
*res = p
default:
return to, cantConvert(fromID, toID)
}
}
case IntID:
{
if len(data) < 8 {
return to, errors.Errorf("Invalid data for int64 %v", data)
}
vc := int64(binary.LittleEndian.Uint64(data))
switch toID {
case IntID:
*res = vc
case BinaryID:
var bs [8]byte
binary.LittleEndian.PutUint64(bs[:], uint64(vc))
*res = bs[:]
case FloatID:
*res = float64(vc)
case BoolID:
*res = vc != 0
case StringID, DefaultID:
*res = strconv.FormatInt(vc, 10)
case DateTimeID:
*res = time.Unix(vc, 0).UTC()
default:
return to, cantConvert(fromID, toID)
}
}
case FloatID:
{
if len(data) < 8 {
return to, errors.Errorf("Invalid data for float %v", data)
}
i := binary.LittleEndian.Uint64(data)
vc := math.Float64frombits(i)
switch toID {
case FloatID:
*res = vc
case BinaryID:
var bs [8]byte
u := math.Float64bits(vc)
binary.LittleEndian.PutUint64(bs[:], u)
*res = bs[:]
case IntID:
if vc > math.MaxInt64 || vc < math.MinInt64 || math.IsNaN(vc) {
return to, errors.Errorf("Float out of int64 range")
}
*res = int64(vc)
case BoolID:
*res = vc != 0
case StringID, DefaultID:
*res = strconv.FormatFloat(vc, 'G', -1, 64)
case DateTimeID:
secs := int64(vc)
fracSecs := vc - float64(secs)
nsecs := int64(fracSecs * nanoSecondsInSec)
*res = time.Unix(secs, nsecs).UTC()
default:
return to, cantConvert(fromID, toID)
}
}
case BoolID:
{
var vc bool
if len(data) == 0 || data[0] > 1 {
return to, errors.Errorf("Invalid value for bool %v", data)
}
vc = data[0] == 1
switch toID {
case BoolID:
*res = vc
case BinaryID:
*res = []byte{0}
if vc {
*res = []byte{1}
}
case IntID:
*res = int64(0)
if vc {
*res = int64(1)
}
case FloatID:
*res = float64(0)
if vc {
*res = float64(1)
}
case StringID, DefaultID:
*res = strconv.FormatBool(vc)
default:
return to, cantConvert(fromID, toID)
}
}
case DateTimeID:
{
var t time.Time
if err := t.UnmarshalBinary(data); err != nil {
return to, err
}
switch toID {
case DateTimeID:
*res = t
case BinaryID:
r, err := t.MarshalBinary()
if err != nil {
return to, err
}
*res = r
case StringID, DefaultID:
val, err := t.MarshalText()
if err != nil {
return to, err
}
*res = string(val)
case IntID:
*res = t.Unix()
case FloatID:
*res = float64(t.UnixNano()) / float64(nanoSecondsInSec)
default:
return to, cantConvert(fromID, toID)
}
}
case GeoID:
{
vc, err := wkb.Unmarshal(data)
if err != nil {
return to, err
}
switch toID {
case GeoID:
*res = vc
case BinaryID:
r, err := wkb.Marshal(vc, binary.LittleEndian)
if err != nil {
return to, err
}
*res = r
case StringID, DefaultID:
val, err := geojson.Marshal(vc)
if err != nil {
return to, nil
}
*res = string(bytes.Replace(val, []byte("\""), []byte("'"), -1))
default:
return to, cantConvert(fromID, toID)
}
}
case PasswordID:
{
vc := string(data)
switch toID {
case BinaryID:
*res = []byte(vc)
case StringID, PasswordID:
*res = vc
default:
return to, cantConvert(fromID, toID)
}
}
default:
return to, cantConvert(fromID, toID)
}
return to, nil
}
func Marshal(from Val, to *Val) error {
if to == nil {
return errors.Errorf("Invalid conversion %s to nil", from.Tid.Name())
}
fromID := from.Tid
toID := to.Tid
val := from.Value
res := &to.Value
// This is a default value from sg.fillVars, don't convert it's empty.
// Fixes issue #2980.
if val == nil {
*to = ValueForType(toID)
return nil
}
switch fromID {
case BinaryID:
vc := val.([]byte)
switch toID {
case StringID, DefaultID:
*res = string(vc)
case BinaryID:
*res = vc
default:
return cantConvert(fromID, toID)
}
case StringID, DefaultID:
vc := val.(string)
switch toID {
case StringID, DefaultID:
*res = vc
case BinaryID:
*res = []byte(vc)
default:
return cantConvert(fromID, toID)
}
case IntID:
vc := val.(int64)
switch toID {
case StringID, DefaultID:
*res = strconv.FormatInt(vc, 10)
case BinaryID:
var bs [8]byte
binary.LittleEndian.PutUint64(bs[:], uint64(vc))
*res = bs[:]
default:
return cantConvert(fromID, toID)
}
case FloatID:
vc := val.(float64)
switch toID {
case StringID, DefaultID:
*res = strconv.FormatFloat(vc, 'G', -1, 64)
case BinaryID:
var bs [8]byte
u := math.Float64bits(vc)
binary.LittleEndian.PutUint64(bs[:], u)
*res = bs[:]
default:
return cantConvert(fromID, toID)
}
case BoolID:
vc := val.(bool)
switch toID {
case StringID, DefaultID:
*res = strconv.FormatBool(vc)
case BinaryID:
*res = []byte{0}
if vc {
*res = []byte{1}
}
default:
return cantConvert(fromID, toID)
}
case DateTimeID:
vc := val.(time.Time)
switch toID {
case StringID, DefaultID:
val, err := vc.MarshalText()
if err != nil {
return err
}
*res = string(val)
case BinaryID:
r, err := vc.MarshalBinary()
if err != nil {
return err
}
*res = r
default:
return cantConvert(fromID, toID)
}
case GeoID:
vc, ok := val.(geom.T)
if !ok {
return errors.Errorf("Expected a Geo type")
}
switch toID {
case BinaryID:
r, err := wkb.Marshal(vc, binary.LittleEndian)
if err != nil {
return err
}
*res = r
case StringID, DefaultID:
val, err := geojson.Marshal(vc)
if err != nil {
return nil
}
*res = string(bytes.Replace(val, []byte("\""), []byte("'"), -1))
default:
return cantConvert(fromID, toID)
}
case PasswordID:
vc := val.(string)
switch toID {
case StringID:
*res = vc
case BinaryID:
*res = []byte(vc)
default:
return cantConvert(fromID, toID)
}
default:
return cantConvert(fromID, toID)
}
return nil
}
// ObjectValue converts into api.Value.
func ObjectValue(id TypeID, value interface{}) (*api.Value, error) {
def := &api.Value{Val: &api.Value_StrVal{StrVal: ""}}
var ok bool
// Lets set the object value according to the storage type.
switch id {
case StringID:
var v string
if v, ok = value.(string); !ok {
return def, errors.Errorf("Expected value of type string. Got : %v", value)
}
return &api.Value{Val: &api.Value_StrVal{StrVal: v}}, nil
case DefaultID:
var v string
if v, ok = value.(string); !ok {
return def, errors.Errorf("Expected value of type string. Got : %v", value)
}
return &api.Value{Val: &api.Value_DefaultVal{DefaultVal: v}}, nil
case IntID:
var v int64
if v, ok = value.(int64); !ok {
return def, errors.Errorf("Expected value of type int64. Got : %v", value)
}
return &api.Value{Val: &api.Value_IntVal{IntVal: v}}, nil
case FloatID:
var v float64
if v, ok = value.(float64); !ok {
return def, errors.Errorf("Expected value of type float64. Got : %v", value)
}
return &api.Value{Val: &api.Value_DoubleVal{DoubleVal: v}}, nil
case BoolID:
var v bool
if v, ok = value.(bool); !ok {
return def, errors.Errorf("Expected value of type bool. Got : %v", value)
}
return &api.Value{Val: &api.Value_BoolVal{BoolVal: v}}, nil
case BinaryID:
var v []byte
if v, ok = value.([]byte); !ok {
return def, errors.Errorf("Expected value of type []byte. Got : %v", value)
}
return &api.Value{Val: &api.Value_BytesVal{BytesVal: v}}, nil
// Geo and datetime are stored in binary format in the N-Quad, so lets
// convert them here.
case GeoID:
b, err := toBinary(id, value)
if err != nil {
return def, err
}
return &api.Value{Val: &api.Value_GeoVal{GeoVal: b}}, nil
case DateTimeID:
b, err := toBinary(id, value)
if err != nil {
return def, err
}
return &api.Value{Val: &api.Value_DatetimeVal{DatetimeVal: b}}, nil
case PasswordID:
var v string
if v, ok = value.(string); !ok {
return def, errors.Errorf("Expected value of type password. Got : %v", value)
}
return &api.Value{Val: &api.Value_PasswordVal{PasswordVal: v}}, nil
default:
return def, errors.Errorf("ObjectValue not available for: %v", id)
}
}
func toBinary(id TypeID, b interface{}) ([]byte, error) {
p1 := ValueForType(BinaryID)
if err := Marshal(Val{id, b}, &p1); err != nil {
return nil, err
}
return p1.Value.([]byte), nil
}
func cantConvert(from TypeID, to TypeID) error {
return errors.Errorf("Cannot convert %s to type %s", from.Name(), to.Name())
}
// MarshalJSON makes Val satisfy the json.Marshaler interface.
func (v Val) MarshalJSON() ([]byte, error) {
switch v.Tid {
case IntID:
return json.Marshal(v.Value.(int64))
case BoolID:
return json.Marshal(v.Value.(bool))
case FloatID:
return json.Marshal(v.Value.(float64))
case DateTimeID:
return json.Marshal(v.Value.(time.Time))
case GeoID:
return geojson.Marshal(v.Value.(geom.T))
case StringID, DefaultID:
return json.Marshal(v.Safe().(string))
case PasswordID:
return json.Marshal(v.Value.(string))
}
return nil, errors.Errorf("Invalid type for MarshalJSON: %v", v.Tid)
}