Skip to content

fix: add import references when needed #1281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/bbe2e7de-79d3-4aea-97d1-d5c244ede433.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "bbe2e7de-79d3-4aea-97d1-d5c244ede433",
"type": "bugfix",
"description": "Adds import to the symbols references when needed. This issue surfaced when building the protocol tests with the latest Smithy release"
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class KotlinSymbolProvider(private val model: Model, private val settings: Kotli
}

targetShape.isDocumentShape -> getDefaultValueForDocument(node)
targetShape.isTimestampShape -> getDefaultValueForTimestamp(node.asNumberNode().get())
targetShape.isTimestampShape -> getDefaultValueForTimestamp(this, node.asNumberNode().get())

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

private fun getDefaultValueForTimestamp(node: NumberNode): String {
val instant = RuntimeTypes.Core.Instant

private fun getDefaultValueForTimestamp(builder: Symbol.Builder, node: NumberNode): String {
builder.addReferences(RuntimeTypes.Core.Instant)
return if (node.isFloatingPointNumber) {
val fromEpochMilliseconds = RuntimeTypes.Core.fromEpochMilliseconds

val value = node.value as Double
val ms = round(value * 1e3).toLong()
"$fromEpochMilliseconds.invoke($instant, $ms)"
"Instant.fromEpochMilliseconds($ms)"
} else {
"$instant.fromEpochSeconds(${node.value}, 0)"
"Instant.fromEpochSeconds(${node.value}, 0)"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package software.amazon.smithy.kotlin.codegen.rendering

import software.amazon.smithy.codegen.core.CodegenException
import software.amazon.smithy.codegen.core.SymbolProvider
import software.amazon.smithy.codegen.core.SymbolReference
import software.amazon.smithy.kotlin.codegen.core.*
import software.amazon.smithy.kotlin.codegen.model.SymbolProperty
import software.amazon.smithy.kotlin.codegen.model.hasTrait
Expand Down Expand Up @@ -90,9 +91,9 @@ class ShapeValueGenerator(
private fun mapDeclaration(writer: KotlinWriter, shape: MapShape, block: () -> Unit) {
writer.pushState()
writer.trimTrailingSpaces(false)

val collectionGeneratorFunction = symbolProvider.toSymbol(shape).expectProperty(SymbolProperty.IMMUTABLE_COLLECTION_FUNCTION)

val mapSymbol = symbolProvider.toSymbol(shape)
writer.addImportReferences(mapSymbol, SymbolReference.ContextOption.USE)
val collectionGeneratorFunction = mapSymbol.expectProperty(SymbolProperty.IMMUTABLE_COLLECTION_FUNCTION)
writer.writeInline("$collectionGeneratorFunction(")
.ensureNewline()
.indent()
Expand All @@ -109,11 +110,8 @@ class ShapeValueGenerator(
writer.trimTrailingSpaces(false)

val collectionSymbol = symbolProvider.toSymbol(shape)
writer.addImportReferences(collectionSymbol, SymbolReference.ContextOption.USE)
val generatorFn = collectionSymbol.expectProperty(SymbolProperty.IMMUTABLE_COLLECTION_FUNCTION)

collectionSymbol.references.forEach {
writer.addImport(it.symbol)
}
writer.writeInline("$generatorFn(")
.ensureNewline()
.indent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package software.amazon.smithy.kotlin.codegen.rendering.serde

import software.amazon.smithy.codegen.core.CodegenException
import software.amazon.smithy.codegen.core.SymbolReference
import software.amazon.smithy.kotlin.codegen.DefaultValueSerializationMode
import software.amazon.smithy.kotlin.codegen.core.*
import software.amazon.smithy.kotlin.codegen.model.*
Expand Down Expand Up @@ -595,6 +596,7 @@ open class SerializeStructGenerator(
val postfix = if (memberShape.hasTrait<IdempotencyTokenTrait>()) idempotencyTokenPostfix(memberShape) else ""
val memberSymbol = ctx.symbolProvider.toSymbol(memberShape)
val memberName = ctx.symbolProvider.toMemberName(memberShape)
writer.addImportReferences(memberSymbol, SymbolReference.ContextOption.USE)
if (memberSymbol.isNullable) {
val identifier = valueToSerializeName("it")
val fn = serializerFn.format(memberShape, identifier)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ class SymbolProviderTest {
"boolean,true,true",
"bigInteger,5,5",
"bigDecimal,9.0123456789,9.0123456789",
"timestamp,1684869901,'aws.smithy.kotlin.runtime.time.Instant.fromEpochSeconds(1684869901, 0)'",
"timestamp,1.5,'aws.smithy.kotlin.runtime.time.fromEpochMilliseconds.invoke(aws.smithy.kotlin.runtime.time.Instant, 1500)'",
"timestamp,1684869901,'Instant.fromEpochSeconds(1684869901, 0)'",
"timestamp,1.5,'Instant.fromEpochMilliseconds(1500)'",
)
fun `can default simple types`(typeName: String, modeledDefault: String, expectedDefault: String) {
val model = """
Expand Down
Loading