From a4f1190d61322c3df0b6942b9c4990e0f62029b7 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Wed, 13 Sep 2023 15:24:29 -0400 Subject: [PATCH 01/18] Generate internal-only clients with internal visibility --- .../smithy/kotlin/codegen/KotlinSettings.kt | 33 ++++++++++++++++--- .../rendering/ExceptionBaseClassGenerator.kt | 5 ++- .../rendering/ServiceClientGenerator.kt | 11 ++++--- .../codegen/rendering/StructureGenerator.kt | 10 ++++-- .../auth/AuthSchemeParametersGenerator.kt | 5 ++- .../auth/AuthSchemeProviderGenerator.kt | 6 ++-- .../DefaultEndpointProviderGenerator.kt | 7 +++- .../rendering/endpoints/EndpointDelegator.kt | 2 +- .../discovery/EndpointDiscovererGenerator.kt | 5 ++- .../DefaultEndpointProviderGeneratorTest.kt | 10 +++++- 10 files changed, 73 insertions(+), 21 deletions(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt index c23bb0415..b149b67aa 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt @@ -16,7 +16,6 @@ import software.amazon.smithy.model.shapes.Shape import software.amazon.smithy.model.shapes.ShapeId import java.util.Optional import java.util.logging.Logger -import kotlin.streams.toList // shapeId of service from which to generate an SDK private const val SERVICE = "service" @@ -25,6 +24,7 @@ private const val PACKAGE_NAME = "name" private const val PACKAGE_VERSION = "version" private const val PACKAGE_DESCRIPTION = "description" private const val BUILD_SETTINGS = "build" +private const val VISIBILITY_SETTINGS = "visibility" // Optional specification of sdkId for models that provide them, otherwise Service's shape id name is used private const val SDK_ID = "sdkId" @@ -160,6 +160,7 @@ data class BuildSettings( val generateDefaultBuildFiles: Boolean = true, val optInAnnotations: List? = null, val generateMultiplatformProject: Boolean = false, + val visibility: VisibilitySettings = VisibilitySettings.Default ) { companion object { const val ROOT_PROJECT = "rootProject" @@ -170,8 +171,7 @@ data class BuildSettings( fun fromNode(node: Optional): BuildSettings = node.map { val generateFullProject = node.get().getBooleanMemberOrDefault(ROOT_PROJECT, false) val generateBuildFiles = node.get().getBooleanMemberOrDefault(GENERATE_DEFAULT_BUILD_FILES, true) - val generateMultiplatformProject = - node.get().getBooleanMemberOrDefault(GENERATE_MULTIPLATFORM_MODULE, false) + val generateMultiplatformProject = node.get().getBooleanMemberOrDefault(GENERATE_MULTIPLATFORM_MODULE, false) val annotations = node.get().getArrayMember(ANNOTATIONS).map { it.elements.mapNotNull { node -> node.asStringNode().map { stringNode -> @@ -179,8 +179,9 @@ data class BuildSettings( }.orNull() } }.orNull() + val visibility = VisibilitySettings.fromNode(node.get().getObjectMember(VISIBILITY_SETTINGS)) - BuildSettings(generateFullProject, generateBuildFiles, annotations, generateMultiplatformProject) + BuildSettings(generateFullProject, generateBuildFiles, annotations, generateMultiplatformProject, visibility) }.orElse(Default) /** @@ -193,3 +194,27 @@ data class BuildSettings( class UnresolvableProtocolException(message: String) : CodegenException(message) private fun Optional.orNull(): T? = if (isPresent) get() else null + +data class VisibilitySettings( + val serviceClient: String = "public", + val structure: String = "public", + val error: String = "public", +) { + companion object { + const val SERVICE_CLIENT = "serviceClient" + const val STRUCTURE = "structure" + const val ERROR = "error" + + fun fromNode(node: Optional): VisibilitySettings = node.map { + val serviceClient = node.get().getStringMemberOrDefault(SERVICE_CLIENT, "public") + val structure = node.get().getStringMemberOrDefault(STRUCTURE, "public") + val error = node.get().getStringMemberOrDefault(ERROR, "public") + VisibilitySettings(serviceClient, structure, error) + }.orElse(Default) + + /** + * Default visibility settings + */ + val Default: VisibilitySettings = VisibilitySettings() + } +} \ No newline at end of file diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt index 018f3d04b..a248e1066 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt @@ -33,9 +33,8 @@ object ExceptionBaseClassGenerator { val name = clientName(ctx.settings.sdkId) writer.dokka("Base class for all service related exceptions thrown by the $name client") - writer.withBlock( - "public open class #T : #T {", - "}", + writer.withBlock("#L open class #T : #T {", "}", + ctx.settings.build.visibility.error, serviceException, baseException, ) { diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt index ef105bae4..b94856e05 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt @@ -67,11 +67,12 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { requireNotNull(ctx.shape) { "ServiceShape is required to render a service client" } private val serviceSymbol = ctx.symbolProvider.toSymbol(service) private val writer = ctx.writer + private val visibility = ctx.settings.build.visibility.serviceClient fun render() { writer.write("\n\n") - writer.write("public const val ServiceId: String = #S", ctx.settings.sdkId) - writer.write("public const val SdkVersion: String = #S", ctx.settings.pkg.version) + writer.write("$visibility const val ServiceId: String = #S", ctx.settings.sdkId) + writer.write("$visibility const val SdkVersion: String = #S", ctx.settings.pkg.version) writer.write("\n\n") writer.putContext("service.name", ctx.settings.sdkId) @@ -82,7 +83,7 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { writer.renderDocumentation(service) writer.renderAnnotations(service) - writer.openBlock("public interface ${serviceSymbol.name} : #T {", RuntimeTypes.SmithyClient.SdkClient) + writer.openBlock("$visibility interface ${serviceSymbol.name} : #T {", RuntimeTypes.SmithyClient.SdkClient) .call { // allow access to client's Config writer.dokka("${serviceSymbol.name}'s configuration") @@ -198,7 +199,7 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { write("Any resources created on your behalf will be shared between clients, and will only be closed when ALL clients using them are closed.") write("If you provide a resource (e.g. [HttpClientEngine]) to the SDK, you are responsible for managing the lifetime of that resource.") } - writer.withBlock("public fun #1T.withConfig(block: #1T.Config.Builder.() -> Unit): #1T {", "}", serviceSymbol) { + writer.withBlock("$visibility fun #1T.withConfig(block: #1T.Config.Builder.() -> Unit): #1T {", "}", serviceSymbol) { write("val newConfig = config.toBuilder().apply(block).build()") write("return Default#L(newConfig)", serviceSymbol.name) } @@ -219,7 +220,7 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { writer.renderDocumentation(op) writer.renderAnnotations(op) writer.write( - "public suspend inline fun #T.#L(crossinline block: #T.Builder.() -> Unit): #T = #L(#T.Builder().apply(block).build())", + "$visibility suspend inline fun #T.#L(crossinline block: #T.Builder.() -> Unit): #T = #L(#T.Builder().apply(block).build())", serviceSymbol, operationName, inputSymbol, diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt index 85d54f13f..a74a06723 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt @@ -52,7 +52,10 @@ class StructureGenerator( * Renders a normal (non-error) Smithy structure to a Kotlin class */ private fun renderStructure() { - writer.openBlock("public class #T private constructor(builder: Builder) {", symbol) + writer.openBlock("#L class #T private constructor(builder: Builder) {", + ctx.settings.build.visibility.structure, + symbol + ) .call { renderImmutableProperties() } .write("") .call { renderCompanionObject() } @@ -299,7 +302,10 @@ class StructureGenerator( val exceptionBaseClass = ExceptionBaseClassGenerator.baseExceptionSymbol(ctx.settings) writer.addImport(exceptionBaseClass) - writer.openBlock("public class #T private constructor(builder: Builder) : ${exceptionBaseClass.name}() {", symbol) + writer.openBlock("#L class #T private constructor(builder: Builder) : ${exceptionBaseClass.name}() {", + ctx.settings.build.visibility.error, + symbol + ) .write("") .call { renderImmutableProperties() } .write("") diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt index f1ffa4fa2..d39822e12 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt @@ -48,7 +48,10 @@ class AuthSchemeParametersGenerator : AbstractConfigGenerator() { val implSymbol = getImplementationSymbol(ctx.settings) ctx.delegator.useSymbolWriter(symbol) { writer -> - writer.withBlock("public interface #T {", "}", symbol) { + writer.withBlock("#L interface #T {", "}", + ctx.settings.build.visibility.structure, + symbol + ) { dokka("The name of the operation currently being invoked.") write("public val operationName: String") } diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt index 5c265b824..e87ceac67 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt @@ -54,7 +54,8 @@ open class AuthSchemeProviderGenerator { write("See [#T] for the default SDK behavior of this interface.", getDefaultSymbol(ctx.settings)) } writer.write( - "public interface #T : #T<#T>", + "#L interface #T : #T<#T>", + ctx.settings.build.visibility.structure, symbol, RuntimeTypes.Auth.Identity.AuthSchemeProvider, paramsSymbol, @@ -64,8 +65,9 @@ open class AuthSchemeProviderGenerator { private fun renderDefaultImpl(ctx: ProtocolGenerator.GenerationContext, writer: KotlinWriter) { // FIXME - probably can't remain an object writer.withBlock( - "public object #T : #T {", + "#L object #T : #T {", "}", + ctx.settings.build.visibility.structure, getDefaultSymbol(ctx.settings), getSymbol(ctx.settings), ) { diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt index bbb06cd9d..b7614500f 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt @@ -62,6 +62,7 @@ class DefaultEndpointProviderGenerator( private val defaultProviderSymbol: Symbol, private val interfaceSymbol: Symbol, private val paramsSymbol: Symbol, + private val settings: KotlinSettings, private val externalFunctions: Map = emptyMap(), private val propertyRenderers: Map = emptyMap(), ) : ExpressionRenderer { @@ -78,7 +79,11 @@ class DefaultEndpointProviderGenerator( fun render() { renderDocumentation() - writer.withBlock("public class #T: #T {", "}", defaultProviderSymbol, interfaceSymbol) { + writer.withBlock("#L class #T: #T {", "}", + settings.build.visibility.structure, + defaultProviderSymbol, + interfaceSymbol + ) { renderResolve() } } diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointDelegator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointDelegator.kt index be3e29469..cac97608f 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointDelegator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointDelegator.kt @@ -38,7 +38,7 @@ interface EndpointDelegator { if (rules != null) { ctx.delegator.useFileWriter(defaultProviderSymbol) { - DefaultEndpointProviderGenerator(it, rules, defaultProviderSymbol, providerSymbol, paramsSymbol).render() + DefaultEndpointProviderGenerator(it, rules, defaultProviderSymbol, providerSymbol, paramsSymbol, ctx.settings).render() } } } diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt index ee29c9c53..2b94248ef 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt @@ -43,7 +43,10 @@ class EndpointDiscovererGenerator(private val ctx: CodegenContext, private val d calls. """.trimIndent(), ) - withBlock("public class #T {", "}", symbol) { + withBlock("#L class #T {", "}", + ctx.settings.build.visibility.structure, + symbol + ) { write( "private val cache = #T(10.#T, #T.System)", RuntimeTypes.Core.Utils.ReadThroughCache, diff --git a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGeneratorTest.kt b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGeneratorTest.kt index 7599bd2cc..5a4b2b71c 100644 --- a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGeneratorTest.kt +++ b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGeneratorTest.kt @@ -4,6 +4,7 @@ */ package software.amazon.smithy.kotlin.codegen.rendering.endpoints +import software.amazon.smithy.kotlin.codegen.KotlinSettings import software.amazon.smithy.kotlin.codegen.core.KotlinWriter import software.amazon.smithy.kotlin.codegen.model.buildSymbol import software.amazon.smithy.kotlin.codegen.test.TestModelDefault @@ -11,6 +12,7 @@ import software.amazon.smithy.kotlin.codegen.test.assertBalancedBracesAndParens import software.amazon.smithy.kotlin.codegen.test.formatForTest import software.amazon.smithy.kotlin.codegen.test.shouldContainOnlyOnceWithDiff import software.amazon.smithy.model.node.Node +import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.rulesengine.language.EndpointRuleSet import kotlin.test.* @@ -142,7 +144,13 @@ class DefaultEndpointProviderGeneratorTest { name = "DefaultEndpointProvider" namespace = TestModelDefault.NAMESPACE } - DefaultEndpointProviderGenerator(writer, rules, defaultSymbol, interfaceSymbol, paramsSymbol).render() + val settings = KotlinSettings( + service = ShapeId.from("EndpointProviderGeneratorTest"), + pkg = KotlinSettings.PackageSettings("name", "version"), + sdkId = "testSdkId" + ) + + DefaultEndpointProviderGenerator(writer, rules, defaultSymbol, interfaceSymbol, paramsSymbol, settings).render() generatedClass = writer.toString() } From 2440471af3f43effe66d3c58d7303bb2a723637c Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Wed, 13 Sep 2023 15:27:57 -0400 Subject: [PATCH 02/18] changelog --- .changes/b6288fbe-a409-473c-a6ac-12c3c310b963.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changes/b6288fbe-a409-473c-a6ac-12c3c310b963.json diff --git a/.changes/b6288fbe-a409-473c-a6ac-12c3c310b963.json b/.changes/b6288fbe-a409-473c-a6ac-12c3c310b963.json new file mode 100644 index 000000000..8d88a208b --- /dev/null +++ b/.changes/b6288fbe-a409-473c-a6ac-12c3c310b963.json @@ -0,0 +1,5 @@ +{ + "id": "b6288fbe-a409-473c-a6ac-12c3c310b963", + "type": "misc", + "description": "Generate internal-only clients with visibility" +} \ No newline at end of file From 3515130163f672fa5a48483d34e67b210a12d311 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Wed, 13 Sep 2023 15:31:49 -0400 Subject: [PATCH 03/18] ktlint --- .../amazon/smithy/kotlin/codegen/KotlinSettings.kt | 4 ++-- .../codegen/rendering/ExceptionBaseClassGenerator.kt | 4 +++- .../kotlin/codegen/rendering/StructureGenerator.kt | 10 ++++++---- .../rendering/auth/AuthSchemeParametersGenerator.kt | 6 ++++-- .../endpoints/DefaultEndpointProviderGenerator.kt | 6 ++++-- .../endpoints/discovery/EndpointDiscovererGenerator.kt | 6 ++++-- .../endpoints/DefaultEndpointProviderGeneratorTest.kt | 2 +- 7 files changed, 24 insertions(+), 14 deletions(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt index b149b67aa..ad6f590b5 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt @@ -160,7 +160,7 @@ data class BuildSettings( val generateDefaultBuildFiles: Boolean = true, val optInAnnotations: List? = null, val generateMultiplatformProject: Boolean = false, - val visibility: VisibilitySettings = VisibilitySettings.Default + val visibility: VisibilitySettings = VisibilitySettings.Default, ) { companion object { const val ROOT_PROJECT = "rootProject" @@ -217,4 +217,4 @@ data class VisibilitySettings( */ val Default: VisibilitySettings = VisibilitySettings() } -} \ No newline at end of file +} diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt index a248e1066..7a4e1c761 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt @@ -33,7 +33,9 @@ object ExceptionBaseClassGenerator { val name = clientName(ctx.settings.sdkId) writer.dokka("Base class for all service related exceptions thrown by the $name client") - writer.withBlock("#L open class #T : #T {", "}", + writer.withBlock( + "#L open class #T : #T {", + "}", ctx.settings.build.visibility.error, serviceException, baseException, diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt index a74a06723..2d47daae8 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt @@ -52,9 +52,10 @@ class StructureGenerator( * Renders a normal (non-error) Smithy structure to a Kotlin class */ private fun renderStructure() { - writer.openBlock("#L class #T private constructor(builder: Builder) {", + writer.openBlock( + "#L class #T private constructor(builder: Builder) {", ctx.settings.build.visibility.structure, - symbol + symbol, ) .call { renderImmutableProperties() } .write("") @@ -302,9 +303,10 @@ class StructureGenerator( val exceptionBaseClass = ExceptionBaseClassGenerator.baseExceptionSymbol(ctx.settings) writer.addImport(exceptionBaseClass) - writer.openBlock("#L class #T private constructor(builder: Builder) : ${exceptionBaseClass.name}() {", + writer.openBlock( + "#L class #T private constructor(builder: Builder) : ${exceptionBaseClass.name}() {", ctx.settings.build.visibility.error, - symbol + symbol, ) .write("") .call { renderImmutableProperties() } diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt index d39822e12..0ec4c9074 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt @@ -48,9 +48,11 @@ class AuthSchemeParametersGenerator : AbstractConfigGenerator() { val implSymbol = getImplementationSymbol(ctx.settings) ctx.delegator.useSymbolWriter(symbol) { writer -> - writer.withBlock("#L interface #T {", "}", + writer.withBlock( + "#L interface #T {", + "}", ctx.settings.build.visibility.structure, - symbol + symbol, ) { dokka("The name of the operation currently being invoked.") write("public val operationName: String") diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt index b7614500f..8ff5a8f2b 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt @@ -79,10 +79,12 @@ class DefaultEndpointProviderGenerator( fun render() { renderDocumentation() - writer.withBlock("#L class #T: #T {", "}", + writer.withBlock( + "#L class #T: #T {", + "}", settings.build.visibility.structure, defaultProviderSymbol, - interfaceSymbol + interfaceSymbol, ) { renderResolve() } diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt index 2b94248ef..d7c70b90f 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt @@ -43,9 +43,11 @@ class EndpointDiscovererGenerator(private val ctx: CodegenContext, private val d calls. """.trimIndent(), ) - withBlock("#L class #T {", "}", + withBlock( + "#L class #T {", + "}", ctx.settings.build.visibility.structure, - symbol + symbol, ) { write( "private val cache = #T(10.#T, #T.System)", diff --git a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGeneratorTest.kt b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGeneratorTest.kt index 5a4b2b71c..e5bec8289 100644 --- a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGeneratorTest.kt +++ b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGeneratorTest.kt @@ -147,7 +147,7 @@ class DefaultEndpointProviderGeneratorTest { val settings = KotlinSettings( service = ShapeId.from("EndpointProviderGeneratorTest"), pkg = KotlinSettings.PackageSettings("name", "version"), - sdkId = "testSdkId" + sdkId = "testSdkId", ) DefaultEndpointProviderGenerator(writer, rules, defaultSymbol, interfaceSymbol, paramsSymbol, settings).render() From 46bbf125942d522cf3c5718471a0fc08d75e5752 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Wed, 13 Sep 2023 15:35:22 -0400 Subject: [PATCH 04/18] Fix test shape ID --- .../rendering/endpoints/DefaultEndpointProviderGeneratorTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGeneratorTest.kt b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGeneratorTest.kt index e5bec8289..a2e411c40 100644 --- a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGeneratorTest.kt +++ b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGeneratorTest.kt @@ -145,7 +145,7 @@ class DefaultEndpointProviderGeneratorTest { namespace = TestModelDefault.NAMESPACE } val settings = KotlinSettings( - service = ShapeId.from("EndpointProviderGeneratorTest"), + service = ShapeId.from("com.test#Test"), pkg = KotlinSettings.PackageSettings("name", "version"), sdkId = "testSdkId", ) From 51d6df840ff95e82c6086c8a0f5ca12183862b2f Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Wed, 13 Sep 2023 15:43:00 -0400 Subject: [PATCH 05/18] Replace toList import --- .../software/amazon/smithy/kotlin/codegen/KotlinSettings.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt index ad6f590b5..e73c7c7b6 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt @@ -16,6 +16,7 @@ import software.amazon.smithy.model.shapes.Shape import software.amazon.smithy.model.shapes.ShapeId import java.util.Optional import java.util.logging.Logger +import kotlin.streams.toList // shapeId of service from which to generate an SDK private const val SERVICE = "service" From f88aaa55d818aad087279e030824df858cc8e1a5 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Wed, 13 Sep 2023 16:58:59 -0400 Subject: [PATCH 06/18] Also remove endpoints-related constructs from the API --- .../codegen/rendering/endpoints/EndpointDelegator.kt | 4 ++-- .../rendering/endpoints/EndpointParametersGenerator.kt | 6 +++++- .../rendering/endpoints/EndpointProviderGenerator.kt | 4 +++- .../endpoints/EndpointParametersGeneratorTest.kt | 9 ++++++++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointDelegator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointDelegator.kt index cac97608f..5d2eca98f 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointDelegator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointDelegator.kt @@ -33,7 +33,7 @@ interface EndpointDelegator { val defaultProviderSymbol = DefaultEndpointProviderGenerator.getSymbol(ctx.settings) ctx.delegator.useFileWriter(providerSymbol) { - EndpointProviderGenerator(it, providerSymbol, paramsSymbol).render() + EndpointProviderGenerator(it, ctx.settings, providerSymbol, paramsSymbol).render() } if (rules != null) { @@ -49,7 +49,7 @@ interface EndpointDelegator { fun generateEndpointParameters(ctx: ProtocolGenerator.GenerationContext, rules: EndpointRuleSet?) { val paramsSymbol = EndpointParametersGenerator.getSymbol(ctx.settings) ctx.delegator.useFileWriter(paramsSymbol) { - EndpointParametersGenerator(it, rules, paramsSymbol).render() + EndpointParametersGenerator(it, ctx.settings, rules, paramsSymbol).render() } } diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt index 34d08ab67..266b695ef 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt @@ -22,6 +22,7 @@ private const val DEFAULT_DEPRECATED_MESSAGE = */ class EndpointParametersGenerator( private val writer: KotlinWriter, + private val settings: KotlinSettings, rules: EndpointRuleSet?, private val paramsSymbol: Symbol, ) { @@ -51,7 +52,10 @@ class EndpointParametersGenerator( fun render() { renderDocumentation() // FIXME - this should probably be an interface - writer.withBlock("public class #T private constructor(builder: Builder) {", "}", paramsSymbol) { + writer.withBlock("#L class #T private constructor(builder: Builder) {", "}", + settings.build.visibility.structure, + paramsSymbol + ) { renderFields() renderCompanionObject() write("") diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt index c1cc9633c..0415352dd 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt @@ -18,6 +18,7 @@ import software.amazon.smithy.kotlin.codegen.model.buildSymbol */ class EndpointProviderGenerator( private val writer: KotlinWriter, + private val settings: KotlinSettings, private val providerSymbol: Symbol, private val paramsSymbol: Symbol, ) { @@ -33,7 +34,8 @@ class EndpointProviderGenerator( fun render() { renderDocumentation() writer.write( - "public fun interface #T: #T<#T>", + "#L fun interface #T: #T<#T>", + settings.build.visibility.structure, providerSymbol, RuntimeTypes.SmithyClient.Endpoints.EndpointProvider, paramsSymbol, diff --git a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGeneratorTest.kt b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGeneratorTest.kt index 476c7c5c8..db585ec37 100644 --- a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGeneratorTest.kt +++ b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGeneratorTest.kt @@ -4,6 +4,7 @@ */ package software.amazon.smithy.kotlin.codegen.rendering.endpoints +import software.amazon.smithy.kotlin.codegen.KotlinSettings import software.amazon.smithy.kotlin.codegen.core.KotlinWriter import software.amazon.smithy.kotlin.codegen.model.buildSymbol import software.amazon.smithy.kotlin.codegen.test.TestModelDefault @@ -11,6 +12,7 @@ import software.amazon.smithy.kotlin.codegen.test.assertBalancedBracesAndParens import software.amazon.smithy.kotlin.codegen.test.formatForTest import software.amazon.smithy.kotlin.codegen.test.shouldContainOnlyOnceWithDiff import software.amazon.smithy.model.node.Node +import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.rulesengine.language.EndpointRuleSet import kotlin.test.* @@ -83,7 +85,12 @@ class EndpointParametersGeneratorTest { name = "EndpointParameters" namespace = TestModelDefault.NAMESPACE } - EndpointParametersGenerator(writer, rules, paramsSymbol).render() + val settings = KotlinSettings( + service = ShapeId.from("com.test#Test"), + pkg = KotlinSettings.PackageSettings("name", "version"), + sdkId = "testSdkId", + ) + EndpointParametersGenerator(writer, settings, rules, paramsSymbol).render() generatedClass = writer.toString() } From 56214980bf762ae45de802e4c7368061d0973f0f Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Wed, 13 Sep 2023 16:59:32 -0400 Subject: [PATCH 07/18] ktlintFormat --- .../rendering/endpoints/EndpointParametersGenerator.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt index 266b695ef..fb90b56d9 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt @@ -52,9 +52,11 @@ class EndpointParametersGenerator( fun render() { renderDocumentation() // FIXME - this should probably be an interface - writer.withBlock("#L class #T private constructor(builder: Builder) {", "}", + writer.withBlock( + "#L class #T private constructor(builder: Builder) {", + "}", settings.build.visibility.structure, - paramsSymbol + paramsSymbol, ) { renderFields() renderCompanionObject() From 8733525b13ab908ff604b0e208fa1c4501aa2227 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Thu, 14 Sep 2023 09:09:15 -0400 Subject: [PATCH 08/18] a word --- .changes/b6288fbe-a409-473c-a6ac-12c3c310b963.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/b6288fbe-a409-473c-a6ac-12c3c310b963.json b/.changes/b6288fbe-a409-473c-a6ac-12c3c310b963.json index 8d88a208b..28a725e54 100644 --- a/.changes/b6288fbe-a409-473c-a6ac-12c3c310b963.json +++ b/.changes/b6288fbe-a409-473c-a6ac-12c3c310b963.json @@ -1,5 +1,5 @@ { "id": "b6288fbe-a409-473c-a6ac-12c3c310b963", "type": "misc", - "description": "Generate internal-only clients with visibility" + "description": "Generate internal-only clients with `internal` visibility" } \ No newline at end of file From 7f14f8de5bf26a924e91d890dd05170b025a5bf4 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Thu, 14 Sep 2023 09:58:18 -0400 Subject: [PATCH 09/18] Reduce configuration to single `visibility` member under new `api` settings --- .../smithy/kotlin/codegen/KotlinSettings.kt | 37 +++++++++---------- .../rendering/ExceptionBaseClassGenerator.kt | 2 +- .../rendering/ServiceClientGenerator.kt | 2 +- .../codegen/rendering/StructureGenerator.kt | 4 +- .../auth/AuthSchemeParametersGenerator.kt | 2 +- .../auth/AuthSchemeProviderGenerator.kt | 4 +- .../DefaultEndpointProviderGenerator.kt | 2 +- .../endpoints/EndpointParametersGenerator.kt | 2 +- .../endpoints/EndpointProviderGenerator.kt | 2 +- .../discovery/EndpointDiscovererGenerator.kt | 2 +- 10 files changed, 29 insertions(+), 30 deletions(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt index e73c7c7b6..4c132933e 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt @@ -25,7 +25,7 @@ private const val PACKAGE_NAME = "name" private const val PACKAGE_VERSION = "version" private const val PACKAGE_DESCRIPTION = "description" private const val BUILD_SETTINGS = "build" -private const val VISIBILITY_SETTINGS = "visibility" +private const val API_SETTINGS = "api" // Optional specification of sdkId for models that provide them, otherwise Service's shape id name is used private const val SDK_ID = "sdkId" @@ -38,6 +38,7 @@ data class KotlinSettings( val pkg: PackageSettings, val sdkId: String, val build: BuildSettings = BuildSettings.Default, + val api: ApiSettings = ApiSettings.Default, ) { /** @@ -92,11 +93,13 @@ data class KotlinSettings( // Load the sdk id from configurations that define it, fall back to service name for those that don't. val sdkId = config.getStringMemberOrDefault(SDK_ID, serviceId.name) val build = config.getObjectMember(BUILD_SETTINGS) + val api = config.getObjectMember(API_SETTINGS) return KotlinSettings( serviceId, PackageSettings(packageName, version, desc), sdkId, BuildSettings.fromNode(build), + ApiSettings.fromNode(api) ) } } @@ -161,7 +164,6 @@ data class BuildSettings( val generateDefaultBuildFiles: Boolean = true, val optInAnnotations: List? = null, val generateMultiplatformProject: Boolean = false, - val visibility: VisibilitySettings = VisibilitySettings.Default, ) { companion object { const val ROOT_PROJECT = "rootProject" @@ -180,9 +182,8 @@ data class BuildSettings( }.orNull() } }.orNull() - val visibility = VisibilitySettings.fromNode(node.get().getObjectMember(VISIBILITY_SETTINGS)) - BuildSettings(generateFullProject, generateBuildFiles, annotations, generateMultiplatformProject, visibility) + BuildSettings(generateFullProject, generateBuildFiles, annotations, generateMultiplatformProject) }.orElse(Default) /** @@ -196,26 +197,24 @@ class UnresolvableProtocolException(message: String) : CodegenException(message) private fun Optional.orNull(): T? = if (isPresent) get() else null -data class VisibilitySettings( - val serviceClient: String = "public", - val structure: String = "public", - val error: String = "public", +/** + * Contains API settings for a Kotlin project + * @param visibility String representing the visibility of code-generated classes, objects, interfaces, etc. + */ +data class ApiSettings( + val visibility: String = "public", ) { companion object { - const val SERVICE_CLIENT = "serviceClient" - const val STRUCTURE = "structure" - const val ERROR = "error" - - fun fromNode(node: Optional): VisibilitySettings = node.map { - val serviceClient = node.get().getStringMemberOrDefault(SERVICE_CLIENT, "public") - val structure = node.get().getStringMemberOrDefault(STRUCTURE, "public") - val error = node.get().getStringMemberOrDefault(ERROR, "public") - VisibilitySettings(serviceClient, structure, error) + const val VISIBILITY = "visibility" + + fun fromNode(node: Optional): ApiSettings = node.map { + val visibility = node.get().getStringMemberOrDefault(VISIBILITY, "public") + ApiSettings(visibility) }.orElse(Default) /** - * Default visibility settings + * Default build settings */ - val Default: VisibilitySettings = VisibilitySettings() + val Default: ApiSettings = ApiSettings() } } diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt index 7a4e1c761..4dc69ab2f 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt @@ -36,7 +36,7 @@ object ExceptionBaseClassGenerator { writer.withBlock( "#L open class #T : #T {", "}", - ctx.settings.build.visibility.error, + ctx.settings.api.visibility, serviceException, baseException, ) { diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt index b94856e05..15df448f3 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt @@ -67,7 +67,7 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { requireNotNull(ctx.shape) { "ServiceShape is required to render a service client" } private val serviceSymbol = ctx.symbolProvider.toSymbol(service) private val writer = ctx.writer - private val visibility = ctx.settings.build.visibility.serviceClient + private val visibility = ctx.settings.api.visibility fun render() { writer.write("\n\n") diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt index 2d47daae8..632d42902 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt @@ -54,7 +54,7 @@ class StructureGenerator( private fun renderStructure() { writer.openBlock( "#L class #T private constructor(builder: Builder) {", - ctx.settings.build.visibility.structure, + ctx.settings.api.visibility, symbol, ) .call { renderImmutableProperties() } @@ -305,7 +305,7 @@ class StructureGenerator( writer.openBlock( "#L class #T private constructor(builder: Builder) : ${exceptionBaseClass.name}() {", - ctx.settings.build.visibility.error, + ctx.settings.api.visibility, symbol, ) .write("") diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt index 0ec4c9074..57193f19e 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt @@ -51,7 +51,7 @@ class AuthSchemeParametersGenerator : AbstractConfigGenerator() { writer.withBlock( "#L interface #T {", "}", - ctx.settings.build.visibility.structure, + ctx.settings.api.visibility, symbol, ) { dokka("The name of the operation currently being invoked.") diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt index e87ceac67..8f4b59cc6 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt @@ -55,7 +55,7 @@ open class AuthSchemeProviderGenerator { } writer.write( "#L interface #T : #T<#T>", - ctx.settings.build.visibility.structure, + ctx.settings.api.visibility, symbol, RuntimeTypes.Auth.Identity.AuthSchemeProvider, paramsSymbol, @@ -67,7 +67,7 @@ open class AuthSchemeProviderGenerator { writer.withBlock( "#L object #T : #T {", "}", - ctx.settings.build.visibility.structure, + ctx.settings.api.visibility, getDefaultSymbol(ctx.settings), getSymbol(ctx.settings), ) { diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt index 8ff5a8f2b..63eea544d 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt @@ -82,7 +82,7 @@ class DefaultEndpointProviderGenerator( writer.withBlock( "#L class #T: #T {", "}", - settings.build.visibility.structure, + settings.api.visibility, defaultProviderSymbol, interfaceSymbol, ) { diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt index fb90b56d9..517757ab6 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt @@ -55,7 +55,7 @@ class EndpointParametersGenerator( writer.withBlock( "#L class #T private constructor(builder: Builder) {", "}", - settings.build.visibility.structure, + settings.api.visibility, paramsSymbol, ) { renderFields() diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt index 0415352dd..3ffa2155a 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt @@ -35,7 +35,7 @@ class EndpointProviderGenerator( renderDocumentation() writer.write( "#L fun interface #T: #T<#T>", - settings.build.visibility.structure, + settings.api.visibility, providerSymbol, RuntimeTypes.SmithyClient.Endpoints.EndpointProvider, paramsSymbol, diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt index d7c70b90f..6167cd972 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt @@ -46,7 +46,7 @@ class EndpointDiscovererGenerator(private val ctx: CodegenContext, private val d withBlock( "#L class #T {", "}", - ctx.settings.build.visibility.structure, + ctx.settings.api.visibility, symbol, ) { write( From 506da38bbd766162959025e6e6d53b73556cd9c2 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Thu, 14 Sep 2023 11:13:04 -0400 Subject: [PATCH 10/18] Make `visibility` an enum value defaulting to `public` --- .../smithy/kotlin/codegen/KotlinSettings.kt | 17 +++++++++++++++-- .../rendering/ExceptionBaseClassGenerator.kt | 2 +- .../codegen/rendering/ServiceClientGenerator.kt | 2 +- .../codegen/rendering/StructureGenerator.kt | 4 ++-- .../auth/AuthSchemeParametersGenerator.kt | 2 +- .../auth/AuthSchemeProviderGenerator.kt | 4 ++-- .../DefaultEndpointProviderGenerator.kt | 2 +- .../endpoints/EndpointParametersGenerator.kt | 2 +- .../endpoints/EndpointProviderGenerator.kt | 2 +- .../discovery/EndpointDiscovererGenerator.kt | 2 +- 10 files changed, 26 insertions(+), 13 deletions(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt index 4c132933e..4876d4b79 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt @@ -197,18 +197,31 @@ class UnresolvableProtocolException(message: String) : CodegenException(message) private fun Optional.orNull(): T? = if (isPresent) get() else null + +enum class Visibility(val value: String) { + PUBLIC("public"), + INTERNAL("internal"); + + companion object { + public fun fromValue(value: String): Visibility = when (value.toLowerCase()) { + "internal" -> INTERNAL + else -> PUBLIC + } + } +} + /** * Contains API settings for a Kotlin project * @param visibility String representing the visibility of code-generated classes, objects, interfaces, etc. */ data class ApiSettings( - val visibility: String = "public", + val visibility: Visibility = Visibility.PUBLIC, ) { companion object { const val VISIBILITY = "visibility" fun fromNode(node: Optional): ApiSettings = node.map { - val visibility = node.get().getStringMemberOrDefault(VISIBILITY, "public") + val visibility = Visibility.fromValue(node.get().getStringMemberOrDefault(VISIBILITY, "public")) ApiSettings(visibility) }.orElse(Default) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt index 4dc69ab2f..192859de4 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt @@ -36,7 +36,7 @@ object ExceptionBaseClassGenerator { writer.withBlock( "#L open class #T : #T {", "}", - ctx.settings.api.visibility, + ctx.settings.api.visibility.value, serviceException, baseException, ) { diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt index 15df448f3..da9b663b6 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt @@ -67,7 +67,7 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { requireNotNull(ctx.shape) { "ServiceShape is required to render a service client" } private val serviceSymbol = ctx.symbolProvider.toSymbol(service) private val writer = ctx.writer - private val visibility = ctx.settings.api.visibility + private val visibility = ctx.settings.api.visibility.value fun render() { writer.write("\n\n") diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt index 632d42902..20637f8f8 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt @@ -54,7 +54,7 @@ class StructureGenerator( private fun renderStructure() { writer.openBlock( "#L class #T private constructor(builder: Builder) {", - ctx.settings.api.visibility, + ctx.settings.api.visibility.value, symbol, ) .call { renderImmutableProperties() } @@ -305,7 +305,7 @@ class StructureGenerator( writer.openBlock( "#L class #T private constructor(builder: Builder) : ${exceptionBaseClass.name}() {", - ctx.settings.api.visibility, + ctx.settings.api.visibility.value, symbol, ) .write("") diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt index 57193f19e..722545466 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt @@ -51,7 +51,7 @@ class AuthSchemeParametersGenerator : AbstractConfigGenerator() { writer.withBlock( "#L interface #T {", "}", - ctx.settings.api.visibility, + ctx.settings.api.visibility.value, symbol, ) { dokka("The name of the operation currently being invoked.") diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt index 8f4b59cc6..fc0e102b2 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt @@ -55,7 +55,7 @@ open class AuthSchemeProviderGenerator { } writer.write( "#L interface #T : #T<#T>", - ctx.settings.api.visibility, + ctx.settings.api.visibility.value, symbol, RuntimeTypes.Auth.Identity.AuthSchemeProvider, paramsSymbol, @@ -67,7 +67,7 @@ open class AuthSchemeProviderGenerator { writer.withBlock( "#L object #T : #T {", "}", - ctx.settings.api.visibility, + ctx.settings.api.visibility.value, getDefaultSymbol(ctx.settings), getSymbol(ctx.settings), ) { diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt index 63eea544d..cb10a1aee 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt @@ -82,7 +82,7 @@ class DefaultEndpointProviderGenerator( writer.withBlock( "#L class #T: #T {", "}", - settings.api.visibility, + settings.api.visibility.value, defaultProviderSymbol, interfaceSymbol, ) { diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt index 517757ab6..1ca4b0b06 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt @@ -55,7 +55,7 @@ class EndpointParametersGenerator( writer.withBlock( "#L class #T private constructor(builder: Builder) {", "}", - settings.api.visibility, + settings.api.visibility.value, paramsSymbol, ) { renderFields() diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt index 3ffa2155a..debd234d1 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt @@ -35,7 +35,7 @@ class EndpointProviderGenerator( renderDocumentation() writer.write( "#L fun interface #T: #T<#T>", - settings.api.visibility, + settings.api.visibility.value, providerSymbol, RuntimeTypes.SmithyClient.Endpoints.EndpointProvider, paramsSymbol, diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt index 6167cd972..ac82d974c 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt @@ -46,7 +46,7 @@ class EndpointDiscovererGenerator(private val ctx: CodegenContext, private val d withBlock( "#L class #T {", "}", - ctx.settings.api.visibility, + ctx.settings.api.visibility.value, symbol, ) { write( From 245559fdf11fcb0f59dbc1ce73b1fc3f0ea541fa Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Thu, 14 Sep 2023 11:25:44 -0400 Subject: [PATCH 11/18] Use `#L` instead of `$visibility` --- .../rendering/ServiceClientGenerator.kt | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt index da9b663b6..4c1f667f8 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt @@ -67,12 +67,11 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { requireNotNull(ctx.shape) { "ServiceShape is required to render a service client" } private val serviceSymbol = ctx.symbolProvider.toSymbol(service) private val writer = ctx.writer - private val visibility = ctx.settings.api.visibility.value fun render() { writer.write("\n\n") - writer.write("$visibility const val ServiceId: String = #S", ctx.settings.sdkId) - writer.write("$visibility const val SdkVersion: String = #S", ctx.settings.pkg.version) + writer.write("#L const val ServiceId: String = #S", ctx.settings.api.visibility.value, ctx.settings.sdkId) + writer.write("#L const val SdkVersion: String = #S", ctx.settings.api.visibility.value, ctx.settings.pkg.version) writer.write("\n\n") writer.putContext("service.name", ctx.settings.sdkId) @@ -83,7 +82,10 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { writer.renderDocumentation(service) writer.renderAnnotations(service) - writer.openBlock("$visibility interface ${serviceSymbol.name} : #T {", RuntimeTypes.SmithyClient.SdkClient) + writer.openBlock("#L interface ${serviceSymbol.name} : #T {", + ctx.settings.api.visibility.value, + RuntimeTypes.SmithyClient.SdkClient + ) .call { // allow access to client's Config writer.dokka("${serviceSymbol.name}'s configuration") @@ -199,7 +201,10 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { write("Any resources created on your behalf will be shared between clients, and will only be closed when ALL clients using them are closed.") write("If you provide a resource (e.g. [HttpClientEngine]) to the SDK, you are responsible for managing the lifetime of that resource.") } - writer.withBlock("$visibility fun #1T.withConfig(block: #1T.Config.Builder.() -> Unit): #1T {", "}", serviceSymbol) { + writer.withBlock("#1L fun #2T.withConfig(block: #2T.Config.Builder.() -> Unit): #2T {", "}", + ctx.settings.api.visibility.value, + serviceSymbol + ) { write("val newConfig = config.toBuilder().apply(block).build()") write("return Default#L(newConfig)", serviceSymbol.name) } @@ -220,7 +225,8 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { writer.renderDocumentation(op) writer.renderAnnotations(op) writer.write( - "$visibility suspend inline fun #T.#L(crossinline block: #T.Builder.() -> Unit): #T = #L(#T.Builder().apply(block).build())", + "#L suspend inline fun #T.#L(crossinline block: #T.Builder.() -> Unit): #T = #L(#T.Builder().apply(block).build())", + ctx.settings.api.visibility.value, serviceSymbol, operationName, inputSymbol, From 7c43ce908a7da23587a280fa407d057235c22401 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Thu, 14 Sep 2023 11:26:19 -0400 Subject: [PATCH 12/18] ktlint --- .../amazon/smithy/kotlin/codegen/KotlinSettings.kt | 6 +++--- .../codegen/rendering/ServiceClientGenerator.kt | 11 +++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt index 4876d4b79..4d5b8e18f 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt @@ -99,7 +99,7 @@ data class KotlinSettings( PackageSettings(packageName, version, desc), sdkId, BuildSettings.fromNode(build), - ApiSettings.fromNode(api) + ApiSettings.fromNode(api), ) } } @@ -197,10 +197,10 @@ class UnresolvableProtocolException(message: String) : CodegenException(message) private fun Optional.orNull(): T? = if (isPresent) get() else null - enum class Visibility(val value: String) { PUBLIC("public"), - INTERNAL("internal"); + INTERNAL("internal"), + ; companion object { public fun fromValue(value: String): Visibility = when (value.toLowerCase()) { diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt index 4c1f667f8..aeb52747b 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt @@ -82,9 +82,10 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { writer.renderDocumentation(service) writer.renderAnnotations(service) - writer.openBlock("#L interface ${serviceSymbol.name} : #T {", + writer.openBlock( + "#L interface ${serviceSymbol.name} : #T {", ctx.settings.api.visibility.value, - RuntimeTypes.SmithyClient.SdkClient + RuntimeTypes.SmithyClient.SdkClient, ) .call { // allow access to client's Config @@ -201,9 +202,11 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { write("Any resources created on your behalf will be shared between clients, and will only be closed when ALL clients using them are closed.") write("If you provide a resource (e.g. [HttpClientEngine]) to the SDK, you are responsible for managing the lifetime of that resource.") } - writer.withBlock("#1L fun #2T.withConfig(block: #2T.Config.Builder.() -> Unit): #2T {", "}", + writer.withBlock( + "#1L fun #2T.withConfig(block: #2T.Config.Builder.() -> Unit): #2T {", + "}", ctx.settings.api.visibility.value, - serviceSymbol + serviceSymbol, ) { write("val newConfig = config.toBuilder().apply(block).build()") write("return Default#L(newConfig)", serviceSymbol.name) From 816d6bfa138f82a5d463c98b1f3bc99f5a71cab8 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Thu, 14 Sep 2023 11:31:11 -0400 Subject: [PATCH 13/18] update docs --- .../software/amazon/smithy/kotlin/codegen/KotlinSettings.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt index 4d5b8e18f..deae0e72d 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt @@ -197,6 +197,10 @@ class UnresolvableProtocolException(message: String) : CodegenException(message) private fun Optional.orNull(): T? = if (isPresent) get() else null +/** + * The visibility of code-generated classes, objects, interfaces, etc. + * Valid values are `public` and `internal`. `private` not supported because codegen would not compile with private classes. + */ enum class Visibility(val value: String) { PUBLIC("public"), INTERNAL("internal"), @@ -212,7 +216,7 @@ enum class Visibility(val value: String) { /** * Contains API settings for a Kotlin project - * @param visibility String representing the visibility of code-generated classes, objects, interfaces, etc. + * @param visibility Enum representing the visibility of code-generated classes, objects, interfaces, etc. */ data class ApiSettings( val visibility: Visibility = Visibility.PUBLIC, From c5d14d4599a007a60e1bb3bd02951040cf40b2ff Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Thu, 14 Sep 2023 11:37:45 -0400 Subject: [PATCH 14/18] add tests --- .../kotlin/codegen/KotlinSettingsTest.kt | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettingsTest.kt b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettingsTest.kt index bfecad4b4..0dc12fcbd 100644 --- a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettingsTest.kt +++ b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettingsTest.kt @@ -184,4 +184,82 @@ class KotlinSettingsTest { Node.parse(contents).expectObjectNode(), ) } + + @Test + fun `supports internal visibility`() { + val model = javaClass.getResource("simple-service.smithy")!!.toSmithyModel() + + val contents = """ + { + "package": { + "name": "aws.sdk.kotlin.runtime.protocoltest.awsrestjson", + "version": "1.0.0" + }, + "build": { + "optInAnnotations": ["foo", "bar"] + }, + "api": { + "visibility": "internal" + } + } + """.trimIndent() + + val settings = KotlinSettings.from( + model, + Node.parse(contents).expectObjectNode(), + ) + + assertEquals(Visibility.INTERNAL, settings.api.visibility) + } + + @Test + fun `defaults to public visibility`() { + val model = javaClass.getResource("simple-service.smithy")!!.toSmithyModel() + + val contents = """ + { + "package": { + "name": "aws.sdk.kotlin.runtime.protocoltest.awsrestjson", + "version": "1.0.0" + }, + "build": { + "optInAnnotations": ["foo", "bar"] + } + } + """.trimIndent() + + val settings = KotlinSettings.from( + model, + Node.parse(contents).expectObjectNode(), + ) + + assertEquals(Visibility.PUBLIC, settings.api.visibility) + } + + @Test + fun `works with unsupported visibility values`() { + val model = javaClass.getResource("simple-service.smithy")!!.toSmithyModel() + + val contents = """ + { + "package": { + "name": "aws.sdk.kotlin.runtime.protocoltest.awsrestjson", + "version": "1.0.0" + }, + "build": { + "optInAnnotations": ["foo", "bar"] + }, + "api": { + "visibility": "I don't know, just make it visible" + } + } + """.trimIndent() + + val settings = KotlinSettings.from( + model, + Node.parse(contents).expectObjectNode(), + ) + + assertEquals(Visibility.PUBLIC, settings.api.visibility) + } } From c3b41cf4b059490dda1aa359cd78a2ea00ebabe6 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Thu, 14 Sep 2023 11:43:24 -0400 Subject: [PATCH 15/18] update lowercase function --- .../software/amazon/smithy/kotlin/codegen/KotlinSettings.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt index deae0e72d..a1354f3e5 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt @@ -207,7 +207,7 @@ enum class Visibility(val value: String) { ; companion object { - public fun fromValue(value: String): Visibility = when (value.toLowerCase()) { + public fun fromValue(value: String): Visibility = when (value.lowercase()) { "internal" -> INTERNAL else -> PUBLIC } From a249befbf837ceaf18164e4ab99ada0e1eee7376 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Thu, 14 Sep 2023 12:05:08 -0400 Subject: [PATCH 16/18] Throw IllegalArgumentException, override toString --- .../smithy/kotlin/codegen/KotlinSettings.kt | 8 +++++++- .../rendering/ExceptionBaseClassGenerator.kt | 2 +- .../codegen/rendering/ServiceClientGenerator.kt | 10 +++++----- .../codegen/rendering/StructureGenerator.kt | 4 ++-- .../auth/AuthSchemeParametersGenerator.kt | 2 +- .../auth/AuthSchemeProviderGenerator.kt | 4 ++-- .../DefaultEndpointProviderGenerator.kt | 2 +- .../endpoints/EndpointParametersGenerator.kt | 2 +- .../endpoints/EndpointProviderGenerator.kt | 2 +- .../discovery/EndpointDiscovererGenerator.kt | 2 +- .../smithy/kotlin/codegen/KotlinSettingsTest.kt | 16 +++++++++------- 11 files changed, 31 insertions(+), 23 deletions(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt index a1354f3e5..8253ef85e 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt @@ -14,6 +14,7 @@ import software.amazon.smithy.model.node.StringNode import software.amazon.smithy.model.shapes.ServiceShape import software.amazon.smithy.model.shapes.Shape import software.amazon.smithy.model.shapes.ShapeId +import java.lang.IllegalArgumentException import java.util.Optional import java.util.logging.Logger import kotlin.streams.toList @@ -206,10 +207,15 @@ enum class Visibility(val value: String) { INTERNAL("internal"), ; + override fun toString(): String { + return value + } + companion object { public fun fromValue(value: String): Visibility = when (value.lowercase()) { + "public" -> PUBLIC "internal" -> INTERNAL - else -> PUBLIC + else -> throw IllegalArgumentException("$value is not a valid Visibility, expected ${PUBLIC} or ${INTERNAL}") } } } diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt index 192859de4..4dc69ab2f 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ExceptionBaseClassGenerator.kt @@ -36,7 +36,7 @@ object ExceptionBaseClassGenerator { writer.withBlock( "#L open class #T : #T {", "}", - ctx.settings.api.visibility.value, + ctx.settings.api.visibility, serviceException, baseException, ) { diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt index aeb52747b..6151cbacc 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceClientGenerator.kt @@ -70,8 +70,8 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { fun render() { writer.write("\n\n") - writer.write("#L const val ServiceId: String = #S", ctx.settings.api.visibility.value, ctx.settings.sdkId) - writer.write("#L const val SdkVersion: String = #S", ctx.settings.api.visibility.value, ctx.settings.pkg.version) + writer.write("#L const val ServiceId: String = #S", ctx.settings.api.visibility, ctx.settings.sdkId) + writer.write("#L const val SdkVersion: String = #S", ctx.settings.api.visibility, ctx.settings.pkg.version) writer.write("\n\n") writer.putContext("service.name", ctx.settings.sdkId) @@ -84,7 +84,7 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { writer.renderAnnotations(service) writer.openBlock( "#L interface ${serviceSymbol.name} : #T {", - ctx.settings.api.visibility.value, + ctx.settings.api.visibility, RuntimeTypes.SmithyClient.SdkClient, ) .call { @@ -205,7 +205,7 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { writer.withBlock( "#1L fun #2T.withConfig(block: #2T.Config.Builder.() -> Unit): #2T {", "}", - ctx.settings.api.visibility.value, + ctx.settings.api.visibility, serviceSymbol, ) { write("val newConfig = config.toBuilder().apply(block).build()") @@ -229,7 +229,7 @@ class ServiceClientGenerator(private val ctx: RenderingContext) { writer.renderAnnotations(op) writer.write( "#L suspend inline fun #T.#L(crossinline block: #T.Builder.() -> Unit): #T = #L(#T.Builder().apply(block).build())", - ctx.settings.api.visibility.value, + ctx.settings.api.visibility, serviceSymbol, operationName, inputSymbol, diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt index 20637f8f8..632d42902 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt @@ -54,7 +54,7 @@ class StructureGenerator( private fun renderStructure() { writer.openBlock( "#L class #T private constructor(builder: Builder) {", - ctx.settings.api.visibility.value, + ctx.settings.api.visibility, symbol, ) .call { renderImmutableProperties() } @@ -305,7 +305,7 @@ class StructureGenerator( writer.openBlock( "#L class #T private constructor(builder: Builder) : ${exceptionBaseClass.name}() {", - ctx.settings.api.visibility.value, + ctx.settings.api.visibility, symbol, ) .write("") diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt index 722545466..57193f19e 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeParametersGenerator.kt @@ -51,7 +51,7 @@ class AuthSchemeParametersGenerator : AbstractConfigGenerator() { writer.withBlock( "#L interface #T {", "}", - ctx.settings.api.visibility.value, + ctx.settings.api.visibility, symbol, ) { dokka("The name of the operation currently being invoked.") diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt index fc0e102b2..8f4b59cc6 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/auth/AuthSchemeProviderGenerator.kt @@ -55,7 +55,7 @@ open class AuthSchemeProviderGenerator { } writer.write( "#L interface #T : #T<#T>", - ctx.settings.api.visibility.value, + ctx.settings.api.visibility, symbol, RuntimeTypes.Auth.Identity.AuthSchemeProvider, paramsSymbol, @@ -67,7 +67,7 @@ open class AuthSchemeProviderGenerator { writer.withBlock( "#L object #T : #T {", "}", - ctx.settings.api.visibility.value, + ctx.settings.api.visibility, getDefaultSymbol(ctx.settings), getSymbol(ctx.settings), ) { diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt index cb10a1aee..63eea544d 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderGenerator.kt @@ -82,7 +82,7 @@ class DefaultEndpointProviderGenerator( writer.withBlock( "#L class #T: #T {", "}", - settings.api.visibility.value, + settings.api.visibility, defaultProviderSymbol, interfaceSymbol, ) { diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt index 1ca4b0b06..517757ab6 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointParametersGenerator.kt @@ -55,7 +55,7 @@ class EndpointParametersGenerator( writer.withBlock( "#L class #T private constructor(builder: Builder) {", "}", - settings.api.visibility.value, + settings.api.visibility, paramsSymbol, ) { renderFields() diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt index debd234d1..3ffa2155a 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/EndpointProviderGenerator.kt @@ -35,7 +35,7 @@ class EndpointProviderGenerator( renderDocumentation() writer.write( "#L fun interface #T: #T<#T>", - settings.api.visibility.value, + settings.api.visibility, providerSymbol, RuntimeTypes.SmithyClient.Endpoints.EndpointProvider, paramsSymbol, diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt index ac82d974c..6167cd972 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/discovery/EndpointDiscovererGenerator.kt @@ -46,7 +46,7 @@ class EndpointDiscovererGenerator(private val ctx: CodegenContext, private val d withBlock( "#L class #T {", "}", - ctx.settings.api.visibility.value, + ctx.settings.api.visibility, symbol, ) { write( diff --git a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettingsTest.kt b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettingsTest.kt index 0dc12fcbd..0be629567 100644 --- a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettingsTest.kt +++ b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettingsTest.kt @@ -5,11 +5,13 @@ package software.amazon.smithy.kotlin.codegen +import org.junit.jupiter.api.assertThrows import software.amazon.smithy.codegen.core.CodegenException import software.amazon.smithy.kotlin.codegen.test.TestModelDefault import software.amazon.smithy.kotlin.codegen.test.toSmithyModel import software.amazon.smithy.model.node.Node import software.amazon.smithy.model.shapes.ShapeId +import java.lang.IllegalArgumentException import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -237,7 +239,7 @@ class KotlinSettingsTest { } @Test - fun `works with unsupported visibility values`() { + fun `throws on unsupported visibility values`() { val model = javaClass.getResource("simple-service.smithy")!!.toSmithyModel() val contents = """ @@ -255,11 +257,11 @@ class KotlinSettingsTest { } """.trimIndent() - val settings = KotlinSettings.from( - model, - Node.parse(contents).expectObjectNode(), - ) - - assertEquals(Visibility.PUBLIC, settings.api.visibility) + assertThrows { + KotlinSettings.from( + model, + Node.parse(contents).expectObjectNode(), + ) + } } } From c0ec3a394bf362a939ee3daf121dbb009efad804 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Thu, 14 Sep 2023 12:06:17 -0400 Subject: [PATCH 17/18] ktlint --- .../software/amazon/smithy/kotlin/codegen/KotlinSettings.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt index 8253ef85e..59558ae83 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettings.kt @@ -207,15 +207,13 @@ enum class Visibility(val value: String) { INTERNAL("internal"), ; - override fun toString(): String { - return value - } + override fun toString(): String = value companion object { public fun fromValue(value: String): Visibility = when (value.lowercase()) { "public" -> PUBLIC "internal" -> INTERNAL - else -> throw IllegalArgumentException("$value is not a valid Visibility, expected ${PUBLIC} or ${INTERNAL}") + else -> throw IllegalArgumentException("$value is not a valid Visibility value, expected $PUBLIC or $INTERNAL") } } } From 4b58f2ed7fcb2a1840fe1418aa560ec2b0dada13 Mon Sep 17 00:00:00 2001 From: Matas Lauzadis Date: Thu, 14 Sep 2023 12:08:42 -0400 Subject: [PATCH 18/18] add test for explicit `public` visibility --- .../kotlin/codegen/KotlinSettingsTest.kt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettingsTest.kt b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettingsTest.kt index 0be629567..3893b4e46 100644 --- a/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettingsTest.kt +++ b/codegen/smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/KotlinSettingsTest.kt @@ -238,6 +238,33 @@ class KotlinSettingsTest { assertEquals(Visibility.PUBLIC, settings.api.visibility) } + @Test + fun `supports public visibility`() { + val model = javaClass.getResource("simple-service.smithy")!!.toSmithyModel() + + val contents = """ + { + "package": { + "name": "aws.sdk.kotlin.runtime.protocoltest.awsrestjson", + "version": "1.0.0" + }, + "build": { + "optInAnnotations": ["foo", "bar"] + }, + "api": { + "visibility": "public" + } + } + """.trimIndent() + + val settings = KotlinSettings.from( + model, + Node.parse(contents).expectObjectNode(), + ) + + assertEquals(Visibility.PUBLIC, settings.api.visibility) + } + @Test fun `throws on unsupported visibility values`() { val model = javaClass.getResource("simple-service.smithy")!!.toSmithyModel()