/
runner_oplog.go
305 lines (264 loc) · 6.69 KB
/
runner_oplog.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package fenneg
import (
"go/types"
"github.com/sirkon/errors"
"github.com/sirkon/fenneg/internal/app"
"github.com/sirkon/fenneg/internal/generator"
"github.com/sirkon/fenneg/internal/handlers"
"github.com/sirkon/fenneg/internal/logger"
"github.com/sirkon/fenneg/internal/tdetect"
"github.com/sirkon/gogh"
"github.com/sirkon/message"
"golang.org/x/tools/go/packages"
)
// RunnerOpLog oplog codegen runner.
type RunnerOpLog struct {
r *Runner
src sourcePoint
typ sourcePoint
hnd sourcePoint
dispatch string
genlen bool
}
// Run codegen.
func (r *RunnerOpLog) Run() error {
log := r.r.logger
gargs, err := r.computeOplogArgs()
if err != nil {
return errors.Wrap(err, "detect types")
}
aret, wret, ok := checkOpLogTypesPrerequisites(log, gargs.typ)
if !ok {
return errors.Newf(
"encoder type %s:%s prerequisites checks failed",
r.typ.Path,
r.typ.ID,
)
}
noWriteBuffer := wret == nil
if noWriteBuffer {
wret = aret
}
p, gr, err := getRenderer(app.Name, r.r.fset, gargs.typ)
if err != nil {
return errors.Wrap(err, "set up renderer")
}
r.r.handlers.setArgsAliases(gargs.types)
h, err := getOpLogArgsHandlers(log, gargs.src.Underlying().(*types.Interface), r.r.handlers)
if err != nil {
return errors.Wrap(err, "set up arguments types handlers")
}
g := generator.NewOpLog(
log,
gargs.src,
gargs.typ,
gargs.hnd,
r.dispatch,
r.genlen,
gr,
h,
wret,
noWriteBuffer,
)
// All code generation in this call.
message.AddFatalHook(func() {
panic("fatal stacktrace")
})
g.Generate()
if err := p.Render(); err != nil {
return errors.Wrap(err, "render generated source code")
}
return nil
}
// DispatchFunc sets dispatch func name.
func (r *RunnerOpLog) DispatchFunc(name string) *RunnerOpLog {
r.dispatch = name
return r
}
// DispatchHandler sets handler type for the dispatching type.
func (r *RunnerOpLog) DispatchHandler(pkg, typ string) *RunnerOpLog {
r.hnd = sourcePoint{
Path: pkg,
ID: typ,
}
return r
}
// LengthPrefix sets generation of a length uleb128 prefix before the rest.
func (r *RunnerOpLog) LengthPrefix(on bool) *RunnerOpLog {
r.genlen = on
return r
}
// OpLog run operation log generator.
func (r *Runner) OpLog() *RunnerOpLogSource {
return &RunnerOpLogSource{
r: &RunnerOpLog{
r: r,
},
}
}
// RunnerOpLogSource oplog runner component to set up
// oplog source interface.
type RunnerOpLogSource struct {
r *RunnerOpLog
}
// RunnerOpLogType oplog runner component to set up
// oplog encoding type.
type RunnerOpLogType struct {
r *RunnerOpLog
}
// Source set up oplog source interface.
func (r *RunnerOpLogSource) Source(pkg, typ string) *RunnerOpLogType {
r.r.src = sourcePoint{
Path: pkg,
ID: typ,
}
r.r.hnd = r.r.src
return &RunnerOpLogType{
r: r.r,
}
}
// Type set up oplog encoding type.
func (r *RunnerOpLogType) Type(pkg, typ string) *RunnerOpLog {
r.r.typ = sourcePoint{
Path: pkg,
ID: typ,
}
r.r.dispatch = gogh.Public(typ, "Dispatch")
return r.r
}
func (r *RunnerOpLog) computeOplogArgs() (
res struct {
src *types.Named
typ *types.Named
hnd *types.Named
spkg *packages.Package
types map[*types.Var]*types.Named
},
err error,
) {
l := newSouceLoader(r.r.fset)
spkg, src, err := getType(l, r.src, typeByNameSpecific[*types.Interface], "source")
if err != nil {
return res, errors.Wrap(err, "get source interface")
}
res.src = src
res.spkg = spkg
res.types = l.mapExactTypes(src)
_, typ, err := getType(l, r.typ, typeByName, "type")
if err != nil {
return res, errors.Wrap(err, "get type")
}
res.typ = typ
res.hnd = src
if !r.hnd.IsValid() {
return res, nil
}
_, hnd, err := getType(l, r.hnd, typeByName, "handler")
if err != nil {
return res, errors.Wrap(err, "get handler type")
}
res.hnd = hnd
return res, nil
}
// getOpLogArgsHandlers set up handlers for every argument of the
// source interface.
func getOpLogArgsHandlers(
log logger.Type,
iface *types.Interface,
dispatch *TypesHandlers,
) (map[*types.Var]handlers.Type, error) {
var errcount int
res := map[*types.Var]handlers.Type{}
outer:
for i := 0; i < iface.NumMethods(); i++ {
ps := iface.Method(i).Type().(*types.Signature).Params()
for j := 0; j < ps.Len(); j++ {
p := ps.At(j)
hnd := dispatch.Handler(p)
if hnd == nil {
if errcount == 10 {
break outer
}
log.Pos(p.Pos(), errors.New("argument type is not supported").Stg("type-name", p.Type()))
errcount++
continue
}
res[p] = hnd
}
}
if errcount > 0 {
return nil, errors.New("unsupported arguments types detected, cannot continue")
}
return res, nil
}
// checkOpLogTypesPrerequisites do
//
// 1. check if allocateBuffer(int) []byte is here
// 2. check if writeBuffer([]byte) <tuple> is here, this is optional.
//
// Result values:
// - aret is a result tuple of the allocateBuffer.
// - wret is a result tuple of the writeBuffer if it does exist.
// - err != nil if checks haven't passed.
func checkOpLogTypesPrerequisites(
l LoggerType,
typ *types.Named,
) (aret *types.Tuple, wret *types.Tuple, ok bool) {
var allocSuccess bool
var hasAlloc bool
writeBufferSuccess := true
for i := 0; i < typ.NumMethods(); i++ {
m := typ.Method(i)
switch m.Name() {
case "allocateBuffer":
hasAlloc = true
aret, allocSuccess = checkAllocateBuffer(l, m)
case "writeBuffer":
wret, writeBufferSuccess = checkWriteBuffer(l, m)
default:
continue
}
}
if !hasAlloc {
l.Pos(typ.Obj().Pos(), errors.New("missing allocateBuffer method on this type"))
return nil, nil, false
}
return aret, wret, allocSuccess && writeBufferSuccess
}
func checkAllocateBuffer(l LoggerType, f *types.Func) (*types.Tuple, bool) {
s := f.Type().(*types.Signature)
if s.Params().Len() != 1 || !tdetect.IsBasic(s.Params().At(0).Type(), types.Int) {
l.Pos(
f.Pos(),
errors.New("allocateBuffer: invalid number of parameters and/or their types").
Pfx("allocate-buffer-params").
Str("expected", "int").
Stg("actual", s.Params()),
)
return nil, false
}
if s.Results().Len() != 1 || !tdetect.IsByteSlice(s.Results().At(0).Type()) {
l.Pos(
f.Pos(),
errors.New("allocateBuffer: invalid number of result values and/or their types").
Pfx("allocate-buffer-params").
Str("expected", "[]byte").
Stg("actual", s.Results()),
)
return nil, false
}
return s.Results(), true
}
func checkWriteBuffer(l LoggerType, m *types.Func) (*types.Tuple, bool) {
s := m.Type().(*types.Signature)
if s.Params().Len() != 1 || !tdetect.IsByteSlice(s.Params().At(0).Type()) {
l.Pos(
m.Pos(),
errors.New("writeBuffer: invalid number of parameters and/or their types").
Pfx("allocate-buffer-params").
Str("expected", "[]byte").
Stg("actual", s.Params()),
)
}
return s.Results(), true
}