|
| 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.rustsdk.customize.s3 |
| 7 | + |
| 8 | +import software.amazon.smithy.model.Model |
| 9 | +import software.amazon.smithy.model.shapes.MemberShape |
| 10 | +import software.amazon.smithy.model.shapes.OperationShape |
| 11 | +import software.amazon.smithy.model.shapes.ServiceShape |
| 12 | +import software.amazon.smithy.model.shapes.ShapeType |
| 13 | +import software.amazon.smithy.model.shapes.StringShape |
| 14 | +import software.amazon.smithy.model.shapes.StructureShape |
| 15 | +import software.amazon.smithy.model.traits.DeprecatedTrait |
| 16 | +import software.amazon.smithy.model.traits.DocumentationTrait |
| 17 | +import software.amazon.smithy.model.traits.HttpHeaderTrait |
| 18 | +import software.amazon.smithy.model.traits.OutputTrait |
| 19 | +import software.amazon.smithy.model.transform.ModelTransformer |
| 20 | +import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext |
| 21 | +import software.amazon.smithy.rust.codegen.client.smithy.ClientRustSettings |
| 22 | +import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator |
| 23 | +import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationCustomization |
| 24 | +import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationSection |
| 25 | +import software.amazon.smithy.rust.codegen.core.rustlang.Writable |
| 26 | +import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate |
| 27 | +import software.amazon.smithy.rust.codegen.core.rustlang.writable |
| 28 | +import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType |
| 29 | +import software.amazon.smithy.rust.codegen.core.util.getTrait |
| 30 | +import software.amazon.smithy.rust.codegen.core.util.hasTrait |
| 31 | +import software.amazon.smithy.rust.codegen.core.util.outputShape |
| 32 | +import software.amazon.smithy.rustsdk.InlineAwsDependency |
| 33 | +import kotlin.streams.asSequence |
| 34 | + |
| 35 | +/** |
| 36 | + * Enforces that Expires fields have the DateTime type (since in the future the model will change to model them as String), |
| 37 | + * and add an ExpiresString field to maintain the raw string value sent. |
| 38 | + */ |
| 39 | +class S3ExpiresDecorator : ClientCodegenDecorator { |
| 40 | + override val name: String = "S3ExpiresDecorator" |
| 41 | + override val order: Byte = 0 |
| 42 | + private val expires = "Expires" |
| 43 | + private val expiresString = "ExpiresString" |
| 44 | + |
| 45 | + override fun transformModel( |
| 46 | + service: ServiceShape, |
| 47 | + model: Model, |
| 48 | + settings: ClientRustSettings, |
| 49 | + ): Model { |
| 50 | + val transformer = ModelTransformer.create() |
| 51 | + |
| 52 | + // Ensure all `Expires` shapes are timestamps |
| 53 | + val expiresShapeTimestampMap = |
| 54 | + model.shapes() |
| 55 | + .asSequence() |
| 56 | + .mapNotNull { shape -> |
| 57 | + shape.members() |
| 58 | + .singleOrNull { member -> member.memberName.equals(expires, ignoreCase = true) } |
| 59 | + ?.target |
| 60 | + } |
| 61 | + .associateWith { ShapeType.TIMESTAMP } |
| 62 | + var transformedModel = transformer.changeShapeType(model, expiresShapeTimestampMap) |
| 63 | + |
| 64 | + // Add an `ExpiresString` string shape to the model |
| 65 | + val expiresStringShape = StringShape.builder().id("aws.sdk.rust.s3.synthetic#$expiresString").build() |
| 66 | + transformedModel = transformedModel.toBuilder().addShape(expiresStringShape).build() |
| 67 | + |
| 68 | + // For output shapes only, deprecate `Expires` and add a synthetic member that targets `ExpiresString` |
| 69 | + transformedModel = |
| 70 | + transformer.mapShapes(transformedModel) { shape -> |
| 71 | + if (shape.hasTrait<OutputTrait>() && shape.memberNames.any { it.equals(expires, ignoreCase = true) }) { |
| 72 | + val builder = (shape as StructureShape).toBuilder() |
| 73 | + |
| 74 | + // Deprecate `Expires` |
| 75 | + val expiresMember = shape.members().single { it.memberName.equals(expires, ignoreCase = true) } |
| 76 | + |
| 77 | + builder.removeMember(expiresMember.memberName) |
| 78 | + val deprecatedTrait = |
| 79 | + DeprecatedTrait.builder() |
| 80 | + .message("Please use `expires_string` which contains the raw, unparsed value of this field.") |
| 81 | + .build() |
| 82 | + |
| 83 | + builder.addMember( |
| 84 | + expiresMember.toBuilder() |
| 85 | + .addTrait(deprecatedTrait) |
| 86 | + .build(), |
| 87 | + ) |
| 88 | + |
| 89 | + // Add a synthetic member targeting `ExpiresString` |
| 90 | + val expiresStringMember = MemberShape.builder() |
| 91 | + expiresStringMember.target(expiresStringShape.id) |
| 92 | + expiresStringMember.id(expiresMember.id.toString() + "String") // i.e. com.amazonaws.s3.<MEMBER_NAME>$ExpiresString |
| 93 | + expiresStringMember.addTrait(HttpHeaderTrait(expiresString)) // Add HttpHeaderTrait to ensure the field is deserialized |
| 94 | + expiresMember.getTrait<DocumentationTrait>()?.let { |
| 95 | + expiresStringMember.addTrait(it) // Copy documentation from `Expires` |
| 96 | + } |
| 97 | + builder.addMember(expiresStringMember.build()) |
| 98 | + builder.build() |
| 99 | + } else { |
| 100 | + shape |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return transformedModel |
| 105 | + } |
| 106 | + |
| 107 | + override fun operationCustomizations( |
| 108 | + codegenContext: ClientCodegenContext, |
| 109 | + operation: OperationShape, |
| 110 | + baseCustomizations: List<OperationCustomization>, |
| 111 | + ): List<OperationCustomization> { |
| 112 | + val outputShape = operation.outputShape(codegenContext.model) |
| 113 | + |
| 114 | + if (outputShape.memberNames.any { it.equals(expires, ignoreCase = true) }) { |
| 115 | + return baseCustomizations + |
| 116 | + ParseExpiresFieldsCustomization( |
| 117 | + codegenContext, |
| 118 | + ) |
| 119 | + } else { |
| 120 | + return baseCustomizations |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +class ParseExpiresFieldsCustomization( |
| 126 | + private val codegenContext: ClientCodegenContext, |
| 127 | +) : OperationCustomization() { |
| 128 | + override fun section(section: OperationSection): Writable = |
| 129 | + writable { |
| 130 | + when (section) { |
| 131 | + is OperationSection.AdditionalInterceptors -> { |
| 132 | + section.registerInterceptor(codegenContext.runtimeConfig, this) { |
| 133 | + val interceptor = |
| 134 | + RuntimeType.forInlineDependency( |
| 135 | + InlineAwsDependency.forRustFile("s3_expires_interceptor"), |
| 136 | + ).resolve("S3ExpiresInterceptor") |
| 137 | + rustTemplate( |
| 138 | + """ |
| 139 | + #{S3ExpiresInterceptor} |
| 140 | + """, |
| 141 | + "S3ExpiresInterceptor" to interceptor, |
| 142 | + ) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + else -> {} |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments