Skip to content

Instantly share code, notes, and snippets.

@rrichardson
Created October 19, 2011 02:49
Show Gist options
  • Save rrichardson/1297364 to your computer and use it in GitHub Desktop.
Save rrichardson/1297364 to your computer and use it in GitHub Desktop.
trait SlashemField[V, M <: Record[M]] extends OwnedField[M] {
def valueBoxFromAny(a: Any): Box[V] = {
try {
Full(a.asInstanceOf[V])
} catch {
case _ => Empty
}
}
}
class SlashemObjectIdField[T <: Record[T]](owner: T) extends ObjectIdField[T](owner) with SlashemField[ObjectId, T] {
override def valueBoxFromAny(a: Any) : Box[ObjectId]= {
try {
a match {
case "" => Empty
case s: String => Full(new ObjectId(s))
case i: ObjectId => Full(i)
case _ => Empty
}
} catch {
case _ => Empty
}
}
}
// error
[error] /slashem/src/main/scala/com/foursquare/slashem/Schema.scala:567: overriding method valueBoxFromAny in class ObjectIdField of type (a: Any)net.liftweb.common.Box[org.bson.types.ObjectId] with Serializable;
[error] method valueBoxFromAny has incompatible type
[error] override def valueBoxFromAny(a: Any) : Box[ObjectId]= {
[error] ^
[error] one error found
@rrichardson
Copy link
Author

class SlashemIntListField[T <: Record[T]](owner: T) extends IntListFieldT with SlashemField[List[Int], T] {
override def valueBoxFromAny(a: Any) ={
try {
a match {
case "" => Empty
case ar: Array[Int] => Full(ar.toList)
case ar: Array[Integer] => Full(ar.toList.map(x=>x.intValue))
case s: String => Full(s.split(" ").map(x => x.toInt).toList)
case _ => Empty
}
} catch {
case _ => Empty
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment