Skip to content

Commit 40562a3

Browse files
author
Michael Brewer
committed
refactor: Update libs and apply kotlin style changes
Changes: - bump gradle to 7.3.2 - bump kotlin to 1.6.10 - add jvm 11 compile time optimizations for kotlin - add helper function `missingId` - apply ktlint formatting - remove redundant typing or unused variables
1 parent ac3097d commit 40562a3

File tree

11 files changed

+63
-62
lines changed

11 files changed

+63
-62
lines changed

software/build.gradle.kts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2-
import com.github.jk1.license.render.ReportRenderer
3-
import com.github.jk1.license.render.InventoryHtmlReportRenderer
41
import com.github.jk1.license.filter.DependencyFilter
52
import com.github.jk1.license.filter.LicenseBundleNormalizer
3+
import com.github.jk1.license.render.InventoryHtmlReportRenderer
4+
import com.github.jk1.license.render.ReportRenderer
5+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
66

77
plugins {
8-
kotlin("jvm") version "1.6.0"
9-
kotlin("plugin.serialization") version "1.5.31"
10-
id("com.github.johnrengelman.shadow") version "5.1.0"
8+
kotlin("jvm") version "1.6.10"
9+
kotlin("plugin.serialization") version "1.5.32"
10+
id("com.github.johnrengelman.shadow") version "7.1.0"
1111
id("com.github.jk1.dependency-license-report") version "2.0"
1212
}
1313

1414
licenseReport {
15-
renderers = arrayOf<ReportRenderer>(InventoryHtmlReportRenderer("report.html","Backend"))
15+
renderers = arrayOf<ReportRenderer>(InventoryHtmlReportRenderer("report.html", "Backend"))
1616
filters = arrayOf<DependencyFilter>(LicenseBundleNormalizer())
1717
}
1818

@@ -35,6 +35,7 @@ tasks.test {
3535
useJUnitPlatform()
3636
}
3737

38-
tasks.withType<KotlinCompile>() {
38+
tasks.withType<KotlinCompile> {
3939
kotlinOptions.jvmTarget = "11"
40-
}
40+
kotlinOptions.freeCompilerArgs = listOf("-Xjsr305=strict -Xstring-concat=indy-with-constants")
41+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

software/settings.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
rootProject.name = "serverless-kotlin-demo"
2-
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0
3+
4+
package com.amazonaws.sample
5+
6+
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPResponse
7+
8+
fun missingId() = APIGatewayV2HTTPResponse().apply {
9+
statusCode = 500
10+
headers = mapOf("Content-Type" to "application/json")
11+
body = """{ "message": "Missing 'id' parameter in path" }"""
12+
}

software/src/main/kotlin/com/amazonaws/sample/DeleteProductHandler.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,18 @@ class DeleteProductHandler : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2H
2424
override fun handleRequest(event: APIGatewayV2HTTPEvent, context: Context): APIGatewayV2HTTPResponse {
2525
val logger = context.logger
2626

27-
val id = event.pathParameters?.get("id") ?: return APIGatewayV2HTTPResponse().apply {
28-
statusCode = 500
29-
headers = mapOf("Content-Type" to "application/json")
30-
body = """{ "message": "Missing 'id' parameter in path" }"""
31-
}
27+
val id = event.pathParameters?.get("id") ?: return missingId()
3228

3329
try {
3430
runBlocking {
35-
dynamoDbClient.deleteItem(DeleteItemRequest {
36-
tableName = productTable
37-
key = mapOf("PK" to AttributeValue.S(id))
38-
})
31+
dynamoDbClient.deleteItem(
32+
DeleteItemRequest {
33+
tableName = productTable
34+
key = mapOf("PK" to AttributeValue.S(id))
35+
}
36+
)
3937
}
40-
} catch (e : Exception) {
38+
} catch (e: Exception) {
4139
logger.log("ERROR ${e.message}")
4240
return APIGatewayV2HTTPResponse().apply {
4341
statusCode = 500
@@ -52,4 +50,4 @@ class DeleteProductHandler : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2H
5250
body = """{"message": "Product deleted"}"""
5351
}
5452
}
55-
}
53+
}

software/src/main/kotlin/com/amazonaws/sample/DynamoDBExtensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ package com.amazonaws.sample
55

66
import aws.sdk.kotlin.services.dynamodb.model.AttributeValue
77

8-
val AttributeValue.asString : String get() = (this as AttributeValue.S).value
9-
val AttributeValue.asFloat : Float get() = (this as AttributeValue.N).value.toFloat()
8+
val AttributeValue.asString get() = (this as AttributeValue.S).value
9+
val AttributeValue.asFloat get() = (this as AttributeValue.N).value.toFloat()

software/src/main/kotlin/com/amazonaws/sample/GetAllProductsHandler.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ class GetAllProductsHandler : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2
2929

