add http_payload type declaration based on smithy model

This commit is contained in:
Emil Lerch 2022-05-28 18:00:38 -07:00
parent 611849bde6
commit 803581f647
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
2 changed files with 19 additions and 1 deletions

View File

@ -580,6 +580,7 @@ fn generateComplexTypeFor(shape_id: []const u8, members: []smithy.TypeMember, ty
_ = try writer.write(" {\n"); _ = try writer.write(" {\n");
var child_state = state; var child_state = state;
child_state.indent_level += 1; child_state.indent_level += 1;
var payload: ?[]const u8 = null;
for (members) |member| { for (members) |member| {
// This is our mapping // This is our mapping
const snake_case_member = try snake.fromPascalCase(state.allocator, member.name); 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_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_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 => {}, else => {},
} }
} }
@ -640,6 +649,12 @@ fn generateComplexTypeFor(shape_id: []const u8, members: []smithy.TypeMember, ty
// return @field(mappings, field_name); // 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 writer.writeByte('\n');
try outputIndent(child_state, writer); try outputIndent(child_state, writer);
_ = try writer.write("pub fn fieldNameFor(_: @This(), comptime field_name: []const u8) []const u8 {\n"); _ = try writer.write("pub fn fieldNameFor(_: @This(), comptime field_name: []const u8) []const u8 {\n");

View File

@ -95,6 +95,7 @@ pub const TraitType = enum {
http_header, http_header,
http_label, http_label,
http_query, http_query,
http_payload,
json_name, json_name,
xml_name, xml_name,
required, required,
@ -128,6 +129,7 @@ pub const Trait = union(TraitType) {
http_header: []const u8, http_header: []const u8,
http_label: []const u8, http_label: []const u8,
http_query: []const u8, http_query: []const u8,
http_payload: struct {},
required: struct {}, required: struct {},
documentation: []const u8, documentation: []const u8,
pattern: []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 }; return Trait{ .http_query = value.String };
if (std.mem.eql(u8, trait_type, "smithy.api#httpHeader")) if (std.mem.eql(u8, trait_type, "smithy.api#httpHeader"))
return Trait{ .http_header = value.String }; 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? // TODO: Maybe care about these traits?
if (std.mem.eql(u8, trait_type, "smithy.api#title")) 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#httpError
\\smithy.api#httpChecksumRequired \\smithy.api#httpChecksumRequired
\\smithy.api#httpLabel \\smithy.api#httpLabel
\\smithy.api#httpPayload
\\smithy.api#httpPrefixHeaders \\smithy.api#httpPrefixHeaders
\\smithy.api#httpQueryParams \\smithy.api#httpQueryParams
\\smithy.api#httpResponseCode \\smithy.api#httpResponseCode