kotlin-orm/ktorm-ksp

带泛型的 converter怎么写

onXoot opened this issue · 2 comments

表里面有个json字段,类似这样

interface Event : Entity<Event> {
    var id: Long
    var info: Map<String, *>
}

object Events : Table<Event>("events") {
    val id = long("id").primaryKey().bindTo { it.id }
    val info = json<Map<String, *>>("info").bindTo { it.info }
}

使用 ksp 该怎么写converter呢

@org.ktorm.ksp.api.Table(tableName = "events")
data class Event(
    @PrimaryKey
    var id: Long,
    @Column(converter = JsonTypeConvertor::class)
    var info: Map<String, *>
)

我试了

object JsonTypeConvertor: SingleTypeConverter<Map<String, *>> {
    override fun convert(
        table: BaseTable<*>,
        columnName: String,
        propertyType: KClass<Map<String, *>>
    ): Column<Map<String, *>> = with(table) { json(columnName) }
}

以及

object JsonTypeConvertor : MultiTypeConverter {
    override fun <T : Any> convert(
        table: BaseTable<*>,
        columnName: String,
        propertyType: KClass<T>
    ): Column<T> {
        return with(table) {
            varchar(columnName).transform(
                { JsonUtil.deSerialize(it, propertyType.java)!! },
                { JsonUtil.serialize(it) }
            )
        }
    }
}

自动生成的代码都是

public object Events : BaseTable<Event>(tableName = "events",  entityClass = Event::class) {
    public val id: Column<Long> = long("id").primaryKey()
    public val info: Column<Map> = JsonTypeConvertor.convert(this,"info",Map::class)
}

而且会报错
public val info: Column<Map>:
2 type arguments expected for interface Map<K, out V>

这该怎么写呢

这是一个bug,后续我会修复它

v1.0.0-RC3已修复