Skip to content

Commit

Permalink
Merge pull request #221 from ChristopherDavenport/addTaglessModule
Browse files Browse the repository at this point in the history
Add FUUIDGen Algebra
  • Loading branch information
ChristopherDavenport authored Feb 10, 2020
2 parents 06ed622 + 9aed937 commit 1d08b61
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ target/
tags
.metals
.bloop
project/metals.sbt
9 changes: 3 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ lazy val core = crossProject(JSPlatform, JVMPlatform)
name := "fuuid"
)


lazy val coreJS = core.js
lazy val coreJVM = core.jvm

Expand Down Expand Up @@ -95,12 +94,10 @@ lazy val contributors = Seq(
lazy val commonSettings = Seq(
organization := "io.chrisdavenport",

scalaVersion := "2.13.0",
crossScalaVersions := Seq(scalaVersion.value, "2.12.8"),

scalacOptions += "-Yrangepos",
scalaVersion := "2.13.1",
crossScalaVersions := Seq(scalaVersion.value, "2.12.10"),

addCompilerPlugin("org.typelevel" % "kind-projector" % "0.10.3" cross CrossVersion.binary),
addCompilerPlugin("org.typelevel" % "kind-projector" % "0.11.0" cross CrossVersion.full),
addCompilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.1"),
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
Expand Down
46 changes: 46 additions & 0 deletions modules/core/src/main/scala/io/chrisdavenport/fuuid/FUUIDGen.scala
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)
}
}

0 comments on commit 1d08b61

Please sign in to comment.