Skip to content

Commit

Permalink
Use custom Base Model (#36)
Browse files Browse the repository at this point in the history
* Use custom Base Model

This allows us to set config to control Pydantic behaviour.

* [GENERATE] Pydantic models from JSON Schema

---------

Co-authored-by: dapla-bot[bot] <143391972+dapla-bot[bot]@users.noreply.github.com>
  • Loading branch information
mmwinther and dapla-bot[bot] authored Jan 5, 2024
1 parent 3d03a99 commit 558092c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/workflows/generate-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ jobs:
--input-file-type jsonschema
--input src/metadata-container-json-schema.json
--output-model-type pydantic_v2.BaseModel
--base-class "datadoc_model.datadoc_base_model.DatadocBaseModel"
--use-default
--use-one-literal-as-default
--force-optional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from pydantic import ConfigDict, BaseModel


class DatadocBaseModel(BaseModel):
"""Defines configuration which applies to all Models in this package."""

model_config = ConfigDict(validate_assignment=True, use_enum_values=True)
22 changes: 12 additions & 10 deletions generated/python/datadoc_model/datadoc_model/model.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# generated by datamodel-codegen:
# filename: metadata-container-json-schema.json
# timestamp: 2024-01-04T14:47:13+00:00
# timestamp: 2024-01-05T12:58:00+00:00

from __future__ import annotations

from enum import Enum
from typing import Any, Literal, Optional
from uuid import UUID

from pydantic import AnyUrl, AwareDatetime, BaseModel, Field
from pydantic import AnyUrl, AwareDatetime, Field

from datadoc_model.datadoc_base_model import DatadocBaseModel


class Assessment(str, Enum):
Expand Down Expand Up @@ -48,7 +50,7 @@ class VariableRole(str, Enum):
ATTRIBUTE = "ATTRIBUTE"


class LanguageStringType(BaseModel):
class LanguageStringType(DatadocBaseModel):
en: Optional[str] = None
nn: Optional[str] = None
nb: Optional[str] = None
Expand Down Expand Up @@ -85,7 +87,7 @@ class TemporalityTypeType(str, Enum):
EVENT = "EVENT"


class PseudoDataset(BaseModel):
class PseudoDataset(DatadocBaseModel):
dataset_pseudo_time: Optional[AwareDatetime] = Field(
None,
description="Time at which the dataset was pseudonymized. In ISO 8601 format.",
Expand All @@ -105,7 +107,7 @@ class SourceVariableDatatype(str, Enum):
DATETIME = "DATETIME"


class PseudoVariable(BaseModel):
class PseudoVariable(DatadocBaseModel):
short_name: Optional[str] = Field(
None,
description="Physical name of the variable in the dataset. Should match the recommended short name.",
Expand Down Expand Up @@ -156,13 +158,13 @@ class PseudoVariable(BaseModel):
)


class PseudonymizationJsonSchema(BaseModel):
class PseudonymizationJsonSchema(DatadocBaseModel):
document_version: Literal["0.1.0"] = "0.1.0"
pseudo_dataset: Optional[PseudoDataset] = None
pseudo_variables: Optional[list[PseudoVariable]] = None


class Dataset(BaseModel):
class Dataset(DatadocBaseModel):
short_name: Optional[str] = Field(
None,
description="Name of (physical) data file, data table or dataset",
Expand Down Expand Up @@ -274,7 +276,7 @@ class Dataset(BaseModel):
)


class Variable(BaseModel):
class Variable(DatadocBaseModel):
short_name: Optional[str] = Field(
None,
description="Physical name of the variable in the dataset. Should match the recommended short name.",
Expand Down Expand Up @@ -363,7 +365,7 @@ class Variable(BaseModel):
)


class DatadocJsonSchema(BaseModel):
class DatadocJsonSchema(DatadocBaseModel):
percentage_complete: Optional[int] = Field(
None, description="Percentage of obligatory metadata fields populated."
)
Expand All @@ -374,7 +376,7 @@ class DatadocJsonSchema(BaseModel):
variables: Optional[list[Variable]] = None


class MetadataContainer(BaseModel):
class MetadataContainer(DatadocBaseModel):
document_version: Literal["0.0.1"] = "0.0.1"
datadoc: Optional[DatadocJsonSchema] = None
pseudonymization: Optional[PseudonymizationJsonSchema] = None

0 comments on commit 558092c

Please sign in to comment.