-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresult.go
More file actions
402 lines (361 loc) · 9.69 KB
/
result.go
File metadata and controls
402 lines (361 loc) · 9.69 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package flyt
import (
"encoding/json"
"fmt"
"reflect"
)
// Result is a wrapper type that provides convenient type assertion methods.
// It's used as return type for Prep and Exec methods to improve developer experience.
// It can hold any value and provides type-safe accessors.
type Result struct {
value any
err error // Store error if this result represents a failure
}
// NewResult creates a new Result from any value.
func NewResult(v any) Result {
return Result{value: v}
}
// NewErrorResult creates a new Result representing an error.
func NewErrorResult(err error) Result {
return Result{err: err}
}
// R is a shorthand for NewResult.
func R(v any) Result {
return NewResult(v)
}
// AsString retrieves the Result as a string.
// Returns empty string and false if the Result value is nil or not a string.
func (r Result) AsString() (string, bool) {
if r.value == nil {
return "", false
}
s, ok := r.value.(string)
return s, ok
}
// AsStringOr retrieves the Result as a string.
// Returns the provided default value if the Result is nil or not a string.
func (r Result) AsStringOr(defaultVal string) string {
s, ok := r.AsString()
if !ok {
return defaultVal
}
return s
}
// MustString retrieves the Result as a string.
// Panics if the Result is nil or not a string.
func (r Result) MustString() string {
s, ok := r.AsString()
if !ok {
panic(fmt.Sprintf("Result.MustString: value is not a string (type %T)", r))
}
return s
}
// AsInt retrieves the Result as an int.
// Returns 0 and false if the Result value is nil or cannot be converted to int.
// Supports conversion from int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, and float types.
func (r Result) AsInt() (int, bool) {
if r.value == nil {
return 0, false
}
switch v := r.value.(type) {
case int:
return v, true
case int8:
return int(v), true
case int16:
return int(v), true
case int32:
return int(v), true
case int64:
return int(v), true
case uint:
return int(v), true
case uint8:
return int(v), true
case uint16:
return int(v), true
case uint32:
return int(v), true
case uint64:
return int(v), true
case float32:
return int(v), true
case float64:
return int(v), true
default:
return 0, false
}
}
// AsIntOr retrieves the Result as an int.
// Returns the provided default value if the Result is nil or cannot be converted to int.
func (r Result) AsIntOr(defaultVal int) int {
i, ok := r.AsInt()
if !ok {
return defaultVal
}
return i
}
// MustInt retrieves the Result as an int.
// Panics if the Result is nil or cannot be converted to int.
func (r Result) MustInt() int {
i, ok := r.AsInt()
if !ok {
panic(fmt.Sprintf("Result.MustInt: value cannot be converted to int (type %T)", r))
}
return i
}
// AsFloat64 retrieves the Result as a float64.
// Returns 0.0 and false if the Result value is nil or cannot be converted to float64.
// Supports conversion from various numeric types.
func (r Result) AsFloat64() (float64, bool) {
if r.value == nil {
return 0.0, false
}
switch v := r.value.(type) {
case float64:
return v, true
case float32:
return float64(v), true
case int:
return float64(v), true
case int8:
return float64(v), true
case int16:
return float64(v), true
case int32:
return float64(v), true
case int64:
return float64(v), true
case uint:
return float64(v), true
case uint8:
return float64(v), true
case uint16:
return float64(v), true
case uint32:
return float64(v), true
case uint64:
return float64(v), true
default:
return 0.0, false
}
}
// AsFloat64Or retrieves the Result as a float64.
// Returns the provided default value if the Result is nil or cannot be converted to float64.
func (r Result) AsFloat64Or(defaultVal float64) float64 {
f, ok := r.AsFloat64()
if !ok {
return defaultVal
}
return f
}
// MustFloat64 retrieves the Result as a float64.
// Panics if the Result is nil or cannot be converted to float64.
func (r Result) MustFloat64() float64 {
f, ok := r.AsFloat64()
if !ok {
panic(fmt.Sprintf("Result.MustFloat64: value cannot be converted to float64 (type %T)", r))
}
return f
}
// AsBool retrieves the Result as a bool.
// Returns false and false if the Result value is nil or not a bool.
func (r Result) AsBool() (bool, bool) {
if r.value == nil {
return false, false
}
b, ok := r.value.(bool)
return b, ok
}
// AsBoolOr retrieves the Result as a bool.
// Returns the provided default value if the Result is nil or not a bool.
func (r Result) AsBoolOr(defaultVal bool) bool {
b, ok := r.AsBool()
if !ok {
return defaultVal
}
return b
}
// MustBool retrieves the Result as a bool.
// Panics if the Result is nil or not a bool.
func (r Result) MustBool() bool {
b, ok := r.AsBool()
if !ok {
panic(fmt.Sprintf("Result.MustBool: value is not a bool (type %T)", r))
}
return b
}
// AsSlice retrieves the Result as a []any slice.
// Returns nil and false if the Result value is nil or not a slice.
// Uses ToSlice to convert various slice types to []any.
func (r Result) AsSlice() ([]any, bool) {
if r.value == nil {
return nil, false
}
// Check if it's already []any
if slice, ok := r.value.([]any); ok {
return slice, true
}
// Try to convert using ToSlice
result := ToSlice(r.value)
// ToSlice wraps non-slice values, so check if it's actually a slice
if len(result) == 1 && result[0] == r.value {
// ToSlice wrapped a non-slice value
return nil, false
}
return result, true
}
// AsSliceOr retrieves the Result as a []any slice.
// Returns the provided default value if the Result is nil or not a slice.
func (r Result) AsSliceOr(defaultVal []any) []any {
s, ok := r.AsSlice()
if !ok {
return defaultVal
}
return s
}
// MustSlice retrieves the Result as a []any slice.
// Panics if the Result is nil or not a slice.
func (r Result) MustSlice() []any {
s, ok := r.AsSlice()
if !ok {
panic(fmt.Sprintf("Result.MustSlice: value is not a slice (type %T)", r))
}
return s
}
// AsMap retrieves the Result as a map[string]any.
// Returns nil and false if the Result value is nil or not a map[string]any.
func (r Result) AsMap() (map[string]any, bool) {
if r.value == nil {
return nil, false
}
m, ok := r.value.(map[string]any)
return m, ok
}
// AsMapOr retrieves the Result as a map[string]any.
// Returns the provided default value if the Result is nil or not a map[string]any.
func (r Result) AsMapOr(defaultVal map[string]any) map[string]any {
m, ok := r.AsMap()
if !ok {
return defaultVal
}
return m
}
// MustMap retrieves the Result as a map[string]any.
// Panics if the Result is nil or not a map[string]any.
func (r Result) MustMap() map[string]any {
m, ok := r.AsMap()
if !ok {
panic(fmt.Sprintf("Result.MustMap: value is not a map[string]any (type %T)", r))
}
return m
}
// Bind binds the Result to a struct using JSON marshaling/unmarshaling.
// This allows for easy conversion of complex types.
// The destination must be a pointer to the target struct.
// Returns an error if the Result value is nil or binding fails.
//
// Example:
//
// type User struct {
// ID int `json:"id"`
// Name string `json:"name"`
// }
// var user User
// err := result.Bind(&user)
func (r Result) Bind(dest any) error {
if r.value == nil {
return fmt.Errorf("cannot bind nil Result value")
}
// Check if dest is a pointer
rv := reflect.ValueOf(dest)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return fmt.Errorf("destination must be a non-nil pointer")
}
// If Result value is already the correct type, assign directly
valType := reflect.TypeOf(r.value)
destType := rv.Type().Elem()
if valType == destType {
rv.Elem().Set(reflect.ValueOf(r.value))
return nil
}
// Otherwise use JSON as intermediate format
jsonBytes, err := json.Marshal(r.value)
if err != nil {
return fmt.Errorf("failed to marshal Result: %w", err)
}
if err := json.Unmarshal(jsonBytes, dest); err != nil {
return fmt.Errorf("failed to unmarshal to destination: %w", err)
}
return nil
}
// MustBind is like Bind but panics if binding fails.
// Use this only when binding failure should be considered a programming error.
//
// Example:
//
// var config Config
// result.MustBind(&config) // Panics if binding fails
func (r Result) MustBind(dest any) {
if err := r.Bind(dest); err != nil {
panic(fmt.Sprintf("Result.MustBind failed: %v", err))
}
}
// IsNil checks if the Result value is nil.
func (r Result) IsNil() bool {
return r.value == nil
}
// Type returns the underlying type of the Result value as a string.
// Returns "nil" if the Result value is nil.
func (r Result) Type() string {
if r.value == nil {
return "nil"
}
return fmt.Sprintf("%T", r.value)
}
// Value returns the underlying value as any.
// This is useful when you need to pass the Result to functions expecting any.
// Returns nil if the Result represents an error.
func (r Result) Value() any {
if r.err != nil {
return nil
}
return r.value
}
// IsError checks if the Result represents an error.
func (r Result) IsError() bool {
return r.err != nil
}
// Error returns the error if this Result represents a failure, nil otherwise.
func (r Result) Error() error {
return r.err
}
// As attempts to retrieve the Result as the specified type T.
// Returns the typed value and true if successful, or zero value and false if not.
//
// Example:
//
// if user, ok := result.As[*User](); ok {
// // Use typed user
// }
func As[T any](r Result) (T, bool) {
var zero T
if r.value == nil {
return zero, false
}
typed, ok := r.value.(T)
return typed, ok
}
// MustAs retrieves the Result as the specified type T.
// Panics if the Result is nil or not of type T.
//
// Example:
//
// user := MustAs[*User](result)
func MustAs[T any](r Result) T {
typed, ok := As[T](r)
if !ok {
panic(fmt.Sprintf("Result.MustAs: value is not of type %T", *new(T)))
}
return typed
}