|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.rust.codegen.client.smithy.generators |
| 7 | + |
| 8 | +import software.amazon.smithy.model.node.Node |
| 9 | +import software.amazon.smithy.model.shapes.BlobShape |
| 10 | +import software.amazon.smithy.model.shapes.BooleanShape |
| 11 | +import software.amazon.smithy.model.shapes.DocumentShape |
| 12 | +import software.amazon.smithy.model.shapes.EnumShape |
| 13 | +import software.amazon.smithy.model.shapes.ListShape |
| 14 | +import software.amazon.smithy.model.shapes.MapShape |
| 15 | +import software.amazon.smithy.model.shapes.MemberShape |
| 16 | +import software.amazon.smithy.model.shapes.NumberShape |
| 17 | +import software.amazon.smithy.model.shapes.StringShape |
| 18 | +import software.amazon.smithy.model.shapes.StructureShape |
| 19 | +import software.amazon.smithy.model.shapes.TimestampShape |
| 20 | +import software.amazon.smithy.model.shapes.UnionShape |
| 21 | +import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext |
| 22 | +import software.amazon.smithy.rust.codegen.core.rustlang.RustModule |
| 23 | +import software.amazon.smithy.rust.codegen.core.rustlang.Writable |
| 24 | +import software.amazon.smithy.rust.codegen.core.rustlang.map |
| 25 | +import software.amazon.smithy.rust.codegen.core.rustlang.rust |
| 26 | +import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate |
| 27 | +import software.amazon.smithy.rust.codegen.core.rustlang.some |
| 28 | +import software.amazon.smithy.rust.codegen.core.rustlang.writable |
| 29 | +import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType |
| 30 | +import software.amazon.smithy.rust.codegen.core.smithy.generators.BuilderGenerator |
| 31 | +import software.amazon.smithy.rust.codegen.core.smithy.generators.PrimitiveInstantiator |
| 32 | +import software.amazon.smithy.rust.codegen.core.smithy.protocols.shapeFunctionName |
| 33 | +import software.amazon.smithy.rust.codegen.core.util.isEventStream |
| 34 | +import software.amazon.smithy.rust.codegen.core.util.isStreaming |
| 35 | + |
| 36 | +/** |
| 37 | + * For AWS-services, the spec defines error correction semantics to recover from missing default values for required members: |
| 38 | + * https://smithy.io/2.0/spec/aggregate-types.html?highlight=error%20correction#client-error-correction |
| 39 | + */ |
| 40 | + |
| 41 | +private fun ClientCodegenContext.errorCorrectedDefault(member: MemberShape): Writable? { |
| 42 | + if (!member.isRequired) { |
| 43 | + return null |
| 44 | + } |
| 45 | + symbolProvider.toSymbol(member) |
| 46 | + val target = model.expectShape(member.target) |
| 47 | + val targetSymbol = symbolProvider.toSymbol(target) |
| 48 | + if (member.isEventStream(model) || member.isStreaming(model)) { |
| 49 | + return null |
| 50 | + } |
| 51 | + val instantiator = PrimitiveInstantiator(runtimeConfig, symbolProvider) |
| 52 | + return writable { |
| 53 | + when (target) { |
| 54 | + is EnumShape -> rustTemplate(""""no value was set".parse::<#{Shape}>().ok()""", "Shape" to targetSymbol) |
| 55 | + is BooleanShape, is NumberShape, is StringShape, is DocumentShape, is ListShape, is MapShape -> rust("Some(Default::default())") |
| 56 | + is StructureShape -> rustTemplate( |
| 57 | + "{ let builder = #{Builder}::default(); #{instantiate} }", |
| 58 | + "Builder" to symbolProvider.symbolForBuilder(target), |
| 59 | + "instantiate" to builderInstantiator().finalizeBuilder("builder", target).map { |
| 60 | + if (BuilderGenerator.hasFallibleBuilder(target, symbolProvider)) { |
| 61 | + rust("#T.ok()", it) |
| 62 | + } else { |
| 63 | + it.some()(this) |
| 64 | + } |
| 65 | + }, |
| 66 | + ) |
| 67 | + |
| 68 | + is TimestampShape -> instantiator.instantiate(target, Node.from(0)).some()(this) |
| 69 | + is BlobShape -> instantiator.instantiate(target, Node.from("")).some()(this) |
| 70 | + is UnionShape -> rust("Some(#T::Unknown)", targetSymbol) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +fun ClientCodegenContext.correctErrors(shape: StructureShape): RuntimeType { |
| 76 | + val name = symbolProvider.shapeFunctionName(serviceShape, shape) + "_correct_errors" |
| 77 | + val corrections = writable { |
| 78 | + shape.members().forEach { member -> |
| 79 | + val memberName = symbolProvider.toMemberName(member) |
| 80 | + errorCorrectedDefault(member)?.also { default -> |
| 81 | + rustTemplate( |
| 82 | + """if builder.$memberName.is_none() { builder.$memberName = #{default} }""", |
| 83 | + "default" to default, |
| 84 | + ) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return RuntimeType.forInlineFun(name, RustModule.private("serde_util")) { |
| 90 | + rustTemplate( |
| 91 | + """ |
| 92 | + pub(crate) fn $name(mut builder: #{Builder}) -> #{Builder} { |
| 93 | + #{corrections} |
| 94 | + builder |
| 95 | + } |
| 96 | +
|
| 97 | + """, |
| 98 | + "Builder" to symbolProvider.symbolForBuilder(shape), |
| 99 | + "corrections" to corrections, |
| 100 | + ) |
| 101 | + } |
| 102 | +} |
0 commit comments