2021-05-30 01:17:45 +00:00
|
|
|
const std = @import("std");
|
2021-06-30 16:05:21 +00:00
|
|
|
const smithy = @import("smithy");
|
2021-05-30 01:17:45 +00:00
|
|
|
const snake = @import("snake.zig");
|
2021-09-05 20:10:48 +00:00
|
|
|
const json_zig = @embedFile("json.zig");
|
2021-05-30 01:17:45 +00:00
|
|
|
|
2023-08-15 05:56:19 +00:00
|
|
|
var verbose = false;
|
|
|
|
|
2021-05-30 01:17:45 +00:00
|
|
|
pub fn main() anyerror!void {
|
|
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
|
|
defer arena.deinit();
|
2021-12-23 16:51:48 +00:00
|
|
|
const allocator = arena.allocator();
|
2021-05-30 01:17:45 +00:00
|
|
|
|
|
|
|
const args = try std.process.argsAlloc(allocator);
|
|
|
|
defer std.process.argsFree(allocator, args);
|
|
|
|
const stdout = std.io.getStdOut().writer();
|
2023-08-15 05:38:37 +00:00
|
|
|
|
|
|
|
var output_dir = std.fs.cwd();
|
2023-08-15 14:26:14 +00:00
|
|
|
defer if (output_dir.fd > 0) output_dir.close();
|
2023-08-15 05:38:37 +00:00
|
|
|
var models_dir: ?std.fs.IterableDir = null;
|
|
|
|
defer if (models_dir) |*m| m.close();
|
|
|
|
for (args, 0..) |arg, i| {
|
|
|
|
if (std.mem.eql(u8, "--help", arg) or
|
|
|
|
std.mem.eql(u8, "-h", arg))
|
|
|
|
{
|
2023-08-15 05:56:19 +00:00
|
|
|
try stdout.print("usage: {s} [--verbose] [--models dir] [--output dir] [file...]\n\n", .{args[0]});
|
2023-08-15 05:38:37 +00:00
|
|
|
try stdout.print(" --models specifies a directory with all model files (do not specify files if --models is used)\n", .{});
|
|
|
|
try stdout.print(" --output specifies an output directory, otherwise the current working directory will be used\n", .{});
|
|
|
|
std.process.exit(0);
|
|
|
|
}
|
|
|
|
if (std.mem.eql(u8, "--output", arg))
|
|
|
|
output_dir = try output_dir.openDir(args[i + 1], .{});
|
|
|
|
if (std.mem.eql(u8, "--models", arg))
|
|
|
|
models_dir = try std.fs.cwd().openIterableDir(args[i + 1], .{});
|
|
|
|
}
|
|
|
|
// TODO: Seems like we should remove this in favor of a package
|
|
|
|
const json_file = try output_dir.createFile("json.zig", .{});
|
2021-09-05 20:10:48 +00:00
|
|
|
defer json_file.close();
|
|
|
|
try json_file.writer().writeAll(json_zig);
|
2023-08-15 05:38:37 +00:00
|
|
|
const manifest_file = try output_dir.createFile("service_manifest.zig", .{});
|
2021-06-04 00:40:47 +00:00
|
|
|
defer manifest_file.close();
|
|
|
|
const manifest = manifest_file.writer();
|
2023-08-15 05:38:37 +00:00
|
|
|
var files_processed: usize = 0;
|
|
|
|
var skip_next = true;
|
2021-05-30 01:17:45 +00:00
|
|
|
for (args) |arg| {
|
2023-08-15 05:38:37 +00:00
|
|
|
if (skip_next) {
|
|
|
|
skip_next = false;
|
|
|
|
continue;
|
|
|
|
}
|
2023-08-15 05:56:19 +00:00
|
|
|
if (std.mem.eql(u8, "--verbose", arg)) {
|
|
|
|
verbose = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-08-15 05:38:37 +00:00
|
|
|
if (std.mem.eql(u8, "--models", arg) or
|
|
|
|
std.mem.eql(u8, "--output", arg))
|
|
|
|
{
|
|
|
|
skip_next = true;
|
2021-05-30 01:17:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-08-15 05:38:37 +00:00
|
|
|
try processFile(arg, stdout, output_dir, manifest);
|
|
|
|
files_processed += 1;
|
|
|
|
}
|
|
|
|
if (files_processed == 0) {
|
|
|
|
// no files specified, look for json files in models directory or cwd
|
|
|
|
if (models_dir) |m| {
|
|
|
|
var cwd = try std.fs.cwd().openDir(".", .{});
|
|
|
|
defer cwd.close();
|
|
|
|
defer cwd.setAsCwd() catch unreachable;
|
|
|
|
|
|
|
|
try m.dir.setAsCwd();
|
|
|
|
var mi = m.iterate();
|
|
|
|
while (try mi.next()) |e| {
|
|
|
|
if ((e.kind == .file or e.kind == .sym_link) and
|
|
|
|
std.mem.endsWith(u8, e.name, ".json"))
|
|
|
|
try processFile(e.name, stdout, output_dir, manifest);
|
|
|
|
}
|
|
|
|
}
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (args.len == 0)
|
2021-06-04 00:40:47 +00:00
|
|
|
_ = try generateServices(allocator, ";", std.io.getStdIn(), stdout);
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 05:38:37 +00:00
|
|
|
fn processFile(file_name: []const u8, stdout: anytype, output_dir: std.fs.Dir, manifest: anytype) !void {
|
2021-05-30 01:17:45 +00:00
|
|
|
// It's probably best to create our own allocator here so we can deint at the end and
|
|
|
|
// toss all allocations related to the services in this file
|
|
|
|
// I can't guarantee we're not leaking something, and at the end of the
|
|
|
|
// day I'm not sure we want to track down leaks
|
|
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
|
|
defer arena.deinit();
|
2021-12-23 16:51:48 +00:00
|
|
|
const allocator = arena.allocator();
|
2021-06-04 00:40:47 +00:00
|
|
|
var writer = &stdout;
|
|
|
|
var file: std.fs.File = undefined;
|
2023-08-15 05:38:37 +00:00
|
|
|
const output_file_name = try std.fmt.allocPrint(allocator, "{s}.zig", .{file_name});
|
|
|
|
defer allocator.free(output_file_name);
|
|
|
|
file = try output_dir.createFile(output_file_name, .{ .truncate = true });
|
2021-06-04 00:40:47 +00:00
|
|
|
errdefer file.close();
|
|
|
|
writer = &file.writer();
|
2021-09-05 20:10:48 +00:00
|
|
|
_ = try writer.write("const std = @import(\"std\");\n");
|
|
|
|
_ = try writer.write("const serializeMap = @import(\"json.zig\").serializeMap;\n");
|
2021-06-30 16:07:35 +00:00
|
|
|
_ = try writer.write("const smithy = @import(\"smithy\");\n\n");
|
2023-08-15 05:56:19 +00:00
|
|
|
if (verbose) std.log.info("Processing file: {s}", .{file_name});
|
2023-08-15 05:38:37 +00:00
|
|
|
const service_names = generateServicesForFilePath(allocator, ";", file_name, writer) catch |err| {
|
|
|
|
std.log.err("Error processing file: {s}", .{file_name});
|
2021-06-04 00:40:47 +00:00
|
|
|
return err;
|
|
|
|
};
|
|
|
|
defer {
|
|
|
|
for (service_names) |name| allocator.free(name);
|
|
|
|
allocator.free(service_names);
|
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
for (service_names) |name| {
|
2023-08-15 05:38:37 +00:00
|
|
|
try manifest.print("pub const {s} = @import(\"{s}\");\n", .{ name, std.fs.path.basename(output_file_name) });
|
2021-06-04 00:40:47 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-30 01:17:45 +00:00
|
|
|
|
2023-08-15 05:38:37 +00:00
|
|
|
fn generateServicesForFilePath(
|
|
|
|
allocator: std.mem.Allocator,
|
|
|
|
comptime terminator: []const u8,
|
|
|
|
path: []const u8,
|
|
|
|
writer: anytype,
|
|
|
|
) ![][]const u8 {
|
2023-08-04 17:06:54 +00:00
|
|
|
const file = try std.fs.cwd().openFile(path, .{});
|
2021-06-04 00:40:47 +00:00
|
|
|
defer file.close();
|
|
|
|
return try generateServices(allocator, terminator, file, writer);
|
|
|
|
}
|
2021-09-06 22:43:14 +00:00
|
|
|
|
|
|
|
fn addReference(id: []const u8, map: *std.StringHashMap(u64)) !void {
|
|
|
|
const res = try map.getOrPut(id);
|
|
|
|
if (res.found_existing) {
|
|
|
|
res.value_ptr.* += 1;
|
|
|
|
} else {
|
|
|
|
res.value_ptr.* = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn countAllReferences(shape_ids: [][]const u8, shapes: std.StringHashMap(smithy.ShapeInfo), shape_references: *std.StringHashMap(u64), stack: *std.ArrayList([]const u8)) anyerror!void {
|
|
|
|
for (shape_ids) |id| {
|
|
|
|
try countReferences(shapes.get(id).?, shapes, shape_references, stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn countTypeMembersReferences(type_members: []smithy.TypeMember, shapes: std.StringHashMap(smithy.ShapeInfo), shape_references: *std.StringHashMap(u64), stack: *std.ArrayList([]const u8)) anyerror!void {
|
|
|
|
for (type_members) |m| {
|
|
|
|
try countReferences(shapes.get(m.target).?, shapes, shape_references, stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn countReferences(shape: smithy.ShapeInfo, shapes: std.StringHashMap(smithy.ShapeInfo), shape_references: *std.StringHashMap(u64), stack: *std.ArrayList([]const u8)) anyerror!void {
|
|
|
|
// Add ourselves as a reference, then we will continue down the tree
|
|
|
|
try addReference(shape.id, shape_references);
|
|
|
|
// Put ourselves on the stack. If we come back to ourselves, we want to end.
|
|
|
|
for (stack.items) |i| {
|
|
|
|
if (std.mem.eql(u8, shape.id, i))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try stack.append(shape.id);
|
|
|
|
defer _ = stack.pop();
|
|
|
|
// Well, this is a fun read: https://awslabs.github.io/smithy/1.0/spec/core/model.html#recursive-shape-definitions
|
|
|
|
// Looks like recursion has special rules in the spec to accomodate Java.
|
|
|
|
// This is silly and we will ignore
|
|
|
|
switch (shape.shape) {
|
|
|
|
// We don't care about these primitives - they don't have children
|
|
|
|
.blob,
|
|
|
|
.boolean,
|
|
|
|
.string,
|
|
|
|
.byte,
|
|
|
|
.short,
|
|
|
|
.integer,
|
|
|
|
.long,
|
|
|
|
.float,
|
|
|
|
.double,
|
|
|
|
.bigInteger,
|
|
|
|
.bigDecimal,
|
|
|
|
.timestamp,
|
|
|
|
=> {},
|
|
|
|
.document, .member, .resource => {}, // less sure about these?
|
|
|
|
.list => |i| try countReferences(shapes.get(i.member_target).?, shapes, shape_references, stack),
|
|
|
|
.set => |i| try countReferences(shapes.get(i.member_target).?, shapes, shape_references, stack),
|
|
|
|
.map => |i| {
|
|
|
|
try countReferences(shapes.get(i.key).?, shapes, shape_references, stack);
|
|
|
|
try countReferences(shapes.get(i.value).?, shapes, shape_references, stack);
|
|
|
|
},
|
|
|
|
.structure => |m| try countTypeMembersReferences(m.members, shapes, shape_references, stack),
|
|
|
|
.uniontype => |m| try countTypeMembersReferences(m.members, shapes, shape_references, stack),
|
|
|
|
.service => |i| try countAllReferences(i.operations, shapes, shape_references, stack),
|
|
|
|
.operation => |op| {
|
|
|
|
if (op.input) |i| try countReferences(shapes.get(i).?, shapes, shape_references, stack);
|
|
|
|
if (op.output) |i| try countReferences(shapes.get(i).?, shapes, shape_references, stack);
|
|
|
|
if (op.errors) |i| try countAllReferences(i, shapes, shape_references, stack);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:51:48 +00:00
|
|
|
fn generateServices(allocator: std.mem.Allocator, comptime _: []const u8, file: std.fs.File, writer: anytype) ![][]const u8 {
|
2021-05-30 01:17:45 +00:00
|
|
|
const json = try file.readToEndAlloc(allocator, 1024 * 1024 * 1024);
|
|
|
|
defer allocator.free(json);
|
|
|
|
const model = try smithy.parse(allocator, json);
|
|
|
|
defer model.deinit();
|
2021-09-05 20:09:22 +00:00
|
|
|
var shapes = std.StringHashMap(smithy.ShapeInfo).init(allocator);
|
2021-05-30 01:17:45 +00:00
|
|
|
defer shapes.deinit();
|
2021-09-05 20:09:22 +00:00
|
|
|
var services = std.ArrayList(smithy.ShapeInfo).init(allocator);
|
2021-05-30 01:17:45 +00:00
|
|
|
defer services.deinit();
|
|
|
|
for (model.shapes) |shape| {
|
|
|
|
try shapes.put(shape.id, shape);
|
|
|
|
switch (shape.shape) {
|
|
|
|
.service => try services.append(shape),
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 22:43:14 +00:00
|
|
|
// At this point we want to generate a graph of shapes, starting
|
|
|
|
// services -> operations -> other shapes. This will allow us to get
|
|
|
|
// a reference count in case there are recursive data structures
|
|
|
|
var shape_references = std.StringHashMap(u64).init(allocator);
|
|
|
|
defer shape_references.deinit();
|
|
|
|
var stack = std.ArrayList([]const u8).init(allocator);
|
|
|
|
defer stack.deinit();
|
|
|
|
for (services.items) |service|
|
|
|
|
try countReferences(service, shapes, &shape_references, &stack);
|
|
|
|
|
2021-06-04 00:40:47 +00:00
|
|
|
var constant_names = std.ArrayList([]const u8).init(allocator);
|
|
|
|
defer constant_names.deinit();
|
2021-09-06 22:43:14 +00:00
|
|
|
var unresolved = std.ArrayList(smithy.ShapeInfo).init(allocator);
|
|
|
|
defer unresolved.deinit();
|
|
|
|
var generated = std.StringHashMap(void).init(allocator);
|
|
|
|
defer generated.deinit();
|
|
|
|
|
|
|
|
var state = FileGenerationState{
|
|
|
|
.shape_references = shape_references,
|
|
|
|
.additional_types_to_generate = &unresolved,
|
|
|
|
.additional_types_generated = &generated,
|
|
|
|
.shapes = shapes,
|
|
|
|
};
|
2021-05-30 01:17:45 +00:00
|
|
|
for (services.items) |service| {
|
|
|
|
var sdk_id: []const u8 = undefined;
|
|
|
|
var version: []const u8 = service.shape.service.version;
|
2021-08-13 00:51:07 +00:00
|
|
|
var name: []const u8 = service.name;
|
2021-05-30 01:17:45 +00:00
|
|
|
var arn_namespace: []const u8 = undefined;
|
|
|
|
var sigv4_name: []const u8 = undefined;
|
|
|
|
var endpoint_prefix: []const u8 = undefined;
|
|
|
|
var aws_protocol: smithy.AwsProtocol = undefined;
|
|
|
|
for (service.shape.service.traits) |trait| {
|
|
|
|
// need the info/get the info
|
|
|
|
switch (trait) {
|
|
|
|
.aws_api_service => {
|
|
|
|
arn_namespace = trait.aws_api_service.arn_namespace;
|
|
|
|
sdk_id = trait.aws_api_service.sdk_id;
|
|
|
|
endpoint_prefix = trait.aws_api_service.endpoint_prefix;
|
|
|
|
},
|
|
|
|
.aws_auth_sigv4 => sigv4_name = trait.aws_auth_sigv4.name,
|
|
|
|
.aws_protocol => aws_protocol = trait.aws_protocol,
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service struct
|
|
|
|
// name of the field will be snake_case of whatever comes in from
|
|
|
|
// sdk_id. Not sure this will simple...
|
2021-07-23 21:04:12 +00:00
|
|
|
const constant_name = try constantName(allocator, sdk_id);
|
2021-06-04 00:40:47 +00:00
|
|
|
try constant_names.append(constant_name);
|
2021-06-30 20:40:20 +00:00
|
|
|
try writer.print("const Self = @This();\n", .{});
|
|
|
|
try writer.print("pub const version: []const u8 = \"{s}\";\n", .{version});
|
|
|
|
try writer.print("pub const sdk_id: []const u8 = \"{s}\";\n", .{sdk_id});
|
|
|
|
try writer.print("pub const arn_namespace: []const u8 = \"{s}\";\n", .{arn_namespace});
|
|
|
|
try writer.print("pub const endpoint_prefix: []const u8 = \"{s}\";\n", .{endpoint_prefix});
|
|
|
|
try writer.print("pub const sigv4_name: []const u8 = \"{s}\";\n", .{sigv4_name});
|
2021-08-13 00:51:07 +00:00
|
|
|
try writer.print("pub const name: []const u8 = \"{s}\";\n", .{name});
|
2021-06-30 20:40:20 +00:00
|
|
|
// TODO: This really should just be ".whatevs". We're fully qualifying here, which isn't typical
|
2023-08-05 19:41:04 +00:00
|
|
|
try writer.print("pub const aws_protocol: smithy.AwsProtocol = {};\n\n", .{aws_protocol});
|
2021-09-05 21:32:20 +00:00
|
|
|
_ = try writer.write("pub const service_metadata: struct {\n");
|
2021-06-30 16:10:08 +00:00
|
|
|
try writer.print(" version: []const u8 = \"{s}\",\n", .{version});
|
2021-05-30 01:17:45 +00:00
|
|
|
try writer.print(" sdk_id: []const u8 = \"{s}\",\n", .{sdk_id});
|
|
|
|
try writer.print(" arn_namespace: []const u8 = \"{s}\",\n", .{arn_namespace});
|
|
|
|
try writer.print(" endpoint_prefix: []const u8 = \"{s}\",\n", .{endpoint_prefix});
|
|
|
|
try writer.print(" sigv4_name: []const u8 = \"{s}\",\n", .{sigv4_name});
|
2021-08-13 00:51:07 +00:00
|
|
|
try writer.print(" name: []const u8 = \"{s}\",\n", .{name});
|
2021-05-30 01:17:45 +00:00
|
|
|
// TODO: This really should just be ".whatevs". We're fully qualifying here, which isn't typical
|
2023-08-05 19:41:04 +00:00
|
|
|
try writer.print(" aws_protocol: smithy.AwsProtocol = {},\n", .{aws_protocol});
|
2021-06-30 20:40:20 +00:00
|
|
|
_ = try writer.write("} = .{};\n");
|
2021-05-30 01:17:45 +00:00
|
|
|
|
|
|
|
// Operations
|
|
|
|
for (service.shape.service.operations) |op|
|
2021-09-06 22:43:14 +00:00
|
|
|
try generateOperation(allocator, shapes.get(op).?, state, writer);
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|
2021-09-06 22:43:14 +00:00
|
|
|
try generateAdditionalTypes(allocator, state, writer);
|
2021-06-04 00:39:38 +00:00
|
|
|
return constant_names.toOwnedSlice();
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|
2021-09-06 22:43:14 +00:00
|
|
|
|
2021-12-23 16:51:48 +00:00
|
|
|
fn generateAdditionalTypes(allocator: std.mem.Allocator, file_state: FileGenerationState, writer: anytype) !void {
|
2021-09-06 22:43:14 +00:00
|
|
|
// More types may be added during processing
|
|
|
|
while (file_state.additional_types_to_generate.popOrNull()) |t| {
|
|
|
|
if (file_state.additional_types_generated.getEntry(t.name) != null) continue;
|
|
|
|
// std.log.info("\t\t{s}", .{t.name});
|
|
|
|
var type_stack = std.ArrayList(*const smithy.ShapeInfo).init(allocator);
|
|
|
|
defer type_stack.deinit();
|
|
|
|
const state = GenerationState{
|
|
|
|
.type_stack = &type_stack,
|
|
|
|
.file_state = file_state,
|
|
|
|
.allocator = allocator,
|
|
|
|
.indent_level = 0,
|
|
|
|
};
|
|
|
|
const type_name = avoidReserved(t.name);
|
|
|
|
try writer.print("\npub const {s} = ", .{type_name});
|
|
|
|
try file_state.additional_types_generated.putNoClobber(t.name, {});
|
|
|
|
_ = try generateTypeFor(t.id, writer, state, true);
|
|
|
|
_ = try writer.write(";\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:51:48 +00:00
|
|
|
fn constantName(allocator: std.mem.Allocator, id: []const u8) ![]const u8 {
|
2021-07-23 21:04:12 +00:00
|
|
|
// There are some ids that don't follow consistent rules, so we'll
|
|
|
|
// look for the exceptions and, if not found, revert to the snake case
|
|
|
|
// algorithm
|
|
|
|
|
|
|
|
// This one might be a bug in snake, but it's the only example so HPDL
|
|
|
|
if (std.mem.eql(u8, id, "SESv2")) return try std.fmt.allocPrint(allocator, "ses_v2", .{});
|
2022-05-29 00:59:22 +00:00
|
|
|
if (std.mem.eql(u8, id, "CloudFront")) return try std.fmt.allocPrint(allocator, "cloudfront", .{});
|
2021-07-23 21:04:12 +00:00
|
|
|
// IoT is an acryonym, but snake wouldn't know that. Interestingly not all
|
|
|
|
// iot services are capitalizing that way.
|
2022-05-29 00:59:22 +00:00
|
|
|
if (std.mem.eql(u8, id, "IoTSiteWise")) return try std.fmt.allocPrint(allocator, "iot_sitewise", .{});
|
2021-07-23 21:04:12 +00:00
|
|
|
if (std.mem.eql(u8, id, "IoTFleetHub")) return try std.fmt.allocPrint(allocator, "iot_fleet_hub", .{});
|
|
|
|
if (std.mem.eql(u8, id, "IoTSecureTunneling")) return try std.fmt.allocPrint(allocator, "iot_secure_tunneling", .{});
|
|
|
|
if (std.mem.eql(u8, id, "IoTThingsGraph")) return try std.fmt.allocPrint(allocator, "iot_things_graph", .{});
|
|
|
|
// snake turns this into dev_ops, which is a little weird
|
|
|
|
if (std.mem.eql(u8, id, "DevOps Guru")) return try std.fmt.allocPrint(allocator, "devops_guru", .{});
|
|
|
|
if (std.mem.eql(u8, id, "FSx")) return try std.fmt.allocPrint(allocator, "fsx", .{});
|
|
|
|
|
|
|
|
// Not a special case - just snake it
|
|
|
|
return try snake.fromPascalCase(allocator, id);
|
|
|
|
}
|
2021-09-05 20:09:22 +00:00
|
|
|
|
2021-09-06 22:43:14 +00:00
|
|
|
const FileGenerationState = struct {
|
|
|
|
shapes: std.StringHashMap(smithy.ShapeInfo),
|
|
|
|
shape_references: std.StringHashMap(u64),
|
|
|
|
additional_types_to_generate: *std.ArrayList(smithy.ShapeInfo),
|
|
|
|
additional_types_generated: *std.StringHashMap(void),
|
|
|
|
};
|
2021-09-05 20:09:22 +00:00
|
|
|
const GenerationState = struct {
|
|
|
|
type_stack: *std.ArrayList(*const smithy.ShapeInfo),
|
2021-09-06 22:43:14 +00:00
|
|
|
file_state: FileGenerationState,
|
2021-09-05 21:31:38 +00:00
|
|
|
// we will need some sort of "type decls needed" for recursive structures
|
2021-12-23 16:51:48 +00:00
|
|
|
allocator: std.mem.Allocator,
|
2021-09-05 20:09:22 +00:00
|
|
|
indent_level: u64,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn outputIndent(state: GenerationState, writer: anytype) !void {
|
|
|
|
const n_chars = 4 * state.indent_level;
|
|
|
|
try writer.writeByteNTimes(' ', n_chars);
|
|
|
|
}
|
2021-12-23 16:51:48 +00:00
|
|
|
fn generateOperation(allocator: std.mem.Allocator, operation: smithy.ShapeInfo, file_state: FileGenerationState, writer: anytype) !void {
|
2021-06-04 00:39:38 +00:00
|
|
|
const snake_case_name = try snake.fromPascalCase(allocator, operation.name);
|
|
|
|
defer allocator.free(snake_case_name);
|
2021-05-30 01:17:45 +00:00
|
|
|
|
|
|
|
var type_stack = std.ArrayList(*const smithy.ShapeInfo).init(allocator);
|
|
|
|
defer type_stack.deinit();
|
2021-09-05 20:09:22 +00:00
|
|
|
const state = GenerationState{
|
|
|
|
.type_stack = &type_stack,
|
2021-09-06 22:43:14 +00:00
|
|
|
.file_state = file_state,
|
2021-09-05 20:09:22 +00:00
|
|
|
.allocator = allocator,
|
|
|
|
.indent_level = 1,
|
|
|
|
};
|
|
|
|
var child_state = state;
|
|
|
|
child_state.indent_level += 1;
|
2021-05-30 01:17:45 +00:00
|
|
|
// indent should start at 4 spaces here
|
2021-06-09 23:14:09 +00:00
|
|
|
const operation_name = avoidReserved(snake_case_name);
|
2021-06-30 20:40:20 +00:00
|
|
|
try writer.print("pub const {s}: struct ", .{operation_name});
|
2021-05-30 01:17:45 +00:00
|
|
|
_ = try writer.write("{\n");
|
2021-08-13 18:03:11 +00:00
|
|
|
for (operation.shape.operation.traits) |trait| {
|
|
|
|
if (trait == .http) {
|
2021-09-05 20:09:22 +00:00
|
|
|
try outputIndent(state, writer);
|
|
|
|
_ = try writer.write("pub const http_config = .{\n");
|
|
|
|
try outputIndent(child_state, writer);
|
|
|
|
try writer.print(".method = \"{s}\",\n", .{trait.http.method});
|
|
|
|
try outputIndent(child_state, writer);
|
|
|
|
try writer.print(".uri = \"{s}\",\n", .{trait.http.uri});
|
|
|
|
try outputIndent(child_state, writer);
|
|
|
|
try writer.print(".success_code = {d},\n", .{trait.http.code});
|
|
|
|
try outputIndent(state, writer);
|
|
|
|
_ = try writer.write("};\n\n");
|
2021-08-13 18:03:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 20:09:22 +00:00
|
|
|
try outputIndent(state, writer);
|
|
|
|
try writer.print("action_name: []const u8 = \"{s}\",\n", .{operation.name});
|
|
|
|
try outputIndent(state, writer);
|
|
|
|
_ = try writer.write("Request: type = ");
|
2021-05-30 01:17:45 +00:00
|
|
|
if (operation.shape.operation.input) |member| {
|
2021-09-06 22:43:14 +00:00
|
|
|
if (try generateTypeFor(member, writer, state, false)) unreachable; // we expect only structs here
|
2021-06-09 23:14:09 +00:00
|
|
|
_ = try writer.write("\n");
|
2021-09-05 20:09:22 +00:00
|
|
|
try generateMetadataFunction(operation_name, state, writer);
|
2021-06-09 23:14:09 +00:00
|
|
|
} else {
|
|
|
|
_ = try writer.write("struct {\n");
|
2021-09-05 20:09:22 +00:00
|
|
|
try generateMetadataFunction(operation_name, state, writer);
|
2021-06-09 23:14:09 +00:00
|
|
|
}
|
2021-05-30 01:17:45 +00:00
|
|
|
_ = try writer.write(",\n");
|
2021-09-05 20:09:22 +00:00
|
|
|
try outputIndent(state, writer);
|
|
|
|
_ = try writer.write("Response: type = ");
|
2021-05-30 01:17:45 +00:00
|
|
|
if (operation.shape.operation.output) |member| {
|
2021-09-06 22:43:14 +00:00
|
|
|
if (try generateTypeFor(member, writer, state, true)) unreachable; // we expect only structs here
|
2021-05-30 01:17:45 +00:00
|
|
|
} else _ = try writer.write("struct {}"); // we want to maintain consistency with other ops
|
|
|
|
_ = try writer.write(",\n");
|
|
|
|
|
|
|
|
if (operation.shape.operation.errors) |errors| {
|
2021-09-05 20:09:22 +00:00
|
|
|
try outputIndent(state, writer);
|
|
|
|
_ = try writer.write("ServiceError: type = error{\n");
|
2021-05-30 01:17:45 +00:00
|
|
|
for (errors) |err| {
|
2021-09-06 22:43:14 +00:00
|
|
|
const err_name = getErrorName(file_state.shapes.get(err).?.name); // need to remove "exception"
|
2021-09-05 20:09:22 +00:00
|
|
|
try outputIndent(child_state, writer);
|
|
|
|
try writer.print("{s},\n", .{err_name});
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|
2021-09-05 20:09:22 +00:00
|
|
|
try outputIndent(state, writer);
|
|
|
|
_ = try writer.write("},\n");
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|
2021-06-30 20:40:20 +00:00
|
|
|
_ = try writer.write("} = .{};\n");
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 20:09:22 +00:00
|
|
|
fn generateMetadataFunction(operation_name: []const u8, state: GenerationState, writer: anytype) !void {
|
2021-06-09 23:14:09 +00:00
|
|
|
// TODO: Shove these lines in here, and also the else portion
|
|
|
|
// pub fn metaInfo(self: @This()) struct { service: @TypeOf(sts), action: @TypeOf(sts.get_caller_identity) } {
|
|
|
|
// return .{ .service = sts, .action = sts.get_caller_identity };
|
|
|
|
// }
|
|
|
|
// We want to add a short "get my parents" function into the response
|
2021-09-05 20:09:22 +00:00
|
|
|
var child_state = state;
|
|
|
|
child_state.indent_level += 1;
|
|
|
|
try outputIndent(child_state, writer);
|
2021-08-25 00:02:28 +00:00
|
|
|
_ = try writer.write("pub fn metaInfo() struct { ");
|
2021-06-30 20:40:20 +00:00
|
|
|
try writer.print("service_metadata: @TypeOf(service_metadata), action: @TypeOf({s})", .{operation_name});
|
2021-09-05 20:09:22 +00:00
|
|
|
_ = try writer.write(" } {\n");
|
|
|
|
child_state.indent_level += 1;
|
|
|
|
try outputIndent(child_state, writer);
|
|
|
|
_ = try writer.write("return .{ .service_metadata = service_metadata, ");
|
2021-06-30 20:40:20 +00:00
|
|
|
try writer.print(".action = {s}", .{operation_name});
|
2021-09-05 20:09:22 +00:00
|
|
|
_ = try writer.write(" };\n");
|
|
|
|
child_state.indent_level -= 1;
|
|
|
|
try outputIndent(child_state, writer);
|
|
|
|
_ = try writer.write("}\n");
|
|
|
|
try outputIndent(state, writer);
|
|
|
|
try writer.writeByte('}');
|
2021-06-09 23:14:09 +00:00
|
|
|
}
|
2021-05-30 01:17:45 +00:00
|
|
|
fn getErrorName(err_name: []const u8) []const u8 {
|
|
|
|
if (endsWith("Exception", err_name))
|
|
|
|
return err_name[0 .. err_name.len - "Exception".len];
|
|
|
|
|
|
|
|
if (endsWith("Fault", err_name))
|
|
|
|
return err_name[0 .. err_name.len - "Fault".len];
|
|
|
|
return err_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn endsWith(item: []const u8, str: []const u8) bool {
|
|
|
|
if (str.len < item.len) return false;
|
|
|
|
return std.mem.eql(u8, item, str[str.len - item.len ..]);
|
|
|
|
}
|
2021-09-06 22:43:14 +00:00
|
|
|
|
|
|
|
fn reuseCommonType(shape: smithy.ShapeInfo, writer: anytype, state: GenerationState) !bool {
|
|
|
|
// We want to return if we're at the top level of the stack. There are three
|
|
|
|
// reasons for this:
|
|
|
|
// 1. For operations, we have a request that includes a metadata function
|
|
|
|
// to enable aws.zig eventually to find the action based on a request.
|
|
|
|
// This could be considered a hack and maybe we should remove that
|
|
|
|
// caller convenience ability.
|
|
|
|
// 2. Given the state of zig compiler tooling, "intellisense" or whatever
|
|
|
|
// we're calling it these days, isn't real mature, so we end up looking
|
|
|
|
// at the models quite a bit. Leaving the top level alone here reduces
|
|
|
|
// the need for users to hop around too much looking at types as they
|
|
|
|
// can at least see the top level.
|
|
|
|
// 3. When we come through at the end, we want to make sure we're writing
|
|
|
|
// something or we'll have an infinite loop!
|
|
|
|
if (state.type_stack.items.len == 1) return false;
|
2021-09-05 21:31:38 +00:00
|
|
|
var rc = false;
|
2021-09-06 22:43:14 +00:00
|
|
|
if (state.file_state.shape_references.get(shape.id)) |r| {
|
|
|
|
if (r > 1 and (shape.shape == .structure or shape.shape == .uniontype)) {
|
|
|
|
rc = true;
|
|
|
|
_ = try writer.write(avoidReserved(shape.name)); // This can't possibly be this easy...
|
|
|
|
if (state.file_state.additional_types_generated.getEntry(shape.name) == null)
|
|
|
|
try state.file_state.additional_types_to_generate.append(shape);
|
|
|
|
}
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|
2021-09-06 22:43:14 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
/// return type is anyerror!void as this is a recursive function, so the compiler cannot properly infer error types
|
|
|
|
fn generateTypeFor(shape_id: []const u8, writer: anytype, state: GenerationState, end_structure: bool) anyerror!bool {
|
|
|
|
var rc = false;
|
2021-05-30 01:17:45 +00:00
|
|
|
|
|
|
|
// We assume it must exist
|
2021-09-06 22:43:14 +00:00
|
|
|
const shape_info = state.file_state.shapes.get(shape_id) orelse {
|
|
|
|
std.debug.print("Shape ID not found. This is most likely a bug. Shape ID: {s}\n", .{shape_id});
|
|
|
|
return error.InvalidType;
|
|
|
|
};
|
2021-05-30 01:17:45 +00:00
|
|
|
const shape = shape_info.shape;
|
|
|
|
// Check for ourselves up the stack
|
|
|
|
var self_occurences: u8 = 0;
|
2021-09-05 20:09:22 +00:00
|
|
|
for (state.type_stack.items) |i| {
|
2021-05-30 01:17:45 +00:00
|
|
|
// NOTE: shapes.get isn't providing a consistent pointer - is it allocating each time?
|
|
|
|
// we will therefore need to compare ids
|
|
|
|
if (std.mem.eql(u8, i.*.id, shape_info.id))
|
|
|
|
self_occurences = self_occurences + 1;
|
|
|
|
}
|
|
|
|
// Debugging
|
|
|
|
// if (std.mem.eql(u8, shape_info.name, "Expression")) {
|
|
|
|
// std.log.info(" Type stack len: {d}, occurences: {d}\n", .{ type_stack.items.len, self_occurences });
|
|
|
|
// if (type_stack.items.len > 15) {
|
|
|
|
// std.log.info(" Type stack:\n", .{});
|
|
|
|
// for (type_stack.items) |i|
|
|
|
|
// std.log.info(" {s}: {*}", .{ i.*.id, i });
|
|
|
|
// return error.BugDetected;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// End Debugging
|
|
|
|
if (self_occurences > 2) { // TODO: What's the appropriate number here?
|
|
|
|
// TODO: Determine if this warrants the creation of another public
|
|
|
|
// type to properly reference. Realistically, AWS or the service
|
|
|
|
// must be blocking deep recursion somewhere or this would be a great
|
|
|
|
// DOS attack
|
2021-09-05 20:09:22 +00:00
|
|
|
try generateSimpleTypeFor("nothing", "[]const u8", writer);
|
2021-05-30 01:17:45 +00:00
|
|
|
std.log.warn("Type cycle detected, limiting depth. Type: {s}", .{shape_id});
|
2021-09-05 21:31:38 +00:00
|
|
|
// if (std.mem.eql(u8, "com.amazonaws.workmail#Timestamp", shape_id)) {
|
|
|
|
// std.log.info(" Type stack:\n", .{});
|
|
|
|
// for (state.type_stack.items) |i|
|
|
|
|
// std.log.info(" {s}", .{i.*.id});
|
|
|
|
// }
|
|
|
|
return false; // not a map
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|
2021-09-05 20:09:22 +00:00
|
|
|
try state.type_stack.append(&shape_info);
|
2021-09-05 21:31:38 +00:00
|
|
|
defer _ = state.type_stack.pop();
|
2021-05-30 01:17:45 +00:00
|
|
|
switch (shape) {
|
2021-06-30 16:10:08 +00:00
|
|
|
.structure => {
|
2021-09-06 22:43:14 +00:00
|
|
|
if (!try reuseCommonType(shape_info, writer, state)) {
|
|
|
|
try generateComplexTypeFor(shape_id, shape.structure.members, "struct", writer, state);
|
|
|
|
if (end_structure) {
|
|
|
|
// epilog
|
|
|
|
try outputIndent(state, writer);
|
|
|
|
_ = try writer.write("}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.uniontype => {
|
|
|
|
if (!try reuseCommonType(shape_info, writer, state)) {
|
|
|
|
try generateComplexTypeFor(shape_id, shape.uniontype.members, "union", writer, state);
|
2021-06-09 23:14:09 +00:00
|
|
|
// epilog
|
2021-09-05 20:09:22 +00:00
|
|
|
try outputIndent(state, writer);
|
2021-06-09 23:14:09 +00:00
|
|
|
_ = try writer.write("}");
|
|
|
|
}
|
|
|
|
},
|
2021-09-05 20:09:22 +00:00
|
|
|
.string => |s| try generateSimpleTypeFor(s, "[]const u8", writer),
|
|
|
|
.integer => |s| try generateSimpleTypeFor(s, "i64", writer),
|
2021-06-30 16:10:08 +00:00
|
|
|
.list => {
|
2021-05-30 01:17:45 +00:00
|
|
|
_ = try writer.write("[]");
|
2021-09-05 21:31:38 +00:00
|
|
|
// The serializer will have to deal with the idea we might be an array
|
2021-09-06 22:43:14 +00:00
|
|
|
return try generateTypeFor(shape.list.member_target, writer, state, true);
|
2021-05-30 01:17:45 +00:00
|
|
|
},
|
2021-06-30 16:10:08 +00:00
|
|
|
.set => {
|
2021-05-30 01:17:45 +00:00
|
|
|
_ = try writer.write("[]");
|
2021-09-05 21:31:38 +00:00
|
|
|
// The serializer will have to deal with the idea we might be an array
|
2021-09-06 22:43:14 +00:00
|
|
|
return try generateTypeFor(shape.set.member_target, writer, state, true);
|
2021-05-30 01:17:45 +00:00
|
|
|
},
|
2021-09-05 20:09:22 +00:00
|
|
|
.timestamp => |s| try generateSimpleTypeFor(s, "i64", writer),
|
|
|
|
.blob => |s| try generateSimpleTypeFor(s, "[]const u8", writer),
|
|
|
|
.boolean => |s| try generateSimpleTypeFor(s, "bool", writer),
|
|
|
|
.double => |s| try generateSimpleTypeFor(s, "f64", writer),
|
|
|
|
.float => |s| try generateSimpleTypeFor(s, "f32", writer),
|
|
|
|
.long => |s| try generateSimpleTypeFor(s, "i64", writer),
|
2021-05-30 01:17:45 +00:00
|
|
|
.map => {
|
|
|
|
_ = try writer.write("[]struct {\n");
|
2021-09-05 20:09:22 +00:00
|
|
|
var child_state = state;
|
|
|
|
child_state.indent_level += 1;
|
|
|
|
try outputIndent(child_state, writer);
|
|
|
|
_ = try writer.write("key: ");
|
|
|
|
try writeOptional(shape.map.traits, writer, null);
|
2021-09-05 21:31:38 +00:00
|
|
|
var sub_maps = std.ArrayList([]const u8).init(state.allocator);
|
|
|
|
defer sub_maps.deinit();
|
2021-09-06 22:43:14 +00:00
|
|
|
if (try generateTypeFor(shape.map.key, writer, child_state, true))
|
2021-09-05 21:31:38 +00:00
|
|
|
try sub_maps.append("key");
|
2021-09-05 20:09:22 +00:00
|
|
|
try writeOptional(shape.map.traits, writer, " = null");
|
2021-05-30 01:17:45 +00:00
|
|
|
_ = try writer.write(",\n");
|
2021-09-05 20:09:22 +00:00
|
|
|
try outputIndent(child_state, writer);
|
|
|
|
_ = try writer.write("value: ");
|
|
|
|
try writeOptional(shape.map.traits, writer, null);
|
2021-09-06 22:43:14 +00:00
|
|
|
if (try generateTypeFor(shape.map.value, writer, child_state, true))
|
2021-09-05 21:31:38 +00:00
|
|
|
try sub_maps.append("value");
|
2021-09-05 20:09:22 +00:00
|
|
|
try writeOptional(shape.map.traits, writer, " = null");
|
2021-05-30 01:17:45 +00:00
|
|
|
_ = try writer.write(",\n");
|
2021-09-05 21:31:38 +00:00
|
|
|
if (sub_maps.items.len > 0) {
|
|
|
|
_ = try writer.write("\n");
|
|
|
|
try writeStringify(state, sub_maps.items, writer);
|
|
|
|
}
|
2021-09-05 20:09:22 +00:00
|
|
|
try outputIndent(state, writer);
|
2021-05-30 01:17:45 +00:00
|
|
|
_ = try writer.write("}");
|
2021-09-05 21:31:38 +00:00
|
|
|
|
|
|
|
rc = true;
|
2021-05-30 01:17:45 +00:00
|
|
|
},
|
|
|
|
else => {
|
|
|
|
std.log.err("encountered unimplemented shape type {s} for shape_id {s}. Generated code will not compile", .{ @tagName(shape), shape_id });
|
|
|
|
// Not sure we want to return here - I think we want an exhaustive list
|
|
|
|
// return error{UnimplementedShapeType}.UnimplementedShapeType;
|
|
|
|
},
|
|
|
|
}
|
2021-09-05 21:31:38 +00:00
|
|
|
return rc;
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 20:09:22 +00:00
|
|
|
fn generateSimpleTypeFor(_: anytype, type_name: []const u8, writer: anytype) !void {
|
|
|
|
_ = try writer.write(type_name); // This had required stuff but the problem was elsewhere. Better to leave as function just in case
|
2021-05-30 04:03:45 +00:00
|
|
|
}
|
2021-09-06 22:43:14 +00:00
|
|
|
fn generateComplexTypeFor(shape_id: []const u8, members: []smithy.TypeMember, type_type_name: []const u8, writer: anytype, state: GenerationState) anyerror!void {
|
2021-09-05 20:09:22 +00:00
|
|
|
_ = shape_id;
|
2022-02-10 23:13:44 +00:00
|
|
|
const Mapping = struct { snake: []const u8, original: []const u8 };
|
|
|
|
var field_name_mappings = try std.ArrayList(Mapping).initCapacity(state.allocator, members.len);
|
2021-08-13 17:28:29 +00:00
|
|
|
defer {
|
2022-02-10 23:13:44 +00:00
|
|
|
for (field_name_mappings.items) |mapping|
|
2021-09-05 20:09:22 +00:00
|
|
|
state.allocator.free(mapping.snake);
|
2022-02-10 23:13:44 +00:00
|
|
|
field_name_mappings.deinit();
|
2021-08-14 01:12:05 +00:00
|
|
|
}
|
|
|
|
// There is an httpQueryParams trait as well, but nobody is using it. API GW
|
|
|
|
// pretends to, but it's an empty map
|
|
|
|
//
|
|
|
|
// Same with httpPayload
|
|
|
|
//
|
|
|
|
// httpLabel is interesting - right now we just assume anything can be used - do we need to track this?
|
2021-09-05 20:09:22 +00:00
|
|
|
var http_query_mappings = try std.ArrayList(Mapping).initCapacity(state.allocator, members.len);
|
2021-08-14 01:12:05 +00:00
|
|
|
defer {
|
2021-09-05 20:09:22 +00:00
|
|
|
for (http_query_mappings.items) |mapping|
|
|
|
|
state.allocator.free(mapping.snake);
|
2021-08-14 01:12:05 +00:00
|
|
|
http_query_mappings.deinit();
|
|
|
|
}
|
2021-09-05 20:09:22 +00:00
|
|
|
var http_header_mappings = try std.ArrayList(Mapping).initCapacity(state.allocator, members.len);
|
2021-08-14 01:12:05 +00:00
|
|
|
defer {
|
2021-09-05 20:09:22 +00:00
|
|
|
for (http_header_mappings.items) |mapping|
|
|
|
|
state.allocator.free(mapping.snake);
|
2021-08-14 01:12:05 +00:00
|
|
|
http_header_mappings.deinit();
|
2021-08-13 17:28:29 +00:00
|
|
|
}
|
2021-09-05 21:31:38 +00:00
|
|
|
var map_fields = std.ArrayList([]const u8).init(state.allocator);
|
|
|
|
defer {
|
|
|
|
for (map_fields.items) |f| state.allocator.free(f);
|
|
|
|
map_fields.deinit();
|
|
|
|
}
|
2021-05-30 01:17:45 +00:00
|
|
|
// prolog. We'll rely on caller to get the spacing correct here
|
2021-05-30 04:03:45 +00:00
|
|
|
_ = try writer.write(type_type_name);
|
|
|
|
_ = try writer.write(" {\n");
|
2021-09-05 20:09:22 +00:00
|
|
|
var child_state = state;
|
|
|
|
child_state.indent_level += 1;
|
2022-05-29 01:00:38 +00:00
|
|
|
var payload: ?[]const u8 = null;
|
2021-05-30 01:17:45 +00:00
|
|
|
for (members) |member| {
|
2021-08-13 17:28:29 +00:00
|
|
|
// This is our mapping
|
2021-09-05 20:09:22 +00:00
|
|
|
const snake_case_member = try snake.fromPascalCase(state.allocator, member.name);
|
2021-08-13 17:28:29 +00:00
|
|
|
// So it looks like some services have duplicate names?! Check out "httpMethod"
|
|
|
|
// in API Gateway. Not sure what we're supposed to do there. Checking the go
|
|
|
|
// sdk, they move this particular duplicate to 'http_method' - not sure yet
|
|
|
|
// if this is a hard-coded exception`
|
2021-08-14 01:12:05 +00:00
|
|
|
var found_name_trait = false;
|
2021-08-13 17:28:29 +00:00
|
|
|
for (member.traits) |trait| {
|
2021-08-14 01:12:05 +00:00
|
|
|
switch (trait) {
|
2023-08-04 17:06:54 +00:00
|
|
|
.json_name => |n| {
|
2021-08-14 01:12:05 +00:00
|
|
|
found_name_trait = true;
|
2023-08-04 17:06:54 +00:00
|
|
|
field_name_mappings.appendAssumeCapacity(.{ .snake = try state.allocator.dupe(u8, snake_case_member), .original = n });
|
2021-08-14 01:12:05 +00:00
|
|
|
},
|
2023-08-04 17:06:54 +00:00
|
|
|
.xml_name => |n| {
|
2022-02-10 23:13:44 +00:00
|
|
|
found_name_trait = true;
|
2023-08-04 17:06:54 +00:00
|
|
|
field_name_mappings.appendAssumeCapacity(.{ .snake = try state.allocator.dupe(u8, snake_case_member), .original = n });
|
2022-02-10 23:13:44 +00:00
|
|
|
},
|
2023-08-04 17:06:54 +00:00
|
|
|
.http_query => |n| http_query_mappings.appendAssumeCapacity(.{ .snake = try state.allocator.dupe(u8, snake_case_member), .original = n }),
|
2022-02-10 23:13:44 +00:00
|
|
|
.http_header => http_header_mappings.appendAssumeCapacity(.{ .snake = try state.allocator.dupe(u8, snake_case_member), .original = trait.http_header }),
|
2022-05-29 01:00:38 +00:00
|
|
|
.http_payload => {
|
|
|
|
// Don't assert as that will be optimized for Release* builds
|
|
|
|
// We'll continue here and treat the above as a warning
|
|
|
|
if (payload) |first| {
|
|
|
|
std.log.err("Found multiple httpPayloads in violation of smithy spec! Ignoring '{s}' and using '{s}'", .{ first, snake_case_member });
|
|
|
|
}
|
|
|
|
payload = try state.allocator.dupe(u8, snake_case_member);
|
|
|
|
},
|
2021-08-14 01:12:05 +00:00
|
|
|
else => {},
|
2021-08-13 17:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-14 01:12:05 +00:00
|
|
|
if (!found_name_trait)
|
2022-02-10 23:13:44 +00:00
|
|
|
field_name_mappings.appendAssumeCapacity(.{ .snake = try state.allocator.dupe(u8, snake_case_member), .original = member.name });
|
2021-09-05 20:09:22 +00:00
|
|
|
defer state.allocator.free(snake_case_member);
|
|
|
|
try outputIndent(child_state, writer);
|
2021-09-05 21:31:38 +00:00
|
|
|
const member_name = avoidReserved(snake_case_member);
|
|
|
|
try writer.print("{s}: ", .{member_name});
|
2021-09-05 20:09:22 +00:00
|
|
|
try writeOptional(member.traits, writer, null);
|
2021-09-06 22:43:14 +00:00
|
|
|
if (try generateTypeFor(member.target, writer, child_state, true))
|
2021-09-05 21:31:38 +00:00
|
|
|
try map_fields.append(try std.fmt.allocPrint(state.allocator, "{s}", .{member_name}));
|
|
|
|
|
2021-09-05 20:09:22 +00:00
|
|
|
if (!std.mem.eql(u8, "union", type_type_name))
|
2021-06-09 23:14:09 +00:00
|
|
|
try writeOptional(member.traits, writer, " = null");
|
2021-05-30 01:17:45 +00:00
|
|
|
_ = try writer.write(",\n");
|
|
|
|
}
|
2021-08-13 17:28:29 +00:00
|
|
|
|
2021-08-14 01:12:05 +00:00
|
|
|
// Add in http query metadata (only relevant to REST JSON APIs - do we care?
|
|
|
|
// pub const http_query = .{
|
|
|
|
// .master_region = "MasterRegion",
|
|
|
|
// .function_version = "FunctionVersion",
|
|
|
|
// .marker = "Marker",
|
|
|
|
// .max_items = "MaxItems",
|
|
|
|
// };
|
|
|
|
if (http_query_mappings.items.len > 0) _ = try writer.write("\n");
|
2021-09-05 20:09:22 +00:00
|
|
|
try writeMappings(child_state, "pub ", "http_query", http_query_mappings, false, writer);
|
2021-08-14 01:12:05 +00:00
|
|
|
if (http_query_mappings.items.len > 0 and http_header_mappings.items.len > 0) _ = try writer.write("\n");
|
2021-09-05 20:09:22 +00:00
|
|
|
try writeMappings(child_state, "pub ", "http_header", http_header_mappings, false, writer);
|
2021-08-14 01:12:05 +00:00
|
|
|
|
2021-08-13 17:28:29 +00:00
|
|
|
// Add in json mappings. The function looks like this:
|
|
|
|
//
|
|
|
|
// pub fn jsonFieldNameFor(_: @This(), comptime field_name: []const u8) []const u8 {
|
|
|
|
// const mappings = .{
|
|
|
|
// .exclusive_start_table_name = "ExclusiveStartTableName",
|
|
|
|
// .limit = "Limit",
|
|
|
|
// };
|
|
|
|
// return @field(mappings, field_name);
|
|
|
|
// }
|
|
|
|
//
|
2022-05-29 01:00:38 +00:00
|
|
|
if (payload) |load| {
|
|
|
|
try writer.writeByte('\n');
|
|
|
|
try outputIndent(child_state, writer);
|
|
|
|
try writer.print("pub const http_payload: []const u8 = \"{s}\";", .{load});
|
|
|
|
}
|
|
|
|
|
2021-09-05 20:09:22 +00:00
|
|
|
try writer.writeByte('\n');
|
|
|
|
try outputIndent(child_state, writer);
|
2022-02-10 23:13:44 +00:00
|
|
|
_ = try writer.write("pub fn fieldNameFor(_: @This(), comptime field_name: []const u8) []const u8 {\n");
|
2021-09-05 20:09:22 +00:00
|
|
|
var grandchild_state = child_state;
|
|
|
|
grandchild_state.indent_level += 1;
|
|
|
|
// We need to force output here becaseu we're referencing the field in the return statement below
|
2022-02-10 23:13:44 +00:00
|
|
|
try writeMappings(grandchild_state, "", "mappings", field_name_mappings, true, writer);
|
2021-09-05 20:09:22 +00:00
|
|
|
try outputIndent(grandchild_state, writer);
|
|
|
|
_ = try writer.write("return @field(mappings, field_name);\n");
|
|
|
|
try outputIndent(child_state, writer);
|
|
|
|
_ = try writer.write("}\n");
|
2021-09-05 21:31:38 +00:00
|
|
|
try writeStringify(child_state, map_fields.items, writer);
|
|
|
|
}
|
2021-09-05 20:09:22 +00:00
|
|
|
|
2021-09-05 21:31:38 +00:00
|
|
|
fn writeStringify(state: GenerationState, fields: [][]const u8, writer: anytype) !void {
|
|
|
|
if (fields.len > 0) {
|
|
|
|
// pub fn jsonStringifyField(self: @This(), comptime field_name: []const u8, options: anytype, out_stream: anytype) !bool {
|
|
|
|
// if (std.mem.eql(u8, "tags", field_name))
|
|
|
|
// return try serializeMap(self.tags, self.jsonFieldNameFor("tags"), options, out_stream);
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
var child_state = state;
|
|
|
|
child_state.indent_level += 1;
|
|
|
|
try writer.writeByte('\n');
|
|
|
|
try outputIndent(state, writer);
|
|
|
|
_ = try writer.write("pub fn jsonStringifyField(self: @This(), comptime field_name: []const u8, options: anytype, out_stream: anytype) !bool {\n");
|
|
|
|
var return_state = child_state;
|
|
|
|
return_state.indent_level += 1;
|
|
|
|
for (fields) |field| {
|
|
|
|
try outputIndent(child_state, writer);
|
|
|
|
try writer.print("if (std.mem.eql(u8, \"{s}\", field_name))\n", .{field});
|
|
|
|
try outputIndent(return_state, writer);
|
2022-02-10 23:13:44 +00:00
|
|
|
try writer.print("return try serializeMap(self.{s}, self.fieldNameFor(\"{s}\"), options, out_stream);\n", .{ field, field });
|
2021-09-05 21:31:38 +00:00
|
|
|
}
|
|
|
|
try outputIndent(child_state, writer);
|
|
|
|
_ = try writer.write("return false;\n");
|
|
|
|
try outputIndent(state, writer);
|
|
|
|
_ = try writer.write("}\n");
|
|
|
|
}
|
2021-08-14 01:12:05 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 20:09:22 +00:00
|
|
|
fn writeMappings(state: GenerationState, @"pub": []const u8, mapping_name: []const u8, mappings: anytype, force_output: bool, writer: anytype) !void {
|
|
|
|
if (mappings.items.len == 0 and !force_output) return;
|
|
|
|
try outputIndent(state, writer);
|
|
|
|
if (mappings.items.len == 0) {
|
|
|
|
try writer.print("{s}const {s} = ", .{ @"pub", mapping_name });
|
|
|
|
_ = try writer.write(".{};\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try writer.print("{s}const {s} = .", .{ @"pub", mapping_name });
|
2021-08-13 17:28:29 +00:00
|
|
|
_ = try writer.write("{\n");
|
2021-09-05 20:09:22 +00:00
|
|
|
var child_state = state;
|
|
|
|
child_state.indent_level += 1;
|
2021-08-13 17:28:29 +00:00
|
|
|
for (mappings.items) |mapping| {
|
2021-09-05 20:09:22 +00:00
|
|
|
try outputIndent(child_state, writer);
|
2022-02-10 23:13:44 +00:00
|
|
|
try writer.print(".{s} = \"{s}\",\n", .{ avoidReserved(mapping.snake), mapping.original });
|
2021-08-13 17:28:29 +00:00
|
|
|
}
|
2021-09-05 20:09:22 +00:00
|
|
|
try outputIndent(state, writer);
|
2021-08-14 01:12:05 +00:00
|
|
|
_ = try writer.write("};\n");
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn writeOptional(traits: ?[]smithy.Trait, writer: anytype, value: ?[]const u8) !void {
|
|
|
|
if (traits) |ts| {
|
|
|
|
for (ts) |t|
|
2021-05-30 04:03:45 +00:00
|
|
|
if (t == .required) return;
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|
2021-05-30 04:03:45 +00:00
|
|
|
|
|
|
|
// not required
|
|
|
|
if (value) |v| {
|
|
|
|
_ = try writer.write(v);
|
2021-06-09 23:14:09 +00:00
|
|
|
} else _ = try writer.write("?");
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|
2021-12-23 16:51:48 +00:00
|
|
|
fn camelCase(allocator: std.mem.Allocator, name: []const u8) ![]const u8 {
|
2021-05-30 01:17:45 +00:00
|
|
|
const first_letter = name[0] + ('a' - 'A');
|
|
|
|
return try std.fmt.allocPrint(allocator, "{c}{s}", .{ first_letter, name[1..] });
|
|
|
|
}
|
2021-06-09 23:14:09 +00:00
|
|
|
fn avoidReserved(snake_name: []const u8) []const u8 {
|
|
|
|
if (std.mem.eql(u8, snake_name, "error")) return "@\"error\"";
|
|
|
|
if (std.mem.eql(u8, snake_name, "return")) return "@\"return\"";
|
|
|
|
if (std.mem.eql(u8, snake_name, "not")) return "@\"not\"";
|
|
|
|
if (std.mem.eql(u8, snake_name, "and")) return "@\"and\"";
|
|
|
|
if (std.mem.eql(u8, snake_name, "or")) return "@\"or\"";
|
|
|
|
if (std.mem.eql(u8, snake_name, "test")) return "@\"test\"";
|
|
|
|
if (std.mem.eql(u8, snake_name, "null")) return "@\"null\"";
|
|
|
|
return snake_name;
|
2021-05-30 01:17:45 +00:00
|
|
|
}
|