in smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGenerator.kt [424:485]
protected open fun renderUri(
ctx: ProtocolGenerator.GenerationContext,
op: OperationShape,
writer: KotlinWriter
) {
val resolver = getProtocolHttpBindingResolver(ctx.model, ctx.service)
val httpTrait = resolver.httpTrait(op)
val requestBindings = resolver.requestBindings(op)
val pathBindings = requestBindings.filter { it.location == HttpBinding.Location.LABEL }
if (pathBindings.isNotEmpty()) {
writer.openBlock("val pathSegments = listOf(", ")") {
httpTrait.uri.segments.forEach { segment ->
if (segment.isLabel || segment.isGreedyLabel) {
// spec dictates member name and label name MUST be the same
val binding = pathBindings.find { binding ->
binding.memberName == segment.content
} ?: throw CodegenException("failed to find corresponding member for httpLabel `${segment.content}")
// shape must be string, number, boolean, or timestamp
val targetShape = ctx.model.expectShape(binding.member.target)
val identifier = if (targetShape.isTimestampShape) {
writer.addImport(RuntimeTypes.Core.TimestampFormat)
val tsFormat = resolver.determineTimestampFormat(
binding.member,
HttpBinding.Location.LABEL,
defaultTimestampFormat
)
val tsLabel = formatInstant("input.${binding.member.defaultName()}?", tsFormat, forceString = true)
tsLabel
} else {
"input.${binding.member.defaultName()}"
}
val encodeSymbol = RuntimeTypes.Http.encodeLabel
writer.addImport(encodeSymbol)
val encodeFn = if (segment.isGreedyLabel) {
writer.format("#T(greedy = true)", encodeSymbol)
} else {
writer.format("#T()", encodeSymbol)
}
writer.write("#S.$encodeFn,", "\${$identifier}")
} else {
// literal
writer.write("\"#L\",", segment.content.toEscapedLiteral())
}
}
}
writer.write("""path = pathSegments.joinToString(separator = "/", prefix = "/")""")
} else {
// all literals, inline directly
val resolvedPath = httpTrait.uri.segments.joinToString(
separator = "/",
prefix = "/",
transform = {
it.content.toEscapedLiteral()
}
)
writer.write("path = \"#L\"", resolvedPath)
}
}