forked from henrybear327/Proton-API-Bridge
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcompat_reflect.go
More file actions
116 lines (95 loc) · 2.73 KB
/
compat_reflect.go
File metadata and controls
116 lines (95 loc) · 2.73 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
package proton_api_bridge
import (
"errors"
"fmt"
"reflect"
)
type methodCompatibilityError struct {
err error
}
func (e *methodCompatibilityError) Error() string {
return e.err.Error()
}
func (e *methodCompatibilityError) Unwrap() error {
return e.err
}
func newMethodCompatibilityError(format string, args ...any) error {
return &methodCompatibilityError{err: fmt.Errorf(format, args...)}
}
func isMethodCompatibilityError(err error) bool {
var compatErr *methodCompatibilityError
return errors.As(err, &compatErr)
}
func findAndCallMethod(target any, methodName string, args ...any) (_ []reflect.Value, called bool, err error) {
defer func() {
if recovered := recover(); recovered != nil {
called = true
err = fmt.Errorf("%s panic: %v", methodName, recovered)
}
}()
if target == nil {
return nil, false, nil
}
targetValue := reflect.ValueOf(target)
if !targetValue.IsValid() {
return nil, false, nil
}
method := targetValue.MethodByName(methodName)
if !method.IsValid() {
return nil, false, nil
}
methodType := method.Type()
if methodType.NumIn() != len(args) {
return nil, true, newMethodCompatibilityError("%s has incompatible argument count", methodName)
}
callArgs := make([]reflect.Value, len(args))
for i := range args {
paramType := methodType.In(i)
argValue, err := getCallableValue(paramType, args[i])
if err != nil {
return nil, true, newMethodCompatibilityError("%s argument %d: %w", methodName, i, err)
}
callArgs[i] = argValue
}
return method.Call(callArgs), true, nil
}
func getCallableValue(paramType reflect.Type, arg any) (reflect.Value, error) {
if arg == nil {
switch paramType.Kind() {
case reflect.Interface, reflect.Pointer, reflect.Map, reflect.Slice, reflect.Func, reflect.Chan:
return reflect.Zero(paramType), nil
default:
return reflect.Value{}, fmt.Errorf("nil not assignable to %s", paramType)
}
}
v := reflect.ValueOf(arg)
if v.Type().AssignableTo(paramType) {
return v, nil
}
if v.Type().ConvertibleTo(paramType) {
return v.Convert(paramType), nil
}
return reflect.Value{}, fmt.Errorf("%s not assignable to %s", v.Type(), paramType)
}
func extractErrorResult(value reflect.Value) (error, error) {
errorType := reflect.TypeOf((*error)(nil)).Elem()
if !value.Type().Implements(errorType) {
return nil, fmt.Errorf("result does not implement error")
}
if isNilableKind(value.Kind()) && value.IsNil() {
return nil, nil
}
err, ok := value.Interface().(error)
if !ok {
return nil, fmt.Errorf("result cannot be converted to error")
}
return err, nil
}
func isNilableKind(kind reflect.Kind) bool {
switch kind {
case reflect.Interface, reflect.Pointer, reflect.Map, reflect.Slice, reflect.Func, reflect.Chan:
return true
default:
return false
}
}