forked from vesoft-inc/nebula-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_wrapper.go
374 lines (338 loc) · 11.2 KB
/
value_wrapper.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
/* Copyright (c) 2020 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/
package nebula_go
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/vesoft-inc/nebula-go/v2/nebula"
)
type ValueWrapper struct {
value *nebula.Value
timezoneInfo timezoneInfo
}
func (valWrap ValueWrapper) IsEmpty() bool {
return valWrap.GetType() == "empty"
}
func (valWrap ValueWrapper) IsNull() bool {
return valWrap.value.IsSetNVal()
}
func (valWrap ValueWrapper) IsBool() bool {
return valWrap.value.IsSetBVal()
}
func (valWrap ValueWrapper) IsInt() bool {
return valWrap.value.IsSetIVal()
}
func (valWrap ValueWrapper) IsFloat() bool {
return valWrap.value.IsSetFVal()
}
func (valWrap ValueWrapper) IsString() bool {
return valWrap.value.IsSetSVal()
}
func (valWrap ValueWrapper) IsTime() bool {
return valWrap.value.IsSetTVal()
}
func (valWrap ValueWrapper) IsDate() bool {
return valWrap.value.IsSetDVal()
}
func (valWrap ValueWrapper) IsDateTime() bool {
return valWrap.value.IsSetDtVal()
}
func (valWrap ValueWrapper) IsList() bool {
return valWrap.value.IsSetLVal()
}
func (valWrap ValueWrapper) IsSet() bool {
return valWrap.value.IsSetUVal()
}
func (valWrap ValueWrapper) IsMap() bool {
return valWrap.value.IsSetMVal()
}
func (valWrap ValueWrapper) IsVertex() bool {
return valWrap.value.IsSetVVal()
}
func (valWrap ValueWrapper) IsEdge() bool {
return valWrap.value.IsSetEVal()
}
func (valWrap ValueWrapper) IsPath() bool {
return valWrap.value.IsSetPVal()
}
// AsNull converts the ValueWrapper to nebula.NullType
func (valWrap ValueWrapper) AsNull() (nebula.NullType, error) {
if valWrap.value.IsSetNVal() {
return valWrap.value.GetNVal(), nil
}
return -1, fmt.Errorf("failed to convert value %s to Null", valWrap.GetType())
}
// AsBool converts the ValueWrapper to a boolean value
func (valWrap ValueWrapper) AsBool() (bool, error) {
if valWrap.value.IsSetBVal() {
return valWrap.value.GetBVal(), nil
}
return false, fmt.Errorf("failed to convert value %s to bool", valWrap.GetType())
}
// AsInt converts the ValueWrapper to an int64
func (valWrap ValueWrapper) AsInt() (int64, error) {
if valWrap.value.IsSetIVal() {
return valWrap.value.GetIVal(), nil
}
return -1, fmt.Errorf("failed to convert value %s to int", valWrap.GetType())
}
// AsFloat converts the ValueWrapper to a float64
func (valWrap ValueWrapper) AsFloat() (float64, error) {
if valWrap.value.IsSetFVal() {
return valWrap.value.GetFVal(), nil
}
return -1, fmt.Errorf("failed to convert value %s to float", valWrap.GetType())
}
// AsString converts the ValueWrapper to a String
func (valWrap ValueWrapper) AsString() (string, error) {
if valWrap.value.IsSetSVal() {
return string(valWrap.value.GetSVal()), nil
}
return "", fmt.Errorf("failed to convert value %s to string", valWrap.GetType())
}
// AsTime converts the ValueWrapper to a TimeWrapper
func (valWrap ValueWrapper) AsTime() (*TimeWrapper, error) {
if valWrap.value.IsSetTVal() {
rawTime := valWrap.value.GetTVal()
time, err := genTimeWrapper(rawTime, valWrap.timezoneInfo)
if err != nil {
return nil, err
}
return time, nil
}
return nil, fmt.Errorf("failed to convert value %s to Time", valWrap.GetType())
}
// AsDate converts the ValueWrapper to a nebula.Date
func (valWrap ValueWrapper) AsDate() (*nebula.Date, error) {
if valWrap.value.IsSetDVal() {
return valWrap.value.GetDVal(), nil
}
return nil, fmt.Errorf("failed to convert value %s to Date", valWrap.GetType())
}
// AsDateTime converts the ValueWrapper to a DateTimeWrapper
func (valWrap ValueWrapper) AsDateTime() (*DateTimeWrapper, error) {
if valWrap.value.IsSetDtVal() {
rawTimeDate := valWrap.value.GetDtVal()
timeDate, err := genDateTimeWrapper(rawTimeDate, valWrap.timezoneInfo)
if err != nil {
return nil, err
}
return timeDate, nil
}
return nil, fmt.Errorf("failed to convert value %s to DateTime", valWrap.GetType())
}
// AsList converts the ValueWrapper to a slice of ValueWrapper
func (valWrap ValueWrapper) AsList() ([]ValueWrapper, error) {
if valWrap.value.IsSetLVal() {
var varList []ValueWrapper
vals := valWrap.value.GetLVal().Values
for _, val := range vals {
varList = append(varList, ValueWrapper{val, valWrap.timezoneInfo})
}
return varList, nil
}
return nil, fmt.Errorf("failed to convert value %s to List", valWrap.GetType())
}
// AsDedupList converts the ValueWrapper to a slice of ValueWrapper that has unique elements
func (valWrap ValueWrapper) AsDedupList() ([]ValueWrapper, error) {
if valWrap.value.IsSetUVal() {
var varList []ValueWrapper
vals := valWrap.value.GetUVal().Values
for _, val := range vals {
varList = append(varList, ValueWrapper{val, valWrap.timezoneInfo})
}
return varList, nil
}
return nil, fmt.Errorf("failed to convert value %s to set(deduped list)", valWrap.GetType())
}
// AsMap converts the ValueWrapper to a map of string and ValueWrapper
func (valWrap ValueWrapper) AsMap() (map[string]ValueWrapper, error) {
if valWrap.value.IsSetMVal() {
newMap := make(map[string]ValueWrapper)
kvs := valWrap.value.GetMVal().Kvs
for key, val := range kvs {
newMap[key] = ValueWrapper{val, valWrap.timezoneInfo}
}
return newMap, nil
}
return nil, fmt.Errorf("failed to convert value %s to Map", valWrap.GetType())
}
// AsNode converts the ValueWrapper to a Node
func (valWrap ValueWrapper) AsNode() (*Node, error) {
if !valWrap.value.IsSetVVal() {
return nil, fmt.Errorf("failed to convert value %s to Node, value is not an vertex", valWrap.GetType())
}
vertex := valWrap.value.VVal
node, err := genNode(vertex, valWrap.timezoneInfo)
if err != nil {
return nil, err
}
return node, nil
}
// AsRelationship converts the ValueWrapper to a Relationship
func (valWrap ValueWrapper) AsRelationship() (*Relationship, error) {
if !valWrap.value.IsSetEVal() {
return nil, fmt.Errorf("failed to convert value %s to Relationship, value is not an edge", valWrap.GetType())
}
edge := valWrap.value.EVal
relationship, err := genRelationship(edge, valWrap.timezoneInfo)
if err != nil {
return nil, err
}
return relationship, nil
}
// AsPath converts the ValueWrapper to a PathWrapper
func (valWrap ValueWrapper) AsPath() (*PathWrapper, error) {
if !valWrap.value.IsSetPVal() {
return nil, fmt.Errorf("failed to convert value %s to PathWrapper, value is not an edge", valWrap.GetType())
}
path, err := genPathWrapper(valWrap.value.PVal, valWrap.timezoneInfo)
if err != nil {
return nil, err
}
return path, nil
}
// GetType returns the value type of value in the valWrap as a string
func (valWrap ValueWrapper) GetType() string {
if valWrap.value.IsSetNVal() {
return "null"
} else if valWrap.value.IsSetBVal() {
return "bool"
} else if valWrap.value.IsSetIVal() {
return "int"
} else if valWrap.value.IsSetFVal() {
return "float"
} else if valWrap.value.IsSetSVal() {
return "string"
} else if valWrap.value.IsSetDVal() {
return "date"
} else if valWrap.value.IsSetTVal() {
return "time"
} else if valWrap.value.IsSetDtVal() {
return "datetime"
} else if valWrap.value.IsSetVVal() {
return "vertex"
} else if valWrap.value.IsSetEVal() {
return "edge"
} else if valWrap.value.IsSetPVal() {
return "path"
} else if valWrap.value.IsSetLVal() {
return "list"
} else if valWrap.value.IsSetMVal() {
return "map"
} else if valWrap.value.IsSetUVal() {
return "set"
}
return "empty"
}
// String() returns the value in the ValueWrapper as a string.
//
// Maps in the output will be sorted by key value in alphabetical order.
//
// For vetex, the output is in form (vid: tagName{propKey: propVal, propKey2, propVal2}),
// For edge, the output is in form (SrcVid)-[name]->(DstVid)@Ranking{prop1: val1, prop2: val2}
// where arrow direction depends on edgeType.
// For path, the output is in form (v1)-[name@edgeRanking]->(v2)-[name@edgeRanking]->(v3)
//
// For time, and dateTime, String returns the value calculated using the timezone offset
// from graph service by default.
func (valWrap ValueWrapper) String() string {
value := valWrap.value
if value.IsSetNVal() {
return value.GetNVal().String()
} else if value.IsSetBVal() {
return fmt.Sprintf("%t", value.GetBVal())
} else if value.IsSetIVal() {
return fmt.Sprintf("%d", value.GetIVal())
} else if value.IsSetFVal() {
fStr := strconv.FormatFloat(value.GetFVal(), 'f', -1, 64)
if !strings.Contains(fStr, ".") {
fStr = fStr + ".0"
}
return fStr
} else if value.IsSetSVal() {
return `"` + string(value.GetSVal()) + `"`
} else if value.IsSetDVal() { // Date yyyy-mm-dd
date := value.GetDVal()
dateWrapper, _ := genDateWrapper(date)
return fmt.Sprintf("%04d-%02d-%02d",
dateWrapper.getYear(),
dateWrapper.getMonth(),
dateWrapper.getDay())
} else if value.IsSetTVal() { // Time HH:MM:SS.MSMSMS
rawTime := value.GetTVal()
time, _ := genTimeWrapper(rawTime, valWrap.timezoneInfo)
localTime, _ := time.getLocalTime()
return fmt.Sprintf("%02d:%02d:%02d.%06d",
localTime.GetHour(),
localTime.GetMinute(),
localTime.GetSec(),
localTime.GetMicrosec())
} else if value.IsSetDtVal() { // DateTime yyyy-mm-ddTHH:MM:SS.MSMSMS
rawDateTime := value.GetDtVal()
dateTime, _ := genDateTimeWrapper(rawDateTime, valWrap.timezoneInfo)
localDateTime, _ := dateTime.getLocalDateTime()
return fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d.%06d",
localDateTime.GetYear(),
localDateTime.GetMonth(),
localDateTime.GetDay(),
localDateTime.GetHour(),
localDateTime.GetMinute(),
localDateTime.GetSec(),
localDateTime.GetMicrosec())
} else if value.IsSetVVal() { // Vertex format: ("VertexID" :tag1{k0: v0,k1: v1}:tag2{k2: v2})
vertex := value.GetVVal()
node, _ := genNode(vertex, valWrap.timezoneInfo)
return node.String()
} else if value.IsSetEVal() { // Edge format: [:edge src->dst @ranking {propKey1: propVal1}]
edge := value.GetEVal()
relationship, _ := genRelationship(edge, valWrap.timezoneInfo)
return relationship.String()
} else if value.IsSetPVal() {
// Path format:
// ("VertexID" :tag1{k0: v0,k1: v1})-
// [:TypeName@ranking {propKey1: propVal1}]->
// ("VertexID2" :tag1{k0: v0,k1: v1} :tag2{k2: v2})-
// [:TypeName@ranking {propKey2: propVal2}]->
// ("VertexID3" :tag1{k0: v0,k1: v1})
path := value.GetPVal()
pathWrap, _ := genPathWrapper(path, valWrap.timezoneInfo)
return pathWrap.String()
} else if value.IsSetLVal() { // List
lval := value.GetLVal()
var strs []string
for _, val := range lval.Values {
strs = append(strs, ValueWrapper{val, valWrap.timezoneInfo}.String())
}
return fmt.Sprintf("[%s]", strings.Join(strs, ", "))
} else if value.IsSetMVal() { // Map
// {k0: v0, k1: v1}
mval := value.GetMVal()
var keyList []string
var output []string
kvs := mval.Kvs
for k := range kvs {
keyList = append(keyList, k)
}
sort.Strings(keyList)
for _, k := range keyList {
output = append(output, fmt.Sprintf("%s: %s", k, ValueWrapper{kvs[k], valWrap.timezoneInfo}.String()))
}
return fmt.Sprintf("{%s}", strings.Join(output, ", "))
} else if value.IsSetUVal() {
// set to string
uval := value.GetUVal()
var strs []string
for _, val := range uval.Values {
strs = append(strs, ValueWrapper{val, valWrap.timezoneInfo}.String())
}
return fmt.Sprintf("[%s]", strings.Join(strs, ", "))
} else { // is empty
return ""
}
}