Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement UUIDGen capability trait. #1772

Merged
merged 4 commits into from
Jul 7, 2021
Merged

Conversation

Wosin
Copy link
Contributor

@Wosin Wosin commented Mar 9, 2021

This PR implements UUIDGen capability trait as discussed in #1741.

The implementation is as simple as possible, not sure if we need any tests for that, but if so I will be happy to add that.
The issue mentions introducing potential literal macro, IMHO it's not a very commonly used feature, so I've just decided to skip it but If we want to have it anyway, again happy to add.

@vasilmkd
Copy link
Member

Make sure to run compile in sbt, it will generate the needed license header. 😄

@vasilmkd
Copy link
Member

I guess you're going to have to run scalafmtAll in sbt as well. While we're at it, do you mind adding a basic scaladoc to this file? You can look at the other modules in cats/effect/std for inspiration.

@Wosin
Copy link
Contributor Author

Wosin commented Mar 10, 2021

I guess you're going to have to run scalafmtAll in sbt as well. While we're at it, do you mind adding a basic scaladoc to this file? You can look at the other modules in cats/effect/std for inspiration.

Yeah, something actually went wrong with my git, so it was left in some weird half-state :<

}

object UUIDGen {
def randomUUID[F[_]: Sync]: F[UUID] = Sync[F].delay(UUID.randomUUID())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 methods should require F[_] : UUIDGen bound.

And the companion object should have:

implicit def fromSync[F[_] : Sync]: UUIDGen[F] = ???

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, is that what You meant ? I think I didn't fully understand what You meant .

def randomUUID[F[_]: Sync]: F[UUID] = Sync[F].delay(UUID.randomUUID())
def fromString[F[_]: Sync](uuidString: String): F[UUID] =
Sync[F].delay(UUID.fromString(uuidString))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add this to the companion?

  def randomString[F[_]: Functor: UUIDGen]: F[String] = randomUUID[F].map(_.toString)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, sounds reasonable.

* @param uuidString a standard String UUID representation
* @return A UUID with specified value
*/
def fromString(uuidString: String): F[UUID]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This trait is a UUID generator. Does this method reflect the purpose of the trait? And if so, why is there no method for making a UUID from a byte array?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe we should rename the trait and add some methods?

I also don't like the Gen suffix because it's used in kernel (yet, as a prefix) for generic type classes like GenConcurrent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was kinda basing on https://github.com/davenverse/fuuid here. I had an idea to implement the byte array version too, but decided to skip it for now.

IMHO, if we are doing this it should cover all basic use-cases so I think including at least a fromString would be a good idea, but happy to discuss this :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to think this is a bit out of scope. At the very least, I would have expected it to return Option[UUID].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will drop it and stick to random and randomString

* @param uuidString a standard String UUID representation
* @return A UUID with specified value
*/
def fromString(uuidString: String): F[UUID]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to think this is a bit out of scope. At the very least, I would have expected it to return Option[UUID].

* Generates a UUID in a pseudorandom manner and returns String representation of this UUID.
* @return randomly generated UUID string
*/
def randomString: F[String]
Copy link
Contributor

@catostrophe catostrophe Mar 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that the trait would have only def randomUUID: F[UUID].
While randomString would be a method on the object.

Like:

object UUIDGen {
  def apply[F[_]](implicit ev: UUIDGen[F]): UUIDGen[F] = ev

  implicit def fromSync[F[_]: Sync]: UUIDGen[F] = new UUIDGen[F] {
    override def randomUUID: F[UUID] = Sync[F].delay(UUID.randomUUID())
  }

  def randomUUID[F[_]: UUIDGen]: F[UUID] = UUIDGen[F].randomUUID
  def randomString[F[_]: UUIDGen: Functor]: F[String] = randomUUID[F].map(_.toString)
}

Or we can just forget about it at all :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, done!

@mpilquist
Copy link
Member

Where does this one stand?

@djspiewak djspiewak merged commit 4602bc7 into typelevel:series/3.x Jul 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants