Skip to content

Commit

Permalink
Merge branch 'develop' into v1_0_0_docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sosna authored Jan 17, 2025
2 parents 9db6247 + 6c89bc1 commit f1b9386
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/pysdmx/io/json/fusion/messages/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def __handle_date(self, datestr: str) -> datetime:
def __get_val(
self, a: FusionAnnotation
) -> Tuple[Optional[datetime], Optional[datetime]]:
vals = a.title.split("/")
if a.title.startswith("/"):
vals = a.title.split("/") # type: ignore[union-attr]
if a.title.startswith("/"): # type: ignore[union-attr]
return (None, self.__handle_date(vals[1]))
else:
valid_from = self.__handle_date(vals[0])
Expand Down
54 changes: 42 additions & 12 deletions src/pysdmx/io/json/fusion/messages/core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Collection of Fusion-JSON schemas for common artefacts."""

from datetime import datetime
from typing import Any, Optional, Sequence
from typing import Any, Optional, Sequence, Union

import msgspec

Expand All @@ -10,20 +10,23 @@
from pysdmx.util import find_by_urn


class FusionAnnotation(msgspec.Struct, frozen=True):
"""Fusion-JSON payload for annotations."""

title: str
type: str


class FusionString(msgspec.Struct, frozen=True):
"""Fusion-JSON payload for an international string."""

locale: str
value: str


class FusionAnnotation(msgspec.Struct, frozen=True):
"""Fusion-JSON payload for annotations."""

id: Optional[str] = None
title: Optional[str] = None
type: Optional[str] = None
value: Optional[str] = None
text: Sequence[FusionString] = ()


class FusionLink(msgspec.Struct, frozen=True):
"""Fusion-JSON payload for link objects."""

Expand Down Expand Up @@ -55,6 +58,12 @@ class FusionRepresentation(msgspec.Struct, frozen=True):
minOccurs: Optional[int] = None
maxOccurs: Optional[int] = None

def __to_number(self, input: str) -> Union[int, float]:
try:
return int(input)
except ValueError:
return float(input)

def to_facets(self) -> Optional[Facets]:
"""Return a Facets domain object."""
if self.textFormat and (
Expand All @@ -70,14 +79,35 @@ def to_facets(self) -> Optional[Facets]:
or self.textFormat.startTime
or self.textFormat.endTime
):
min = (
self.__to_number(self.textFormat.minValue)
if self.textFormat.minValue
else None
)
max = (
self.__to_number(self.textFormat.maxValue)
if self.textFormat.maxValue
else None
)
sta = (
self.__to_number(self.textFormat.startValue)
if self.textFormat.startValue
else None
)
end = (
self.__to_number(self.textFormat.endValue)
if self.textFormat.endValue
else None
)

return Facets(
min_length=self.textFormat.minLength,
max_length=self.textFormat.maxLength,
is_sequence=self.textFormat.isSequence,
min_value=self.textFormat.minValue, # type: ignore[arg-type]
max_value=self.textFormat.maxValue, # type: ignore[arg-type]
start_value=self.textFormat.startValue, # type: ignore
end_value=self.textFormat.endValue, # type: ignore[arg-type]
min_value=min,
max_value=max,
start_value=sta,
end_value=end,
decimals=self.textFormat.decimals,
pattern=self.textFormat.pattern,
start_time=self.textFormat.startTime,
Expand Down
8 changes: 8 additions & 0 deletions tests/api/fmr/samples/code/freq.fusion.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
"value": "N:Annual"
}
]
},
{
"text": [
{
"locale": "en",
"value": "Empty"
}
]
}
],
"names": [
Expand Down
6 changes: 6 additions & 0 deletions tests/api/fmr/samples/code/freq.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
"texts": {
"en": "N:Annual"
}
},
{
"text": "Empty",
"texts": {
"en": "Empty"
}
}
],
"name": "Annual",
Expand Down
12 changes: 8 additions & 4 deletions tests/api/fmr/samples/df/no_const.fusion.json
Original file line number Diff line number Diff line change
Expand Up @@ -10619,9 +10619,11 @@
"urn": "urn:sdmx:org.sdmx.infomodel.datastructure.DataAttribute=BIS:BIS_CBS(1.0).UNIT_MULT",
"representation": {
"textFormat": {
"textType": "String",
"textType": "Integer",
"minLength": 1,
"maxLength": 2
"maxLength": 2,
"minValue": "-15",
"maxValue": "15"
},
"representation": "urn:sdmx:org.sdmx.infomodel.codelist.Codelist=BIS:CL_UNIT_MULT(1.0)"
},
Expand All @@ -10637,9 +10639,11 @@
"urn": "urn:sdmx:org.sdmx.infomodel.datastructure.Measure=BIS:BIS_CBS(1.0).OBS_VALUE",
"representation": {
"textFormat": {
"textType": "String",
"textType": "Double",
"minLength": 1,
"maxLength": 15
"maxLength": 15,
"startValue": "0.00",
"endValue": "42.99"
}
},
"concept": "urn:sdmx:org.sdmx.infomodel.conceptscheme.Concept=BIS:STANDALONE_CONCEPT_SCHEME(1.0).OBS_VALUE",
Expand Down
10 changes: 6 additions & 4 deletions tests/api/fmr/samples/df/no_const.json
Original file line number Diff line number Diff line change
Expand Up @@ -15474,7 +15474,9 @@
"enumerationFormat": {
"maxLength": 2,
"minLength": 1,
"dataType": "String"
"minValue": -15,
"maxValue": 15,
"dataType": "Integer"
},
"enumeration": "urn:sdmx:org.sdmx.infomodel.codelist.Codelist=BIS:CL_UNIT_MULT(1.0)"
},
Expand Down Expand Up @@ -15803,9 +15805,9 @@
"id": "OBS_VALUE",
"localRepresentation": {
"format": {
"maxLength": 15,
"minLength": 1,
"dataType": "String"
"dataType": "Double",
"startValue": 0.0,
"endValue": 42.99
}
},
"conceptIdentity": "urn:sdmx:org.sdmx.infomodel.conceptscheme.Concept=BIS:STANDALONE_CONCEPT_SCHEME(1.0).OBS_VALUE"
Expand Down
2 changes: 1 addition & 1 deletion tests/api/fmr/samples/df/schema.fusion.json
Original file line number Diff line number Diff line change
Expand Up @@ -4693,7 +4693,7 @@
"urn": "urn:sdmx:org.sdmx.infomodel.datastructure.DataAttribute=BIS:BIS_CBS(1.0).UNIT_MULT",
"representation": {
"textFormat": {
"textType": "String",
"textType": "Integer",
"minLength": 1,
"maxLength": 2,
"minValue": "-15",
Expand Down
2 changes: 1 addition & 1 deletion tests/api/fmr/samples/df/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6680,7 +6680,7 @@
"minLength": 1,
"minValue": -15,
"maxValue": 15,
"dataType": "String"
"dataType": "Integer"
},
"enumeration": "urn:sdmx:org.sdmx.infomodel.codelist.Codelist=BIS:CL_UNIT_MULT(1.0)"
},
Expand Down
Loading

0 comments on commit f1b9386

Please sign in to comment.