Skip to content
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
5 changes: 5 additions & 0 deletions scaleway-core/scaleway_core/bridge/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
from .timeseries import unmarshal_TimeSeriesPoint
from .timeseries import marshal_TimeSeriesPoint

from .decimal import unmarshal_Decimal
from .decimal import marshal_Decimal

__all__ = [
"Money",
"unmarshal_Money",
Expand All @@ -42,4 +45,6 @@
"marshal_TimeSeries",
"unmarshal_TimeSeriesPoint",
"marshal_TimeSeriesPoint",
"unmarshal_Decimal",
"marshal_Decimal"
]
28 changes: 28 additions & 0 deletions scaleway-core/scaleway_core/bridge/decimal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from decimal import Decimal
from typing import Any, Dict

def unmarshal_Decimal(data: Any) -> Decimal:
"""
Unmarshal an instance of Decimal from the given data.
"""
if not isinstance(data, dict):
raise TypeError(
"Unmarshalling the type 'Decimal' failed as data isn't a dictionary."
)

if "value" not in data:
raise TypeError(
"Unmarshalling the type 'Decimal' failed as data does not contain a 'value' key."
)


return Decimal(data["value"])


def marshal_Decimal(data: Decimal) -> Dict[str, Any]:
"""
Marshal an instance of Decimal into google.protobuf.Decimal JSON representation.
"""
return {
"value": str(data),
}
13 changes: 13 additions & 0 deletions scaleway-core/tests/test_bridge_marshal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import unittest
from decimal import Decimal

from scaleway_core.bridge import unmarshal_Decimal, marshal_Decimal

class TestBridgeMarshal(unittest.TestCase):
def test_decimal_marshal(self):
decimal = Decimal('1.2')
self.assertEqual(marshal_Decimal(decimal), {'value': '1.2'})

def test_decimal_unmarshal(self):
decimal = Decimal('1.2')
self.assertEqual(unmarshal_Decimal({'value': '1.2'}), decimal)