Skip to content

Commit

Permalink
add nullability check mode as an API setting
Browse files Browse the repository at this point in the history
  • Loading branch information
aajtodd committed Sep 27, 2023
1 parent 08721de commit 23edf6a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ package software.amazon.smithy.kotlin.codegen

import software.amazon.smithy.codegen.core.CodegenException
import software.amazon.smithy.kotlin.codegen.lang.isValidPackageName
import software.amazon.smithy.kotlin.codegen.utils.toCamelCase
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.knowledge.NullableIndex.CheckMode
import software.amazon.smithy.model.knowledge.ServiceIndex
import software.amazon.smithy.model.node.ObjectNode
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.IllegalArgumentException
import kotlin.streams.toList

// shapeId of service from which to generate an SDK
Expand Down Expand Up @@ -218,19 +220,27 @@ enum class Visibility(val value: String) {
}
}

private fun checkModefromValue(value: String): CheckMode {
val camelCaseToMode = CheckMode.values().associateBy { it.toString().toCamelCase() }
return requireNotNull(camelCaseToMode[value]) { "$value is not a valid CheckMode, expected one of ${camelCaseToMode.keys}" }
}

/**
* Contains API settings for a Kotlin project
* @param visibility Enum representing the visibility of code-generated classes, objects, interfaces, etc.
*/
data class ApiSettings(
val visibility: Visibility = Visibility.PUBLIC,
val nullabilityCheckMode: CheckMode = CheckMode.CLIENT_CAREFUL,
) {
companion object {
const val VISIBILITY = "visibility"
const val NULLABILITY_CHECK_MODE = "nullabilityCheckMode"

fun fromNode(node: Optional<ObjectNode>): ApiSettings = node.map {
val visibility = Visibility.fromValue(node.get().getStringMemberOrDefault(VISIBILITY, "public"))
ApiSettings(visibility)
val checkMode = checkModefromValue(node.get().getStringMemberOrDefault(NULLABILITY_CHECK_MODE, "clientCareful"))
ApiSettings(visibility, checkMode)
}.orElse(Default)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
package software.amazon.smithy.kotlin.codegen

import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
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.knowledge.NullableIndex.CheckMode
import software.amazon.smithy.model.node.Node
import software.amazon.smithy.model.shapes.ShapeId
import java.lang.IllegalArgumentException
Expand Down Expand Up @@ -291,4 +294,24 @@ class KotlinSettingsTest {
)
}
}

@ParameterizedTest(name = "{0} ==> {1}")
@CsvSource(
"client, CLIENT",
"clientCareful, CLIENT_CAREFUL",
"clientZeroValueV1, CLIENT_ZERO_VALUE_V1",
"clientZeroValueV1NoInput, CLIENT_ZERO_VALUE_V1_NO_INPUT",
"server, SERVER",
)
fun testNullabilityCheckMode(pluginSetting: String, expectedEnumString: String) {
val expected = CheckMode.valueOf(expectedEnumString)
val contents = """
{
"nullabilityCheckMode": "$pluginSetting"
}
""".trimIndent()
val apiSettings = ApiSettings.fromNode(Node.parse(contents).asObjectNode())

assertEquals(expected, apiSettings.nullabilityCheckMode)
}
}

0 comments on commit 23edf6a

Please sign in to comment.