-
Notifications
You must be signed in to change notification settings - Fork 3
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 #25 from llfrometa89/splitFFUID
Update docs and Refactor to split FFUUID into several type classes
- Loading branch information
Showing
9 changed files
with
127 additions
and
147 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 |
---|---|---|
@@ -1 +1,14 @@ | ||
|
||
|
||
|
||
# 0.1.5 | ||
|
||
- Update docs | ||
- Refactor to split FFUUID into several type classes [#25](https://github.com/pure4s/uuid4s/pull/) | ||
|
||
# 0.1.4 | ||
|
||
- remove redundant UUID instances (thanks to [cde1gado](https://github.com/cde1gado)) [#24](https://github.com/pure4s/uuid4s/pull/24) | ||
|
||
# 0.1.3 | ||
|
||
- Refactor to split FUUID into several type classes (thanks to [cde1gado](https://github.com/cde1gado)) [#23](https://github.com/pure4s/uuid4s/pull/23) |
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
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 |
---|---|---|
|
@@ -9,33 +9,36 @@ position: 3 | |
```scala | ||
import java.util.UUID | ||
import cats.effect.IO | ||
import cats.implicits._ | ||
import org.pure4s.uuid4s.FUUID | ||
import org.pure4s.uuid4s.implicits._ | ||
|
||
object BasicExampleMain extends App { | ||
|
||
//Parsing | ||
val uuid1: UUID = | ||
FUUID[IO].fromString("7cfb70a9-0764-4851-a28c-309393aea2eb").unsafeRunSync() | ||
// Parsing | ||
val uuid1: UUID = FUUID[IO].fromString("7cfb70a9-0764-4851-a28c-309393aea2eb").unsafeRunSync() | ||
// uuid1: java.util.UUID = 7cfb70a9-0764-4851-a28c-309393aea2eb | ||
|
||
//Generating | ||
// Generating | ||
val uuid2: UUID = FUUID[IO].random.unsafeRunSync() | ||
// uuid2: java.util.UUID = f94e2de4-1c08-4189-9664-105954589e52 | ||
} | ||
``` | ||
|
||
# Basic uuid4s-fast example | ||
```scala | ||
import java.util.UUID | ||
import cats.effect.IO | ||
import org.pure4s.uuid4s.FFUUID | ||
|
||
object BasicFastExampleMain extends App { | ||
|
||
// Parsing | ||
val uuid1: UUID = FFUUID[IO].fromString("7cfb70a9-0764-4851-a28c-309393aea2eb").unsafeRunSync() | ||
val uuid1: UUID = FUUID[IO].fromString("7cfb70a9-0764-4851-a28c-309393aea2eb").unsafeRunSync() | ||
// uuid1: java.util.UUID = 7cfb70a9-0764-4851-a28c-309393aea2eb | ||
|
||
//Comparing | ||
val result1: Boolean = uuid2 < uuid1 | ||
val result2: Boolean = uuid2 <= uuid1 | ||
val result3: Boolean = uuid1 > uuid2 | ||
val result4: Boolean = uuid1 >= uuid2 | ||
val result5: Boolean = uuid1 === uuid1 | ||
|
||
println(s" -> uuid1 = $uuid1") | ||
println(s" -> uuid2 = $uuid2") | ||
println(s" --> uuid2 < uuid1 = $result1") | ||
println(s" --> uuid2 <= uuid1 = $result2") | ||
println(s" --> uuid1 > uuid2 = $result4") | ||
println(s" --> uuid1 >= uuid2 = $result4") | ||
println(s" --> uuid1 === uuid1 = $result5") | ||
// Generating | ||
val uuid2: UUID = FFUUID[IO].random.unsafeRunSync() | ||
// uuid2: java.util.UUID = f94e2de4-1c08-4189-9664-105954589e52 | ||
} | ||
``` | ||
|
||
|
@@ -46,27 +49,17 @@ import cats.effect.{IO, Sync} | |
import cats.implicits._ | ||
import org.pure4s.uuid4s.FUUID | ||
|
||
case class ProfileResponse(userId: String, | ||
email: String, | ||
companyId: String, | ||
companyName: String, | ||
photo: String) | ||
|
||
case class ProfileResponse(userId: String, email: String, companyId: String, companyName: String, photo: String) | ||
case class Company(id: UUID, name: String) | ||
|
||
case class User(id: UUID, email: String) | ||
|
||
case class Profile(user: User, company: Company, photo: String) | ||
|
||
class ProfileConverter { | ||
def fromF[F[_]: Sync](profileResponse: ProfileResponse): F[Profile] = { | ||
for { | ||
userId <- FUUID[F].fromString(profileResponse.userId) | ||
userId <- FUUID[F].fromString(profileResponse.userId) | ||
companyId <- FUUID[F].fromString(profileResponse.companyId) | ||
} yield | ||
Profile(User(userId, profileResponse.email), | ||
Company(companyId, profileResponse.companyName), | ||
profileResponse.photo) | ||
} yield Profile(User(userId, profileResponse.email), Company(companyId, profileResponse.companyName), profileResponse.photo) | ||
} | ||
} | ||
|
||
|
@@ -82,61 +75,27 @@ class ProfileRestRepository[F[_]: Sync](converter: ProfileConverter) | |
* This method simulates the call external API Rest. Use Sync[F].delay when perform request to API Rest real | ||
*/ | ||
def performRequest: F[Option[ProfileResponse]] = | ||
Option( | ||
ProfileResponse("7cfb70a9-0764-4851-a28c-309393aea2eb", | ||
"[email protected]", | ||
"e7f86fa0-ff91-47ba-baff-0954957af20f", | ||
"pure4s", | ||
"http://example.com/example.jpg")).pure[F] | ||
Option(ProfileResponse("7cfb70a9-0764-4851-a28c-309393aea2eb", | ||
"[email protected]", | ||
"e7f86fa0-ff91-47ba-baff-0954957af20f", | ||
"pure4s", | ||
"http://example.com/example.jpg")).pure[F] | ||
|
||
for { | ||
maybeProfileResponse <- performRequest | ||
maybeProfile <- maybeProfileResponse.traverse(converter.fromF[F]) | ||
maybeProfile <- maybeProfileResponse.traverse(converter.fromF[F]) | ||
} yield maybeProfile | ||
} | ||
} | ||
|
||
object ComplexExampleMain extends App { | ||
val converter = new ProfileConverter | ||
val repository: ProfileRepository[IO] = | ||
new ProfileRestRepository[IO](converter) | ||
val maybeProfile = | ||
repository.findByEmail("[email protected]").unsafeRunSync() | ||
val repository: ProfileRepository[IO] = new ProfileRestRepository[IO](converter) | ||
val maybeProfile = repository.findByEmail("[email protected]").unsafeRunSync() | ||
println(s"-> maybeProfile = $maybeProfile") | ||
} | ||
``` | ||
|
||
# Basic uuid4s-fast example | ||
```scala | ||
import java.util.UUID | ||
|
||
import cats.effect.IO | ||
import cats.implicits._ | ||
import org.pure4s.uuid4s.FFUUID | ||
import org.pure4s.uuid4s.implicits._ | ||
|
||
object BasicFastExampleMain extends App { | ||
|
||
//Parsing | ||
val uuid1: UUID = | ||
FFUUID[IO].fromString("7cfb70a9-0764-4851-a28c-309393aea2eb").unsafeRunSync() | ||
|
||
//Generating | ||
val uuid2: UUID = FFUUID[IO].random.unsafeRunSync() | ||
|
||
//Comparing | ||
val result1: Boolean = uuid2 < uuid1 | ||
val result2: Boolean = uuid2 <= uuid1 | ||
val result3: Boolean = uuid1 > uuid2 | ||
val result4: Boolean = uuid1 >= uuid2 | ||
val result5: Boolean = uuid1 === uuid1 | ||
|
||
println(s" -> uuid1 = $uuid1") | ||
println(s" -> uuid2 = $uuid2") | ||
println(s" --> uuid2 < uuid1 = $result1") | ||
println(s" --> uuid2 <= uuid1 = $result2") | ||
println(s" --> uuid1 > uuid2 = $result4") | ||
println(s" --> uuid1 >= uuid2 = $result4") | ||
println(s" --> uuid1 === uuid1 = $result5") | ||
//maybeProfile: Option[ProfileResponse] = Some(Profile(User(7cfb70a9-0764-4851-a28c-309393aea2eb,[email protected]), | ||
Company(e7f86fa0-ff91-47ba-baff-0954957af20f,pure4s), | ||
http://example.com/example.jpg)) | ||
} | ||
``` |
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
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 |
---|---|---|
@@ -1,18 +1,20 @@ | ||
package org.pure4s.uuid4s | ||
|
||
import cats.effect.Sync | ||
import java.util.UUID | ||
import com.eatthepath.uuid.FastUUID | ||
|
||
import cats.effect.Sync | ||
|
||
/** | ||
* Tagless final implementation of a FFUUID. | ||
*/ | ||
object FFUUID { | ||
|
||
def apply[F[_]](implicit F: FUUID[F]): FUUID[F] = F | ||
implicit def sync[F[_]: Sync]: FUUID[F] = new FUUID[F] { | ||
override def fromString(value: String): F[UUID] = Sync[F].delay(FastUUID.parseUUID(value)) | ||
override def toString(uuid: UUID): F[String] = Sync[F].delay(FastUUID.toString(uuid)) | ||
override def fromUUID(uuid: UUID): F[UUID] = Sync[F].pure(uuid) | ||
override def random: F[UUID] = Sync[F].delay(UUID.randomUUID) | ||
|
||
implicit def instance[F[_]: Sync]: FUUID[F] = new FUUID[F] { | ||
override def fromString(value: String): F[UUID] = FFromString[F].fromString(value) | ||
override def fromUUID(uuid: UUID): F[UUID] = FromUUID[F].fromUUID(uuid) | ||
override def toString(uuid: UUID): F[String] = FToString[F].toString(uuid) | ||
override def random: F[UUID] = Random[F].random | ||
} | ||
} |
Oops, something went wrong.