-
Notifications
You must be signed in to change notification settings - Fork 0
/
description.go
82 lines (75 loc) · 2.11 KB
/
description.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
package function
import (
"fmt"
"reflect"
"strconv"
"strings"
)
type Description interface {
Name() string
String() string
NumArgs() int
ContextArg() bool
NumResults() int
ErrorResult() bool
ArgNames() []string
ArgDescriptions() []string
ArgTypes() []reflect.Type
ResultTypes() []reflect.Type
}
func ReflectDescription(name string, f any) (Description, error) {
t := reflect.ValueOf(f).Type()
if t.Kind() != reflect.Func {
return nil, fmt.Errorf("%s passed instead of a function", t)
}
info := &description{
name: name,
argNames: make([]string, t.NumIn()),
argDescriptions: make([]string, t.NumIn()),
argTypes: make([]reflect.Type, t.NumIn()),
resultTypes: make([]reflect.Type, t.NumOut()),
}
for i := range info.argTypes {
info.argNames[i] = "a" + strconv.Itoa(i)
info.argTypes[i] = t.In(i)
}
for i := range info.resultTypes {
info.resultTypes[i] = t.Out(i)
}
return info, nil
}
type description struct {
name string
argNames []string
argDescriptions []string
argTypes []reflect.Type
resultTypes []reflect.Type
}
func (f *description) Name() string { return f.name }
func (f *description) String() string {
var b strings.Builder
b.WriteString(f.name)
b.WriteByte('(')
for i, argName := range f.argNames {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(argName)
b.WriteByte(' ')
b.WriteString(f.argTypes[i].String())
}
b.WriteByte(')')
return b.String()
}
func (f *description) NumArgs() int { return len(f.argNames) }
func (f *description) ContextArg() bool {
return len(f.argTypes) > 0 && f.argTypes[0].String() == "context.Context"
}
func (f *description) NumResults() int { return len(f.resultTypes) }
func (f *description) ErrorResult() bool {
return len(f.resultTypes) > 0 && f.resultTypes[0].String() == "error"
}
func (f *description) ArgNames() []string { return f.argNames }
func (f *description) ArgDescriptions() []string { return f.argDescriptions }
func (f *description) ArgTypes() []reflect.Type { return f.argTypes }
func (f *description) ResultTypes() []reflect.Type { return f.resultTypes }