Skip to content

Commit

Permalink
Refactor the as_validation_exception_field function definition in `…
Browse files Browse the repository at this point in the history
…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](#3718).

Closes: [3178](#3718)

---------

Co-authored-by: Fahad Zubair <[email protected]>
Co-authored-by: david-perez <[email protected]>
  • Loading branch information
3 people authored Jun 27, 2024
1 parent b748878 commit cba3edf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,27 @@ class ValidationExceptionWithReasonConversionGenerator(private val codegenContex
}
}

override fun builderConstraintViolationImplBlock(constraintViolations: Collection<ConstraintViolation>) =
override fun builderConstraintViolationFn(constraintViolations: Collection<ConstraintViolation>) =
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,
},
""",
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,26 @@ class SmithyValidationExceptionConversionGenerator(private val codegenContext: S
)
}

override fun builderConstraintViolationImplBlock(constraintViolations: Collection<ConstraintViolation>) =
override fun builderConstraintViolationFn(constraintViolations: Collection<ConstraintViolation>) =
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}",
},
""",
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ interface ValidationExceptionConversionGenerator {
model: Model,
): Writable

fun builderConstraintViolationImplBlock(constraintViolations: Collection<ConstraintViolation>): Writable
fun builderConstraintViolationFn(constraintViolations: Collection<ConstraintViolation>): Writable

fun collectionShapeConstraintViolationImplBlock(
collectionConstraintsInfo: Collection<CollectionTraitInfo>,
Expand Down

0 comments on commit cba3edf

Please sign in to comment.