-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package _newage | ||
|
||
import cats.implicits._ | ||
|
||
import java.util.UUID | ||
import scala.util.Try | ||
import cats.Show | ||
|
||
object PrefixedUuidPlayground2 extends App { | ||
|
||
private trait IdExtractor[A] extends (A => String) | ||
|
||
abstract class IdBase[A](private val prefix: String, private val idFn: IdExtractor[A]) { | ||
implicit val show: Show[A] = (a: A) => s"$prefix${idFn(a)}" | ||
|
||
def unapply(raw: String): Option[UUID] = | ||
Option(raw) | ||
.collect { case s if s.startsWith(prefix) => s.substring(prefix.length) } | ||
.flatMap(s => Try(UUID.fromString(s)).toOption) | ||
|
||
def is(raw: String): Boolean = unapply(raw).fold(false)(_ => true) | ||
} | ||
|
||
case class CarId(id: UUID) extends AnyVal | ||
object CarId extends IdBase[CarId]("CAR-", _.id.toString) | ||
|
||
// usecase | ||
val s = CarId(UUID.randomUUID()).show | ||
println(s) | ||
|
||
// matching | ||
def check(raw: String): Either[String, UUID] = raw match { | ||
case CarId(id) => id.asRight | ||
case _ => "not a car id".asLeft | ||
} | ||
|
||
Seq( | ||
s, | ||
"whatever" | ||
).map(x => check(x) -> CarId.is(x)) | ||
.foreach(println) | ||
} |