3030
val scanResponse = try {
3131
runBlocking {
32-
dynamoDbClient.scan(ScanRequest {
33-
tableName = productTable
34-
limit = 20
35-
})
32+
dynamoDbClient.scan(
33+
ScanRequest {
34+
tableName = productTable
35+
limit = 20
36+
}
37+
)
3638
}
37-
} catch (e : Exception) {
39+
} catch (e: Exception) {
3840
logger.log("ERROR: ${e.message}")
3941
return APIGatewayV2HTTPResponse().apply {
4042
statusCode = 500
@@ -45,7 +47,7 @@ class GetAllProductsHandler : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2
4547

4648
val products = ArrayList<Product>()
4749

48-
scanResponse.items?.map { it
50+
scanResponse.items?.map {
4951
val id = it.getValue("PK").asString
5052
val name = it.getValue("name").asString
5153
val price = it.getValue("price").asFloat
@@ -58,4 +60,4 @@ class GetAllProductsHandler : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2
5860
body = Json.encodeToString(Products(products))
5961
}
6062
}
61-
}
63+
}

software/src/main/kotlin/com/amazonaws/sample/GetProductHandler.kt

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,17 @@ class GetProductHandler : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTP
2727
override fun handleRequest(event: APIGatewayV2HTTPEvent, context: Context): APIGatewayV2HTTPResponse {
2828
val logger = context.logger
2929

30-
val requestId = event.pathParameters["id"]
31-
requestId ?: run {
32-
logger.log("WARN: Missing 'id' parameter in path")
33-
return APIGatewayV2HTTPResponse().apply {
34-
statusCode = 400
35-
headers = mapOf("Content-Type" to "application/json")
36-
body = """{ "message": "Missing 'id' parameter in path" }"""
37-
}
38-
}
30+
val requestId = event.pathParameters?.get("id") ?: return missingId()
3931

40-
logger.log("INFO: Fetching product [$requestId]");
32+
logger.log("INFO: Fetching product [$requestId]")
4133

4234
val response = runBlocking {
43-
dynamoDbClient.getItem(GetItemRequest {
44-
tableName = productTable
45-
key = mapOf("PK" to AttributeValue.S(requestId))
46-
})
35+
dynamoDbClient.getItem(
36+
GetItemRequest {
37+
tableName = productTable
38+
key = mapOf("PK" to AttributeValue.S(requestId))
39+
}
40+
)
4741
}
4842

4943
val id = response.item?.getValue("PK")?.asString
@@ -64,4 +58,4 @@ class GetProductHandler : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTP
6458
body = Json.encodeToString(Product(id, name, price))
6559
}
6660
}
67-
}
61+
}

software/src/main/kotlin/com/amazonaws/sample/PutProductHandler.kt

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,7 @@ class PutProductHandler : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTP
2828
val logger = context.logger
2929
logger.log(event.toString())
3030

31-
val id = event.pathParameters["id"] ?: let {
32-
logger.log("WARN: Missing 'id' parameter in path")
33-
return APIGatewayV2HTTPResponse().apply {
34-
statusCode = 400
35-
headers = mapOf("Content-Type" to "application/json")
36-
body = """{ "message": "Missing 'id' parameter in path" }"""
37-
}
38-
}
31+
val id = event.pathParameters?.get("id") ?: return missingId()
3932

4033
if (event.body == null || event.body.isEmpty()) {
4134
return APIGatewayV2HTTPResponse().apply {
@@ -46,7 +39,7 @@ class PutProductHandler : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTP
4639
}
4740

4841
val product = try {
49-
Json.decodeFromString<Product>(event.body)
42+
Json.decodeFromString<Product>(event.body)
5043
} catch (e: Exception) {
5144
logger.log(e.message)
5245
return APIGatewayV2HTTPResponse().apply {
@@ -58,7 +51,7 @@ class PutProductHandler : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTP
5851

5952
logger.log("Product: $product")
6053

61-
if(id != product.id) {
54+
if (id != product.id) {
6255
logger.log("ERROR: Product ID in path ($id) does not match product ID in body (${product.id})")
6356
return APIGatewayV2HTTPResponse().apply {
6457
statusCode = 400
@@ -74,10 +67,12 @@ class PutProductHandler : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTP
7467
)
7568

7669
runBlocking {
77-
dynamoDbClient.putItem(PutItemRequest {
78-
tableName = productTable
79-
item = itemValues
80-
})
70+
dynamoDbClient.putItem(
71+
PutItemRequest {
72+
tableName = productTable
73+
item = itemValues
74+
}
75+
)
8176
}
8277

8378
return APIGatewayV2HTTPResponse().apply {

software/src/main/kotlin/com/amazonaws/sample/model/Product.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ package com.amazonaws.sample.model
66
import kotlinx.serialization.Serializable
77

88
@Serializable
9-
data class Product(val id: String, val name: String, val price: Float)
9+
data class Product(val id: String, val name: String, val price: Float)

0 commit comments

Comments
 (0)