forked from neo4j/neo4j-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
values_spatial_handlers.go
117 lines (101 loc) · 2.8 KB
/
values_spatial_handlers.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
/*
* Copyright (c) 2002-2019 "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 neo4j
import (
"math"
"reflect"
"github.com/neo4j-drivers/gobolt"
)
const (
point2DSignature int16 = 'X'
point2DSize = 3
point3DSignature int16 = 'Y'
point3DSize = 4
)
type pointValueHandler struct {
}
func (handler *pointValueHandler) ReadableStructs() []int16 {
return []int16{point2DSignature, point3DSignature}
}
func (handler *pointValueHandler) WritableTypes() []reflect.Type {
return []reflect.Type{reflect.TypeOf(Point{}), reflect.TypeOf(&Point{})}
}
func (handler *pointValueHandler) Read(signature int16, values []interface{}) (interface{}, error) {
var (
dimension int
srId int
x, y, z float64
)
switch signature {
case point2DSignature:
if len(values) != point2DSize {
return nil, gobolt.NewValueHandlerError("expected Point2D struct to have %d fields but received %d", point2DSize, len(values))
}
dimension = 2
srId = int(values[0].(int64))
x = values[1].(float64)
y = values[2].(float64)
z = math.NaN()
case point3DSignature:
if len(values) != point3DSize {
return nil, gobolt.NewValueHandlerError("expected Point3D struct to have %d fields but received %d", point3DSize, len(values))
}
dimension = 3
srId = int(values[0].(int64))
x = values[1].(float64)
y = values[2].(float64)
z = values[3].(float64)
default:
return nil, gobolt.NewValueHandlerError("unexpected struct signature provided to PointValueHandler: %#x", signature)
}
return &Point{
dimension: dimension,
srId: srId,
x: x,
y: y,
z: z,
}, nil
}
func (handler *pointValueHandler) Write(value interface{}) (int16, []interface{}, error) {
var point *Point
var ok bool
if point, ok = value.(*Point); !ok {
if pointVal, ok := value.(Point); ok {
point = &pointVal
}
}
if point != nil {
switch point.dimension {
case 2:
return point2DSignature, []interface{}{
point.srId,
point.x,
point.y,
}, nil
case 3:
return point3DSignature, []interface{}{
point.srId,
point.x,
point.y,
point.z,
}, nil
}
}
return 0, nil, gobolt.NewValueHandlerError("passed in value %v is not supported by PointValueHandler", value)
}