ktorm icon indicating copy to clipboard operation
ktorm copied to clipboard

KSP: Keep entity's deprecation annotations in generated table definitions.

Open vincentlauvlwj opened this issue 1 year ago • 0 comments

For example:

@Table
interface Employee: Entity<Employee> {
    var id: Long
    var name: String
    @Deprecated("Use name instead.")
    var nickname: String
}

The generated file should look like:

// Auto-generated by ktorm-ksp-compiler, DO NOT EDIT!
@file:Suppress("RedundantVisibilityModifier", "DEPRECATION")

import org.ktorm.database.Database
import org.ktorm.entity.Entity
import org.ktorm.entity.EntitySequence
import org.ktorm.entity.sequenceOf
import org.ktorm.ksp.annotation.Undefined
import org.ktorm.schema.Column
import org.ktorm.schema.Table
import org.ktorm.schema.long
import org.ktorm.schema.varchar
import kotlin.Long
import kotlin.String
import kotlin.Suppress

/**
 * Table employee.
 */
public open class Employees(alias: String?) : Table<Employee>("employee", alias) {
    /**
     * Column id.
     */
    public val id: Column<Long> = long("id").bindTo { it.id }

    /**
     * Column name.
     */
    public val name: Column<String> = varchar("name").bindTo { it.name }

    /**
     * Column nickname.
     */
    @Deprecated("Use name instead.")
    public val nickname: Column<String> = varchar("nickname").bindTo { it.nickname }

    /**
     * Return a new-created table object with all properties (including the table name and columns and
     * so on) being copied from this table, but applying a new alias given by the parameter.
     */
    public override fun aliased(alias: String): Employees = Employees(alias)

    /**
     * The default table object of employee.
     */
    public companion object : Employees(alias = null)
}

/**
 * Return the default entity sequence of [Employees].
 */
public val Database.employees: EntitySequence<Employee, Employees> get() = this.sequenceOf(Employees)

/**
 * Create an entity of [Employee] and specify the initial values for each property, properties that
 * doesn't have an initial value will leave unassigned.
 */
public fun Employee(
    id: Long? = Undefined.of(),
    name: String? = Undefined.of(),
    nickname: String? = Undefined.of()
): Employee {
    val entity = Entity.create<Employee>()
    if (id !== Undefined.of<Long>()) {
        entity.id = id ?: error("`id` should not be null.")
    }
    if (name !== Undefined.of<String>()) {
        entity.name = name ?: error("`name` should not be null.")
    }
    if (nickname !== Undefined.of<String>()) {
        entity.nickname = nickname ?: error("`nickname` should not be null.")
    }
    return entity
}

/**
 * Return a deep copy of this entity (which has the same property values and tracked statuses), and
 * alter the specified property values.
 */
public fun Employee.copy(
    id: Long? = Undefined.of(),
    name: String? = Undefined.of(),
    nickname: String? = Undefined.of()
): Employee {
    val entity = this.copy()
    if (id !== Undefined.of<Long>()) {
        entity.id = id ?: error("`id` should not be null.")
    }
    if (name !== Undefined.of<String>()) {
        entity.name = name ?: error("`name` should not be null.")
    }
    if (nickname !== Undefined.of<String>()) {
        entity.nickname = nickname ?: error("`nickname` should not be null.")
    }
    return entity
}

/**
 * Return the value of [Employee.id].
 */
public operator fun Employee.component1(): Long = this.id

/**
 * Return the value of [Employee.name].
 */
public operator fun Employee.component2(): String = this.name

/**
 * Return the value of [Employee.nickname].
 */
@Deprecated("Use name instead.")
public operator fun Employee.component3(): String = this.nickname

vincentlauvlwj avatar Aug 08 '24 03:08 vincentlauvlwj