-
Notifications
You must be signed in to change notification settings - Fork 5
/
gen.go
55 lines (48 loc) · 1.07 KB
/
gen.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
package generic
import (
"reflect"
"fmt"
"crypto/sha1"
"encoding/base32"
"runtime"
)
func genName(typ reflect.Type) string {
if typ.PkgPath() != "" {
if typ.PkgPath() == state.pkgPath {
return typ.Name()
}
state.importPackages[typ.PkgPath()] = true
}
switch typ.Kind() {
case reflect.Ptr:
genName(typ.Elem())
case reflect.Slice:
genName(typ.Elem())
case reflect.Array:
genName(typ.Elem())
}
return typ.String()
}
func genKey(typ reflect.Type) reflect.Type {
return typ.Key()
}
func genElem(typ reflect.Type) reflect.Type {
return typ.Elem()
}
func genMethod(methodName string, typ reflect.Type) (*reflect.Method, error) {
method, found := typ.MethodByName(methodName)
if found {
return &method, nil
} else {
return nil, fmt.Errorf("method %s not found in %s", methodName, typ.String())
}
}
func genReturnType(method *reflect.Method) reflect.Type {
return method.Type.Out(0)
}
func hash(source string) string {
h := sha1.New()
h.Write([]byte(source))
h.Write([]byte(runtime.Version()))
return "g" + base32.StdEncoding.EncodeToString(h.Sum(nil))
}