From cba3edfbe3ecba43f8c607272e7490130a8df9fb Mon Sep 17 00:00:00 2001 From: Fahad Zubair Date: Thu, 27 Jun 2024 11:48:28 +0100 Subject: [PATCH] Refactor the `as_validation_exception_field` function definition in `ServerBuilderConstraintViolations`. (#3719) This PR refactors the code to limit the visibility of `crate::model::ValidationExceptionField` to only those decorators and associated classes that are invoked when `smithy.framework#ValidationException` is being used. More information is in [this issue](https://github.com/smithy-lang/smithy-rs/issues/3718). Closes: [3178](https://github.com/smithy-lang/smithy-rs/issues/3718) --------- Co-authored-by: Fahad Zubair Co-authored-by: david-perez --- ...mValidationExceptionWithReasonDecorator.kt | 35 +++++++++++-------- .../SmithyValidationExceptionDecorator.kt | 33 +++++++++-------- .../ServerBuilderConstraintViolations.kt | 6 ++-- .../ValidationExceptionConversionGenerator.kt | 2 +- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/CustomValidationExceptionWithReasonDecorator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/CustomValidationExceptionWithReasonDecorator.kt index ad080febb..b7a37f9ed 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/CustomValidationExceptionWithReasonDecorator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/CustomValidationExceptionWithReasonDecorator.kt @@ -237,22 +237,27 @@ class ValidationExceptionWithReasonConversionGenerator(private val codegenContex } } - override fun builderConstraintViolationImplBlock(constraintViolations: Collection) = + override fun builderConstraintViolationFn(constraintViolations: Collection) = writable { - rustBlock("match self") { - constraintViolations.forEach { - if (it.hasInner()) { - rust("""ConstraintViolation::${it.name()}(inner) => inner.as_validation_exception_field(path + "/${it.forMember.memberName}"),""") - } else { - rust( - """ - ConstraintViolation::${it.name()} => crate::model::ValidationExceptionField { - message: format!("Value at '{}/${it.forMember.memberName}' failed to satisfy constraint: Member must not be null", path), - name: path + "/${it.forMember.memberName}", - reason: crate::model::ValidationExceptionFieldReason::Other, - }, - """, - ) + rustBlockTemplate( + "pub(crate) fn as_validation_exception_field(self, path: #{String}) -> crate::model::ValidationExceptionField", + "String" to RuntimeType.String, + ) { + rustBlock("match self") { + constraintViolations.forEach { + if (it.hasInner()) { + rust("""ConstraintViolation::${it.name()}(inner) => inner.as_validation_exception_field(path + "/${it.forMember.memberName}"),""") + } else { + rust( + """ + ConstraintViolation::${it.name()} => crate::model::ValidationExceptionField { + message: format!("Value at '{}/${it.forMember.memberName}' failed to satisfy constraint: Member must not be null", path), + name: path + "/${it.forMember.memberName}", + reason: crate::model::ValidationExceptionFieldReason::Other, + }, + """, + ) + } } } } diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/SmithyValidationExceptionDecorator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/SmithyValidationExceptionDecorator.kt index 255099601..061787f68 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/SmithyValidationExceptionDecorator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/SmithyValidationExceptionDecorator.kt @@ -190,21 +190,26 @@ class SmithyValidationExceptionConversionGenerator(private val codegenContext: S ) } - override fun builderConstraintViolationImplBlock(constraintViolations: Collection) = + override fun builderConstraintViolationFn(constraintViolations: Collection) = writable { - rustBlock("match self") { - constraintViolations.forEach { - if (it.hasInner()) { - rust("""ConstraintViolation::${it.name()}(inner) => inner.as_validation_exception_field(path + "/${it.forMember.memberName}"),""") - } else { - rust( - """ - ConstraintViolation::${it.name()} => crate::model::ValidationExceptionField { - message: format!("Value at '{}/${it.forMember.memberName}' failed to satisfy constraint: Member must not be null", path), - path: path + "/${it.forMember.memberName}", - }, - """, - ) + rustBlockTemplate( + "pub(crate) fn as_validation_exception_field(self, path: #{String}) -> crate::model::ValidationExceptionField", + "String" to RuntimeType.String, + ) { + rustBlock("match self") { + constraintViolations.forEach { + if (it.hasInner()) { + rust("""ConstraintViolation::${it.name()}(inner) => inner.as_validation_exception_field(path + "/${it.forMember.memberName}"),""") + } else { + rust( + """ + ConstraintViolation::${it.name()} => crate::model::ValidationExceptionField { + message: format!("Value at '{}/${it.forMember.memberName}' failed to satisfy constraint: Member must not be null", path), + path: path + "/${it.forMember.memberName}", + }, + """, + ) + } } } } diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerBuilderConstraintViolations.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerBuilderConstraintViolations.kt index 3e9595ca6..524fad425 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerBuilderConstraintViolations.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerBuilderConstraintViolations.kt @@ -171,12 +171,10 @@ class ServerBuilderConstraintViolations( writer.rustTemplate( """ impl ConstraintViolation { - pub(crate) fn as_validation_exception_field(self, path: #{String}) -> crate::model::ValidationExceptionField { - #{ValidationExceptionFieldWritable:W} - } + #{ValidationExceptionFnWritable:W} } """, - "ValidationExceptionFieldWritable" to validationExceptionConversionGenerator.builderConstraintViolationImplBlock((all)), + "ValidationExceptionFnWritable" to validationExceptionConversionGenerator.builderConstraintViolationFn((all)), "String" to RuntimeType.String, ) } diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ValidationExceptionConversionGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ValidationExceptionConversionGenerator.kt index b43af2a1f..8e85b6dfd 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ValidationExceptionConversionGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ValidationExceptionConversionGenerator.kt @@ -47,7 +47,7 @@ interface ValidationExceptionConversionGenerator { model: Model, ): Writable - fun builderConstraintViolationImplBlock(constraintViolations: Collection): Writable + fun builderConstraintViolationFn(constraintViolations: Collection): Writable fun collectionShapeConstraintViolationImplBlock( collectionConstraintsInfo: Collection, -- GitLab