Skip to content

Instantly share code, notes, and snippets.

@ovstetun
Last active March 31, 2016 16:33
Show Gist options
  • Save ovstetun/1af0514a7ee4087548ffa2c5d7f3cb9f to your computer and use it in GitHub Desktop.
Save ovstetun/1af0514a7ee4087548ffa2c5d7f3cb9f to your computer and use it in GitHub Desktop.
binding trait instance
trait ResId {
def toASCIIString: String
}
object ResId {
def apply(uri: String): ResId = new StringUri(uri)
def apply(uri: URI): ResId = new StringUri(uri.toString)
private def validated(uri: String): String = URI.create(uri).toString
case class StringUri(value: String) extends ResId {
override val toASCIIString: String = validated(value)
}
}
def convert(resId: ResId): Rdf#URI = URI(resId.toASCIIString)
implicit val resIdBinder: PGBinder[Rdf, ResId] = new PGBinder[Rdf, ResId] {
override def toPG(t: ResId): PointedGraph[Rdf] = convert(t).toPG
override def fromPG(pointed: PointedGraph[Rdf]): Try[ResId] = pointed.pointer.fold (
uri => Success(ResId(uri.getString)),
_ => Failure(new IllegalArgumentException),
_ => Failure(new IllegalArgumentException)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment