Last active
March 31, 2016 16:33
-
-
Save ovstetun/1af0514a7ee4087548ffa2c5d7f3cb9f to your computer and use it in GitHub Desktop.
binding trait instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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