-
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 changes
- Loading branch information
Showing
16 changed files
with
262 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 setFieldBase(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
29 changes: 29 additions & 0 deletions
29
.../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,29 @@ | ||
/* | ||
* 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 a 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 into a built object (or in the case of builders-of-builders, return the builder directly).*/ | ||
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 setFieldBase(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.