Skip to content
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 .gts-spec-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.12.1
v0.12.2
39 changes: 35 additions & 4 deletions gts-id/src/gts_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ impl GtsId {
.collect()
}

/// Generate a deterministic UUID v5 from this GTS ID.
/// Return this GTS ID's UUID.
///
/// The UUID is derived from the validated identifier string under a fixed
/// GTS namespace, so it is stable across processes and runs: the same ID
/// always maps to the same UUID.
/// If the identifier already has a UUID tail, that UUID is returned directly.
/// Otherwise the UUID is derived from the validated identifier string under
/// a fixed GTS namespace, so it is stable across processes and runs: the
/// same ID always maps to the same UUID.
///
/// Requires the `uuid` feature.
#[cfg(feature = "uuid")]
Expand All @@ -114,6 +115,10 @@ impl GtsId {
static GTS_NS: LazyLock<uuid::Uuid> =
LazyLock::new(|| uuid::Uuid::new_v5(&uuid::Uuid::NAMESPACE_URL, b"gts"));

if let Some(uuid) = self.segments.last().and_then(GtsIdSegment::uuid) {
return uuid;
}

uuid::Uuid::new_v5(&GTS_NS, self.id.as_bytes())
}

Expand Down Expand Up @@ -613,4 +618,30 @@ mod tests {
let id2 = GtsId::try_new(&gts_id("x.core.events.event.v2~")).expect("test");
assert_ne!(id1.to_uuid(), id2.to_uuid());
}

#[cfg(feature = "uuid")]
#[test]
fn test_to_uuid_returns_uuid_tail() {
const UUID_TAIL: &str = "7a1d2f34-5678-49ab-9012-abcdef123456";
let id =
GtsId::try_new(&gts_id(&format!("x.core.events.event.v1~{UUID_TAIL}"))).expect("test");
assert_eq!(
id.to_uuid(),
uuid::Uuid::parse_str(UUID_TAIL).expect("uuid")
);
}

#[cfg(feature = "uuid")]
#[test]
fn test_to_uuid_returns_uuid_tail_after_chained_type() {
const UUID_TAIL: &str = "446ef7e5-1a27-4abf-a7c4-f336505c7aa0";
let id = GtsId::try_new(&gts_id(&format!(
"x.core.events.event.v1~acme.orders.events.order.v1~{UUID_TAIL}"
)))
.expect("test");
assert_eq!(
id.to_uuid(),
uuid::Uuid::parse_str(UUID_TAIL).expect("uuid")
);
}
}
Loading