-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
collect.go
143 lines (120 loc) · 2.68 KB
/
collect.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"go/types"
"sort"
"strings"
"golang.org/x/tools/go/packages"
"github.com/gotd/td/telegram/query/internal/genutil"
)
func isHashField(field *types.Var) bool {
basic, ok := field.Type().(*types.Basic)
if !ok {
return false
}
return basic.Kind() == types.Int && field.Name() == "Hash"
}
func hasHashField(st *types.Struct) bool {
for i := 0; i < st.NumFields(); i++ {
if isHashField(st.Field(i)) {
return true
}
}
return false
}
type request struct {
name string
params []Param
}
func isCachedQuery(args *types.Tuple) (request, bool) {
arg := args.At(1)
switch req := arg.Type().(type) {
case *types.Pointer:
named, ok := req.Elem().(*types.Named)
if !ok {
return request{}, false
}
st, ok := named.Underlying().(*types.Struct)
if !ok {
return request{}, false
}
var r []Param
for i := 0; i < st.NumFields(); i++ {
field := st.Field(i)
if strings.Contains(field.Name(), "Offset") {
return request{}, false
}
if isHashField(field) || field.Name() == "Flags" {
continue
}
r = append(r, varToParam(field))
}
return request{
name: named.Obj().Name(),
params: sortParams(r),
}, hasHashField(st)
case *types.Basic:
if req.Kind() != types.Int || arg.Name() != "hash" {
return request{}, false
}
return request{}, true
default:
return request{}, false
}
}
func collect(pkg *packages.Package) []CachedQuery {
var r []CachedQuery
for _, def := range genutil.Funcs(pkg, func(f genutil.Func) bool {
return f.Args().Len() == 2 && f.Results().Len() == 2
}) {
args := def.Args()
req, ok := isCachedQuery(args)
if !ok {
continue
}
resultNamed, ok := def.Results().At(0).Type().(*types.Named)
if !ok {
continue
}
result, ok := resultNamed.Underlying().(*types.Interface)
if !ok {
continue
}
impls := genutil.Implementations(pkg, result)
if len(impls) != 2 {
continue
}
var (
notModified *types.Named
pure *types.Named
)
for _, impl := range impls {
if notModified == nil && strings.Contains(impl.Obj().Name(), "NotModified") {
notModified = impl
continue
}
if pure == nil {
pure = impl
}
}
if pure == nil || notModified == nil {
continue
}
pureStruct, ok := pure.Underlying().(*types.Struct)
if !ok {
continue
}
r = append(r, CachedQuery{
Name: def.Decl.Name(),
MethodName: def.Decl.Name(),
RequestName: req.name,
ManualHash: !hasHashField(pureStruct),
RequestParams: req.params,
ResultName: pure.Obj().Name(),
NotModifiedName: notModified.Obj().Name(),
})
}
sort.SliceStable(r, func(i, j int) bool {
return r[i].Name < r[j].Name
})
return r
}