objectbox / objectbox/objectbox-java
Support Kotlin inline classes for ids
未关闭
还没有人认领这个 Issue。
enhancement
- 主要语言
- Java
- 星标
- 4.6k
- 派生
- 311
- PR 合并指标
- 30 天内没有已合并 PR
描述
Inline classes are a good way to add compile time type safety to database queries. They would prevent error caused by querying a box with the wrong id type.
@Entity
data class EntityA(
val str: String,
val id: EntityAId
)
inline class EntityAId(val value: Long)
@Entity
data class EntityB(
val str: String,
val id: EntityBId
)
inline class EntityBId(val value: Long)
val unsafeId: Long = getSomeId() //returns an id meant for EntityA
boxStore.boxFor<EntityB>()[unsafeId] //Runtime error
val safeId: EntityAId = getSomeId()
boxStore.boxFor<EntityB>()[safeId] //Compile time error
And the unsafe call only crashes if you're lucky, other times it might just seem like the database is returning garbage data, since you queried it with the wrong ids.
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从使用 inline classes(例如 EntityAId 和 EntityBId)的 Kotlin 示例开始,跟踪 boxStore.boxFor()[id] 调用。完成的标准是,inline-class ID 在编译时保持 entity ID 类型之间的区分,并在编译期间拒绝类型错误的 ID。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- kotlin
- 领域
- databases
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 30/100