Giter VIP home page Giter VIP logo

uuid4cats-effect's Introduction

Maven Central javadoc Code of Conduct codecov

uuid4cats-effect - UUID and TypeID Generation for cats effect

Although cats-effect has some support for generating UUIDs, it is limited to the UUIDv4 pseudo-random type.

This library add support for the following types:

time-based sortable random
UUID v1
gregorian calendar
UUID v4
UUID v6
gregorian calendar
UUID v7
unix epoch

Implementation based on this UUID RFC Draft

In addition to UUID, there is also support for TypeIDs. TypeIDs are a modern, type-safe extension of UUIDv7. This implementation is compatible with the 0.3.0 version of the specification

Quickstart

To use uuid4cats-effect in an existing SBT project with Scala 2.13 or a later version, add the following dependency to your build.sbt:

libraryDependencies += "tech.ant8e" %% "uuid4cats-effect" % "<version>"

uuid4cats-effect is published for Scala 2.13, and 3 on JVM and JS.

Example

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import tech.ant8e.uuid4cats.UUIDv6
import tech.ant8e.uuid4cats.UUIDUtil
import tech.ant8e.uuid4cats.TypeID

val ids = for {
  generator <- UUIDv6.generator[IO]
  typeIDGenerator <- TypeID.generator[IO]
  uuid1 <- generator.uuid
  uuid2 <- generator.uuid
  typeid <- typeIDGenerator.typeid("myprefix")
  ts = UUIDUtil.extractTimestamp(uuid1)
} yield (uuid1, uuid2, typeid.value, ts)

ids.unsafeRunSync()


val ids: cats.effect.IO[(java.util.UUID, java.util.UUID, String)] = IO(...)
val res1: (java.util.UUID, java.util.UUID, String) = (1ee22392-7669-6aa0-8000-ca7de6e5d540,1ee22392-766c-61b0-8000-422a97a9dbaa,myprefix_01h5a2ccabe0080m1hrkj0p0qp, Some(2024-04-28T11:10:44.501Z))

Uniqueness of generated time-based UUIDs is guaranteed when using the same generator. Collisions across generators are theoretically possible although unlikely.

uuid4cats-effect's People

Contributors

ant8e avatar mergify[bot] avatar scala-steward avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

uuid4cats-effect's Issues

Extract timestamp from time-based UUIDs

Hello,
glad to have the honour of writing the first issue 😄

The project looks quite promising and I'd love to adopt it at $WORK, ditching com.github.f4b6a3:uuid-creator, but there's a missing piece of functionality we'd require: extracting timestamps from UUIDv6 and v7 by looking at the time-encoding bits. So something like

object UUID4CatsEffect {
  // could also return Option[Long] with epoch millis or be overloaded
  def extractTimestamp(uuid: UUID): Option[java.time.Instant] = ... 
}

Inspired by UUIDUtil.getInstant.

I'm not sure I can find the time to contribute, but in any way would be glad if this feature made it into the library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.