Skip to content

Commit

Permalink
MAINT: Define schema inside FmuResults model (#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnatt authored Jan 14, 2025
1 parent 9331f16 commit 3edc1dc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
24 changes: 24 additions & 0 deletions schemas/0.8.0/fmu_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,12 @@
"CaseMetadata": {
"description": "The FMU metadata model for an FMU case.\n\nA case represent a set of iterations that belong together, either by being part of\nthe same run (i.e. history matching) or by being placed together by the user,\ncorresponding to /scratch/<asset>/<user>/<my case name>/.",
"properties": {
"$schema": {
"format": "uri",
"minLength": 1,
"title": "$Schema",
"type": "string"
},
"access": {
"$ref": "#/$defs/Access"
},
Expand Down Expand Up @@ -3529,6 +3535,12 @@
"IterationMetadata": {
"description": "The FMU metadata model for an FMU Iteration.\n\nAn object representing a single Iteration of a specific case.",
"properties": {
"$schema": {
"format": "uri",
"minLength": 1,
"title": "$Schema",
"type": "string"
},
"access": {
"$ref": "#/$defs/Access"
},
Expand Down Expand Up @@ -4614,6 +4626,12 @@
"ObjectMetadata": {
"description": "The FMU metadata model for a given data object.",
"properties": {
"$schema": {
"format": "uri",
"minLength": 1,
"title": "$Schema",
"type": "string"
},
"access": {
"$ref": "#/$defs/SsdlAccess"
},
Expand Down Expand Up @@ -6454,6 +6472,12 @@
"RealizationMetadata": {
"description": "The FMU metadata model for an FMU Realization.\n\nAn object representing a single Realization of a specific Iteration.",
"properties": {
"$schema": {
"format": "uri",
"minLength": 1,
"title": "$Schema",
"type": "string"
},
"access": {
"$ref": "#/$defs/Access"
},
Expand Down
21 changes: 3 additions & 18 deletions src/fmu/dataio/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@

from typing import TYPE_CHECKING, Final, List, Literal, Optional, Union

from pydantic import (
AnyHttpUrl,
BaseModel,
Field,
)
from pydantic import Field

from ._logging import null_logger
from ._models.fmu_results import data, fields
from ._models.fmu_results.enums import FMUClass
from ._models.fmu_results.fmu_results import (
CaseMetadata,
FmuResultsSchema,
ObjectMetadata,
)
from ._models.fmu_results.global_configuration import GlobalConfiguration
Expand All @@ -42,17 +37,7 @@
logger: Final = null_logger(__name__)


class JsonSchemaMetadata(BaseModel):
"""Mixin to inject the $schema field into exported metadata."""

schema_: AnyHttpUrl = Field(
default_factory=lambda: AnyHttpUrl(FmuResultsSchema.url()),
alias="$schema",
frozen=True,
)


class ObjectMetadataExport(JsonSchemaMetadata, ObjectMetadata, populate_by_name=True):
class ObjectMetadataExport(ObjectMetadata, populate_by_name=True):
"""Wraps the schema ObjectMetadata, adjusting some values to optional for pragmatic
purposes when exporting metadata."""

Expand All @@ -65,7 +50,7 @@ class ObjectMetadataExport(JsonSchemaMetadata, ObjectMetadata, populate_by_name=
preprocessed: Optional[bool] = Field(alias="_preprocessed", default=None)


class CaseMetadataExport(JsonSchemaMetadata, CaseMetadata, populate_by_name=True):
class CaseMetadataExport(CaseMetadata, populate_by_name=True):
"""Adds the optional description field for backward compatibility."""

class_: Literal[FMUClass.case] = Field(
Expand Down
7 changes: 7 additions & 0 deletions src/fmu/dataio/_models/fmu_results/fmu_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import TYPE_CHECKING, Literal, Union

from pydantic import (
AnyHttpUrl,
BaseModel,
Field,
GetJsonSchemaHandler,
Expand Down Expand Up @@ -131,6 +132,12 @@ class MetadataBase(BaseModel):
version: str = Field(default=FmuResultsSchema.VERSION)
"""The version of the schema that generated this data."""

schema_: AnyHttpUrl = Field(
default_factory=lambda: AnyHttpUrl(FmuResultsSchema.url()),
alias="$schema",
)
"""The url of the schema that generated this data."""


class CaseMetadata(MetadataBase):
"""The FMU metadata model for an FMU case.
Expand Down
10 changes: 9 additions & 1 deletion tests/test_units/test_metadata_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
OperatingSystem,
TracklogEvent,
)
from fmu.dataio._utils import prettyprint_dict
from fmu.dataio._utils import prettyprint_dict, read_metadata_from_file
from fmu.dataio.providers.objectdata._provider import objectdata_provider_factory

# pylint: disable=no-member
Expand All @@ -33,6 +33,14 @@ def test_metadata_dollars(edataobj1, regsurf):
assert mymeta["$schema"] == FmuResultsSchema.url()
assert mymeta["source"] == FmuResultsSchema.SOURCE

# also check that it is preserved in the exported metadata
exportpath = edataobj1.export(regsurf)
exportmeta = read_metadata_from_file(exportpath)

assert exportmeta["version"] == FmuResultsSchema.VERSION
assert exportmeta["$schema"] == FmuResultsSchema.url()
assert exportmeta["source"] == FmuResultsSchema.SOURCE


# --------------------------------------------------------------------------------------
# Tracklog
Expand Down

0 comments on commit 3edc1dc

Please sign in to comment.