scalapenos / stamina

Schema evolution for akka-persistence

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A type for key would be nice. (instead of String)

RayRoestenburg opened this issue · comments

Same for version. Int is a bit much for that as well. And you don't want negative values right?

Using a type for keys instead of a String sounds ok but I thought about it a bit a while ago and I couldn't come up with any good reason why you'd want that. Care to suggest one?

Since the only version numbers are generated internally through type-safe Vn classes, the Int doesn't seem like such a huge risk of generating negative versions. What would be the alternative?

Like a nonemptystring nothing fancy
On Sun 8 Feb 2015 at 18:38 Age Mooij notifications@github.com wrote:

Using a type for keys instead of a String sounds ok but I thought about it
a bit a while ago and I couldn't come up with any good reason why you'd
want that. Care to suggest one?

Since the only version numbers are generated internally through type-safe
Vn classes, the Int doesn't seem like such a huge risk of generating
negative versions. What would be the alternative?


Reply to this email directly or view it on GitHub
#4 (comment).

I looked around and there doesn't seem to have been anyone online who has created a compile-time checker for non-emptiness in Strings.

The easy answer would be to just check at runtime but where would be the fun in that? NonEmptyString macro coming up I guess... PRs welcome ;)

How about this:

screen shot 2015-02-15 at 11 47 45

Almost done with the commit and I learned me some macro stuff again ;)

  case class Key(value: String) {
    import java.nio.charset.StandardCharsets.UTF_8
    def getUtf8Bytes: Array[Byte] = value.getBytes(UTF_8)
    override def toString = value
  }

  object KeyMacro {
    import scala.reflect.macros.Context

    def nonEmptyStringToKey(c: Context)(in: c.Tree): c.Tree = {
      import c.universe._
      val q"${ s: String }" = q"${in}"
      if (s.isEmpty) c.abort(c.enclosingPosition, s"Persistence keys must be non-empty!")
      else q"Key($in)"
    }
  }

  implicit def nonEmptyStringToKey(in: String): Key = macro KeyMacro.nonEmptyStringToKey

Notice the unlifting to get back a String to check for emptiness. This will be in stamina soon.

I've been trying to get the Macros to be backwards compatible with 2.10 but that's pretty damn tricky it seems 😢

Closing this for now. There doesn't seem to enough reason or interest for typing persister keys. Perhaps the public beta release will bring back similar requests at a later date.