-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract builderInstantiator interface to prepare for nullability chan…
…ges (#2988) ## Motivation and Context #1725 exposed the need for easily configuring builder behavior between client & server ## Description - extract builderGenerator interface and attach it to `codegenContext` to avoid loads of threading it up and down ## Testing - codegen unit tests - [x] codegen diff audit ## Checklist <!--- If a checkbox below is not applicable, then please DELETE it rather than leaving it unchecked --> - [x] I have updated `CHANGELOG.next.toml` if I made changes to the smithy-rs codegen or runtime crates - [x] I have updated `CHANGELOG.next.toml` if I made changes to the AWS SDK, generated SDK code, or SDK runtime crates ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
- Loading branch information
Showing
15 changed files
with
258 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...software/amazon/smithy/rust/codegen/client/smithy/generators/ClientBuilderInstantiator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rust.codegen.client.smithy.generators | ||
|
||
import software.amazon.smithy.model.shapes.MemberShape | ||
import software.amazon.smithy.model.shapes.StructureShape | ||
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext | ||
import software.amazon.smithy.rust.codegen.core.rustlang.Writable | ||
import software.amazon.smithy.rust.codegen.core.rustlang.map | ||
import software.amazon.smithy.rust.codegen.core.rustlang.rust | ||
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate | ||
import software.amazon.smithy.rust.codegen.core.rustlang.writable | ||
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider | ||
import software.amazon.smithy.rust.codegen.core.smithy.generators.BuilderGenerator | ||
import software.amazon.smithy.rust.codegen.core.smithy.generators.BuilderInstantiator | ||
|
||
fun ClientCodegenContext.builderInstantiator(): BuilderInstantiator = ClientBuilderInstantiator(symbolProvider) | ||
|
||
class ClientBuilderInstantiator(private val symbolProvider: RustSymbolProvider) : BuilderInstantiator { | ||
override fun setField(builder: String, value: Writable, field: MemberShape): Writable { | ||
return setFieldWithSetter(builder, value, field) | ||
} | ||
|
||
override fun finalizeBuilder(builder: String, shape: StructureShape, mapErr: Writable?): Writable = writable { | ||
if (BuilderGenerator.hasFallibleBuilder(shape, symbolProvider)) { | ||
rustTemplate( | ||
"$builder.build()#{mapErr}?", | ||
"mapErr" to ( | ||
mapErr?.map { | ||
rust(".map_err(#T)", it) | ||
} ?: writable { } | ||
), | ||
) | ||
} else { | ||
rust("$builder.build()") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
.../kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/BuilderInstantiator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rust.codegen.core.smithy.generators | ||
|
||
import software.amazon.smithy.model.shapes.MemberShape | ||
import software.amazon.smithy.model.shapes.StructureShape | ||
import software.amazon.smithy.rust.codegen.core.rustlang.Writable | ||
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate | ||
import software.amazon.smithy.rust.codegen.core.rustlang.writable | ||
|
||
/** Abstraction for instantiating builders. | ||
* | ||
* Builder abstractions vary—clients MAY use `build_with_error_correction`, e.g., and builders can vary in fallibility. | ||
* */ | ||
interface BuilderInstantiator { | ||
/** Set a field on a builder. */ | ||
fun setField(builder: String, value: Writable, field: MemberShape): Writable | ||
|
||
/** Finalize a builder, turning it into a built object | ||
* - In the case of builders-of-builders, the value should be returned directly | ||
* - If an error is returned, you MUST use `mapErr` to convert the error type | ||
*/ | ||
fun finalizeBuilder(builder: String, shape: StructureShape, mapErr: Writable? = null): Writable | ||
|
||
/** Set a field on a builder using the `$setterName` method. $value will be passed directly. */ | ||
fun setFieldWithSetter(builder: String, value: Writable, field: MemberShape) = writable { | ||
rustTemplate("$builder = $builder.${field.setterName()}(#{value})", "value" to value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.