Browse Source

server: Add Blockhash model

scalafmt-draft
Alexis Hernandez 7 years ago
parent
commit
8720dc9447
  1. 31
      server/app/com/xsn/explorer/models/Blockhash.scala

31
server/app/com/xsn/explorer/models/Blockhash.scala

@ -0,0 +1,31 @@
package com.xsn.explorer.models
import play.api.libs.json._
class Blockhash private (val string: String) extends AnyVal
object Blockhash {
private val pattern = "^[a-f0-9]{64}$".r.pattern
def from(string: String): Option[Blockhash] = {
val lowercaseString = string.toLowerCase
if (pattern.matcher(lowercaseString).matches()) {
Some(new Blockhash(lowercaseString))
} else {
None
}
}
implicit val reads: Reads[Blockhash] = Reads { json =>
json.validate[String].flatMap { string =>
from(string)
.map(JsSuccess.apply(_))
.getOrElse {
JsError.apply("Invalid blockhash")
}
}
}
implicit val writes: Writes[Blockhash] = Writes { obj => JsString(obj.string) }
}
Loading…
Cancel
Save