Skip to content

Commit 48c788c

Browse files
committed
extend the Encoder/Decoder state API to be type-aware
1 parent de861e1 commit 48c788c

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

localstack-core/localstack/state/core.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,42 +97,48 @@ def __str__(self) -> str:
9797

9898

9999
class Encoder:
100-
def encodes(self, obj: Any) -> bytes:
100+
def encodes(self, obj: Any, py_type: type = None) -> bytes:
101101
"""
102102
Encode an object into bytes.
103103
104104
:param obj: the object to encode
105+
:param py_type: the type of the object. needed by some encoders that don't have implicit type knowledge.
105106
:return: the encoded object
106107
"""
107108
b = io.BytesIO()
108109
self.encode(obj, b)
109110
return b.getvalue()
110111

111-
def encode(self, obj: Any, file: IO[bytes]):
112+
def encode(self, obj: Any, file: IO[bytes], py_type: type = None):
112113
"""
113114
Encode an object into bytes.
114115
115116
:param obj: the object to encode
117+
:param py_type: the type of the object. needed by some encoders that don't have implicit type knowledge.
116118
:param file: the file to write the encoded data into
117119
"""
118120
raise NotImplementedError
119121

120122

121123
class Decoder:
122-
def decodes(self, data: bytes) -> Any:
124+
def decodes(self, data: bytes, py_type: type = None) -> Any:
123125
"""
124126
Decode a previously encoded object.
125127
126128
:param data: the encoded object to decode
129+
:param py_type: the type that is expected as return type. Needed by some decoders that don't have implicit
130+
type knowledge.
127131
:return: the decoded object
128132
"""
129-
return self.decode(io.BytesIO(data))
133+
return self.decode(io.BytesIO(data), py_type)
130134

131-
def decode(self, file: IO[bytes]) -> Any:
135+
def decode(self, file: IO[bytes], py_type: type = None) -> Any:
132136
"""
133137
Decode a previously encoded object.
134138
135139
:param file: the io object containing the object to decode
140+
:param py_type: the type that is expected as return type. Needed by some decoders that don't have implicit
141+
type knowledge.
136142
:return: the decoded object
137143
"""
138144
raise NotImplementedError

localstack-core/localstack/state/pickle.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class PickleEncoder(Encoder):
240240
def __init__(self, pickler_class: type[dill.Pickler] = None):
241241
self.pickler_class = pickler_class or Pickler
242242

243-
def encode(self, obj: Any, file: BinaryIO):
243+
def encode(self, obj: Any, file: BinaryIO, py_type: type = None) -> Any:
244244
return self.pickler_class(file).dump(obj)
245245

246246

@@ -255,7 +255,7 @@ class PickleDecoder(Decoder):
255255
def __init__(self, unpickler_class: type[dill.Unpickler] = None):
256256
self.unpickler_class = unpickler_class or dill.Unpickler
257257

258-
def decode(self, file: BinaryIO) -> Any:
258+
def decode(self, file: BinaryIO, py_type=None) -> Any:
259259
return self.unpickler_class(file).load()
260260

261261

@@ -267,7 +267,7 @@ class JsonEncoder(Encoder):
267267
def __init__(self, pickler_class: type[jsonpickle.Pickler] = None):
268268
self.pickler_class = pickler_class or jsonpickle.Pickler()
269269

270-
def encode(self, obj: Any, file: IO[bytes]):
270+
def encode(self, obj: Any, file: IO[bytes], py_type: type = None):
271271
json_str = jsonpickle.encode(obj, context=self.pickler_class)
272272
file.write(json_str.encode("utf-8"))
273273

@@ -282,7 +282,7 @@ class JsonDecoder(Decoder):
282282
def __init__(self, unpickler_class: type[jsonpickle.Unpickler] = None):
283283
self.unpickler_class = unpickler_class or jsonpickle.Unpickler()
284284

285-
def decode(self, file: IO[bytes]) -> Any:
285+
def decode(self, file: IO[bytes], py_type: type = None) -> Any:
286286
json_str = file.read().decode("utf-8")
287287
return jsonpickle.decode(json_str, context=self.unpickler_class)
288288

0 commit comments

Comments
 (0)