objectbox / objectbox/objectbox-java

First Class Support for Enums

Open
#287 3 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
4.6k
Forks
311
PR merge metrics
No merged PRs in 30d

Description

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)
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the custom types documentation linked in the issue and the existing enum and custom-type handling. Done means enums receive first-class persistence support without a converter for each enum type, including the proposed integer and string value mappings.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.