Skip to content

Instantly share code, notes, and snippets.

@zergin
Created February 3, 2017 16:54
Show Gist options
  • Save zergin/8923526af5eefe8499b392fc0374ff3a to your computer and use it in GitHub Desktop.
Save zergin/8923526af5eefe8499b392fc0374ff3a to your computer and use it in GitHub Desktop.
Scala Issue?
import org.json4s._
import org.json4s.native.JsonMethods._
sealed trait Entry {
def value(): Any
def key(): String
def public(implicit dicts: Map[String, Dictionary[Entry]]): Boolean = true
}
case class ContextUriEntry(uri: String, context: String) extends Entry {
override def key(): String = uri
override def value(): String = context
override def public(implicit dicts: Map[String, Dictionary[Entry]]): Boolean = {
context.trim.nonEmpty
}
}
abstract class Dictionary[+T <: Entry: Manifest]() extends Map[String, T] {
implicit val formats = DefaultFormats
val data: Map[String, T] = parse("""[{"uri": "http://rdf/tag/Tag", "context": "Context Content"} ]""")
.extract[List[T]]
.map(e => (e.key().toLowerCase, e))
.toMap[String, T]
// Map implementation
def get(key: String): Option[T] = data.get(key)
def iterator: Iterator[(String, T)] = data.iterator
def +[V1 >: T](kv: (String, V1)): Map[String, V1] = data + kv
def -(key: String): Map[String, T] = data - key
}
object Dictionary {
case class ContextUriDictionary() extends Dictionary[ContextUriEntry]
def getByName(name: String) = name match { case "ctx" => ContextUriDictionary() }
}
object Loader {
val dicts: Map[String, Dictionary[Entry]] = Map("ctx" -> Dictionary.getByName("ctx"))
}
scala> :paste /tmp/issue.scala
Pasting file /tmp/issue.scala...
import org.json4s._
import org.json4s.native.JsonMethods._
defined trait Entry
defined class ContextUriEntry
defined class Dictionary
defined object Dictionary
defined object Loader
scala> implicit val dicts = Loader.dicts
dicts: Map[String,Dictionary[Entry]] = Map(ctx -> Map(http://rdf/tag/tag -> ContextUriEntry(http://rdf/tag/Tag,Context Content)))
scala> dicts("ctx").data.filter { case(u,v) => v.public }
java.util.NoSuchElementException: key not found: Context Content
at scala.collection.immutable.Map$Map1.apply(Map.scala:108)
at ContextUriEntry.public(/tmp/issue.scala:51)
at .$anonfun$res8$1(<console>:38)
at .$anonfun$res8$1$adapted(<console>:38)
at scala.collection.TraversableLike.$anonfun$filterImpl$1(TraversableLike.scala:248)
at scala.collection.immutable.Map$Map1.foreach(Map.scala:120)
at scala.collection.TraversableLike.filterImpl(TraversableLike.scala:247)
at scala.collection.TraversableLike.filterImpl$(TraversableLike.scala:245)
at scala.collection.AbstractTraversable.filterImpl(Traversable.scala:104)
at scala.collection.TraversableLike.filter(TraversableLike.scala:259)
at scala.collection.TraversableLike.filter$(TraversableLike.scala:259)
at scala.collection.AbstractTraversable.filter(Traversable.scala:104)
... 39 elided
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment