question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DynamoDB Enhanced to support immutable Kotlin data classes

See original GitHub issue

In Kotlin your goal is to use data classes as immutable. With the current mapper this is not possible. You have to add an parameter less constructor to the class (noargs plugin) and all fields has to be changeable (var’s instead val’s)

Example: data class Example(val name:String) and not data class Example(var name:String)

Describe the Feature

The DynamoDB Enhanced mapper should call the constructor with all attributes read from the dynamodb.

Is your Feature Request related to a problem?

You can’t use immutable data classes and you have to use the norags plugin.

Describe alternatives you’ve considered

Use var’s instead of val’s and use norags plugin:

plugins{
   id("org.jetbrains.kotlin.plugin.noarg") version "1.4.10"
}
noArg {
  annotation("software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean")
}

Your Environment

  • AWS Java SDK version used: 2.15.7
  • JDK version used: 11
  • Operating System and version: Mac 10.15.7

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:34
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
cmaus-cscommented, Dec 30, 2020

The builder’s set methods are supposed to return a builder instance. kotlin generated setters that return void.

The following works for me, but is quite tedious to write:

@DynamoDbImmutable(builder = TweetDocument.Builder::class)
class TweetDocument(
    @get:DynamoDbPartitionKey val partitionKey: String,
    @get:DynamoDbSortKey val sortKey: String,
    val tweetId: Long,
    val searchTerm: String,
    val text: String,
    val lang: String,
    val createdAt: String
) {

    companion object {
        @JvmStatic
        fun builder() = Builder()
    }

    class Builder {
        private var partitionKey: String? = null
        fun partitionKey(value: String) = apply { partitionKey = value }

        private var sortKey: String? = null
        fun sortKey(value: String) = apply { sortKey = value }

        private var tweetId: Long? = null
        fun tweetId(value: Long) = apply { tweetId = value }

        private var searchTerm: String? = null
        fun searchTerm(value: String) = apply { searchTerm = value }

        private var text: String? = null
        fun text(value: String) = apply { text = value }

        private var lang: String? = null
        fun lang(value: String) = apply { lang = value }

        private var createdAt: String? = null
        fun createdAt(value: String) = apply { createdAt = value }

        fun build() = TweetDocument(
            partitionKey!!,
            sortKey!!,
            tweetId!!,
            searchTerm!!,
            text!!,
            lang!!,
            createdAt!!
        )
    }
}
3reactions
oharaandrew314commented, Jun 8, 2022

I made a plugin that allows you to use a data class directly with the DataClassTableSchema; might be worth checking out. https://github.com/oharaandrew314/dynamodb-kotlin-module

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing immutable class mapping for the Enhanced ... - AWS
Step 1 : Annotate your existing immutable class. First, create and annotate an immutable class that will map to records in your DynamoDB...
Read more >
AWS Java SDK V2. DynamoDB enhanced with Kotlin
Define the Data Class That Represents the DynamoDB Item. At the very basic level, the DynamoDB Enhanced Client library provides the following ...
Read more >
DynamoDbImmutable (AWS SDK for Java - 2.18.33)
Class level annotation that identifies this class as being a DynamoDb mappable entity. Any class used to initialize a ImmutableTableSchema must have this ......
Read more >
DynamoDB Enhanced Client for Java: Missing Setters Cause ...
The AWS SDK v2 for Java's DynamoDB Enhanced Client requires setters for every ... DynamoDbSortKey; @DynamoDbBean @Data public class Customer ...
Read more >
Copy most attributes from one class object to another class
Also, all the fields are mutable and nullable. I had to make it this way because it's supposed to be a mapper class...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found