来自 AWS Java SDK 2.x 的带有 StaticTableSchema 和增强型 DynamoDB 客户端的嵌套 bean

Nested beans with StaticTableSchema & enhanced DynamoDB client from AWS Java SDK 2.x

如何为嵌套 bean 创建 StaticTableSchema(例如此处的 items 属性)?

data class Order(
    var id: Long = 0L,
    var items: List<OrderLineItem> = emptyList(),
    …
)
data class OrderLineItem(
    var id: Long = 0L,
    var price: BigDecimal = 0.0.toBigDecimal(),
    …
)

首先,为嵌套 bean 创建一个 TableSchema 实现。可以是 StaticTableSchema or a BeanTableSchema:

val orderLineItemStaticTableSchema = TableSchema
    .builder(OrderLineItem::class.java)
    .newItemSupplier(::OrderLineItem)
    .addAttribute(Long::class.java) {
        it.name("id").getter(OrderLineItem::id::get).setter(OrderLineItem::id::set)
    }
    .addAttribute(BigDecimal::class.java) {
        it.name("price").getter(OrderLineItem::price::get).setter(OrderLineItem::price::set)
    }
    .build()

// or

val orderLineItemBeanTableSchema = TableSchema.fromBean(OrderLineItem::class.java)

然后,将此架构实例与 EnhancedType.documentOf():

一起使用
val tableSchema = TableSchema
    .builder(Order::class.java)
    .newItemSupplier(::Order)
    .addAttribute(Long::class.java) {
        it.name("id").getter(Order::id::get).setter(Order::id::set)
    }
    .addAttribute(
        EnhancedType.listOf(
            EnhancedType.documentOf(OrderLineItem::class.java, orderLineItemTableSchema)
        )
    ) {
        it.name("items").getter(Order::items::get).setter(Order::items::set)
    }
    .build()