/
result.go
225 lines (194 loc) · 6.48 KB
/
result.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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 gremlingo
import (
"fmt"
"reflect"
"strconv"
)
// Result Struct to abstract the Result and provide functions to use it.
type Result struct {
result interface{}
}
// String returns the string representation of the Result struct in Go-syntax format.
func (r *Result) String() string {
return fmt.Sprintf("result{object=%v class=%T}", r.result, r.result)
}
// GetString gets the string representation of the result.
func (r *Result) GetString() string {
return fmt.Sprintf("%v", r.result)
}
// GetInt gets the result by coercing it into an int, else returns an error if not parsable.
func (r *Result) GetInt() (int, error) {
return strconv.Atoi(r.GetString())
}
// GetByte gets the result by coercing it into a byte (uint8), else returns an error if not parsable.
func (r *Result) GetByte() (byte, error) {
res, err := strconv.ParseUint(r.GetString(), 10, 8)
if err != nil {
return 0, err
}
return byte(res), nil
}
// GetUint gets the result by coercing it into an int, else returns an error if not parsable.
func (r *Result) GetUint() (uint, error) {
res, err := strconv.ParseUint(r.GetString(), 10, 64)
if err != nil {
return 0, err
}
return uint(res), nil
}
// GetUint16 gets the result by coercing it into an int16, else returns an error if not parsable.
func (r *Result) GetUint16() (uint16, error) {
res, err := strconv.ParseUint(r.GetString(), 10, 16)
if err != nil {
return 0, err
}
return uint16(res), nil
}
// GetUint32 gets the result by coercing it into a rune(int32), else returns an error if not parsable.
func (r *Result) GetUint32() (uint32, error) {
res, err := strconv.ParseUint(r.GetString(), 10, 32)
if err != nil {
return 0, err
}
return uint32(res), nil
}
// GetUint64 gets the result by coercing it into an int64, else returns an error if not parsable.
func (r *Result) GetUint64() (uint64, error) {
return strconv.ParseUint(r.GetString(), 10, 64)
}
// GetInt8 gets the result by coercing it into an int16, else returns an error if not parsable.
func (r *Result) GetInt8() (int8, error) {
res, err := strconv.ParseInt(r.GetString(), 10, 8)
if err != nil {
return 0, err
}
return int8(res), nil
}
// GetInt16 gets the result by coercing it into an int16, else returns an error if not parsable.
func (r *Result) GetInt16() (int16, error) {
res, err := strconv.ParseInt(r.GetString(), 10, 16)
if err != nil {
return 0, err
}
return int16(res), nil
}
// GetInt32 gets the result by coercing it into a rune(int32), else returns an error if not parsable.
func (r *Result) GetInt32() (int32, error) {
res, err := strconv.ParseInt(r.GetString(), 10, 32)
if err != nil {
return 0, err
}
return int32(res), nil
}
// GetInt64 gets the result by coercing it into an int64, else returns an error if not parsable.
func (r *Result) GetInt64() (int64, error) {
return strconv.ParseInt(r.GetString(), 10, 64)
}
// GetFloat32 gets the result by coercing it into a float32, else returns an error if not parsable.
func (r *Result) GetFloat32() (float32, error) {
res, err := strconv.ParseFloat(r.GetString(), 32)
if err != nil {
return 0, err
}
return float32(res), nil
}
// GetFloat64 gets the result by coercing it into a float64, else returns an error if not parsable.
func (r *Result) GetFloat64() (float64, error) {
return strconv.ParseFloat(r.GetString(), 64)
}
// GetBool gets the result by coercing it into a boolean, else returns an error if not parsable.
func (r *Result) GetBool() (bool, error) {
return strconv.ParseBool(r.GetString())
}
// IsNil checks if the result is null.
func (r *Result) IsNil() bool {
return nil == r.result
}
// GetVertex returns the result if it is a Vertex, otherwise returns an error.
func (r *Result) GetVertex() (*Vertex, error) {
res, ok := r.result.(*Vertex)
if !ok {
return nil, newError(err0601ResultNotVertexError)
}
return res, nil
}
// GetEdge returns the result if it is an edge, otherwise returns an error.
func (r *Result) GetEdge() (*Edge, error) {
res, ok := r.result.(*Edge)
if !ok {
return nil, newError(err0602ResultNotEdgeError)
}
return res, nil
}
// GetElement returns the result if it is an Element, otherwise returns an error.
func (r *Result) GetElement() (*Element, error) {
res, ok := r.result.(*Element)
if !ok {
return nil, newError(err0603ResultNotElementError)
}
return res, nil
}
// GetPath returns the result if it is a path, otherwise returns an error.
func (r *Result) GetPath() (*Path, error) {
res, ok := r.result.(*Path)
if !ok {
return nil, newError(err0604ResultNotPathError)
}
return res, nil
}
// GetProperty returns the result if it is a property, otherwise returns an error.
func (r *Result) GetProperty() (*Property, error) {
res, ok := r.result.(*Property)
if !ok {
return nil, newError(err0605ResultNotPropertyError)
}
return res, nil
}
// GetVertexProperty returns the result if it is a Vertex property, otherwise returns an error.
func (r *Result) GetVertexProperty() (*VertexProperty, error) {
res, ok := r.result.(*VertexProperty)
if !ok {
return nil, newError(err0606ResultNotVertexPropertyError)
}
return res, nil
}
// GetTraverser returns the Result if it is a Traverser, otherwise returns an error.
func (r *Result) GetTraverser() (*Traverser, error) {
res, ok := r.result.(Traverser)
if !ok {
return nil, newError(err0607ResultNotTraverserError)
}
return &res, nil
}
// GetSlice returns the Result if it is a Slice, otherwise returns an error.
func (r *Result) GetSlice() (*[]interface{}, error) {
res, ok := r.result.([]interface{})
if !ok {
return nil, newError(err0608ResultNotSliceError)
}
return &res, nil
}
// GetType returns the type of the result.
func (r *Result) GetType() reflect.Type {
return reflect.TypeOf(r.result)
}
// GetInterface returns the result item.
func (r *Result) GetInterface() interface{} {
return r.result
}