Skip to content

Instantly share code, notes, and snippets.

@chemikadze
Created November 2, 2012 21:04
Show Gist options
  • Save chemikadze/4004286 to your computer and use it in GitHub Desktop.
Save chemikadze/4004286 to your computer and use it in GitHub Desktop.
case class Foo(id: Key = genKey)
case class Bar(x: String, foo: Key = null, id: Key = genKey)
// in tests
Bar(testData, testFoo.id, testBarId)
// somewhere in logic
Bar(sensitiveData, foo.id)
case class Foo(id: Id[Foo] = genId)
case class Bar(x: String, foo: Id[Foo] = null, id: Id[Bar] = genId)
// in tests
Bar(testData, testFoo.id, testBarId)
// somewhere in logic
Bar(sensitiveData, foo.id)
scala> import shapeless.TypeOperators._
import shapeless.TypeOperators._
scala> type MyString = Newtype[String, Any]
defined type alias MyString
scala> val x: MyString = newtype("test")
x: MyString = test
scala> x.getClass
res4: java.lang.Class[_ <: AnyRef] = class java.lang.String
import shapeless._
import shapeless.TypeOperators._
trait Stub[T] {}
type Id[T] = Newtype[Key, Stub[T]]
def Id[T](s: Key): Id[T] = newtype(s)
case class Foo(id: Key)
case class Baz(id: Key)
case class Bar(x: String, baz: Key = null, foo: Key = null, id: Key = genKey)
// in tests
Bar(testData, testFoo.id, testBarId) // messed up!
// somewhere in logic
Bar(sensitiveData, foo.id) // messed up!
case class Foo(id: Id[Foo])
case class Baz(id: Id[Baz])
case class Bar(x: String, baz: Id[Baz] = null, foo: Id[Bar] = null, id: Id[Bar] = genKey)
// in tests
Bar(testData, testFoo.id, testBarId) // compile error!
// somewhere in logic
Bar(sensitiveData, foo.id) // compile error!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment