-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson-to-zig-schema.zig
441 lines (400 loc) · 14.7 KB
/
json-to-zig-schema.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
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
430
431
432
433
434
435
436
437
438
439
440
441
pub const Options = struct {
/// add a jsonParse() method to each object which prints field names
debug_json: bool,
/// print schema json instead of generating zig code
dump_schema: bool,
/// treat input as schema json file and skip build phase
input_schema: bool,
/// add test skeleton to output
include_test: bool,
};
fn usage(exe_path: []const u8) void {
std.debug.print(
\\
\\USAGE: $ {s} <json-file-path> <?options>
\\ options:
\\ --debug-json - add a jsonParse() method to each object which prints field names.
\\ --dump-schema - print schema json instead of generating zig code.
\\ --input-schema - treat input as schema json file and skip build phase.
\\ --include-test - add a test skeleton to ouptut.
\\
\\
, .{std.fs.path.basename(exe_path)});
}
const metak = "__meta__";
const reqk = "required";
const typesk = "type";
const nullablek = "nullable";
const fieldsk = "__fields__";
const reserved_fields = [_][]const u8{ metak, fieldsk }; // TODO StaticStringMap
const Fields = std.StringArrayHashMapUnmanaged(Node);
const root = @This();
const Node = struct {
meta: Meta = .{},
/// max number of fields seen in any object. if this is 1, the object type
/// may be 'union' instead of 'struct'
max_field_count: usize = 0,
fields: Fields = .{},
pub fn deinit(n: *Node, alloc: mem.Allocator) void {
for (n.fields.values()) |*v| v.deinit(alloc);
n.fields.deinit(alloc);
}
pub const build = root.build;
pub const check = root.check;
pub const renderImpl = root.renderImpl;
pub const render = root.render;
pub fn format(n: Node, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
_ = fmt;
_ = options;
try json.stringify(n, .{ .whitespace = .indent_2 }, writer);
}
pub fn jsonStringify(n: Node, jw: anytype) !void {
try jw.beginObject();
try jw.objectField(metak);
{
try jw.beginObject();
try jw.objectField(typesk);
{
try jw.beginArray();
var it = n.meta.type.iterator();
while (it.next()) |tag| {
try jw.write(tag);
}
try jw.endArray();
}
try jw.objectField(reqk);
try jw.write(n.meta.required);
try jw.objectField(nullablek);
try jw.write(n.meta.nullable);
try jw.endObject();
}
for (n.fields.keys(), n.fields.values()) |k, v| {
try jw.objectField(k);
try jw.write(v);
}
try jw.endObject();
}
pub fn jsonParse(alloc: mem.Allocator, source: anytype, options: std.json.ParseOptions) !Node {
// const s: *std.json.Reader(4096, std.io.FixedBufferStream([]u8).Reader) = undefined;
var node: Node = .{};
if (.object_begin != try source.next()) return error.UnexpectedToken;
while (true) {
switch (try source.next()) {
.string, .allocated_string => |key| if (std.mem.eql(u8, metak, key)) {
node.meta = try std.json.innerParse(Meta, alloc, source, options);
} else {
const subnode = try std.json.innerParse(Node, alloc, source, options);
try node.fields.put(alloc, key, subnode);
},
.object_end => break,
else => {
return error.UnexpectedToken;
},
}
}
return node;
}
};
const Type = std.enums.EnumSet(std.meta.Tag(json.Value));
const Meta = struct {
type: Type = .{},
required: bool = true,
nullable: bool = false,
pub fn jsonParse(alloc: mem.Allocator, source: anytype, options: std.json.ParseOptions) !Meta {
// const s: *std.json.Reader(4096, std.io.FixedBufferStream([]u8).Reader) = undefined;
var meta: Meta = .{};
meta = meta;
if (.object_begin != try source.next()) return error.UnexpectedToken;
while (true) {
switch (try source.next()) {
.string, .allocated_string => |key| if (std.mem.eql(u8, typesk, key)) {
if (.array_begin != try source.next()) return error.UnexpectedToken;
while (true) {
switch (try source.next()) {
.array_end => break,
.string, .allocated_string => |ty| {
if (std.meta.stringToEnum(std.meta.Tag(json.Value), ty)) |t| {
meta.type.insert(t);
} else {
return error.UnexpectedToken;
}
},
else => return error.UnexpectedToken,
}
}
} else if (std.mem.eql(u8, key, reqk)) {
meta.required = try std.json.innerParse(bool, alloc, source, options);
} else if (std.mem.eql(u8, key, nullablek)) {
meta.nullable = try std.json.innerParse(bool, alloc, source, options);
} else {
return error.UnexpectedToken;
},
.object_end => break,
else => {
return error.UnexpectedToken;
},
}
}
return meta;
}
};
/// build a schema tree by visiting each node and recording
/// the types each node contains. array nodes are flattened by reusing the
/// same tree.
fn build(node: *Node, alloc: mem.Allocator, json_node: std.json.Value) !void {
node.meta.type.insert(json_node);
node.meta.nullable = json_node == .null;
switch (json_node) {
.array => |a| {
for (a.items) |ele| {
try node.build(alloc, ele);
}
},
.object => |o| {
node.max_field_count = @max(node.max_field_count, o.count());
for (o.keys(), o.values()) |k, v| {
const found = for (reserved_fields) |rf| {
if (mem.eql(u8, k, rf)) break true;
} else false;
if (found) {
std.log.err("name conflict. field '{s}' conflicts with reserved_fields {s}", .{ k, reserved_fields });
return error.NameConflict;
}
const child = try node.fields.getOrPut(alloc, k);
if (!child.found_existing) {
child.value_ptr.* = .{};
}
try child.value_ptr.build(alloc, v);
}
},
else => {},
}
}
/// recursively visit all json nodes and validate type field. set 'required'
/// to false for fields which don't always appear.
pub fn check(node: *Node, json_node: json.Value) !void {
switch (node.meta.type.count()) {
1 => {},
2 => {
if (node.meta.type.contains(.null)) {
node.meta.type.remove(.null);
node.meta.nullable = true;
}
},
else => {},
}
switch (json_node) {
.array => |a| for (a.items) |ele| try node.check(ele),
.object => |o| {
for (node.fields.keys()) |k| {
if (!o.contains(k)) {
node.fields.getPtr(k).?.meta.required = false;
}
}
for (o.keys(), o.values()) |k, v| {
try node.fields.getPtr(k).?.check(v);
}
},
else => {},
}
}
fn typeError(t: Type, comptime fmt: []const u8, args: anytype) void {
std.log.err(fmt, args);
var iter = t.iterator();
var i: u8 = 0;
while (iter.next()) |tag| : (i += 1) {
std.log.err(" type {}: {s}", .{ i, @tagName(tag) });
}
}
fn renderImpl(
node: *Node,
depth: u8,
writer: anytype,
opts: Options,
parent_is_union: bool,
) !void {
const is_union = node.max_field_count == 1 and node.fields.count() > 1;
const qmark: []const u8 = if (!parent_is_union and
(node.meta.nullable or !node.meta.required))
"?"
else
"";
if (node.meta.type.contains(.array) and !node.meta.type.contains(.object)) {
_ = try writer.write("[]const ");
node.meta.type.remove(.array);
try node.renderImpl(depth, writer, opts, parent_is_union);
} else if (node.meta.type.contains(.object)) {
_ = try writer.write(qmark);
if (node.meta.type.contains(.array))
_ = try writer.write("[]const ");
if (is_union)
_ = try writer.write("union(enum) {")
else
_ = try writer.write("struct {");
for (node.fields.keys(), node.fields.values()) |k, *v| {
try writer.print("\n{s: >[1]}{2s}: ", .{ " ", depth * 4, k });
if (v.meta.type.count() == 0)
_ = try writer.write("[]const struct{}")
else
try v.renderImpl(depth + 1, writer, opts, is_union);
if (!v.meta.required and !is_union)
_ = try writer.write(" = null");
_ = try writer.write(",");
}
_ = try writer.write("\n");
if (opts.debug_json) {
try writer.writeByteNTimes(' ', depth * 4);
_ = try writer.write("pub const jsonParse = JsonParse(@This()).jsonParse;\n");
}
try writer.writeByteNTimes(' ', (depth - 1) * 4);
try writer.writeByte('}');
} else if (node.meta.type.count() == 0) {
_ = try writer.write("?u0");
} else if (node.meta.type.count() == 1) {
if (node.meta.type.contains(.string)) {
try writer.print("{s}[]const u8", .{qmark});
} else if (node.meta.type.contains(.integer)) {
try writer.print("{s}i64", .{qmark});
} else if (node.meta.type.contains(.float)) {
try writer.print("{s}f64", .{qmark});
} else if (node.meta.type.contains(.bool)) {
try writer.print("{s}bool", .{qmark});
} else if (node.meta.type.contains(.null)) {
_ = try writer.write("?u0");
}
} else {
// TODO avoid using std.json.Value for more types when possible
if (node.meta.type.contains(.null) and node.meta.type.count() == 2) {
node.meta.nullable = true;
node.meta.type.remove(.null);
try renderImpl(node, depth, writer, opts, parent_is_union);
} else {
try writer.print("{s}std.json.Value", .{qmark});
}
}
}
fn render(node: *Node, writer: anytype, opts: Options) !void {
_ = try writer.write(
\\pub const Root =
);
try node.renderImpl(1, writer, opts, false);
_ = try writer.write(
\\;
\\
\\const std = @import("std");
\\
);
if (opts.include_test) {
_ = try writer.write(
\\
\\test {
\\ const allocator = std.testing.allocator;
\\ const json_text = "<json_document_here>";
\\ const parsed = try std.json.parseFromSlice(Root, allocator, json_text, .{});
\\ defer parsed.deinit();
\\ var l = std.ArrayList(u8).init(allocator);
\\ defer l.deinit();
\\ try std.json.stringify(parsed.value, .{}, l.writer());
\\ try std.testing.expectEqualStrings("<expected_json_document_here>", l.items);
\\}
\\
);
}
if (opts.debug_json) {
const s: []const u8 = @embedFile("json-helper.zig");
const end = comptime mem.lastIndexOf(u8, s, "const std") orelse unreachable;
_ = try writer.write("\n\n" ++ s[0..end]);
}
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const alloc = gpa.allocator();
const args = try std.process.argsAlloc(alloc);
defer std.process.argsFree(alloc, args);
var mjson_path: ?[]const u8 = null;
var opts: Options = .{
.debug_json = false,
.dump_schema = false,
.input_schema = false,
.include_test = false,
};
const E = enum {
@"--debug-json",
@"--dump-schema",
@"--include-test",
@"--input-schema",
@"--help",
@"-h",
};
for (args[1..]) |arg| {
if (std.meta.stringToEnum(E, arg)) |e| {
switch (e) {
.@"--debug-json" => {
opts.debug_json = true;
},
.@"--dump-schema" => {
opts.dump_schema = true;
},
.@"--input-schema" => {
opts.input_schema = true;
},
.@"--include-test" => {
opts.include_test = true;
},
.@"--help", .@"-h" => {
usage(args[0]);
return;
},
}
} else {
if (mjson_path != null) {
usage(args[0]);
std.log.err("unexpected argument '{s}'\n", .{arg});
return error.UnexpectedArgument;
}
mjson_path = arg;
}
}
const json_path = mjson_path orelse {
usage(args[0]);
std.log.err("missing json path", .{});
return error.MissingJsonPath;
};
const f = try std.fs.cwd().openFile(json_path, .{});
defer f.close();
try parseBuildRender(alloc, f.reader(), std.io.getStdOut().writer(), opts);
}
pub fn parseBuildRender(alloc: mem.Allocator, reader: anytype, writer: anytype, opts: Options) !void {
var jr = json.reader(alloc, reader);
defer jr.deinit();
if (opts.input_schema) {
var parsed = try json.parseFromTokenSource(Node, alloc, &jr, .{});
// try std.json.stringify(parsed.value, .{}, std.io.getStdErr().writer());
defer parsed.deinit();
var node = parsed.value;
// TODO does an input schema need to be checked?
// try node.check(parsed.value);
if (opts.dump_schema) {
try writer.print("{}\n", .{node});
} else {
try node.render(writer, opts);
}
} else {
var node: Node = .{};
defer node.deinit(alloc);
const parsed = try json.parseFromTokenSource(json.Value, alloc, &jr, .{});
// try std.json.stringify(parsed.value, .{}, std.io.getStdErr().writer());
defer parsed.deinit();
try node.build(alloc, parsed.value);
try node.check(parsed.value);
if (opts.dump_schema) {
try writer.print("{}\n", .{node});
} else {
try node.render(writer, opts);
}
}
}
const std = @import("std");
const json = std.json;
const mem = std.mem;