Skip to content

Commit bcad3b4

Browse files
authored
fix: add import references when needed (#1281)
1 parent d3089e6 commit bcad3b4

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "bbe2e7de-79d3-4aea-97d1-d5c244ede433",
3+
"type": "bugfix",
4+
"description": "Adds import to the symbols references when needed. This issue surfaced when building the protocol tests with the latest Smithy release"
5+
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ class KotlinSymbolProvider(private val model: Model, private val settings: Kotli
252252
}
253253

254254
targetShape.isDocumentShape -> getDefaultValueForDocument(node)
255-
targetShape.isTimestampShape -> getDefaultValueForTimestamp(node.asNumberNode().get())
255+
targetShape.isTimestampShape -> getDefaultValueForTimestamp(this, node.asNumberNode().get())
256256

257257
node.isNumberNode -> getDefaultValueForNumber(targetShape.type, node.toString())
258258
node.isArrayNode -> "listOf()"
@@ -264,17 +264,14 @@ class KotlinSymbolProvider(private val model: Model, private val settings: Kotli
264264
defaultValue(defaultValue)
265265
}
266266

267-
private fun getDefaultValueForTimestamp(node: NumberNode): String {
268-
val instant = RuntimeTypes.Core.Instant
269-
267+
private fun getDefaultValueForTimestamp(builder: Symbol.Builder, node: NumberNode): String {
268+
builder.addReferences(RuntimeTypes.Core.Instant)
270269
return if (node.isFloatingPointNumber) {
271-
val fromEpochMilliseconds = RuntimeTypes.Core.fromEpochMilliseconds
272-
273270
val value = node.value as Double
274271
val ms = round(value * 1e3).toLong()
275-
"$fromEpochMilliseconds.invoke($instant, $ms)"
272+
"Instant.fromEpochMilliseconds($ms)"
276273
} else {
277-
"$instant.fromEpochSeconds(${node.value}, 0)"
274+
"Instant.fromEpochSeconds(${node.value}, 0)"
278275
}
279276
}
280277

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package software.amazon.smithy.kotlin.codegen.rendering
66

77
import software.amazon.smithy.codegen.core.CodegenException
88
import software.amazon.smithy.codegen.core.SymbolProvider
9+
import software.amazon.smithy.codegen.core.SymbolReference
910
import software.amazon.smithy.kotlin.codegen.core.*
1011
import software.amazon.smithy.kotlin.codegen.model.SymbolProperty
1112
import software.amazon.smithy.kotlin.codegen.model.hasTrait
@@ -90,9 +91,9 @@ class ShapeValueGenerator(
9091
private fun mapDeclaration(writer: KotlinWriter, shape: MapShape, block: () -> Unit) {
9192
writer.pushState()
9293
writer.trimTrailingSpaces(false)
93-
94-
val collectionGeneratorFunction = symbolProvider.toSymbol(shape).expectProperty(SymbolProperty.IMMUTABLE_COLLECTION_FUNCTION)
95-
94+
val mapSymbol = symbolProvider.toSymbol(shape)
95+
writer.addImportReferences(mapSymbol, SymbolReference.ContextOption.USE)
96+
val collectionGeneratorFunction = mapSymbol.expectProperty(SymbolProperty.IMMUTABLE_COLLECTION_FUNCTION)
9697
writer.writeInline("$collectionGeneratorFunction(")
9798
.ensureNewline()
9899
.indent()
@@ -109,11 +110,8 @@ class ShapeValueGenerator(
109110
writer.trimTrailingSpaces(false)
110111

111112
val collectionSymbol = symbolProvider.toSymbol(shape)
113+
writer.addImportReferences(collectionSymbol, SymbolReference.ContextOption.USE)
112114
val generatorFn = collectionSymbol.expectProperty(SymbolProperty.IMMUTABLE_COLLECTION_FUNCTION)
113-
114-
collectionSymbol.references.forEach {
115-
writer.addImport(it.symbol)
116-
}
117115
writer.writeInline("$generatorFn(")
118116
.ensureNewline()
119117
.indent()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package software.amazon.smithy.kotlin.codegen.rendering.serde
66

77
import software.amazon.smithy.codegen.core.CodegenException
8+
import software.amazon.smithy.codegen.core.SymbolReference
89
import software.amazon.smithy.kotlin.codegen.DefaultValueSerializationMode
910
import software.amazon.smithy.kotlin.codegen.core.*
1011
import software.amazon.smithy.kotlin.codegen.model.*
@@ -595,6 +596,7 @@ open class SerializeStructGenerator(
595596
val postfix = if (memberShape.hasTrait<IdempotencyTokenTrait>()) idempotencyTokenPostfix(memberShape) else ""
596597
val memberSymbol = ctx.symbolProvider.toSymbol(memberShape)
597598
val memberName = ctx.symbolProvider.toMemberName(memberShape)
599+
writer.addImportReferences(memberSymbol, SymbolReference.ContextOption.USE)
598600
if (memberSymbol.isNullable) {
599601
val identifier = valueToSerializeName("it")
600602
val fn = serializerFn.format(memberShape, identifier)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ class SymbolProviderTest {
186186
"boolean,true,true",
187187
"bigInteger,5,5",
188188
"bigDecimal,9.0123456789,9.0123456789",
189-
"timestamp,1684869901,'aws.smithy.kotlin.runtime.time.Instant.fromEpochSeconds(1684869901, 0)'",
190-
"timestamp,1.5,'aws.smithy.kotlin.runtime.time.fromEpochMilliseconds.invoke(aws.smithy.kotlin.runtime.time.Instant, 1500)'",
189+
"timestamp,1684869901,'Instant.fromEpochSeconds(1684869901, 0)'",
190+
"timestamp,1.5,'Instant.fromEpochMilliseconds(1500)'",
191191
)
192192
fun `can default simple types`(typeName: String, modeledDefault: String, expectedDefault: String) {
193193
val model = """

0 commit comments

Comments
 (0)