-
Notifications
You must be signed in to change notification settings - Fork 1
/
stubgen.go
334 lines (294 loc) · 10.3 KB
/
stubgen.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"bytes"
"fmt"
"klogproc/servicelog"
"klogproc/servicelog/apiguard"
"klogproc/servicelog/kontext013"
"klogproc/servicelog/kontext015"
"klogproc/servicelog/kontext018"
"klogproc/servicelog/korpusdb"
"klogproc/servicelog/kwords"
"klogproc/servicelog/kwords2"
"klogproc/servicelog/mapka"
"klogproc/servicelog/mapka2"
"klogproc/servicelog/mapka3"
"klogproc/servicelog/masm"
"klogproc/servicelog/morfio"
"klogproc/servicelog/mquery"
"klogproc/servicelog/mquerysru"
"klogproc/servicelog/shiny"
"klogproc/servicelog/ske"
"klogproc/servicelog/syd"
"klogproc/servicelog/treq"
"klogproc/servicelog/vlo"
"klogproc/servicelog/wag06"
"klogproc/servicelog/wag07"
"klogproc/servicelog/wsserver"
"reflect"
"text/template"
)
type FieldInfo struct {
Name string
Type string
IsContainer bool
ContentType string
Nested []FieldInfo // For nested structs
Indent string // For formatting nested fields
}
func getTypeString(t reflect.Type) string {
switch t.Kind() {
case reflect.String:
return "string"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return "number (integer)"
case reflect.Float32, reflect.Float64:
return "number (float)"
case reflect.Bool:
return "boolean"
case reflect.Struct:
return "struct"
default:
return t.String()
}
}
func analyzeStruct(t reflect.Type, indent string) []FieldInfo {
fields := make([]FieldInfo, 0, t.NumField())
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
// Skip unexported fields
if !field.IsExported() {
continue
}
fieldInfo := FieldInfo{
Name: field.Name,
Indent: indent,
}
// Handle different types including nested structs
switch field.Type.Kind() {
case reflect.Struct:
fieldInfo.Type = "table (map)"
fieldInfo.Nested = analyzeStruct(field.Type, indent+" ")
case reflect.String:
fieldInfo.Type = "string"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldInfo.Type = "number (integer)"
case reflect.Float32, reflect.Float64:
fieldInfo.Type = "number (float)"
case reflect.Bool:
fieldInfo.Type = "boolean"
case reflect.Map:
fieldInfo.Type = "table (map)"
fieldInfo.IsContainer = true
if field.Type.Elem().Kind() == reflect.Struct {
fieldInfo.ContentType = fmt.Sprintf("key=%v, value=table", getTypeString(field.Type.Key()))
fieldInfo.Nested = analyzeStruct(field.Type.Elem(), indent+" ")
} else {
fieldInfo.ContentType = fmt.Sprintf("(%v, %v)",
getTypeString(field.Type.Key()),
getTypeString(field.Type.Elem()))
}
case reflect.Slice, reflect.Array:
fieldInfo.Type = "table (seq)"
fieldInfo.IsContainer = true
if field.Type.Elem().Kind() == reflect.Struct {
fieldInfo.ContentType = "table"
fieldInfo.Nested = analyzeStruct(field.Type.Elem(), indent+" ")
} else {
fieldInfo.ContentType = getTypeString(field.Type.Elem())
}
default:
fieldInfo.Type = field.Type.String()
}
fields = append(fields, fieldInfo)
}
return fields
}
func generateLuaStubForType(inputRec servicelog.InputRecord, outputRec servicelog.OutputRecord) (string, error) {
t1 := reflect.TypeOf(inputRec)
if t1.Kind() == reflect.Ptr {
t1 = t1.Elem()
}
if t1.Kind() != reflect.Struct {
return "", fmt.Errorf("input must be a struct, got %v", t1.Kind())
}
t2 := reflect.TypeOf(outputRec)
if t2.Kind() == reflect.Ptr {
t2 = t2.Elem()
}
if t2.Kind() != reflect.Struct {
return "", fmt.Errorf("input must be a struct, got %v", t2.Kind())
}
// Template for the Lua stub
const stubTemplate = `--[[
Input record:
{{range .Fields1}}
{{.Indent}}{{.Name}} {{.Type}} {{if .IsContainer}}of {{.ContentType}}{{end}}{{if .Nested}}{{range .Nested}}
{{.Indent}} {{.Name}} {{.Type}} {{if .IsContainer}}of {{.ContentType}}{{end}}{{end}}{{end}}{{end}}
To get a record time in a normalized (RFC3339) format:
input_rec.GetTime()
Output record:
{{range .Fields2}}
{{.Indent}}{{.Name}} {{.Type}} {{if .IsContainer}}of {{.ContentType}}{{end}}{{if .Nested}}{{range .Nested}}
{{.Indent}} {{.Name}} {{.Type}} {{if .IsContainer}}of {{.ContentType}}{{end}}{{end}}{{end}}{{end}}
To create an output record using default (hardcoded)
transformation (suitable if you want to perform just
a slight modification with your Lua script):
out = transform_default(input_rec, tz_shift_mins)
To create a new output record from scratch:
out = new_out_record()
To set a property in output record:
set_out_prop(rec, name, value)
To test if a record (input or output) has a property:
if record_prop_exists(any_rec, name, value)
For debugging, use:
dump = require('dump')
print(dump(my_value))
To generate a deterministic ID:
local id = out_rec_deterministic_id(out_rec)
For logging:
log.info("message", map_with_args)
(also: warn, debug, error)
The second arg. is optional
Global variables:
app_type, app_version, anonymous_users
]]--
-- preprocess function gives a way how to
-- decide whether the input_rec should be processed
-- (just return {} to ignore the record) or it can
-- be transformed into a set of records with help of
-- the buffer (this is e.g. used with mapka3 to generate
-- "fake" records representing clusters of user activity).
function preprocess(input_rec, buffer)
-- if you want to make sure a possible hardcoded
-- preprocess is always applied, use:
-- local out = preprocess_default(input_rec, buffer)
-- otherwise just return a table of size 1 with the orig. record:
return {input_rec}
end
-- transform function processes the input record and returns an output record
function transform(input_rec)
local out = transform_default(input_rec, 0)
-- setting an output field:
set_out_prop(out, "{{.FirstFieldName}}", string.format("%s[modified]", input_rec.{{.FirstFieldName}}))
-- TODO: Transform input record to output format
-- Available fields and functions are documented above
return out
end
`
fields1 := analyzeStruct(t1, "")
fields2 := analyzeStruct(t2, "")
// Execute template
tmpl, err := template.New("luaStub").Parse(stubTemplate)
if err != nil {
return "", fmt.Errorf("failed to parse template: %v", err)
}
var buf bytes.Buffer
err = tmpl.Execute(
&buf,
struct {
Fields1 []FieldInfo
Fields2 []FieldInfo
FirstFieldName string
}{
fields1,
fields2,
fields2[0].Name,
},
)
if err != nil {
return "", fmt.Errorf("failed to execute template: %v", err)
}
return buf.String(), nil
}
func generateLuaStub(appType, version string) error {
var src string
var err error
switch appType {
case servicelog.AppTypeAkalex:
src, err = generateLuaStubForType(&shiny.InputRecord{}, &shiny.OutputRecord{})
case servicelog.AppTypeAPIGuard:
src, err = generateLuaStubForType(&apiguard.InputRecord{}, &apiguard.OutputRecord{})
case servicelog.AppTypeCalc:
src, err = generateLuaStubForType(&shiny.InputRecord{}, &shiny.OutputRecord{})
case servicelog.AppTypeGramatikat:
src, err = generateLuaStubForType(&shiny.InputRecord{}, &shiny.OutputRecord{})
case servicelog.AppTypeKontext:
switch version {
case servicelog.AppVersionKontext013,
servicelog.AppVersionKontext014:
src, err = generateLuaStubForType(&kontext013.InputRecord{}, &kontext013.OutputRecord{})
case servicelog.AppVersionKontext015,
servicelog.AppVersionKontext016,
servicelog.AppVersionKontext017,
servicelog.AppVersionKontext017API,
servicelog.AppVersionKontext018:
src, err = generateLuaStubForType(&kontext015.InputRecord{}, &kontext015.OutputRecord{})
case "018":
src, err = generateLuaStubForType(&kontext018.InputRecord{}, &kontext015.OutputRecord{})
default:
return fmt.Errorf("failed to create Lua script stub for 'kontext': unknown version '%s'", version)
}
case servicelog.AppTypeKorpusDB:
src, err = generateLuaStubForType(&korpusdb.InputRecord{}, &korpusdb.OutputRecord{})
case servicelog.AppTypeKwords:
switch version {
case "1":
src, err = generateLuaStubForType(&kwords.InputRecord{}, &kwords.OutputRecord{})
case "2":
src, err = generateLuaStubForType(&kwords2.InputRecord{}, &kwords2.OutputRecord{})
default:
return fmt.Errorf("failed to create Lua script stub for 'kwords': unknown version '%s'", version)
}
case servicelog.AppTypeLists:
src, err = generateLuaStubForType(&shiny.InputRecord{}, &shiny.OutputRecord{})
case servicelog.AppTypeMapka:
switch version {
case "1":
src, err = generateLuaStubForType(&mapka.InputRecord{}, &mapka.OutputRecord{})
case "2":
src, err = generateLuaStubForType(&mapka2.InputRecord{}, &mapka2.OutputRecord{})
case "3":
src, err = generateLuaStubForType(&mapka3.InputRecord{}, &mapka3.OutputRecord{})
default:
return fmt.Errorf("failed to create Lua script stub for 'mapka': unknown version '%s'", version)
}
case servicelog.AppTypeMorfio:
src, err = generateLuaStubForType(&morfio.InputRecord{}, &morfio.OutputRecord{})
case servicelog.AppTypeQuitaUp:
src, err = generateLuaStubForType(&shiny.InputRecord{}, &shiny.OutputRecord{})
case servicelog.AppTypeSke:
src, err = generateLuaStubForType(&ske.InputRecord{}, &ske.OutputRecord{})
case servicelog.AppTypeSyd:
src, err = generateLuaStubForType(&syd.InputRecord{}, &syd.OutputRecord{})
case servicelog.AppTypeTreq:
src, err = generateLuaStubForType(&treq.InputRecord{}, &treq.OutputRecord{})
case servicelog.AppTypeWag:
switch version {
case "0.6":
src, err = generateLuaStubForType(&wag06.InputRecord{}, &wag06.OutputRecord{})
case "0.7":
src, err = generateLuaStubForType(&wag07.InputRecord{}, &wag06.OutputRecord{})
default:
return fmt.Errorf("failed to create Lua script stub for 'wag': unknown version '%s'", version)
}
case servicelog.AppTypeWsserver:
src, err = generateLuaStubForType(&wsserver.InputRecord{}, &wsserver.OutputRecord{})
case servicelog.AppTypeMasm:
src, err = generateLuaStubForType(&masm.InputRecord{}, &masm.OutputRecord{})
case servicelog.AppTypeMquery:
src, err = generateLuaStubForType(&mquery.InputRecord{}, &mquery.OutputRecord{})
case servicelog.AppTypeMquerySRU:
src, err = generateLuaStubForType(&mquerysru.InputRecord{}, &mquerysru.OutputRecord{})
case servicelog.AppTypeVLO:
src, err = generateLuaStubForType(&vlo.InputRecord{}, &vlo.OutputRecord{})
default:
return fmt.Errorf("failed to create Lua script stub: unknown application '%s'", version)
}
if err != nil {
return fmt.Errorf("failed to create Lua script stub for %s: %w", appType, err)
} else {
fmt.Println(src)
}
return nil
}