示例:
opaque type UserName = String
此版本自动连载:
case class UserName(value: String) extends AnyVal
我不知道这是否是最优雅的方式,因为我是新手
circe
:
opaque type UserName = String
object UserName:
def apply(s: String): UserName = new UserName(s)
given Encoder[UserName] = new Encoder[UserName]:
def apply(a: UserName): Json = Json.fromString(a.toString)
given Decoder[UserName] = new Decoder[UserName]:
def apply(c: HCursor): Decoder.Result[UserName] =
c.as[String].map{ UserName(_)}