oharaandrew314 / dynamodb-kotlin-module

Kotlin Module for the dynamodb-enhanced SDk
Apache License 2.0
23 stars 3 forks source link

Doesn't support properties defined in class body #7

Closed skibak closed 1 year ago

skibak commented 1 year ago

I have data class with somer properties defined in constructor and some in class body, e.g.

data class MyClass(
    @DynamoKtAttribute
    var prop1: String? = null
) {
    @DynamoKtAttribute
    var prop2: String = "def"  
}

When schema for it created, code here https://github.com/oharaandrew314/dynamodb-kotlin-module/blob/master/src/main/kotlin/io/andrewohara/dynamokt/DataClassAttribute.kt#L55 builds incorrect association between attributes and constructor parameter.

So later mapToItem fails to create instance because of constructor arguments mismatch.

oharaandrew314 commented 1 year ago

The class you've provided is just a java bean; you should be able to use the BeanTableSchema; see this guide for using beans with the v2 sdk.

This module is intended for use with idiomatic Kotlin data classes like

data class Record(
  @DynamoKtPartitionKey val id: UUID,
  val name: String
)
skibak commented 1 year ago

Yes, it is now. I just tried to migrate from using standard annotation to more Kotlin friendly. Minimum we should detect this situation and throw error. Ideally we should invoke constructor, and then invoke remaining property setters.

To explain context: I'm trying to come with better way to support calculated fields. I wanted to put required fields in constructor, and define calculated fields (composite keys used for indexing) inside, plus override some properties setters to trigger fields calculation.

oharaandrew314 commented 1 year ago

Yeah, you're right. I'll look into an improvement.

oharaandrew314 commented 1 year ago

I've added a check to version 0.2.2