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

feat: Implement TypeID specification v0.3.0 #63

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This library add support for the following types:
Implementation based on this [UUID RFC Draft](https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-03)

In addition to UUID, there is also support for [TypeIDs](https://github.com/jetpack-io/typeid). TypeIDs are a modern,
type-safe extension of UUIDv7
type-safe extension of UUIDv7. This implementation is compatible with the [0.3.0 version of the specification](https://github.com/jetify-com/typeid/tree/main/spec#typeid-specification-version-030)

## Quickstart

Expand Down
9 changes: 5 additions & 4 deletions core/src/main/scala/tech/ant8e/uuid4cats/TypeID.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ object TypeID {
enforceUUIDV7: Boolean = true
): ValidatedNec[DecodeError, TypeID] = {
val re =
"^([a-z]{0,63})(_?)([0123456789abcdefghjkmnpqrstvwxyz]+)$".r

"^([a-z](?:[a-z_]{0,61}[a-z])?)?(_)?([0123456789abcdefghjkmnpqrstvwxyz]{26})$".r
def parseID(id: String): Validated[DecodeError, UUID] =
UUIDBase32
.fromBase32(id)
Expand All @@ -124,7 +123,7 @@ object TypeID {
}

typeIDString match {
case re(prefix, sep, id) =>
case re(prefix, sep, id) if prefix != null && sep != null =>
(
Validated
.cond(
Expand All @@ -135,6 +134,8 @@ object TypeID {
.toValidatedNec,
parseID(id).toValidatedNec
).mapN { case (prefix_, uuid_) => newTypeID(prefix_, uuid_) }
case re(null, null, id) =>
parseID(id).toValidatedNec.map(uuid => newTypeID("", uuid))
case _ => Validated.invalidNec(NotParseableTypeID)
}

Expand Down Expand Up @@ -207,7 +208,7 @@ object TypeID {
prefix: String
): Validated[BuildError, String] = Option(prefix)
.map(prefix_ => {
if ("[a-z]{0,63}".r.matches(prefix_))
if ("^([a-z]([a-z_]{0,61}[a-z])?)?$".r.matches(prefix_))
Validated.Valid(prefix_)
else Validated.Invalid(InvalidPrefix)
})
Expand Down
22 changes: 19 additions & 3 deletions core/src/test/scala/tech/ant8e/uuid4cats/TypeIDSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ class TypeIDSuite extends FunSuite {
prefix = "prefix",
uuid = uuid"01890a5d-ac96-774b-bcce-b302099a8057"
)

// prefix-underscore added in v0.3.0
assertValidEncoding(
typeID = "pre_fix_00000000000000000000000000",
prefix = "pre_fix",
uuid = uuid"00000000-0000-0000-0000-000000000000"
)
}

test("TypeID should not build invalid typeIDs") {
val tooLongPrefix =
"0123456789012345678901234567890123456789012345678901234567890123456789"
Expand Down Expand Up @@ -167,9 +175,6 @@ class TypeIDSuite extends FunSuite {
// prefix-period
assertInvalidDecoding(typeID = "pre.fix_00000000000000000000000000")

// prefix-underscore
assertInvalidDecoding(typeID = "pre_fix_00000000000000000000000000")

// prefix-non-ascii
assertInvalidDecoding(typeID = "préfix_00000000000000000000000000")

Expand Down Expand Up @@ -224,6 +229,17 @@ class TypeIDSuite extends FunSuite {
assertInvalidDecoding(
typeID = "prefix0000000000000000000000000g"
)

// prefix-underscore-start
assertInvalidDecoding(
typeID = "_prefix_00000000000000000000000000"
)

// prefix-underscore-end
assertInvalidDecoding(
typeID = "prefix__00000000000000000000000000"
)

}

test("TypeID should have an Eq instance") {
Expand Down
Loading