FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix Python BSON Binary and Code round-trip by xiaolu-ai26 · Pull Request #21 · onenodehq/onenode · GitHub

Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
33 changes: 29 additions & 4 deletions onenode-py/onenode/_collection.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@
ObjectId: lambda v: {"$oid": str(v)},
datetime: lambda v: {"$date": v.isoformat()},
Decimal128: lambda v: {"$numberDecimal": str(v)},
Binary: lambda v: {"$binary": v.hex()},
Binary: lambda v: {"$binary": {"data": v.hex(), "subType": v.subtype}},
Regex: lambda v: {"$regex": v.pattern, "$options": v.flags},
Code: lambda v: {"$code": str(v)},
Timestamp: lambda v: {"$timestamp": {"t": v.time, "i": v.inc}},
MinKey: lambda v: {"$minKey": 1},
MaxKey: lambda v: {"$maxKey": 1},
}


def _parse_binary_subtype(subtype):
if isinstance(subtype, str):
return int(subtype, 16)
return int(subtype)


class APIClientError(Exception):
"""Base class for all API client-related errors."""

Expand Down Expand Up @@ -81,6 +86,12 @@ def get_headers(self) -> dict:

def __serialize(self, value):
"""Serialize BSON types, Text, and nested structures into JSON-compatible formats."""
if isinstance(value, Code):
result = {"$code": str(value)}
if value.scope is not None:
result["$scope"] = self.__serialize(dict(value.scope))
return result

if value is None or isinstance(value, (bool, int, float, str)):
return value

Expand Down Expand Up @@ -146,11 +157,25 @@ def __deserialize(self, value, depth=0):
if key == "$numberDecimal":
return Decimal128(value["$numberDecimal"])
if key == "$binary":
return Binary(bytes.fromhex(value["$binary"]))
binary_value = value["$binary"]
if isinstance(binary_value, dict):
subtype = _parse_binary_subtype(
binary_value.get("subType", 0)
)
return Binary(
bytes.fromhex(binary_value.get("data", "")),
subtype=subtype,
)
return Binary(bytes.fromhex(binary_value))
if key == "$regex":
return Regex(value["$regex"], value.get("$options", 0))
if key == "$code":
return Code(value["$code"])
scope = (
self.__deserialize(value["$scope"], depth + 1)
if "$scope" in value
else None
)
return Code(value["$code"], scope)
if key == "$timestamp":
return Timestamp(
value["$timestamp"]["t"], value["$timestamp"]["i"]
Expand Down

Back | FazBrowse Home | New Git URL