Skip to content

Commit

Permalink
Add injection hook for structures from a ServiceShape (#3147)
Browse files Browse the repository at this point in the history
Analogous to #3001, for
`ServiceShape`s


----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._

Signed-off-by: Daniele Ahmed <[email protected]>
  • Loading branch information
82marbag authored Nov 8, 2023
1 parent cc2b947 commit 79a0e72
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,9 @@ open class ServerCodegenVisitor(

ScopeMacroGenerator(codegenContext).render(this)
}

codegenDecorator.postprocessServiceGenerateAdditionalStructures(shape)
.forEach { structureShape -> this.structureShape(structureShape) }
}

/**
Expand All @@ -649,7 +652,7 @@ open class ServerCodegenVisitor(
protocolGenerator.renderOperation(this, shape)
}

codegenDecorator.postprocessGenerateAdditionalStructures(shape)
codegenDecorator.postprocessOperationGenerateAdditionalStructures(shape)
.forEach { structureShape -> this.structureShape(structureShape) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package software.amazon.smithy.rust.codegen.server.smithy.customize

import software.amazon.smithy.build.PluginContext
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.rust.codegen.core.smithy.customize.CombinedCoreCodegenDecorator
Expand Down Expand Up @@ -41,7 +42,15 @@ interface ServerCodegenDecorator : CoreCodegenDecorator<ServerCodegenContext, Se
* making the resulting crate not compile (since it will contain more than one struct with the same name).
* Therefore, ensure that all the structure shapes returned by this method are not in the service's closure.
*/
fun postprocessGenerateAdditionalStructures(operationShape: OperationShape): List<StructureShape> = emptyList()
fun postprocessOperationGenerateAdditionalStructures(operationShape: OperationShape): List<StructureShape> = emptyList()

/**
* For each service, this hook allows decorators to return a collection of structure shapes that will additionally be generated.
* If a structure shape is in the service's closure, note that returning it here will cause for it to be generated more than once,
* making the resulting crate not compile (since it will contain more than one struct with the same name).
* Therefore, ensure that all the structure shapes returned by this method are not in the service's closure.
*/
fun postprocessServiceGenerateAdditionalStructures(serviceShape: ServiceShape): List<StructureShape> = emptyList()

/**
* Configuration methods that should be injected into the `${serviceName}Config` struct to allow users to configure
Expand Down Expand Up @@ -81,8 +90,11 @@ class CombinedServerCodegenDecorator(decorators: List<ServerCodegenDecorator>) :
decorator.postprocessValidationExceptionNotAttachedErrorMessage(accumulated)
}

override fun postprocessGenerateAdditionalStructures(operationShape: OperationShape): List<StructureShape> =
orderedDecorators.flatMap { it.postprocessGenerateAdditionalStructures(operationShape) }
override fun postprocessOperationGenerateAdditionalStructures(operationShape: OperationShape): List<StructureShape> =
orderedDecorators.flatMap { it.postprocessOperationGenerateAdditionalStructures(operationShape) }

override fun postprocessServiceGenerateAdditionalStructures(serviceShape: ServiceShape): List<StructureShape> =
orderedDecorators.flatMap { it.postprocessServiceGenerateAdditionalStructures(serviceShape) }

override fun configMethods(codegenContext: ServerCodegenContext): List<ConfigMethod> =
orderedDecorators.flatMap { it.configMethods(codegenContext) }
Expand Down

0 comments on commit 79a0e72

Please sign in to comment.