2

I have a complex obj stored in dynamo db which I'm trying to serialize using my converter class.

The problem is dynamo wrapper is still using default method and I can't get the debugger to reach my overridden method.

Is there any config I'm missing ?

@DynamoDBTable(tableName = "")
data class DynamoDataClass
(

        @DynamoDBHashKey
        var msgId: String = "",

        @DynamoDBTypeConverted(converter = MetaConverter::class)
        @DynamoDBAttribute
        var meta: Map<String, Object> = HashMap(),
        
        @DynamoDBTypeConverted(converter = PayloadConverter::class)
        @DynamoDBAttribute
        var payload: List<Map<String, Object>> = ArrayList()
){
        constructor(): this(
                msgId = "",
                meta = HashMap(),
                payload = ArrayList()
        )
}

class PayloadConverter : DynamoDBTypeConverter<String, List<Map<String, Object>>> {
        override fun convert(`object`: List<Map<String, Object>>): String {
                val itemMeta: Map<String, Object> = `object` as Map<String, Object>
                var meta: String = ""
                try {
                        if (itemMeta != null) {
                                meta = itemMeta.toString()
                        }
                } catch (e: Exception) {
                        e.printStackTrace()
                }
                return meta
        }

        override fun unconvert(s: String): List<Map<String, Object>> {
                val itemMeta = ArrayList<HashMap<String, Object>>()
                try {
                        var a = 1
                        var b = 2
                } catch (e: Exception) {
                        e.printStackTrace()
                }
                return itemMeta
        }
}

class MetaConverter : DynamoDBTypeConverter<String, Map<String, Object>> {
        override fun convert(`object`: Map<String, Object>): String {
                val itemMeta: Map<String, Object> = `object` as Map<String, Object>
                var meta: String = ""
                try {
                        if (itemMeta != null) {
                                meta = itemMeta.toString()
                        }
                } catch (e: Exception) {
                        e.printStackTrace()
                }
                return meta
        }

        override fun unconvert(s: String): Map<String, Object> {
                val itemMeta = HashMap<String, Object>()
                try {
                        var a = 1
                        var b = 2
                } catch (e: Exception) {
                        e.printStackTrace()
                }
                return itemMeta
        }
}

The body of the overridden methods are not important as I wanna first see why they are not being used. The conversion logic will be added later

  • Hi, When you are talking about your "overridden" methods, don't you mean "overriding" ones? The overridden methods are the default ones that you meant to override. Regards – Alex Dec 15 '21 at 10:49

1 Answers1

1

I've answered this question as part of my answer here.

In order to use the custom converters, you need to use the AttributeValue in your parameter type, for example, List<AttributeItem> and Map<String, AttributeItem>. Additionally, you specify the custom Converter to use with the @DynamoDBTypeConverted(converter = MyConverter::class) annotation, passing it as a value to the converter parameter.

Here's the code excerpt from my answer above:

data class MyModelDB(

    @DynamoDBHashKey(attributeName = "id")
    var id: String = "",

    @DynamoDBAttribute(attributeName = "myMap")
    @DynamoDBTypeConverted(converter = MapConverter::class)
    var myMap: Map<String, AttributeValue> = mutableMapOf(),

    @DynamoDBAttribute(attributeName = "myList")
    @DynamoDBTypeConverted(converter = ListConverter::class)
    var myList: List<AttributeItem> = mutableListOf(),
) {
    constructor() : this(id = "", myMap = MyMap(), myList = mutableListOf())
}

class MapConverter : DynamoDBTypeConverter<AttributeValue, Map<String,AttributeValue>> {
    override fun convert(map: Map<String,AttributeValue>>): AttributeValue {
        return AttributeValue().withM(map)
    }

    override fun unconvert(itemMap: AttributeValue?): Map<String,AttributeValue>>? {
        return itemMap?.m
    }

}

class ListConverter : DynamoDBTypeConverter<AttributeValue, List<AttributeValue>> {
    override fun convert(list: List<AttributeValue>): AttributeValue {
        return AttributeValue().withL(list)
    }

    override fun unconvert(itemList: AttributeValue?): List<AttributeValue>? {
        return itemList?.l
    }

}
Ryan T.
  • 197
  • 1
  • 15