-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #221 from ChristopherDavenport/addTaglessModule
Add FUUIDGen Algebra
- Loading branch information
Showing
3 changed files
with
50 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ target/ | |
tags | ||
.metals | ||
.bloop | ||
project/metals.sbt |
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
46 changes: 46 additions & 0 deletions
46
modules/core/src/main/scala/io/chrisdavenport/fuuid/FUUIDGen.scala
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,46 @@ | ||
package io.chrisdavenport.fuuid | ||
|
||
import java.util.UUID | ||
import cats.implicits._ | ||
import cats.effect.Sync | ||
|
||
/** | ||
* This trait is an F-algebra representation of the ability to generate FUUID's. | ||
* | ||
* At some edge a Sync is required in order to populate the randomness when required. | ||
*/ | ||
@scala.annotation.implicitNotFound("""Cannot find implicit value for FUUIDGen[${F}]. | ||
Building this implicit value depends on having an implicit | ||
Sync[${F}] or some equivalent type.""") | ||
trait FUUIDGen[F[_]]{ | ||
/** | ||
* Creates a Random FUUID | ||
*/ | ||
def random: F[FUUID] | ||
/** | ||
* Creates an FUUID from a String, if it is valid | ||
*/ | ||
def fromString(s: String): F[FUUID] | ||
/** | ||
* Creates an FUUID from a UUID | ||
*/ | ||
def fromUUID(uuid: UUID): F[FUUID] | ||
/** | ||
* Creates a new name-based UUIDv5. NOTE: Not implemented for Scala.js! | ||
**/ | ||
def nameBased(namespace: FUUID, name: String): F[FUUID] | ||
} | ||
|
||
object FUUIDGen { | ||
def apply[F[_]](implicit ev: FUUIDGen[F]): FUUIDGen[F] = ev | ||
|
||
// Sync f => class FUUIDGen f | ||
implicit def instance[F[_]: Sync]: FUUIDGen[F] = new SyncFUUIDGen[F] | ||
|
||
private class SyncFUUIDGen[F[_]: Sync] extends FUUIDGen[F]{ | ||
def random: F[FUUID] = FUUID.randomFUUID[F] | ||
def fromString(s: String): F[FUUID] = FUUID.fromStringF[F](s) | ||
def fromUUID(uuid: UUID): F[FUUID] = FUUID.fromUUID(uuid).pure[F] | ||
def nameBased(namespace: FUUID, name: String): F[FUUID] = FUUID.nameBased[F](namespace, name) | ||
} | ||
} |