steering_docs/kotlin-tech.md
# Build and Package
./gradlew compileKotlin # Compile source code
./gradlew build # Build with dependencies
./gradlew clean build # Clean and build
# Testing
./gradlew test # Run all tests
./gradlew test --tests ClassName # Run specific test class
./gradlew test --tests ClassName.methodName # Run specific test method
# Execution
./gradlew run
{Service}Action.kt (e.g., S3ListBuckets.kt)Hello{Service}.kt (e.g., HelloS3.kt){Service}ActionTest.ktHello{Service}.kt class with main method with no class declarationcom.example.s3)In Kotlin, suspend functions are designed to be used with coroutines, which provide a way to write asynchronous code that is easy to read and maintain. When it comes to error handling in coroutines, you typically use structured concurrency and the try-catch.
suspend fun listBucketObjects(bucketName: String) {
try {
val request = ListObjectsRequest {
bucket = bucketName
}
S3Client.fromEnvironment { region = "us-east-1" }.use { s3 ->
try {
val response = s3.listObjects(request)
response.contents?.forEach { myObject ->
println("The name of the key is ${myObject.key}")
println("The object is ${myObject.size?.let { calKb(it) }} KBs")
println("The owner is ${myObject.owner}")
}
} catch (e: AwsServiceException) {
println("Service error: ${e.statusCode}, ${e.message}")
} catch (e: SdkClientException) {
println("Client error: ${e.message}")
}
}
} catch (e: Exception) {
println("Unexpected error: ${e.message}")
}
}
@Test, @BeforeEach, @AfterEach)@Tag("IntegrationTest")├── build.gradle.kts
├── settings.gradle.kts
├── src/
│ ├── main/
│ │ ├── kotlin/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └── {service}/
│ │ │ ├── Hello{Service}.kt
│ │ │ ├── {Service}Actions.kt
│ │ │ └── {Service}Scenario.kt
│ │ └── resources/
│ └── test/
│ ├── kotlin/
│ │ └── com/
│ │ └── example/
│ │ └── {service}/
│ │ └── {Service}Test.kt
│ └── resources/
├── gradle/
│ └── wrapper/
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
└── gradlew
└── gradlew.bat
Before creating Kotlin code examples:
coding-standards-KB for "Kotlin-code-example-standards"Kotlin-premium-KB for "Kotlin implementation patterns"