Skip to content

Commit

Permalink
[smithy-rs] Render a Union member of type Unit to an enum variant wit…
Browse files Browse the repository at this point in the history
…hout inner data (#1989)

* Avoid explicitly emitting Unit type within Union

This commit addresses #1546. Previously, the Unit type in a Union was
rendered as an enum variant whose inner data was crate::model::Unit.
The way such a variant appears in Rust code feels a bit odd as it
usually does not carry inner data for `()`. We now render a Union
member of type Unit to an enum variant without inner data.

* Address test failures washed out in CI

This commit updates places that need to be aware of the Unit type attached
to a Union member. Those places will render the Union member in a way that
the generated Rust code does not include inner data of type Unit. It should
help pass `codegen-client-test:test` and `codegen-server-test:test`.

Further refactoring is required because each place we updated performs an
explicit if-else check for special casing, which we should avoid.

* Update codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/UnionGeneratorTest.kt

Co-authored-by: John DiSanti <[email protected]>

* Remove commented-out code

* Add a helper for comparing against ShapeId for Unit

This commit adds a helper for checking whether MemberShape targets the
ShapeId of the Unit type. The benefit of the helper is that each call
site does not have to construct a ShapeId for the Unit type every time
comparison is made.

* Move Unit type bifurcation logic to jsonObjectWriter

This commit moves the if-else expression hard-coded in the StructureShape
matching case within RustWriter.serializeMemberValue to jsonObjectWriter.
The previous approach of inlining the if-else expression out of
jsonObjectWriter to the StructureShape case and tailoring it to the call
site violated the DRY principle.

* Make QuerySerializerGenerator in sync with the change

This commit brings the Unit type related change we've made so far to
QuerySerializerGenerator. All tests in CI passed even without this
commit, but noticing how similar QuerySerializerGenerator is to
JsonSerializerGenerator, we should make the former in sync with the
latter.

* Update CHANGELOG.next.toml

* Refactor ofTypeUnit -> isTargetUnit

This commit addresses smithy-lang/smithy-rs#1989 (comment).
The extension should be renamed to isTargetUnit and moved to Smithy.kt.

* Update codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/JsonSerializerGenerator.kt

Co-authored-by: John DiSanti <[email protected]>

* Simplify if-else in jsonObjectWriter

This commit addresses smithy-lang/smithy-rs#1989 (comment)

* Avoid the union member's reference name being empty

This commit addresses smithy-lang/smithy-rs#1989 (comment)
Even if member's reference name "inner" is present, it will not be an
issue when the member is the Unit type where the reference name "inner"
cannot be extracted. The reason is jsonObjectWriter won't render the
serialization function if the member is the Unit type.

That said, the same change in QuerySerializerGenerator may not be the
case so we'll run the tests in CI and see what breaks.

* Ensure Union with Unit target can be serialized

This commit updates serialization of a Union with a Unit target in
different types of serializers. We first updated protocol tests by
adding a new field of type Unit and some of them failed as expected.
We worked our way back from those failed tests and fixed the said
implementation for serialization accordingly.

* Ensure Union with Unit target can be parsed

This commit handles deserialization of a Union with a Unit target in
XmlBindingTraitParserGenerator. Prior to the commit, we already handled
the said deserialization correctly in JsonParserGenerator but missed it
in XmlBindingTraitParserGenerator. We added a new field of type Unit to
a Union in parser protocols tests, ran them, and worked our way back from
the failed tests.

* Ensure match arm for Unit works in custom Debug impl

This commit handles a use case that came up as a result of combining two
updates, implementing a custom Debug impl for a Union and avoid rendering
the Unit type in a Union. In the custom Debug impl, we now make sure that
if the target is of type Unit, we render the corresponding match arm
without the inner data. We add a new unit test in UnionGeneratorTest.

* Fix unused variables warnings in CI

This commit adds the #[allow(unused_variables)] annotation to a block of code
generated for a member being the Unit type. The annotation is put before we
call the handleOptional function so that it covers the whole block that the
function generates.

* Fix E0658 on unused_variables

This commit fixes an error where attributes on expressions are experimental.
It does so by rearranging code in a way that achieves the same effect but
without #[allow(unused_variables)] on an expression.

Co-authored-by: Yuki Saito <[email protected]>
Co-authored-by: John DiSanti <[email protected]>
  • Loading branch information
3 people authored and aws-sdk-rust-ci committed Dec 14, 2022
1 parent be76de4 commit a7f25f3
Show file tree
Hide file tree
Showing 277 changed files with 10,522 additions and 384 deletions.
38 changes: 38 additions & 0 deletions sdk/accessanalyzer/src/json_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ pub fn serialize_structure_crate_input_create_access_preview_input(
object.key("clientToken").string(var_5.as_str());
}
if let Some(var_6) = &input.configurations {
#[allow(unused_mut)]
let mut object_7 = object.key("configurations").start_object();
for (key_8, value_9) in var_6 {
{
#[allow(unused_mut)]
let mut object_10 = object_7.key(key_8.as_str()).start_object();
crate::json_ser::serialize_union_crate_model_configuration(
&mut object_10,
Expand All @@ -53,6 +55,7 @@ pub fn serialize_structure_crate_input_create_analyzer_input(
let mut array_13 = object.key("archiveRules").start_array();
for item_14 in var_12 {
{
#[allow(unused_mut)]
let mut object_15 = array_13.value().start_object();
crate::json_ser::serialize_structure_crate_model_inline_archive_rule(
&mut object_15,
Expand All @@ -67,6 +70,7 @@ pub fn serialize_structure_crate_input_create_analyzer_input(
object.key("clientToken").string(var_16.as_str());
}
if let Some(var_17) = &input.tags {
#[allow(unused_mut)]
let mut object_18 = object.key("tags").start_object();
for (key_19, value_20) in var_17 {
{
Expand All @@ -89,9 +93,11 @@ pub fn serialize_structure_crate_input_create_archive_rule_input(
object.key("clientToken").string(var_22.as_str());
}
if let Some(var_23) = &input.filter {
#[allow(unused_mut)]
let mut object_24 = object.key("filter").start_object();
for (key_25, value_26) in var_23 {
{
#[allow(unused_mut)]
let mut object_27 = object_24.key(key_25.as_str()).start_object();
crate::json_ser::serialize_structure_crate_model_criterion(
&mut object_27,
Expand All @@ -116,9 +122,11 @@ pub fn serialize_structure_crate_input_list_access_preview_findings_input(
object.key("analyzerArn").string(var_29.as_str());
}
if let Some(var_30) = &input.filter {
#[allow(unused_mut)]
let mut object_31 = object.key("filter").start_object();
for (key_32, value_33) in var_30 {
{
#[allow(unused_mut)]
let mut object_34 = object_31.key(key_32.as_str()).start_object();
crate::json_ser::serialize_structure_crate_model_criterion(
&mut object_34,
Expand Down Expand Up @@ -171,9 +179,11 @@ pub fn serialize_structure_crate_input_list_findings_input(
object.key("analyzerArn").string(var_41.as_str());
}
if let Some(var_42) = &input.filter {
#[allow(unused_mut)]
let mut object_43 = object.key("filter").start_object();
for (key_44, value_45) in var_42 {
{
#[allow(unused_mut)]
let mut object_46 = object_43.key(key_44.as_str()).start_object();
crate::json_ser::serialize_structure_crate_model_criterion(
&mut object_46,
Expand All @@ -194,6 +204,7 @@ pub fn serialize_structure_crate_input_list_findings_input(
object.key("nextToken").string(var_48.as_str());
}
if let Some(var_49) = &input.sort {
#[allow(unused_mut)]
let mut object_50 = object.key("sort").start_object();
crate::json_ser::serialize_structure_crate_model_sort_criteria(&mut object_50, var_49)?;
object_50.finish();
Expand All @@ -209,6 +220,7 @@ pub fn serialize_structure_crate_input_start_policy_generation_input(
object.key("clientToken").string(var_51.as_str());
}
if let Some(var_52) = &input.cloud_trail_details {
#[allow(unused_mut)]
let mut object_53 = object.key("cloudTrailDetails").start_object();
crate::json_ser::serialize_structure_crate_model_cloud_trail_details(
&mut object_53,
Expand All @@ -217,6 +229,7 @@ pub fn serialize_structure_crate_input_start_policy_generation_input(
object_53.finish();
}
if let Some(var_54) = &input.policy_generation_details {
#[allow(unused_mut)]
let mut object_55 = object.key("policyGenerationDetails").start_object();
crate::json_ser::serialize_structure_crate_model_policy_generation_details(
&mut object_55,
Expand Down Expand Up @@ -245,6 +258,7 @@ pub fn serialize_structure_crate_input_tag_resource_input(
input: &crate::input::TagResourceInput,
) -> Result<(), aws_smithy_http::operation::error::SerializationError> {
if let Some(var_58) = &input.tags {
#[allow(unused_mut)]
let mut object_59 = object.key("tags").start_object();
for (key_60, value_61) in var_58 {
{
Expand All @@ -264,9 +278,11 @@ pub fn serialize_structure_crate_input_update_archive_rule_input(
object.key("clientToken").string(var_62.as_str());
}
if let Some(var_63) = &input.filter {
#[allow(unused_mut)]
let mut object_64 = object.key("filter").start_object();
for (key_65, value_66) in var_63 {
{
#[allow(unused_mut)]
let mut object_67 = object_64.key(key_65.as_str()).start_object();
crate::json_ser::serialize_structure_crate_model_criterion(
&mut object_67,
Expand Down Expand Up @@ -335,6 +351,7 @@ pub fn serialize_union_crate_model_configuration(
) -> Result<(), aws_smithy_http::operation::error::SerializationError> {
match input {
crate::model::Configuration::IamRole(inner) => {
#[allow(unused_mut)]
let mut object_79 = object_10.key("iamRole").start_object();
crate::json_ser::serialize_structure_crate_model_iam_role_configuration(
&mut object_79,
Expand All @@ -343,6 +360,7 @@ pub fn serialize_union_crate_model_configuration(
object_79.finish();
}
crate::model::Configuration::KmsKey(inner) => {
#[allow(unused_mut)]
let mut object_80 = object_10.key("kmsKey").start_object();
crate::json_ser::serialize_structure_crate_model_kms_key_configuration(
&mut object_80,
Expand All @@ -351,6 +369,7 @@ pub fn serialize_union_crate_model_configuration(
object_80.finish();
}
crate::model::Configuration::SecretsManagerSecret(inner) => {
#[allow(unused_mut)]
let mut object_81 = object_10.key("secretsManagerSecret").start_object();
crate::json_ser::serialize_structure_crate_model_secrets_manager_secret_configuration(
&mut object_81,
Expand All @@ -359,6 +378,7 @@ pub fn serialize_union_crate_model_configuration(
object_81.finish();
}
crate::model::Configuration::S3Bucket(inner) => {
#[allow(unused_mut)]
let mut object_82 = object_10.key("s3Bucket").start_object();
crate::json_ser::serialize_structure_crate_model_s3_bucket_configuration(
&mut object_82,
Expand All @@ -367,6 +387,7 @@ pub fn serialize_union_crate_model_configuration(
object_82.finish();
}
crate::model::Configuration::SqsQueue(inner) => {
#[allow(unused_mut)]
let mut object_83 = object_10.key("sqsQueue").start_object();
crate::json_ser::serialize_structure_crate_model_sqs_queue_configuration(
&mut object_83,
Expand All @@ -393,9 +414,11 @@ pub fn serialize_structure_crate_model_inline_archive_rule(
object.key("ruleName").string(var_84.as_str());
}
if let Some(var_85) = &input.filter {
#[allow(unused_mut)]
let mut object_86 = object.key("filter").start_object();
for (key_87, value_88) in var_85 {
{
#[allow(unused_mut)]
let mut object_89 = object_86.key(key_87.as_str()).start_object();
crate::json_ser::serialize_structure_crate_model_criterion(
&mut object_89,
Expand Down Expand Up @@ -467,6 +490,7 @@ pub fn serialize_structure_crate_model_cloud_trail_details(
let mut array_103 = object.key("trails").start_array();
for item_104 in var_102 {
{
#[allow(unused_mut)]
let mut object_105 = array_103.value().start_object();
crate::json_ser::serialize_structure_crate_model_trail(&mut object_105, item_104)?;
object_105.finish();
Expand Down Expand Up @@ -515,6 +539,7 @@ pub fn serialize_structure_crate_model_kms_key_configuration(
input: &crate::model::KmsKeyConfiguration,
) -> Result<(), aws_smithy_http::operation::error::SerializationError> {
if let Some(var_111) = &input.key_policies {
#[allow(unused_mut)]
let mut object_112 = object.key("keyPolicies").start_object();
for (key_113, value_114) in var_111 {
{
Expand All @@ -527,6 +552,7 @@ pub fn serialize_structure_crate_model_kms_key_configuration(
let mut array_116 = object.key("grants").start_array();
for item_117 in var_115 {
{
#[allow(unused_mut)]
let mut object_118 = array_116.value().start_object();
crate::json_ser::serialize_structure_crate_model_kms_grant_configuration(
&mut object_118,
Expand Down Expand Up @@ -564,6 +590,7 @@ pub fn serialize_structure_crate_model_s3_bucket_configuration(
let mut array_123 = object.key("bucketAclGrants").start_array();
for item_124 in var_122 {
{
#[allow(unused_mut)]
let mut object_125 = array_123.value().start_object();
crate::json_ser::serialize_structure_crate_model_s3_bucket_acl_grant_configuration(
&mut object_125,
Expand All @@ -575,6 +602,7 @@ pub fn serialize_structure_crate_model_s3_bucket_configuration(
array_123.finish();
}
if let Some(var_126) = &input.bucket_public_access_block {
#[allow(unused_mut)]
let mut object_127 = object.key("bucketPublicAccessBlock").start_object();
crate::json_ser::serialize_structure_crate_model_s3_public_access_block_configuration(
&mut object_127,
Expand All @@ -583,9 +611,11 @@ pub fn serialize_structure_crate_model_s3_bucket_configuration(
object_127.finish();
}
if let Some(var_128) = &input.access_points {
#[allow(unused_mut)]
let mut object_129 = object.key("accessPoints").start_object();
for (key_130, value_131) in var_128 {
{
#[allow(unused_mut)]
let mut object_132 = object_129.key(key_130.as_str()).start_object();
crate::json_ser::serialize_structure_crate_model_s3_access_point_configuration(
&mut object_132,
Expand Down Expand Up @@ -651,6 +681,7 @@ pub fn serialize_structure_crate_model_kms_grant_configuration(
object.key("retiringPrincipal").string(var_143.as_str());
}
if let Some(var_144) = &input.constraints {
#[allow(unused_mut)]
let mut object_145 = object.key("constraints").start_object();
crate::json_ser::serialize_structure_crate_model_kms_grant_constraints(
&mut object_145,
Expand All @@ -672,6 +703,7 @@ pub fn serialize_structure_crate_model_s3_bucket_acl_grant_configuration(
object.key("permission").string(var_147.as_str());
}
if let Some(var_148) = &input.grantee {
#[allow(unused_mut)]
let mut object_149 = object.key("grantee").start_object();
crate::json_ser::serialize_union_crate_model_acl_grantee(&mut object_149, var_148)?;
object_149.finish();
Expand Down Expand Up @@ -700,6 +732,7 @@ pub fn serialize_structure_crate_model_s3_access_point_configuration(
object.key("accessPointPolicy").string(var_152.as_str());
}
if let Some(var_153) = &input.public_access_block {
#[allow(unused_mut)]
let mut object_154 = object.key("publicAccessBlock").start_object();
crate::json_ser::serialize_structure_crate_model_s3_public_access_block_configuration(
&mut object_154,
Expand All @@ -708,6 +741,7 @@ pub fn serialize_structure_crate_model_s3_access_point_configuration(
object_154.finish();
}
if let Some(var_155) = &input.network_origin {
#[allow(unused_mut)]
let mut object_156 = object.key("networkOrigin").start_object();
crate::json_ser::serialize_union_crate_model_network_origin_configuration(
&mut object_156,
Expand All @@ -723,6 +757,7 @@ pub fn serialize_structure_crate_model_kms_grant_constraints(
input: &crate::model::KmsGrantConstraints,
) -> Result<(), aws_smithy_http::operation::error::SerializationError> {
if let Some(var_157) = &input.encryption_context_equals {
#[allow(unused_mut)]
let mut object_158 = object.key("encryptionContextEquals").start_object();
for (key_159, value_160) in var_157 {
{
Expand All @@ -732,6 +767,7 @@ pub fn serialize_structure_crate_model_kms_grant_constraints(
object_158.finish();
}
if let Some(var_161) = &input.encryption_context_subset {
#[allow(unused_mut)]
let mut object_162 = object.key("encryptionContextSubset").start_object();
for (key_163, value_164) in var_161 {
{
Expand Down Expand Up @@ -771,6 +807,7 @@ pub fn serialize_union_crate_model_network_origin_configuration(
) -> Result<(), aws_smithy_http::operation::error::SerializationError> {
match input {
crate::model::NetworkOriginConfiguration::VpcConfiguration(inner) => {
#[allow(unused_mut)]
let mut object_165 = object_156.key("vpcConfiguration").start_object();
crate::json_ser::serialize_structure_crate_model_vpc_configuration(
&mut object_165,
Expand All @@ -779,6 +816,7 @@ pub fn serialize_union_crate_model_network_origin_configuration(
object_165.finish();
}
crate::model::NetworkOriginConfiguration::InternetConfiguration(inner) => {
#[allow(unused_mut)]
let mut object_166 = object_156.key("internetConfiguration").start_object();
crate::json_ser::serialize_structure_crate_model_internet_configuration(
&mut object_166,
Expand Down
1 change: 1 addition & 0 deletions sdk/account/src/json_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub fn serialize_structure_crate_input_put_contact_information_input(
object.key("AccountId").string(var_12.as_str());
}
if let Some(var_13) = &input.contact_information {
#[allow(unused_mut)]
let mut object_14 = object.key("ContactInformation").start_object();
crate::json_ser::serialize_structure_crate_model_contact_information(
&mut object_14,
Expand Down
9 changes: 9 additions & 0 deletions sdk/acm/src/json_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub fn serialize_structure_crate_input_add_tags_to_certificate_input(
let mut array_3 = object.key("Tags").start_array();
for item_4 in var_2 {
{
#[allow(unused_mut)]
let mut object_5 = array_3.value().start_object();
crate::json_ser::serialize_structure_crate_model_tag(&mut object_5, item_4)?;
object_5.finish();
Expand Down Expand Up @@ -91,6 +92,7 @@ pub fn serialize_structure_crate_input_import_certificate_input(
let mut array_16 = object.key("Tags").start_array();
for item_17 in var_15 {
{
#[allow(unused_mut)]
let mut object_18 = array_16.value().start_object();
crate::json_ser::serialize_structure_crate_model_tag(&mut object_18, item_17)?;
object_18.finish();
Expand All @@ -115,6 +117,7 @@ pub fn serialize_structure_crate_input_list_certificates_input(
array_20.finish();
}
if let Some(var_22) = &input.includes {
#[allow(unused_mut)]
let mut object_23 = object.key("Includes").start_object();
crate::json_ser::serialize_structure_crate_model_filters(&mut object_23, var_22)?;
object_23.finish();
Expand Down Expand Up @@ -152,6 +155,7 @@ pub fn serialize_structure_crate_input_put_account_configuration_input(
input: &crate::input::PutAccountConfigurationInput,
) -> Result<(), aws_smithy_http::operation::error::SerializationError> {
if let Some(var_29) = &input.expiry_events {
#[allow(unused_mut)]
let mut object_30 = object.key("ExpiryEvents").start_object();
crate::json_ser::serialize_structure_crate_model_expiry_events_configuration(
&mut object_30,
Expand All @@ -176,6 +180,7 @@ pub fn serialize_structure_crate_input_remove_tags_from_certificate_input(
let mut array_34 = object.key("Tags").start_array();
for item_35 in var_33 {
{
#[allow(unused_mut)]
let mut object_36 = array_34.value().start_object();
crate::json_ser::serialize_structure_crate_model_tag(&mut object_36, item_35)?;
object_36.finish();
Expand Down Expand Up @@ -222,6 +227,7 @@ pub fn serialize_structure_crate_input_request_certificate_input(
let mut array_45 = object.key("DomainValidationOptions").start_array();
for item_46 in var_44 {
{
#[allow(unused_mut)]
let mut object_47 = array_45.value().start_object();
crate::json_ser::serialize_structure_crate_model_domain_validation_option(
&mut object_47,
Expand All @@ -233,6 +239,7 @@ pub fn serialize_structure_crate_input_request_certificate_input(
array_45.finish();
}
if let Some(var_48) = &input.options {
#[allow(unused_mut)]
let mut object_49 = object.key("Options").start_object();
crate::json_ser::serialize_structure_crate_model_certificate_options(
&mut object_49,
Expand All @@ -249,6 +256,7 @@ pub fn serialize_structure_crate_input_request_certificate_input(
let mut array_52 = object.key("Tags").start_array();
for item_53 in var_51 {
{
#[allow(unused_mut)]
let mut object_54 = array_52.value().start_object();
crate::json_ser::serialize_structure_crate_model_tag(&mut object_54, item_53)?;
object_54.finish();
Expand Down Expand Up @@ -283,6 +291,7 @@ pub fn serialize_structure_crate_input_update_certificate_options_input(
object.key("CertificateArn").string(var_58.as_str());
}
if let Some(var_59) = &input.options {
#[allow(unused_mut)]
let mut object_60 = object.key("Options").start_object();
crate::json_ser::serialize_structure_crate_model_certificate_options(
&mut object_60,
Expand Down
Loading

0 comments on commit a7f25f3

Please sign in to comment.