From 803581f647da3abae05b49a2323e4716d5b16bde Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Sat, 28 May 2022 18:00:38 -0700 Subject: [PATCH] add http_payload type declaration based on smithy model --- codegen/src/main.zig | 15 +++++++++++++++ smithy/src/smithy.zig | 5 ++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/codegen/src/main.zig b/codegen/src/main.zig index 13cc9be..70aa4cd 100644 --- a/codegen/src/main.zig +++ b/codegen/src/main.zig @@ -580,6 +580,7 @@ fn generateComplexTypeFor(shape_id: []const u8, members: []smithy.TypeMember, ty _ = try writer.write(" {\n"); var child_state = state; child_state.indent_level += 1; + var payload: ?[]const u8 = null; for (members) |member| { // This is our mapping const snake_case_member = try snake.fromPascalCase(state.allocator, member.name); @@ -600,6 +601,14 @@ fn generateComplexTypeFor(shape_id: []const u8, members: []smithy.TypeMember, ty }, .http_query => http_query_mappings.appendAssumeCapacity(.{ .snake = try state.allocator.dupe(u8, snake_case_member), .original = trait.http_query }), .http_header => http_header_mappings.appendAssumeCapacity(.{ .snake = try state.allocator.dupe(u8, snake_case_member), .original = trait.http_header }), + .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); + }, else => {}, } } @@ -640,6 +649,12 @@ fn generateComplexTypeFor(shape_id: []const u8, members: []smithy.TypeMember, ty // return @field(mappings, field_name); // } // + if (payload) |load| { + try writer.writeByte('\n'); + try outputIndent(child_state, writer); + try writer.print("pub const http_payload: []const u8 = \"{s}\";", .{load}); + } + try writer.writeByte('\n'); try outputIndent(child_state, writer); _ = try writer.write("pub fn fieldNameFor(_: @This(), comptime field_name: []const u8) []const u8 {\n"); diff --git a/smithy/src/smithy.zig b/smithy/src/smithy.zig index dcf6b34..f5f9f71 100644 --- a/smithy/src/smithy.zig +++ b/smithy/src/smithy.zig @@ -95,6 +95,7 @@ pub const TraitType = enum { http_header, http_label, http_query, + http_payload, json_name, xml_name, required, @@ -128,6 +129,7 @@ pub const Trait = union(TraitType) { http_header: []const u8, http_label: []const u8, http_query: []const u8, + http_payload: struct {}, required: struct {}, documentation: []const u8, pattern: []const u8, @@ -573,6 +575,8 @@ fn getTrait(trait_type: []const u8, value: std.json.Value) SmithyParseError!?Tra return Trait{ .http_query = value.String }; if (std.mem.eql(u8, trait_type, "smithy.api#httpHeader")) return Trait{ .http_header = value.String }; + if (std.mem.eql(u8, trait_type, "smithy.api#httpPayload")) + return Trait{ .http_payload = .{} }; // TODO: Maybe care about these traits? if (std.mem.eql(u8, trait_type, "smithy.api#title")) @@ -600,7 +604,6 @@ fn getTrait(trait_type: []const u8, value: std.json.Value) SmithyParseError!?Tra \\smithy.api#httpError \\smithy.api#httpChecksumRequired \\smithy.api#httpLabel - \\smithy.api#httpPayload \\smithy.api#httpPrefixHeaders \\smithy.api#httpQueryParams \\smithy.api#httpResponseCode