objectbox / objectbox/objectbox-java

First Class Support for Enums

未关闭
#287 3 条评论 11 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

enhancement
主要语言
Java
星标
4.6k
派生
311
PR 合并指标
30 天内没有已合并 PR

描述

I can see that there's limited support for enums here but this is such a huge barrier to getting started. I don't want to have to write a converter for each enum type that I use in my code.

I propose first class support for enums using the following mechanism:

This can be repeated for all the natively supported types, and should now be able to be handled internally.

interface EnumInt {
  val value: Int
}

interface EnumString {
  val value: String
}

inline fun <reified T> enumFromInt(value: Int?): T?
    where T : EnumInt, T : Enum<T> =
    value?.let { v ->
      enumValues<T>().let { values ->
        values.find {
          v == it.value
        } ?: throw IllegalArgumentException("Invalid int value: $v")
      }
    }

inline fun <reified T> enumFromString(value: String?): T?
    where T : EnumString, T : Enum<T> =
    value?.let { v ->
      enumValues<T>().let { values ->
        values.find {
          v == it.value
        } ?: throw IllegalArgumentException("Invalid string value: $v")
      }
    }

enum class Type(override val value: String) : EnumString {
      UNKNOWN("unknown"),
      IMAGE("image"),
      VIDEO("video")
}

enum class Status(override val value: Int) : EnumInt {
      ERROR(-3),
      UNRECOVERABLE_ERROR(-2),
      NONE(-1),
      DOWNLOADING(1),
      DOWNLOADED(2),
      UPLOADING(3),
      FINISHED(0)
}

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先查看 issue 中链接的自定义类型文档,以及现有的 enum 和自定义类型处理方式。完成的标准是:enum 获得一等持久化支持,无需为每种 enum 类型提供 converter,并包括所提议的整数值和字符串值映射。

由索引模型根据 Issue 内容生成。

评估

技术栈
kotlin
领域
databases
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。