Socket
Socket
Sign inDemoInstall

io.github.bcarter97:id-generator_2.13

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

io.github.bcarter97:id-generator_2.13

Generate reproducible UUIDs based of a sequence of numbers, with the possibility of generating sub UUIDs from the parent UUID.


Version published
Maintainers
1
Source

id-generator

CI Build CI Release codecov Scala Steward badge

Maven metadata URL Sonatype Nexus (Snapshots)

Generate reproducible UUIDs based of a sequence of numbers, with the possibility of generating sub UUIDs from the parent UUID.

Usage

In your build.sbt add:

libraryDependencies += "io.github.bcarter97" %% "id-generator" % "x.x.x"

Then you can use the Generator like so:

import io.github.bcarter97.Generator

val generator = Generator()

generator.primaryId(5)
// PrimaryId(index=5, uuid=e4da3b7f-bbce-3345-9777-2b0674a318d5, numSubIds=1)

Generating PrimaryIds

As well as using generator.id(n) as above, there are a number of ways to generate a PrimaryId.

To sample a random PrimaryId (starting from the first index), use sample. This will automatically increment the sample index until the specified maxIndex is reached, at which point it will loop back around.

val generator = Generator(2)

val id1 = generator.sample()
// PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9)

val id2 = generator.sample()
// PrimaryId(index=2, uuid=c81e728d-9d4c-3f63-af06-7f89cc14862c, numSubIds=9)

val id3 = generator.sample()
// PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9) <-- same as id1

:bulb: Using id() without an index will call the underlying sample method.

You can sample a range of PrimaryIds by passing a number to sample:

val ids = generator.sample(2)
// Vector(PrimaryId(index=0, uuid=cfcd2084-95d5-35ef-a6e7-dff9f98764da, numSubIds=9), PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9))

Similarly, you can specify the range of UUIDs to generate, to make the list reproducible:

val ids = generator.ids(5, 8)
// Vector(PrimaryId(index=5, uuid=e4da3b7f-bbce-3345-9777-2b0674a318d5, numSubIds=1), PrimaryId(index=6, uuid=1679091c-5a88-3faf-afb5-e6087eb1b2dc, numSubIds=9), PrimaryId(index=7, uuid=8f14e45f-ceea-367a-9a36-dedd4bea2543, numSubIds=6))

Generating SubIds

SubIds are derived from a PrimaryId. Each PrimaryId has a random, reproducible number of SubIds. Every generated SubId contains the PrimaryId that generated it.

To get a list of subIds from a parentId:

val id = generator.sample()

val subIds = id.subIds
// Vector(SubId(index=1, uuid=6512bd43-d9ca-36e0-ac99-0b0a82652dca, primaryId=PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9)) ... )

Any one of these subIds can find its parentId.

val id = generator.sample()
// PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9)

val subId = id.subIds.head
// SubId(index=1, uuid=6512bd43-d9ca-36e0-ac99-0b0a82652dca, primaryId=PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9))

val originalId = generator.subIdFromUuid(subId.uuid)
// Some(SubId(index=1, uuid=6512bd43-d9ca-36e0-ac99-0b0a82652dca, primaryId=PrimaryId(index=1, uuid=c4ca4238-a0b9-3382-8dcc-509a6f75849b, numSubIds=9)))

FAQs

Package last updated on 03 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc