-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.zig
239 lines (205 loc) · 7.96 KB
/
generate.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
const std = @import("std");
const string = []const u8;
const yaml = @import("yaml");
pub fn main() !void {
// const source_url = "https://docs.docker.com/engine/api/v1.41.yaml";
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
// Using local document because upstream has missing chunks
// https://github.com/moby/moby/issues/43336
// https://github.com/moby/moby/issues/43337
// https://github.com/moby/moby/issues/43341
// https://github.com/moby/moby/issues/43345
const body_content = @embedFile("./swagger.yaml");
const doc = try yaml.parse(alloc, body_content);
const f = try std.fs.cwd().createFile("src/direct.zig", .{});
const w = f.writer();
try w.writeAll("const internal = @import(\"./internal.zig\");\n");
try w.writeAll("const string = []const u8;\n");
try w.writeAll("const Top = @This();\n");
{
std.debug.print("definitions:\n", .{});
for (doc.mapping.get("definitions").?.mapping.items) |item| {
std.debug.print("|", .{});
try w.writeAll("\npub const ");
try w.writeAll(item.key);
try w.writeAll(" = ");
try printType(alloc, w, item.value.mapping, true);
try w.writeAll(";\n");
}
std.debug.print("\n", .{});
}
{
std.debug.print("paths:\n", .{});
for (doc.mapping.get("paths").?.mapping.items) |item| {
std.debug.print("|", .{});
try w.writeAll("\npub const ");
try printId(w, item.key);
try w.writeAll(" = struct {\n");
try printEndpoint(alloc, w, item.value.mapping);
try w.writeAll("};\n");
}
std.debug.print("\n", .{});
}
}
const Error = std.fs.File.Writer.Error || std.mem.Allocator.Error;
fn printType(alloc: std.mem.Allocator, w: std.fs.File.Writer, m: yaml.Mapping, trailingcomma: bool) Error!void {
{
const ref = m.get_string("$ref");
if (std.mem.startsWith(u8, ref, "#/definitions/")) {
return try w.writeAll(ref["#/definitions/".len..]);
}
}
{
const of = m.get("allOf");
if (of != null) {
try w.writeAll("internal.AllOf(&.{");
for (of.?.sequence, 0..) |item, i| {
if (i > 0) try w.writeAll(",");
try printType(alloc, w, item.mapping, trailingcomma);
}
if (trailingcomma) try w.writeAll(",");
try w.writeAll("})");
return;
}
}
if (m.get("schema")) |cap| {
return printType(alloc, w, cap.mapping, trailingcomma);
}
const apitype = m.get_string("type");
if (std.mem.eql(u8, apitype, "integer")) return try w.writeAll("i32");
if (std.mem.eql(u8, apitype, "boolean")) return try w.writeAll("bool");
if (std.mem.eql(u8, apitype, "number")) return try w.writeAll("f64");
if (std.mem.eql(u8, apitype, "object")) {
try w.writeAll("struct {");
if (m.get("additionalProperties") == null) {
if (m.get("properties") != null) {
const reqs = try m.get_string_array(alloc, "required");
for (m.get("properties").?.mapping.items, 0..) |item, i| {
if (i > 0) try w.writeAll(",");
try printId(w, item.key);
try w.writeAll(": ");
if (reqs.len > 0) {
if (!contains(reqs, item.key)) try w.writeAll("?");
}
try printType(alloc, w, item.value.mapping, trailingcomma);
if (reqs.len > 0) {
if (!contains(reqs, item.key)) try w.writeAll(" = null");
}
}
if (trailingcomma) try w.writeAll(",");
}
}
try w.writeAll("}");
return;
}
if (std.mem.eql(u8, apitype, "array")) {
try w.writeAll("[]const ");
try printType(alloc, w, m.get("items").?.mapping, trailingcomma);
return;
}
if (std.mem.eql(u8, apitype, "string")) {
if (m.get("enum")) |enumcap| {
try w.writeAll("enum {");
for (enumcap.sequence) |item| {
if (item.string.len == 0) continue;
try printId(w, item.string);
try w.writeAll(",");
}
try w.writeAll("}");
return;
}
return try w.writeAll("string");
}
@panic(apitype);
}
fn contains(haystack: []const string, needle: string) bool {
for (haystack) |item| {
if (std.mem.eql(u8, item, needle)) {
return true;
}
}
return false;
}
fn printId(w: std.fs.File.Writer, id: string) !void {
try std.zig.fmtId(id).format("", .{}, w);
}
fn printEndpoint(alloc: std.mem.Allocator, w: std.fs.File.Writer, m: yaml.Mapping) !void {
for (m.items) |item| {
try printMethod(alloc, w, item.key, item.value.mapping);
}
}
fn printMethod(alloc: std.mem.Allocator, w: std.fs.File.Writer, method: string, m: yaml.Mapping) !void {
try w.writeAll(" pub usingnamespace internal.Fn(\n");
try w.writeAll(".");
try w.writeAll(method);
try w.writeAll(",");
try w.writeAll("internal.name(Top, @This()),");
try printParamStruct(alloc, w, m, "path");
try printParamStruct(alloc, w, m, "query");
try printParamStruct(alloc, w, m, "body");
{
try w.writeAll("union(enum) {\n");
for (m.getT("responses", .mapping).?.items) |item| {
try w.print(" @\"{s}\": ", .{item.key});
const mm: yaml.Mapping = item.value.mapping;
const produces = try m.get_string_array(alloc, "produces");
if (mm.get("type") != null or mm.get("schema") != null) {
try printType(alloc, w, mm, false);
} else if ((contains(produces, "application/octet-stream") or contains(produces, "text/plain")) and std.mem.eql(u8, item.key, "200")) {
try w.writeAll("[]const u8");
} else if (mm.items.len == 1 and mm.get("description") != null) {
try w.writeAll("void");
} else if (std.mem.eql(u8, mm.getT("description", .string).?, "no error")) {
try w.writeAll("void");
} else {
@panic("");
}
try w.writeAll(",\n");
}
try w.writeAll(" },\n");
}
try w.writeAll(");\n");
try w.writeAll("\n");
}
fn capitalize(w: std.fs.File.Writer, s: string) !void {
try w.writeAll(&.{std.ascii.toUpper(s[0])});
try w.writeAll(s[1..]);
}
fn hasParamsOf(m: yaml.Mapping, kind: string) bool {
for (m.getT("parameters", .sequence) orelse &[_]yaml.Item{}) |item| {
if (std.mem.eql(u8, item.mapping.get_string("in"), kind)) {
return true;
}
}
return false;
}
fn printParamStruct(alloc: std.mem.Allocator, w: std.fs.File.Writer, m: yaml.Mapping, ty: string) !void {
if (hasParamsOf(m, ty)) {
try w.writeAll("struct {");
var n: usize = 0;
for (m.getT("parameters", .sequence).?) |item| {
const mm: yaml.Mapping = item.mapping;
if (std.mem.eql(u8, mm.get_string("in"), ty)) {
defer n += 1;
if (n > 0) try w.writeAll(", ");
try printId(w, mm.get_string("name"));
try w.writeAll(": ");
try printType(alloc, w, mm, false);
if (mm.get("default")) |cap| {
try w.writeAll(" = ");
try printDefault(w, cap.string, mm.get_string("type"));
}
}
}
try w.writeAll("},\n");
} else {
try w.writeAll("void,\n");
}
}
fn printDefault(w: std.fs.File.Writer, def: string, ty: string) !void {
if (std.mem.eql(u8, ty, "boolean")) return try w.writeAll(def);
if (std.mem.eql(u8, ty, "string")) return try w.print("\"{}\"", .{std.zig.fmtEscapes(def)});
if (std.mem.eql(u8, ty, "integer")) return try w.writeAll(def);
@panic(ty);
}