-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.sh
executable file
·429 lines (346 loc) · 11.1 KB
/
generate.sh
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env bash
set -xeo pipefail
if [[ -z "$TT_TAG" ]]; then
echo "Environment variable TT_TAG is not set."
exit 1
fi
if [[ -z "${TT_REPO}" ]]; then
TT_REPO="https://github.com/tarantool/tarantool.git"
fi
TT_DIR=tarantool-${TT_TAG}
# Cleanup.
rm -rf ${TT_DIR}
git clone --depth 1 --branch ${TT_TAG} ${TT_REPO} ${TT_DIR}
cd ${TT_DIR}
TT_COMMIT=$(git log -n 1 | head -n 1 | sed "s/commit //")
cd ..
SRC_CONST=${TT_DIR}/src/box/iproto_constants.h
SRC_ERRORS=${TT_DIR}/src/box/errcode.h
SRC_EXECUTE=${TT_DIR}/src/box/execute.h
SRC_FEATURES=${TT_DIR}/src/box/iproto_features.h
SRC_ITERATORS=${TT_DIR}/src/box/iterator_type.h
DST_DOC=doc.go
DST_ERRORS=error.go
DST_ERRORS_TEST=error_test.go
DST_FEATURES=feature.go
DST_FEATURES_TEST=feature_test.go
DST_FLAGS=flag.go
DST_FLAGS_TEST=flag_test.go
DST_ITERATORS=iterator.go
DST_ITERATORS_TEST=iterator_test.go
DST_TYPES=type.go
DST_TYPES_TEST=type_test.go
DST_KEYS=keys.go
DST_KEYS_TEST=keys_test.go
# Cleanup.
rm -rf ${DST_DOC} \
${DST_ERRORS} ${DST_ERRORS_TEST} \
${DST_FEATURES} ${DST_FEATURES_TEST} \
${DST_FLAGS} ${DST_FLAGS_TEST} \
${DST_TYPES} ${DST_TYPES_TEST} \
${DST_KEYS} ${DST_KEYS_TEST}
FOOTER="// Code generated by generate.sh; DO NOT EDIT.
package iproto
"
FOOTER_TEST="// Code generated by generate.sh; DO NOT EDIT.
package iproto_test
import (
\"testing\"
\"github.com/tarantool/go-iproto\"
)
"
# read_enum reads C enum values.
# arg1 - enum name in C code.
# arg2 - path to a C code.
function read_enum {
grep -Pzo "(?s)enum ${1} \{.+?\};" "${2}" | grep -aP "^\t"
}
# read_define_prefix_name reads C name of the prefix for constants.
# arg1 - name of the define with constants.
# arg2 - enum name in C code.
# arg3 - path to a C code.
function read_define_prefix_name {
read_enum $2 $3 | grep -oP "${1}\(.+?\)" | grep -oP "\(.+?\)" | \
# Delete first and last symbols from the string ("(" and ")" respectively).
sed "s/^.//;s/.$//"
}
# read_define reads C constants from define.
# arg1 - name of the define with constants.
# arg2 - path to a C code.
function read_define {
sed -n "/#define ${1}/,/^$/p" "${2}"
}
# read_prefix reads prefix for constants from define.
# arg1 - name of the define, containing the prefix.
# arg2 - path to a C code.
function read_prefix {
read_define $1 $2 | grep -oP "\) .+?#" | \
# Delete first and last symbols from string (")" and "#" respectively).
# Trim spaces.
awk '{$0=substr($0, 2, length($0) - 2); $1=$1};1'
}
# read_define_with_prefix reads all constants from enum (including the ones, that
# will be generated by macros). Also reads comments.
# arg1 - name of the define with constants.
# arg2 - enum name in C code.
# arg3 - path to a C code.
function read_define_with_prefix {
prefix_define_name=$(read_define_prefix_name $1 $2 $3)
full_prefix_define=$(read_define $prefix_define_name $3)
prefix=$(read_prefix $prefix_define_name $3)
double_value_string=$(echo $full_prefix_define | \
# If there is a raising 2 to the power of each constant (that specified in
# the define), we need to place a "1 << " string before each constant
# value. Otherwise, we will place nothing.
grep -oP "<<" | sed 's/^/ 1 /' || echo "")
enum_constants=$(read_enum $2 $3 | \
# Get all constants from the enum (they are not in define) with
# their comments.
grep -Pzo "[\n]?[\t ]*(\/\*)((.|\n)+?)(\*\/)\n[\t ]*[A-Z0-9_]+[\t ]+=[\t ]+[\-0-9 <]+[\-xa-f]*," | \
# If there are no such comments, that means grep returned error.
# Otherwise, we need to delete extra backslash from the end of each line.
sed '$!s/$/\\/' || echo "\\")
read_define $1 $3 | grep -aP "^\t" | \
# Mark the start of each constant (to replace extra "_(" with the prefix).
# Mark the end of each constants value (to delete anything after it).
sed -r "s/(_\()[A-Z0-9_]+[\t ,]+[\t ]+([0-9]+[\-xa-f]*)*/#&#/g" | \
# Mark a comma between constant and its assigned value
# (to replace it with the "=").
sed -r "s/(_\()[A-Z0-9_]+,/&#/g" | \
# Replace marked start with the prefix.
sed -r "s/#_\(/$prefix/g" | \
# Delete anything after a constant value (there are two cases).
sed -r "s/#,.*$//g" | \
sed -r "s/#\).*$//g" | \
# Replace marked comma with the "=".
sed -r "s/,#/ =/g" | \
# Delete backslash at the end of each line.
sed -r 's/\\//g' | \
# Change constants value to the power of two if needed.
sed -r "s/[ ]+[0-9]+/$double_value_string&/g" | \
# Add the remaining constants from the enum.
sed "\$a$enum_constants" | \
# Trim trailing whitespaces.
sed "s/[ \t]*$//g" | \
# Delete empty lines.
sed -r '/^\s*$/d'
}
# generate_enum generates a Golang style enum from a C enum.
# arg1 - enum name in Golang.
function generate_enum {
echo "type ${1} int
const ("
# Remove a constant without value.
sed "/^[\t ]*[a-zA-Z_]\+,\?$/d" | \
# Remove a begin comment line.
sed "/^[\t ]*\/\*\+[\t ]*$/d" | \
# Remove an end comment line.
sed "/^[\t ]*\*\+\/[\t ]*$/d" | \
# Remove an end of a comment.
sed "s/[\t ]*\*\+\///" | \
# Transform comments into a Golang style from at a start of a line.
sed "s/^[\t ]*[ \/]\*\+/\t\/\//" | \
# Transform comments into a Golang style in a middle of a line.
sed "s/\/\*/\/\//" | \
# Remove a coma from an end of a value.
sed "/^[\t ]*[A-Z0-9_]\+[\t ]\+=/s/,//" | \
# Insert type.
sed "/^[\t ]*[A-Z0-9_]\+[\t ]\+=/s/ = / ${1} = /"
echo ")"
}
# generate_test generates a Golang test for a Golang enum from a C enum.
# arg1 - enum name in Golang.
function generate_test {
echo "func Test${1}(t *testing.T) {
cases := []struct{
${1} iproto.${1}
Str string
Val int
} {
"
grep -oP "^[\t ]*[A-Z0-9_]+[\t ]+=[\t ]+[A-Z_]*[\-0-9xa-f <\+]+" | \
awk '{val=$1;$1=$2="";gsub(/[A-Z][A-Z0-9_]+/,"int(iproto.&)",$0);
printf("\t\t{iproto.%s, \"%s\",%s},\n", val, val, $0)}'
echo " }
for _, tc := range cases {
t.Run(tc.Str, func(t *testing.T) {
if tc.${1}.String() != tc.Str {
t.Errorf(\"Got %s, expected %s\", tc.${1}.String(), tc.Str)
}
if int(tc.${1}) != tc.Val {
t.Errorf(\"Got %d, expected %d\", tc.${1}, tc.Val)
}
})
}
}
"
}
#
# Doc.
#
cat << EOF > ${DST_DOC}
// Package iproto contains IPROTO constants.
//
// The package code generated from:
//
// Repository: ${TT_REPO}
// Tag or branch: ${TT_TAG}
// Commit: ${TT_COMMIT}
package iproto
// Code generated by generate.sh; DO NOT EDIT.
EOF
#
# Errors.
#
echo "${FOOTER}" > ${DST_ERRORS}
cat << EOF >> ${DST_ERRORS}
// IPROTO error code constants, generated from
// ${SRC_ERRORS//-${TT_TAG}/}
type Error int
const (
EOF
# Extract body of define ERROR_CODES
ERROR_CODES=`sed -n '/^#define ERROR_CODES(/,/This one should be last/{//!p;}' ${SRC_ERRORS}`
echo "${ERROR_CODES}" | \
# Get line with ER_* description.
sed -nE 's/^[^E]*(ER_.+)\s*\\\s*$/\1/p' | \
# Remove trailing parenthesis or comment's ending.
sed -E 's/(.*)\s*(:?\)|\*\/)\s*$/\1/' | \
# Extract three fields: name, id and first comment string.
# Match string based on https://regex101.com/library/zI0yV6
sed -E 's/^([^"]+)("([^"]|\\")*[^\\]"|"").*$/\1\2/' | \
# Format three fields comma separated for Go lang const declaration.
awk '
BEGIN{ FS=",[[:space:]]*" }
{
com=$3;
for(i=4; i<=NF; i++){ com=com", "$i };
printf("\t// %s\n\t%s Error = %s\n", com, $1, $2)
}' >> ${DST_ERRORS}
echo ")" >> ${DST_ERRORS}
echo "${FOOTER_TEST}" > ${DST_ERRORS_TEST}
cat << EOF >> ${DST_ERRORS_TEST}
func TestError(t *testing.T) {
cases := []struct{
Err iproto.Error
Str string
} {
EOF
echo "${ERROR_CODES}" | grep -o "ER_[A-Z0-9_]\+," | \
sed "s/,$//" | \
awk '{printf("\t\t{iproto.%s, \"%s\"},\n", $1, $1)}' >> ${DST_ERRORS_TEST}
cat << EOF >> ${DST_ERRORS_TEST}
}
for i, tc := range cases {
t.Run(tc.Str, func(t *testing.T) {
if tc.Err.String() != tc.Str {
t.Errorf("Got %s, expected %s", tc.Err.String(), tc.Str)
}
if int(tc.Err) != i {
t.Errorf("Got %d, expected %d", tc.Err, i)
}
})
}
}
EOF
#
# Features.
#
echo "${FOOTER}" > ${DST_FEATURES}
cat << EOF >> ${DST_FEATURES}
// IPROTO feature constants, generated from
// ${SRC_FEATURES//-${TT_TAG}/}
EOF
read_define_with_prefix IPROTO_FEATURES iproto_feature_id ${SRC_FEATURES} | \
generate_enum Feature >> ${DST_FEATURES}
echo "${FOOTER_TEST}" > ${DST_FEATURES_TEST}
read_define_with_prefix IPROTO_FEATURES iproto_feature_id ${SRC_FEATURES} | \
generate_test Feature >> ${DST_FEATURES_TEST}
#
# Flags.
#
echo "${FOOTER}" > ${DST_FLAGS}
cat << EOF >> ${DST_FLAGS}
// IPROTO flag constants, generated from
// ${SRC_CONST//-${TT_TAG}/}
EOF
read_define_with_prefix IPROTO_FLAGS iproto_flag ${SRC_CONST} | \
generate_enum Flag >> ${DST_FLAGS}
echo "${FOOTER_TEST}" > ${DST_FLAGS_TEST}
read_define_with_prefix IPROTO_FLAGS iproto_flag ${SRC_CONST} | \
generate_test Flag >> ${DST_FLAGS_TEST}
#
# Iterators.
#
echo "${FOOTER}" > ${DST_ITERATORS}
cat << EOF >> ${DST_ITERATORS}
// IPROTO iterators constants, generated from
// ${SRC_ITERATORS//-${TT_TAG}/}
EOF
read_enum iterator_type ${SRC_ITERATORS} | \
generate_enum Iterator >> ${DST_ITERATORS}
echo "${FOOTER_TEST}" > ${DST_ITERATORS_TEST}
read_enum iterator_type ${SRC_ITERATORS} | \
generate_test Iterator >> ${DST_ITERATORS_TEST}
#
# Types.
#
echo "${FOOTER}" > ${DST_TYPES}
cat << EOF >> ${DST_TYPES}
// IPROTO type constants, generated from
// ${SRC_CONST//-${TT_TAG}/}
EOF
read_define_with_prefix IPROTO_TYPES iproto_type ${SRC_CONST} | \
generate_enum Type >> ${DST_TYPES}
echo "${FOOTER_TEST}" > ${DST_TYPES_TEST}
read_define_with_prefix IPROTO_TYPES iproto_type ${SRC_CONST} | \
generate_test Type >> ${DST_TYPES_TEST}
#
# Keys.
#
echo "${FOOTER}" > ${DST_KEYS}
cat << EOF >> ${DST_KEYS}
// IPROTO key constants, generated from
// ${SRC_CONST//-${TT_TAG}/}
EOF
read_define_with_prefix IPROTO_KEYS iproto_key ${SRC_CONST} | \
generate_enum Key >> ${DST_KEYS}
cat << EOF >> ${DST_KEYS}
// IPROTO metadata key constants, generated from
// ${SRC_CONST//-${TT_TAG}/}
EOF
read_define_with_prefix IPROTO_METADATA_KEYS iproto_metadata_key ${SRC_CONST} | \
generate_enum MetadataKey >> ${DST_KEYS}
cat << EOF >> ${DST_KEYS}
// IPROTO ballot key constants, generated from
// ${SRC_CONST//-${TT_TAG}/}
EOF
read_define_with_prefix IPROTO_BALLOT_KEYS iproto_ballot_key ${SRC_CONST} | \
generate_enum BallotKey >> ${DST_KEYS}
cat << EOF >> ${DST_KEYS}
// IPROTO raft key constants, generated from
// ${SRC_CONST//-${TT_TAG}/}
EOF
read_define_with_prefix IPROTO_RAFT_KEYS iproto_raft_key ${SRC_CONST} | \
generate_enum RaftKey >> ${DST_KEYS}
cat << EOF >> ${DST_KEYS}
// IPROTO SQL info key constants, generated from
// ${SRC_EXECUTE//-${TT_TAG}/}
EOF
read_enum sql_info_key ${SRC_EXECUTE} | \
generate_enum SqlInfoKey >> ${DST_KEYS}
echo "${FOOTER_TEST}" > ${DST_KEYS_TEST}
read_define_with_prefix IPROTO_KEYS iproto_key ${SRC_CONST} | \
generate_test Key >> ${DST_KEYS_TEST}
read_define_with_prefix IPROTO_METADATA_KEYS iproto_metadata_key ${SRC_CONST} | \
generate_test MetadataKey >> ${DST_KEYS_TEST}
read_define_with_prefix IPROTO_BALLOT_KEYS iproto_ballot_key ${SRC_CONST} | \
generate_test BallotKey >> ${DST_KEYS_TEST}
read_define_with_prefix IPROTO_RAFT_KEYS iproto_raft_key ${SRC_CONST} | \
generate_test RaftKey >> ${DST_KEYS_TEST}
read_enum sql_info_key ${SRC_EXECUTE} | \
generate_test SqlInfoKey >> ${DST_KEYS_TEST}
#
# Cleanup.
#
rm -rf ${TT_DIR}