title: Firebase: clean way for using enum fields in Kotlin/Java?
body: <p>My data on firebase uses many fields which have string type, but really are enum values (which I check in my validation rules). To download the data into my Android app, <a href="https://firebase.google.com/docs/database/android/read-and-write" rel="noreferrer">following the guide</a>, the field must be a basic <code>String</code>. I know I can work around this with a second (excluded) field which is an enum, and set this basing on the string value. A short example:</p>
  
  <pre><code>class UserData : BaseModel() {
      val email: String? = null
      val id: String = ""
      val created: Long = 0
      // ... more fields omitted for clarity
      @Exclude
      var weightUnitEnum: WeightUnit = WeightUnit.KG
      var weightUnit: String
          get() = weightUnitEnum.toString()
          set(value) { weightUnitEnum = WeightUnit.fromString(value) }
  }
  
  enum class WeightUnit(val str: String) {
      KG("kg"), LB("lb");
      override fun toString(): String = str
      companion object {
          @JvmStatic
          fun fromString(s: String): WeightUnit = WeightUnit.valueOf(s.toUpperCase())
      }
  }
  </code></pre>
  
  <p>Now, while this works, it's not really clean:</p>
  
  <ul>
  <li>The <code>enum class</code> itself is (1) kinda long for an
  enum, (2) the insides are repeated for every enum. And I have more of them.</li>
  <li>It's not only enums, the <code>created</code> field above is really a timestamp,
  not a <code>Long</code>.</li>
  <li>Each model uses these enum fields a lot of times, which bloats the model classes with repeatable code...</li>
  <li>The helper field/functions are getting much worse/longer for fields with types such as <code>Map&lt;SomeEnum, Timestamp&gt;</code>...</li>
  </ul>
  
  <p>So, is there any way to do this properly? Some library maybe? Or some way to write a magic "field wrapper" that would automatically convert strings to enums, or numbers to timestamps, and so on, but is still compatible with Firebase library for getting/setting data?</p>
  
  <p>(Java solutions are welcome too :) )</p>