-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathrelocatable_fields.py
35 lines (26 loc) · 1.17 KB
/
relocatable_fields.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import marshmallow.fields as mfields
from starkware.cairo.lang.vm.relocatable import RelocatableValue
class MaybeRelocatableField(mfields.Field):
"""
A field that behaves like a MaybeRelocatable, but serializes to a tuple.
See RelocatableValue.to_tuple() and RelocatableValue.from_tuple().
"""
def _serialize(self, value, attr, obj, **kwargs):
if value is None:
return None
return RelocatableValue.to_tuple(value)
def _deserialize(self, value, attr, data, **kwargs):
return RelocatableValue.from_tuple(value)
class MaybeRelocatableDictField(mfields.Field):
"""
A field that behaves like a MaybeRelocatable dict, but MaybeRelocatable objects serialize to
tuples. See RelocatableValue.to_tuple() and RelocatableValue.from_tuple().
"""
def _serialize(self, value, attr, obj, **kwargs):
if value is None:
return None
return [
(RelocatableValue.to_tuple(x), RelocatableValue.to_tuple(y)) for x, y in value.items()
]
def _deserialize(self, value, attr, data, **kwargs):
return {RelocatableValue.from_tuple(x): RelocatableValue.from_tuple(y) for x, y in value}