-
Notifications
You must be signed in to change notification settings - Fork 22
/
commands_result.go
237 lines (218 loc) · 5.99 KB
/
commands_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
226
227
228
229
230
231
232
233
234
235
236
237
package orient
import (
"fmt"
"reflect"
)
var (
_ Results = errorResult{}
_ Results = (*unknownResult)(nil)
)
// Results is an interface for database command results. Must be closed.
//
// Individual results can be iterated in a next way:
//
// results := db.Command(cmd)
// if err := results.Err(); err != nil {
// // handle command errors; can be omitted and checked later with Err or Close
// }
// var one SomeStruct
// for results.Next(&one) {
// // process result, if any
// }
// if err := results.Close(); err != nil {
// // handle command and/or type conversion errors
// }
//
// Or just retrieved all at once:
//
// var arr []SomeStruct
// if err := results.All(&arr); err != nil {
// // handle command and/or type conversion errors
// }
//
// Some commands may return just one int/bool value:
//
// var affected int
// results.All(&affected)
//
// Also results can be handled manually:
//
// var out interface{}
// results.All(&out)
// switch out.(type) {
// case []OIdentifiable:
// // ...
// case *DocumentRecord:
// // ...
// }
//
type Results interface {
Err() error
Close() error
Next(result interface{}) bool
All(result interface{}) error
}
// errorResult is a simple result type that returns one specific error. Useful for server-side errors.
type errorResult struct {
err error
}
func (e errorResult) Err() error { return e.err }
func (e errorResult) Close() error { return e.err }
func (e errorResult) Next(result interface{}) bool { return false }
func (e errorResult) All(result interface{}) error { return e.err }
func newResults(o interface{}) Results {
return &unknownResult{result: o}
}
// unknownResult is a generic result type that uses reflection to iterate over returned records
type unknownResult struct {
err error
parsed bool
result interface{}
}
func (r *unknownResult) Err() error { return r.err }
func (r *unknownResult) Close() error { return r.err }
func (r *unknownResult) Next(result interface{}) bool { // TODO: implement
if r.parsed {
return false
}
r.parsed = true
r.All(result)
return false
}
func (r *unknownResult) All(result interface{}) error {
// if r.parsed {
// return fmt.Errorf("results are already parsed")
// }
// r.parsed = true
// check for pointer
targ := reflect.ValueOf(result)
if targ.Kind() != reflect.Ptr {
return fmt.Errorf("result is not a pointer: %T", result)
} else if targ.IsNil() {
return fmt.Errorf("nil result pointer")
}
targ = targ.Elem()
return convertTypes(targ, reflect.ValueOf(r.result))
}
type ErrUnsupportedConversion struct {
From reflect.Value
To reflect.Value
}
func (e ErrUnsupportedConversion) Error() string {
var a, b string
if e.From.IsValid() {
a = fmt.Sprintf("%v(%v)", e.From.Type(), e.From.Kind())
} else {
a = "<nil>"
}
if e.To.IsValid() {
b = fmt.Sprintf("%v(%v)", e.To.Type(), e.To.Kind())
} else {
b = "<nil>"
}
return fmt.Sprintf("unsupported conversion: %v -> %v", a, b)
}
func mapToStruct(m interface{}, val interface{}) error {
dec, err := newMapDecoder(val)
if err != nil {
return err
}
return dec.Decode(m)
}
const debugTypeConversion = false
func convertTypes(targ, src reflect.Value) error {
if debugTypeConversion {
fmt.Printf("conv: %T -> %T, %+v -> %+v\n", src.Interface(), targ.Interface(), src.Interface(), targ.Interface())
defer func() {
fmt.Printf("conv out: %T -> %T, %+v -> %+v\n", src.Interface(), targ.Interface(), src.Interface(), targ.Interface())
}()
}
if targ.Type() == src.Type() {
targ.Set(src)
return nil
} else if src.Type().ConvertibleTo(targ.Type()) {
targ.Set(src.Convert(targ.Type()))
return nil
} else if src.Kind() == reflect.Interface {
return convertTypes(targ, src.Elem())
}
// if targ.Kind() == reflect.Ptr {
// if targ.IsNil() {
// targ.Set(reflect.New(targ.Type().Elem()))
// }
// targ = targ.Elem()
// }
// if src.Kind() == reflect.Ptr {
// src = src.Elem()
// }
if targ.Kind() == reflect.Struct || (targ.Kind() == reflect.Ptr && targ.Type().Elem().Kind() == reflect.Struct) {
if targ.Kind() == reflect.Ptr && targ.IsNil() {
targ.Set(reflect.New(targ.Type().Elem()))
}
if src.Kind() == reflect.Map {
return mapToStruct(src.Interface(), targ.Addr().Interface())
}
} else if targ.Kind() == reflect.Slice {
if src.Kind() == reflect.Slice { // slice into slice
if targ.Len() != src.Len() {
targ.Set(reflect.MakeSlice(targ.Type(), src.Len(), src.Len()))
}
for i := 0; i < src.Len(); i++ {
if err := convertTypes(targ.Index(i), src.Index(i)); err != nil {
return err
}
}
return nil
}
// one value into slice
targ.Set(reflect.MakeSlice(targ.Type(), 1, 1))
if err := convertTypes(targ.Index(0), src); err != nil {
targ.Set(reflect.Zero(targ.Type()))
return err
}
return nil
} else if targ.Kind() == reflect.Map {
if src.Kind() == reflect.Map {
targ.Set(reflect.MakeMap(targ.Type()))
for _, k := range src.MapKeys() {
nk := reflect.New(targ.Type().Key()).Elem()
if err := convertTypes(nk, k); err != nil {
return err
}
nv := reflect.New(targ.Type().Elem()).Elem()
if err := convertTypes(nv, src.MapIndex(k)); err != nil {
return err
}
targ.SetMapIndex(nk, nv)
}
return nil
}
}
switch rec := src.Interface().(type) {
case MapSerializable:
m, err := rec.ToMap()
if err != nil {
return err
}
return convertTypes(targ, reflect.ValueOf(m))
case *Document: // Document implements DocumentSerializable for convenience, no need to convert it
case DocumentSerializable:
doc, err := rec.ToDocument()
if err != nil {
return err
}
return convertTypes(targ, reflect.ValueOf(doc))
}
// Target is now converted, process the result set
if src.Kind() == reflect.Slice {
switch src.Len() {
case 0:
return ErrNoRecord
case 1:
return convertTypes(targ, src.Index(0))
default:
return ErrMultipleRecords{N: src.Len(), Err: ErrUnsupportedConversion{From: src, To: targ}}
}
}
return ErrUnsupportedConversion{From: src, To: targ}
}