Skip to content

Commit eae9143

Browse files
committed
feat: add support for reproducible UUIDs
1 parent f30581e commit eae9143

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎src/osw/model/static.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from typing import TYPE_CHECKING, Literal, Optional, Type, TypeVar, Union
6-
from uuid import UUID
6+
from uuid import UUID, uuid4
77
from warnings import warn
88

99
from pydantic.v1 import BaseModel, Field, constr
@@ -120,8 +120,26 @@ def __init__(self, **data):
120120
)
121121
if data.get("name") is None and "label" in data:
122122
data["name"] = pascal_case(data["label"][0].text)
123+
if "uuid" not in data:
124+
# If no uuid is provided, generate a new one
125+
data["uuid"] = OswBaseModel._init_uuid(**data)
123126
super().__init__(**data)
124127

128+
@classmethod
129+
def _init_uuid(cls, **data) -> UUID:
130+
"""Generates a random UUID for the entity if not provided during initialization.
131+
This method can be overridden to generate a UUID based on the data, e.g.
132+
for using a UUIDv5 based on the name:
133+
```python
134+
def _get_uuid(**data) -> UUID:
135+
namespace_uuid = uuid.UUID("0dd6c54a-b162-4552-bab9-9942ccaf4f41")
136+
return uuid.uuid5(namespace_uuid, data["name"])
137+
```
138+
"""
139+
140+
# default: random UUID
141+
return uuid4()
142+
125143
def full_dict(self, **kwargs): # extent BaseClass export function
126144
d = super().dict(**kwargs)
127145
for key in ("_osl_template", "_osl_footer"):

‎tests/test_osl.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import uuid
23
from typing import Any, Dict, Union
34
from uuid import UUID
45

@@ -304,3 +305,53 @@ class have in common. {{Template:Editor/DrawIO
304305
assert "File:OSW51ad8f9d660641f9880006c40f41cb56.png" in file_list
305306
assert "File:OSW0bea84d54c074374a4b45dc84d9ba302.drawio.svg" in file_list
306307
assert "File:OSW58baa09ec00b42ccb0779fe4d58ccf82.drawio.svg" in file_list
308+
309+
310+
def check_reproducible_uuid():
311+
"""Check if the UUID of an entity is reproducible by using the same name
312+
and overwriting the _init_uuid method.
313+
"""
314+
315+
# Create two entities with the same name
316+
entity1 = model.Item(label=[model.Label(text="MyItem")])
317+
entity2 = model.Item(label=[model.Label(text="MyItem")])
318+
# and one with a different name
319+
entity3 = model.Item(label=[model.Label(text="MyItem2")])
320+
321+
# Check if the UUIDs are random / not the same
322+
assert entity1.uuid != entity2.uuid
323+
assert entity1.uuid != entity3.uuid
324+
assert entity2.uuid != entity3.uuid
325+
326+
# a (random) UUID namespace in order to generate reproducible UUIDs
327+
namespace_uuid = UUID("0dd6c54a-b162-4552-bab9-9942ccaf4f41")
328+
329+
# monkey patch the _init_uuid method to use the namespace UUID
330+
# this will generate the same UUID for the same name
331+
model.OswBaseModel._init_uuid = lambda **data: (
332+
uuid.uuid5(namespace_uuid, data["name"]) if "name" in data else uuid.uuid4()
333+
)
334+
335+
# Create two entities with the same name
336+
entity1 = model.Item(label=[model.Label(text="MyItem")])
337+
entity2 = model.Item(label=[model.Label(text="MyItem")])
338+
# and one with a different name
339+
entity3 = model.Item(label=[model.Label(text="MyItem2")])
340+
341+
# Check if the UUIDs are the same
342+
assert entity1.uuid == entity2.uuid
343+
assert entity1.uuid != entity3.uuid
344+
345+
# restore the original _init_uuid method
346+
model.OswBaseModel._init_uuid = lambda **data: uuid.uuid4()
347+
348+
# Create two entities with the same name
349+
entity1 = model.Item(label=[model.Label(text="MyItem")])
350+
entity2 = model.Item(label=[model.Label(text="MyItem")])
351+
# and one with a different name
352+
entity3 = model.Item(label=[model.Label(text="MyItem2")])
353+
354+
# Check if the UUIDs are random / not the same
355+
assert entity1.uuid != entity2.uuid
356+
assert entity1.uuid != entity3.uuid
357+
assert entity2.uuid != entity3.uuid

0 commit comments

Comments
 (0)