8000 Axsuarez/serialization in cosmos storage (#343) · gorpo/botbuilder-python@843edfc · GitHub
[go: up one dir, main page]

Skip to content

Commit 843edfc

Browse files
authored
Axsuarez/serialization in cosmos storage (microsoft#343)
* cosmos storage serialization with jsonpickle * serialization with jsonpickle tested
1 parent 392a5d4 commit 843edfc

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

libraries/botbuilder-azure/botbuilder/azure/cosmosdb_storage.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
from typing import Dict, List
1111
from threading import Semaphore
1212
import json
13+
from jsonpickle.pickler import Pickler
14+
from jsonpickle.unpickler import Unpickler
1315
import azure.cosmos.cosmos_client as cosmos_client # pylint: disable=no-name-in-module,import-error
1416
import azure.cosmos.errors as cosmos_errors # pylint: disable=no-name-in-module,import-error
15-
from botbuilder.core.storage import Storage, StoreItem
17+
from botbuilder.core.storage import Storage
1618

1719

1820
class CosmosDbConfig:
@@ -144,15 +146,15 @@ async def read(self, keys: List[str]) -> Dict[str, object]:
144146
results = list(
145147
self.client.QueryItems(self.__container_link, query, options)
146148
)
147-
# return a dict with a key and a StoreItem
149+
# return a dict with a key and an object
148150
return {r.get("realId"): self.__create_si(r) for r in results}
149151

150152
# No keys passed in, no result to return.
151153
return {}
152154
except TypeError as error:
153155
raise error
154156

155-
async def write(self, changes: Dict[str, StoreItem]):
157+
async def write(self, changes: Dict[str, object]):
156158
"""Save storeitems to storage.
157159
158160
:param changes:
@@ -224,36 +226,38 @@ async def delete(self, keys: List[str]):
224226
except TypeError as error:
225227
raise error
226228

227-
def __create_si(self, result) -> StoreItem:
228-
"""Create a StoreItem from a result out of CosmosDB.
229+
def __create_si(self, result) -> object:
230+
"""Create an object from a result out of CosmosDB.
229231
230232
:param result:
231-
:return StoreItem:
233+
:return object:
232234
"""
233235
# get the document item from the result and turn into a dict
234236
doc = result.get("document")
235-
# readd the e_tag from Cosmos
237+
# read the e_tag from Cosmos
236238
if result.get("_etag"):
237239
doc["e_tag"] = result["_etag"]
238-
# create and return the StoreItem
239-
return StoreItem(**doc)
240240

241-
def __create_dict(self, store_item: StoreItem) -> Dict:
242-
"""Return the dict of a StoreItem.
241+
result_obj = Unpickler().restore(doc)
242+
243+
# create and return the object
244+
return result_obj
245+
246+
def __create_dict(self, store_item: object) -> Dict:
247+
"""Return the dict of an object.
243248
244249
This eliminates non_magic attributes and the e_tag.
245250
246251
:param store_item:
247252
:return dict:
248253
"""
249254
# read the content
250-
non_magic_attr = [
251-
attr
252-
for attr in dir(store_item)
253-
if not attr.startswith("_") or attr.__eq__("e_tag")
254-
]
255+
json_dict = Pickler().flatten(store_item)
256+
if "e_tag" in json_dict:
257+
del json_dict["e_tag"]
258+
255259
# loop through attributes and write and return a dict
256-
return {attr: getattr(store_item, attr) for attr in non_magic_attr}
260+
return json_dict
257261

258262
def __item_link(self, identifier) -> str:
259263
"""Return the item link of a item in the container.

libraries/botbuilder-azure/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"azure-storage-blob>=2.1.0",
1010
"botbuilder-schema>=4.4.0b1",
1111
"botframework-connector>=4.4.0b1",
12+
"jsonpickle>=1.2",
1213
]
1314
TEST_REQUIRES = ["aiounittest>=1.1.0"]
1415

0 commit comments

Comments
 (0)
0