Skip to content

Commit

Permalink
Merge pull request #358 from uptane/time-based-uuid
Browse files Browse the repository at this point in the history
Add support for time based uuids
  • Loading branch information
simao authored Feb 29, 2024
2 parents 0474c2b + bcc26ac commit c801fd3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 5 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ val Library = new {
val flyway = "10.8.1"
}

val javaUuidGenerator = "com.fasterxml.uuid" % "java-uuid-generator" % "4.3.0"

val logback = "ch.qos.logback" % "logback-classic" % Version.logback

val flyway = "org.flywaydb" % "flyway-core" % Version.flyway
Expand Down Expand Up @@ -71,7 +73,9 @@ val Library = new {
lazy val scala213 = "2.13.12"
lazy val supportedScalaVersions = List(scala213)
lazy val commonDeps =
libraryDependencies ++= Library.circe ++ Seq(Library.refined, Library.scalatest) ++ Library.cats :+ Library.logback
libraryDependencies ++= Library.circe ++
Seq(Library.refined, Library.scalatest) ++
Library.cats :+ Library.logback :+ Library.javaUuidGenerator

lazy val commonConfigs = Seq.empty

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.advancedtelematic.libats.data

import java.util.UUID

import cats.Show
import com.fasterxml.uuid.Generators
import io.circe.{Decoder, Encoder, KeyDecoder, KeyEncoder}
import shapeless._
import shapeless.*

object UUIDKey {
private lazy val timeBasedUuidGenerator = Generators.timeBasedEpochGenerator()

abstract class UUIDKeyObj[Self <: UUIDKey] {

type SelfGen = Generic.Aux[Self, UUID :: HNil]

def generate()(implicit gen: SelfGen): Self =
fromJava(UUID.randomUUID())

private def fromJava(value: UUID)(implicit gen: SelfGen): Self = {
protected def fromJava(value: UUID)(implicit gen: SelfGen): Self = {
gen.from(value :: HNil)
}

Expand All @@ -31,6 +33,11 @@ object UUIDKey {
implicit val abstractKeyShow: Show[Self] = Show.show[Self](_.uuid.toString)
}

abstract class UuidKeyObjTimeBased[Self <: UUIDKey] extends UUIDKeyObj[Self] {
override def generate()(implicit gen: SelfGen) =
fromJava(timeBasedUuidGenerator.generate())
}

abstract class UUIDKey {
val uuid: UUID
}
Expand Down

0 comments on commit c801fd3

Please sign in to comment.