-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codegen): generate client side error correction (#958)
- Loading branch information
Showing
11 changed files
with
159 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"id": "d4722cf5-e6ef-4b42-b869-d854fd80be51", | ||
"type": "feature", | ||
"description": "Generate client side error correction for @required members" | ||
} |
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
63 changes: 63 additions & 0 deletions
63
...ain/kotlin/software/amazon/smithy/kotlin/codegen/rendering/serde/ClientErrorCorrection.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,63 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package software.amazon.smithy.kotlin.codegen.rendering.serde | ||
|
||
import software.amazon.smithy.codegen.core.CodegenException | ||
import software.amazon.smithy.kotlin.codegen.core.CodegenContext | ||
import software.amazon.smithy.kotlin.codegen.core.KotlinWriter | ||
import software.amazon.smithy.kotlin.codegen.core.RuntimeTypes | ||
import software.amazon.smithy.kotlin.codegen.model.isEnum | ||
import software.amazon.smithy.model.shapes.MemberShape | ||
import software.amazon.smithy.model.shapes.ShapeType | ||
|
||
object ClientErrorCorrection { | ||
/** | ||
* Determine the default value for a required member based on | ||
* [client error correction](https://smithy.io/2.0/spec/aggregate-types.html?highlight=error%20correction#client-error-correction) | ||
* | ||
* @param ctx the generation context | ||
* @param member the target member shape to get the default value for | ||
* @param writer the writer the default value will be written to, this is used for certain shapes to format the | ||
* default value which will mutate the writer (e.g. add imports). | ||
* @return default value expression as a string | ||
*/ | ||
fun defaultValue( | ||
ctx: CodegenContext, | ||
member: MemberShape, | ||
writer: KotlinWriter, | ||
): String { | ||
val target = ctx.model.expectShape(member.target) | ||
val targetSymbol = ctx.symbolProvider.toSymbol(target) | ||
|
||
// In IDL v1 all enums were `ShapeType.STRING` and you had to explicitly check for the @enum trait, this handles | ||
// the differences in IDL versions | ||
if (target.isEnum) { | ||
return writer.format("#T.SdkUnknown(#S)", targetSymbol, "no value provided") | ||
} | ||
|
||
return when (target.type) { | ||
ShapeType.BLOB -> "ByteArray(0)" | ||
ShapeType.BOOLEAN -> "false" | ||
ShapeType.STRING -> "\"\"" | ||
ShapeType.BYTE -> "0.toByte()" | ||
ShapeType.SHORT -> "0.toShort()" | ||
ShapeType.INTEGER -> "0" | ||
ShapeType.LONG -> "0L" | ||
ShapeType.FLOAT -> "0f" | ||
ShapeType.DOUBLE -> "0.0" | ||
ShapeType.BIG_INTEGER -> writer.format("#T(\"0\")", RuntimeTypes.Core.Content.BigInteger) | ||
ShapeType.BIG_DECIMAL -> writer.format("#T(\"0\")", RuntimeTypes.Core.Content.BigDecimal) | ||
ShapeType.DOCUMENT -> "null" | ||
ShapeType.UNION -> writer.format("#T.SdkUnknown", targetSymbol) | ||
ShapeType.LIST, | ||
ShapeType.SET, | ||
-> "emptyList()" | ||
ShapeType.MAP -> "emptyMap()" | ||
ShapeType.STRUCTURE -> writer.format("#T.Builder().correctErrors().build()", targetSymbol) | ||
ShapeType.TIMESTAMP -> writer.format("#T.fromEpochSeconds(0)", RuntimeTypes.Core.Instant) | ||
else -> throw CodegenException("unexpected member type $member") | ||
} | ||
} | ||
} |
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
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