-
Notifications
You must be signed in to change notification settings - Fork 9
/
reflect.go
49 lines (43 loc) · 933 Bytes
/
reflect.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
package jsonapi
import (
"fmt"
"reflect"
"runtime/debug"
)
func derefValue(v reflect.Value) reflect.Value {
switch v.Kind() {
case reflect.Pointer:
if v.Elem().Kind() == reflect.Invalid {
v.Set(reflect.New(v.Type().Elem()))
return v.Elem()
}
return derefValue(v.Elem())
}
return v
}
func derefType(t reflect.Type) reflect.Type {
switch t.Kind() {
case reflect.Pointer:
return derefType(t.Elem())
}
return t
}
func recoverError(rvr any) error {
var err error
switch e := rvr.(type) {
case error:
err = fmt.Errorf("unknown error: %w %s", e, debug.Stack())
default:
err = fmt.Errorf("%v %s", e, debug.Stack())
}
return err
}
func setFieldValue(fv reflect.Value, v any) {
vv := reflect.ValueOf(v)
// if the field is not a pointer, dereference the value fully in case
// it is a pointer (likely returned by reflect.New)
if fv.Kind() != reflect.Pointer {
vv = derefValue(vv)
}
fv.Set(vv)
}