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

feature/mx-1384 Stop defining REFERENCE spec on identifier subclasses #13

Merged
merged 2 commits into from
Aug 9, 2023
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
44 changes: 8 additions & 36 deletions mex/common/types/identifier.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
import string
from typing import TYPE_CHECKING, Any, ClassVar
from typing import TYPE_CHECKING, Any
from uuid import UUID, uuid4

if TYPE_CHECKING: # pragma: no cover
Expand All @@ -15,8 +15,6 @@
class Identifier(str):
"""Common identifier class."""

REFERENCE: ClassVar[str | None] = None

@classmethod
def generate(cls, seed: int | None = None) -> "Identifier":
"""Generate a new identifier from a seed or random uuid version 4."""
Expand All @@ -39,7 +37,7 @@ def __get_validators__(cls) -> "CallableGenerator":

@classmethod
def validate(cls, value: Any) -> "Identifier":
"""Validate a string, uuid or identfier."""
"""Validate a string, uuid or identifier."""
if isinstance(value, (str, UUID, Identifier)):
value = str(value)
if re.match(MEX_ID_PATTERN, value):
Expand All @@ -51,16 +49,12 @@ def validate(cls, value: Any) -> "Identifier":

@classmethod
def __modify_schema__(cls, field_schema: dict[str, Any]) -> None:
"""Modify the schema to add the ID regex or reference a specific ID field."""
field_schema.clear()
if cls.REFERENCE:
field_schema["$ref"] = cls.REFERENCE
else:
field_schema.update(
title="Identifier",
type="string",
pattern=MEX_ID_PATTERN,
)
"""Modify the schema to add the ID regex and correct title."""
field_schema.update(
title=cls.__name__,
type="string",
pattern=MEX_ID_PATTERN,
)

def __repr__(self) -> str:
"""Overwrite the default representation."""
Expand All @@ -70,64 +64,42 @@ def __repr__(self) -> str:
class AccessPlatformID(Identifier):
"""Identifier for merged access platforms."""

REFERENCE = "#/components/schemas/AccessPlatformID"


class ActivityID(Identifier):
"""Identifier for merged activities."""

REFERENCE = "#/components/schemas/ActivityID"


class ContactPointID(Identifier):
"""Identifier for merged contact points."""

REFERENCE = "#/components/schemas/ContactPointID"


class DistributionID(Identifier):
"""Identifier for merged distributions."""

REFERENCE = "#/components/schemas/DistributionID"


class OrganizationID(Identifier):
"""Identifier for merged organizations."""

REFERENCE = "#/components/schemas/OrganizationID"


class OrganizationalUnitID(Identifier):
"""Identifier for merged organizational units."""

REFERENCE = "#/components/schemas/OrganizationalUnitID"


class PersonID(Identifier):
"""Identifier for merged persons."""

REFERENCE = "#/components/schemas/PersonID"


class PrimarySourceID(Identifier):
"""Identifier for merged primary sources."""

REFERENCE = "#/components/schemas/PrimarySourceID"


class ResourceID(Identifier):
"""Identifier for merged resources."""

REFERENCE = "#/components/schemas/ResourceID"


class VariableID(Identifier):
"""Identifier for merged variables."""

REFERENCE = "#/components/schemas/VariableID"


class VariableGroupID(Identifier):
"""Identifier for merged variable groups."""

REFERENCE = "#/components/schemas/VariableGroupID"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mex-common"
version = "0.12.0"
version = "0.12.1"
description = "RKI MEx common library."
authors = ["RKI MEx Team <mex@rki.de>"]
readme = "README.md"
Expand Down
6 changes: 4 additions & 2 deletions tests/types/test_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class DummyID(Identifier):
REFERENCE = "#/components/Dummy#/id"
pass


class DummyModel(BaseModel):
Expand Down Expand Up @@ -46,7 +46,9 @@ def test_identifier_modifies_schema() -> None:
"pattern": r"^[a-zA-Z0-9]{14,22}$",
}
assert DummyModel.schema()["properties"]["dummy"] == {
"$ref": "#/components/Dummy#/id"
"title": "DummyID",
"type": "string",
"pattern": r"^[a-zA-Z0-9]{14,22}$",
}


Expand Down