-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.go
47 lines (38 loc) · 1.09 KB
/
type.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
package staticplug
import (
"fmt"
"os"
"reflect"
)
// TypeOfInterface returns the reflection type representing the dynamic type of
// iface. Construct a nil value via (*Iface)(nil).
//
// Values of [reflect.Type] are used directly.
//
// Logically this function is equivalent to the following:
//
// reflect.TypeOf((*Iface)(nil)).Elem()
func TypeOfInterface(iface any) (reflect.Type, error) {
if t, ok := iface.(reflect.Type); ok {
if t != nil && t.Kind() == reflect.Interface {
return t, nil
}
return nil, fmt.Errorf("%w: type must be an interface, got %+v", os.ErrInvalid, t)
}
var msgKind string
t := reflect.TypeOf(iface)
if t != nil {
if t.Kind() == reflect.Ptr {
if t := t.Elem(); t.Kind() == reflect.Interface {
return t, nil
}
}
msgKind = fmt.Sprintf(" (kind %v)", t.Kind())
}
return nil, fmt.Errorf("%w: pointer to interface type is required, got %+v%s", os.ErrInvalid, t, msgKind)
}
// MustTypeOfInterface wraps [TypeOfInterface], converting all errors to
// panics.
func MustTypeOfInterface(iface any) reflect.Type {
return must1(TypeOfInterface(iface))
}