-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.3.0: support for TypeScript types generation
- Loading branch information
Showing
8 changed files
with
160 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const std = @import("std"); | ||
const _target = @import("target.zig"); | ||
const Target = _target.Target; | ||
const Jsdoc = _target.Jsdoc; | ||
const Typescript = _target.Typescript; | ||
const sliceEql = @import("util.zig").sliceEql; | ||
|
||
const stdout = std.io.getStdOut().writer(); | ||
|
||
const Args = @This(); | ||
target: Target, | ||
input_file_path: []const u8, | ||
output_file_path: []const u8, | ||
|
||
pub fn init(args: []const []const u8) !Args { | ||
const processed_args = try Args.process(args); | ||
|
||
return Args{ | ||
.target = processed_args.target, | ||
.input_file_path = processed_args.input_file_path, | ||
.output_file_path = processed_args.output_file_path, | ||
}; | ||
} | ||
|
||
pub fn process(args: []const []const u8) !Args { | ||
var target: ?Target = null; | ||
var input_file_path: ?[]const u8 = null; | ||
var output_file_path: ?[]const u8 = null; | ||
|
||
for (args) |arg| { | ||
if (sliceEql(arg, "-h") or sliceEql(arg, "-help")) { | ||
try Args.help(); | ||
std.process.cleanExit(); | ||
} else if (sliceEql(arg, "-target=jsdoc")) { | ||
target = Target{ .jsdoc = Jsdoc{} }; | ||
} else if (sliceEql(arg, "-target=ts") or sliceEql(arg, "-target=typescript")) { | ||
target = Target{ .typescript = Typescript{} }; | ||
} else if (input_file_path == null) { | ||
input_file_path = arg; | ||
} else if (output_file_path == null) { | ||
output_file_path = arg; | ||
} else { | ||
try Args.help(); | ||
return error.InvalidArgument; | ||
} | ||
} | ||
|
||
if (target == null or input_file_path == null or output_file_path == null) { | ||
try Args.help(); | ||
return error.InvalidArgument; | ||
} | ||
|
||
return Args{ | ||
.target = target.?, | ||
.input_file_path = input_file_path.?, | ||
.output_file_path = output_file_path.?, | ||
}; | ||
} | ||
|
||
pub fn help() !void { | ||
try stdout.print( | ||
"Usage: openapi-typegen -target=<jsdoc|ts> <input_file_path> <output_file_path>\n", | ||
.{}, | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const std = @import("std"); | ||
const Allocator = std.mem.Allocator; | ||
|
||
const OpenApi = @import("openapi.zig").OpenApi; | ||
const sliceEql = @import("util.zig").sliceEql; | ||
const _target = @import("target.zig"); | ||
const Target = _target.Target; | ||
const Jsdoc = _target.Jsdoc; | ||
const Typescript = _target.Typescript; | ||
const Args = @import("Args.zig"); | ||
|
||
const stdout = std.io.getStdOut().writer(); | ||
|
||
const Output = struct { | ||
num_types: usize, | ||
str: []u8, | ||
}; | ||
|
||
const Generator = @This(); | ||
allocator: std.mem.Allocator, | ||
args: Args, | ||
output: ?Output, | ||
|
||
pub fn init(allocator: Allocator, args_in: []const []const u8) !Generator { | ||
// Skip the first argument, which is the program name. | ||
const args = args_in[1..]; | ||
const args_struct = try Args.init(args); | ||
|
||
return Generator{ | ||
.allocator = allocator, | ||
.args = args_struct, | ||
.output = null, | ||
}; | ||
} | ||
|
||
pub fn run(self: *Generator, input_file: std.fs.File) !Output { | ||
const stat = try input_file.stat(); | ||
const json_contents = try input_file.readToEndAlloc(self.allocator, stat.size); | ||
|
||
const json = try std.json.parseFromSlice(std.json.Value, self.allocator, json_contents, .{}); | ||
defer json.deinit(); | ||
const openapi = OpenApi.init(json.value); | ||
|
||
var output_slices = std.ArrayList([]const u8).init(self.allocator); | ||
defer output_slices.deinit(); | ||
|
||
const target = self.args.target; | ||
const schemas = openapi.schemas; | ||
for (schemas.keys(), schemas.values()) |key, value| { | ||
try output_slices.append(try target.buildTypedef(self.allocator, key, value)); | ||
} | ||
|
||
const num_types = schemas.keys().len; | ||
const output_str = try std.mem.join(self.allocator, "\n", output_slices.items); | ||
|
||
return Output{ .num_types = num_types, .str = output_str }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters