Skip to content

Commit ef67792

Browse files
authored
feat: big number support (#875)
1 parent b4580e3 commit ef67792

File tree

31 files changed

+330
-14
lines changed

31 files changed

+330
-14
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "97f4f92d-5f3e-4d01-9e58-d967318fa198",
3+
"type": "feature",
4+
"description": "Add support for `BigInteger` and `BigDecimal` in Smithy models",
5+
"issues": [
6+
"awslabs/smithy-kotlin#213"
7+
]
8+
}

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/KotlinSymbolProvider.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,9 @@ class KotlinSymbolProvider(private val model: Model, private val settings: Kotli
8585
private fun numberShape(shape: Shape, typeName: String, defaultValue: String): Symbol =
8686
createSymbolBuilder(shape, typeName, namespace = "kotlin").defaultValue(defaultValue).build()
8787

88-
override fun bigIntegerShape(shape: BigIntegerShape?): Symbol = createBigSymbol(shape, "BigInteger")
88+
override fun bigIntegerShape(shape: BigIntegerShape?): Symbol = RuntimeTypes.Core.Content.BigInteger
8989

90-
override fun bigDecimalShape(shape: BigDecimalShape?): Symbol = createBigSymbol(shape, "BigDecimal")
91-
92-
private fun createBigSymbol(shape: Shape?, symbolName: String): Symbol =
93-
createSymbolBuilder(shape, symbolName, namespace = "java.math", nullable = true).build()
90+
override fun bigDecimalShape(shape: BigDecimalShape?): Symbol = RuntimeTypes.Core.Content.BigDecimal
9491

9592
override fun stringShape(shape: StringShape): Symbol = if (shape.isEnum) {
9693
createEnumSymbol(shape)

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/core/RuntimeTypes.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ object RuntimeTypes {
9999
val ClientException = symbol("ClientException")
100100

101101
object Content : RuntimeTypePackage(KotlinDependency.CORE, "content") {
102+
val BigDecimal = symbol("BigDecimal")
103+
val BigInteger = symbol("BigInteger")
102104
val ByteArrayContent = symbol("ByteArrayContent")
103105
val ByteStream = symbol("ByteStream")
104106
val buildDocument = symbol("buildDocument")

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/serde/DeserializeStructGenerator.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,8 @@ open class DeserializeStructGenerator(
560560
target.type == ShapeType.LONG -> "deserializeLong()"
561561
target.type == ShapeType.FLOAT -> "deserializeFloat()"
562562
target.type == ShapeType.DOUBLE -> "deserializeDouble()"
563+
target.type == ShapeType.BIG_INTEGER -> "deserializeBigInteger()"
564+
target.type == ShapeType.BIG_DECIMAL -> "deserializeBigDecimal()"
563565
target.type == ShapeType.DOCUMENT -> "deserializeDocument()"
564566

565567
target.type == ShapeType.BLOB -> {

codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/core/KotlinDelegatorTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class KotlinDelegatorTest {
7575
KotlinCodegenPlugin().execute(context)
7676

7777
val contents = manifest.getFileString("src/main/kotlin/com/test/model/GetFooRequest.kt").get()
78-
contents.shouldContain("import java.math.BigInteger")
78+
contents.shouldContain("import aws.smithy.kotlin.runtime.content.BigInteger")
7979
// ensure symbol wasn't imported as an alias by default
8080
contents.shouldNotContain("as BigInteger")
8181
}

codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/core/SymbolProviderTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ class SymbolProviderTest {
544544

545545
val provider: SymbolProvider = KotlinCodegenPlugin.createSymbolProvider(model, rootNamespace = "foo.bar")
546546
val bigSymbol = provider.toSymbol(member)
547-
assertEquals("java.math", bigSymbol.namespace)
547+
assertEquals("aws.smithy.kotlin.runtime.content", bigSymbol.namespace)
548548
assertEquals("null", bigSymbol.defaultValue())
549549
assertEquals(true, bigSymbol.isNullable)
550550
assertEquals(type, bigSymbol.name)

codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/serde/DeserializeStructGeneratorTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ class DeserializeStructGeneratorTest {
2121
operations = listOf("Foo"),
2222
).trimIndent()
2323

24-
// TODO ~ Support BigInteger and BigDecimal Types
2524
@ParameterizedTest
26-
@ValueSource(strings = ["String", "Boolean", "Byte", "Short", "Integer", "Long", "Float", "Double"/*, "BigInteger", "BigDecimal"*/])
25+
@ValueSource(strings = ["String", "Boolean", "Byte", "Short", "Integer", "Long", "Float", "Double", "BigInteger", "BigDecimal"])
2726
fun `it deserializes a structure with a simple fields`(memberType: String) {
2827
val model = (
2928
modelPrefix + """

codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/serde/SerializeStructGeneratorTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ class SerializeStructGeneratorTest {
2121
operations = listOf("Foo"),
2222
).trimIndent()
2323

24-
// TODO ~ Support BigInteger and BigDecimal Types - https://github.com/awslabs/smithy-kotlin/issues/213
2524
@ParameterizedTest
26-
@ValueSource(strings = ["String", "Boolean", "Byte", "Short", "Integer", "Long", "Float", "Double"/*, "BigInteger", "BigDecimal"*/])
25+
@ValueSource(strings = ["String", "Boolean", "Byte", "Short", "Integer", "Long", "Float", "Double", "BigInteger", "BigDecimal"])
2726
fun `it serializes a structure with a simple fields`(memberType: String) {
2827
val model = (
2928
modelPrefix + """

docs/design/kotlin-smithy-sdk.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ hard keywords (e.g., "null" will be escaped as `` `null` ``).
3838
| `long` | `Long`
3939
| `float` | `Float`
4040
| `double` | `Double`
41-
| `bigInteger` | *undecided (see [#213](https://github.com/awslabs/smithy-kotlin/issues/213))
42-
| `bigDecimal` | *undecided (see [#213](https://github.com/awslabs/smithy-kotlin/issues/213))
41+
| `bigInteger` | *custom type provided by client runtime (type aliased to `java.math.BigInteger` on JVM)
42+
| `bigDecimal` | *custom type provided by client runtime (type aliased to `java.math.BigDecimal` on JVM)
4343
| `timestamp` | *custom type provided by client runtime
4444
| `document` | *custom type provided by client runtime
4545

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package aws.smithy.kotlin.runtime.content
6+
7+
public expect class BigDecimal(value: String) : Number {
8+
public fun toPlainString(): String
9+
}

0 commit comments

Comments
 (0)