-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord.go
More file actions
284 lines (254 loc) · 6.44 KB
/
Copy pathrecord.go
File metadata and controls
284 lines (254 loc) · 6.44 KB
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
package query
import (
"fmt"
"strings"
"time"
"github.com/neo4j-contrib/query-go-sdk/internal/decode"
)
// Record represents a single row in a query result with named field access.
// It mirrors neo4j.Record in the Go driver for a consistent developer experience.
type Record struct {
keys []string
values []any
}
func newRecord(keys []string, values []any) *Record {
return &Record{keys: keys, values: values}
}
// Get returns the value for the named field and whether the field exists.
// A field that is present but Null returns (nil, true).
// The returned value is one of the types documented on decode.DecodeValue.
func (r *Record) Get(field string) (any, bool) {
for i, k := range r.keys {
if k == field {
if i < len(r.values) {
return r.values[i], true
}
return nil, false
}
}
return nil, false
}
// AsMap returns all fields as a map. Mirrors neo4j.Record.AsMap().
func (r *Record) AsMap() map[string]any {
m := make(map[string]any, len(r.keys))
for i, k := range r.keys {
if i < len(r.values) {
m[k] = r.values[i]
}
}
return m
}
// Keys returns the ordered list of field names.
func (r *Record) Keys() []string {
out := make([]string, len(r.keys))
copy(out, r.keys)
return out
}
// Values returns the ordered list of decoded values.
func (r *Record) Values() []any {
out := make([]any, len(r.values))
copy(out, r.values)
return out
}
// IsNull reports whether the named field exists and has a Null value.
// Use this to distinguish "field is null" from "field not in result".
func (r *Record) IsNull(field string) bool {
v, ok := r.Get(field)
return ok && v == nil
}
// String returns a human-readable representation, useful for debugging.
func (r *Record) String() string {
var b strings.Builder
b.WriteString("{")
for i, k := range r.keys {
if i > 0 {
b.WriteString(", ")
}
var v any
if i < len(r.values) {
v = r.values[i]
}
fmt.Fprintf(&b, "%s: %s", k, formatValue(v))
}
b.WriteString("}")
return b.String()
}
// ============================================================================
// Typed accessors
// ============================================================================
func (r *Record) GetString(field string) (string, bool) {
v, ok := r.Get(field)
if !ok || v == nil {
return "", false
}
s, ok := v.(string)
return s, ok
}
func (r *Record) GetInt64(field string) (int64, bool) {
v, ok := r.Get(field)
if !ok || v == nil {
return 0, false
}
n, ok := v.(int64)
return n, ok
}
func (r *Record) GetFloat64(field string) (float64, bool) {
v, ok := r.Get(field)
if !ok || v == nil {
return 0, false
}
f, ok := v.(float64)
return f, ok
}
func (r *Record) GetBool(field string) (bool, bool) {
v, ok := r.Get(field)
if !ok || v == nil {
return false, false
}
b, ok := v.(bool)
return b, ok
}
func (r *Record) GetBytes(field string) ([]byte, bool) {
v, ok := r.Get(field)
if !ok || v == nil {
return nil, false
}
b, ok := v.([]byte)
return b, ok
}
func (r *Record) GetTime(field string) (time.Time, bool) {
v, ok := r.Get(field)
if !ok || v == nil {
return time.Time{}, false
}
t, ok := v.(time.Time)
return t, ok
}
func (r *Record) GetDuration(field string) (decode.Duration, bool) {
v, ok := r.Get(field)
if !ok || v == nil {
return decode.Duration{}, false
}
d, ok := v.(decode.Duration)
return d, ok
}
func (r *Record) GetPoint(field string) (decode.Point, bool) {
v, ok := r.Get(field)
if !ok || v == nil {
return decode.Point{}, false
}
p, ok := v.(decode.Point)
return p, ok
}
func (r *Record) GetNode(field string) (*decode.Node, bool) {
v, ok := r.Get(field)
if !ok || v == nil {
return nil, false
}
n, ok := v.(*decode.Node)
return n, ok
}
func (r *Record) GetRelationship(field string) (*decode.Relationship, bool) {
v, ok := r.Get(field)
if !ok || v == nil {
return nil, false
}
rel, ok := v.(*decode.Relationship)
return rel, ok
}
func (r *Record) GetPath(field string) (decode.Path, bool) {
v, ok := r.Get(field)
if !ok || v == nil {
return decode.Path{}, false
}
p, ok := v.(decode.Path)
return p, ok
}
func (r *Record) GetList(field string) ([]any, bool) {
v, ok := r.Get(field)
if !ok || v == nil {
return nil, false
}
l, ok := v.([]any)
return l, ok
}
func (r *Record) GetMap(field string) (map[string]any, bool) {
v, ok := r.Get(field)
if !ok || v == nil {
return nil, false
}
m, ok := v.(map[string]any)
return m, ok
}
// ============================================================================
// List element helpers
// ============================================================================
// StringList converts a []any list to []string.
// Returns nil, false if any element is not a string.
func StringList(list []any) ([]string, bool) {
out := make([]string, len(list))
for i, v := range list {
s, ok := v.(string)
if !ok {
return nil, false
}
out[i] = s
}
return out, true
}
// Int64List converts a []any list to []int64.
// Returns nil, false if any element is not an int64.
func Int64List(list []any) ([]int64, bool) {
out := make([]int64, len(list))
for i, v := range list {
n, ok := v.(int64)
if !ok {
return nil, false
}
out[i] = n
}
return out, true
}
// Float64List converts a []any list to []float64.
// Returns nil, false if any element is not a float64.
func Float64List(list []any) ([]float64, bool) {
out := make([]float64, len(list))
for i, v := range list {
f, ok := v.(float64)
if !ok {
return nil, false
}
out[i] = f
}
return out, true
}
// ============================================================================
// formatValue — shared by Record.String() and EagerResult.String()
// ============================================================================
func formatValue(v any) string {
if v == nil {
return "<null>"
}
switch t := v.(type) {
case *decode.Node:
return fmt.Sprintf("Node{id:%s labels:%v props:%v}", t.ElementID, t.Labels, t.Properties)
case *decode.Relationship:
return fmt.Sprintf("Rel{id:%s type:%s props:%v}", t.ElementID, t.Type, t.Properties)
case decode.Path:
return fmt.Sprintf("Path{nodes:%d rels:%d}", len(t.Nodes), len(t.Relationships))
case []any:
parts := make([]string, len(t))
for i, elem := range t {
parts[i] = formatValue(elem)
}
return "[" + strings.Join(parts, ", ") + "]"
case map[string]any:
parts := make([]string, 0, len(t))
for k, val := range t {
parts = append(parts, k+"="+formatValue(val))
}
return "{" + strings.Join(parts, ", ") + "}"
default:
return fmt.Sprintf("%v", v)
}
}