-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgo_bindings.zig
366 lines (330 loc) · 11.5 KB
/
go_bindings.zig
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
const std = @import("std");
const vsr = @import("vsr");
const assert = std.debug.assert;
const stdx = vsr.stdx;
const tb = vsr.tigerbeetle;
const type_mappings = .{
.{ tb.AccountFlags, "AccountFlags" },
.{ tb.TransferFlags, "TransferFlags" },
.{ tb.AccountFilterFlags, "AccountFilterFlags" },
.{ tb.QueryFilterFlags, "QueryFilterFlags" },
.{ tb.Account, "Account" },
.{ tb.Transfer, "Transfer" },
.{ tb.CreateAccountResult, "CreateAccountResult", "Account" },
.{ tb.CreateTransferResult, "CreateTransferResult", "Transfer" },
.{ tb.CreateAccountsResult, "AccountEventResult" },
.{ tb.CreateTransfersResult, "TransferEventResult" },
.{ tb.AccountFilter, "AccountFilter" },
.{ tb.AccountBalance, "AccountBalance" },
.{ tb.QueryFilter, "QueryFilter" },
};
fn go_type(comptime Type: type) []const u8 {
switch (@typeInfo(Type)) {
.Bool => return "bool",
.Enum => return comptime get_mapped_type_name(Type) orelse
@compileError("Type " ++ @typeName(Type) ++ " not mapped."),
.Struct => |info| switch (info.layout) {
.@"packed" => return comptime go_type(std.meta.Int(.unsigned, @bitSizeOf(Type))),
else => return comptime get_mapped_type_name(Type) orelse
@compileError("Type " ++ @typeName(Type) ++ " not mapped."),
},
.Int => |info| {
std.debug.assert(info.signedness == .unsigned);
return switch (info.bits) {
1 => "bool",
8 => "uint8",
16 => "uint16",
32 => "uint32",
64 => "uint64",
128 => "Uint128",
else => @compileError("invalid int type"),
};
},
else => @compileError("Unhandled type: " ++ @typeName(Type)),
}
}
fn get_mapped_type_name(comptime Type: type) ?[]const u8 {
inline for (type_mappings) |type_mapping| {
if (Type == type_mapping[0]) {
return type_mapping[1];
}
} else return null;
}
fn to_pascal_case(comptime input: []const u8, comptime min_len: ?usize) []const u8 {
// TODO(Zig): Cleanup when this is fixed after Zig 0.11.
// Without comptime blk, the compiler thinks slicing the output on return happens at runtime.
return comptime blk: {
var len: usize = 0;
var output = [_]u8{' '} ** (min_len orelse input.len);
var iterator = std.mem.tokenize(u8, input, "_");
while (iterator.next()) |word| {
if (is_upper_case(word)) {
_ = std.ascii.upperString(output[len..], word);
} else {
@memcpy(output[len..][0..word.len], word);
output[len] = std.ascii.toUpper(output[len]);
}
len += word.len;
}
break :blk stdx.comptime_slice(&output, min_len orelse len);
};
}
fn calculate_min_len(comptime type_info: anytype) comptime_int {
comptime {
var min_len: comptime_int = 0;
for (type_info.fields) |field| {
const field_len = to_pascal_case(field.name, null).len;
if (field_len > min_len) {
min_len = field_len;
}
}
return min_len;
}
}
fn is_upper_case(comptime word: []const u8) bool {
// https://github.com/golang/go/wiki/CodeReviewComments#initialisms
const initialisms = .{ "id", "ok" };
inline for (initialisms) |initialism| {
if (std.ascii.eqlIgnoreCase(initialism, word)) {
return true;
}
} else return false;
}
fn emit_enum(
buffer: *std.ArrayList(u8),
comptime Type: type,
comptime name: []const u8,
comptime prefix: []const u8,
comptime tag_type: []const u8,
) !void {
try buffer.writer().print("type {s} {s}\n\n" ++
"const (\n", .{
name,
tag_type,
});
const type_info = @typeInfo(Type).Enum;
const min_len = calculate_min_len(type_info);
inline for (type_info.fields) |field| {
const enum_name = prefix ++ comptime to_pascal_case(field.name, min_len);
if (type_info.tag_type == u1) {
try buffer.writer().print("\t{s} {s} = {s}\n", .{
enum_name,
name,
if (@intFromEnum(@field(Type, field.name)) == 1) "true" else "false",
});
} else {
try buffer.writer().print("\t{s} {s} = {d}\n", .{
enum_name,
name,
@intFromEnum(@field(Type, field.name)),
});
}
}
try buffer.writer().print(")\n\n" ++
"func (i {s}) String() string {{\n", .{
name,
});
if (type_info.tag_type == u1) {
const enum_zero_name = prefix ++ comptime to_pascal_case(
@tagName(@as(Type, @enumFromInt(0))),
null,
);
const enum_one_name = prefix ++ comptime to_pascal_case(
@tagName(@as(Type, @enumFromInt(1))),
null,
);
try buffer.writer().print("\tif (i == {s}) {{\n" ++
"\t\treturn \"{s}\"\n" ++
"\t}} else {{\n" ++
"\t\treturn \"{s}\"\n" ++
"\t}}\n", .{
enum_one_name,
enum_one_name,
enum_zero_name,
});
} else {
try buffer.writer().print("\tswitch i {{\n", .{});
inline for (type_info.fields) |field| {
const enum_name = prefix ++ comptime to_pascal_case(field.name, null);
try buffer.writer().print("\tcase {s}:\n" ++
"\t\treturn \"{s}\"\n", .{
enum_name,
enum_name,
});
}
try buffer.writer().print(
"\t}}\n" ++
"\treturn \"{s}(\" + strconv.FormatInt(int64(i+1), 10) + \")\"\n",
.{name},
);
}
try buffer.writer().print("}}\n\n", .{});
}
fn emit_packed_struct(
buffer: *std.ArrayList(u8),
comptime type_info: anytype,
comptime name: []const u8,
comptime int_type: []const u8,
) !void {
try buffer.writer().print("type {s} struct {{\n", .{
name,
});
const min_len = calculate_min_len(type_info);
inline for (type_info.fields) |field| {
if (comptime std.mem.eql(u8, "padding", field.name)) continue;
try buffer.writer().print("\t{s} {s}\n", .{
to_pascal_case(field.name, min_len),
go_type(field.type),
});
}
// Conversion from struct to packed (e.g. AccountFlags.ToUint16())
try buffer.writer().print("}}\n\n" ++
"func (f {s}) To{s}() {s} {{\n" ++
"\tvar ret {s} = 0\n\n", .{
name,
to_pascal_case(int_type, null),
int_type,
int_type,
});
inline for (type_info.fields, 0..) |field, i| {
if (comptime std.mem.eql(u8, "padding", field.name)) continue;
try buffer.writer().print("\tif f.{s} {{\n" ++
"\t\tret |= (1 << {d})\n" ++
"\t}}\n\n", .{
to_pascal_case(field.name, null),
i,
});
}
try buffer.writer().print("\treturn ret\n" ++
"}}\n\n", .{});
}
fn emit_struct(
buffer: *std.ArrayList(u8),
comptime type_info: anytype,
comptime name: []const u8,
) !void {
try buffer.writer().print("type {s} struct {{\n", .{
name,
});
const min_len = calculate_min_len(type_info);
comptime var flagsField = false;
inline for (type_info.fields) |field| {
switch (@typeInfo(field.type)) {
.Array => |array| {
try buffer.writer().print("\t{s} [{d}]{s}\n", .{
to_pascal_case(field.name, min_len),
array.len,
go_type(array.child),
});
},
else => {
if (comptime std.mem.eql(u8, field.name, "flags")) {
flagsField = true;
}
try buffer.writer().print(
"\t{s} {s}\n",
.{
to_pascal_case(field.name, min_len),
go_type(field.type),
},
);
},
}
}
try buffer.writer().print("}}\n\n", .{});
if (flagsField) {
const flagType = if (comptime std.mem.eql(u8, name, "Account"))
tb.AccountFlags
else if (comptime std.mem.eql(u8, name, "Transfer"))
tb.TransferFlags
else if (comptime std.mem.eql(u8, name, "AccountFilter"))
tb.AccountFilterFlags
else if (comptime std.mem.eql(u8, name, "QueryFilter"))
tb.QueryFilterFlags
else
unreachable;
// Conversion from packed to struct (e.g. Account.AccountFlags())
try buffer.writer().print(
"func (o {s}) {s}Flags() {s}Flags {{\n" ++
"\tvar f {s}Flags\n",
.{
name,
name,
name,
name,
},
);
switch (@typeInfo(flagType)) {
.Struct => |info| switch (info.layout) {
.@"packed" => inline for (info.fields, 0..) |field, i| {
if (comptime std.mem.eql(u8, "padding", field.name)) continue;
try buffer.writer().print("\tf.{s} = ((o.Flags >> {}) & 0x1) == 1\n", .{
to_pascal_case(field.name, null),
i,
});
},
else => unreachable,
},
else => unreachable,
}
try buffer.writer().print("\treturn f\n" ++
"}}\n\n", .{});
}
}
pub fn generate_bindings(buffer: *std.ArrayList(u8)) !void {
@setEvalBranchQuota(100_000);
try buffer.writer().print(
\\///////////////////////////////////////////////////////
\\// This file was auto-generated by go_bindings.zig //
\\// Do not manually modify. //
\\///////////////////////////////////////////////////////
\\
\\package types
\\
\\/*
\\#include "../native/tb_client.h"
\\*/
\\import "C"
\\import "strconv"
\\
\\
, .{});
// Emit Go declarations.
inline for (type_mappings) |type_mapping| {
const ZigType = type_mapping[0];
const name = type_mapping[1];
switch (@typeInfo(ZigType)) {
.Struct => |info| switch (info.layout) {
.auto => @compileError(
"Only packed or extern structs are supported: " ++ @typeName(ZigType),
),
.@"packed" => try emit_packed_struct(
buffer,
info,
name,
comptime go_type(std.meta.Int(.unsigned, @bitSizeOf(ZigType))),
),
.@"extern" => try emit_struct(buffer, info, name),
},
.Enum => try emit_enum(
buffer,
ZigType,
name,
type_mapping[2],
comptime go_type(std.meta.Int(.unsigned, @bitSizeOf(ZigType))),
),
else => @compileError("Type cannot be represented: " ++ @typeName(ZigType)),
}
}
assert(buffer.pop() == '\n');
assert(std.mem.endsWith(u8, buffer.items, "\n"));
assert(!std.mem.endsWith(u8, buffer.items, "\n\n"));
}
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var buffer = std.ArrayList(u8).init(allocator);
try generate_bindings(&buffer);
try std.io.getStdOut().writeAll(buffer.items);
}