Skip to content

Commit

Permalink
MongoDB: Fix BSON decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Sep 25, 2024
1 parent ce4dc2c commit a3f6b6c
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/commons_codec/transform/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import datetime as dt
import logging
import typing as t
from functools import cached_property
from functools import lru_cache
from typing import Iterable

import bson
Expand All @@ -25,6 +25,15 @@
logger = logging.getLogger(__name__)


@lru_cache()
def all_bson_types() -> t.Tuple[t.Type, ...]:
_types: t.List[t.Type] = []
for _typ in bson._ENCODERS:
if hasattr(_typ, "_type_marker"):
_types.append(_typ)
return tuple(_types)


@define
class MongoDBCrateDBConverter:
"""
Expand Down Expand Up @@ -118,7 +127,7 @@ def decode_extended_json(self, value: t.Dict[str, t.Any]) -> t.Any:
else:
out = object_hook(value)

is_bson = isinstance(out, self.all_bson_types)
is_bson = isinstance(out, all_bson_types())

# Decode BSON types.
if isinstance(out, bson.Binary) and out.subtype == bson.UUID_SUBTYPE:
Expand All @@ -145,14 +154,6 @@ def decode_extended_json(self, value: t.Dict[str, t.Any]) -> t.Any:
# Return others converted as-is.
return out

@cached_property
def all_bson_types(self) -> t.Tuple[t.Type, ...]:
_types: t.List[t.Type] = []
for _typ in bson._ENCODERS:
if hasattr(_typ, "_type_marker"):
_types.append(_typ)
return tuple(_types)

@staticmethod
def convert_epoch(value: t.Any) -> float:
if isinstance(value, int):
Expand Down

0 comments on commit a3f6b6c

Please sign in to comment.