Skip to content

Commit 88e29a5

Browse files
committed
Add pretty printer for literal null, undefined, MinKey, and MaxKey.
1 parent 54e3eda commit 88e29a5

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

gdbmongo/bsonmisc_printer.py

+60
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import gdb
2727

2828
from gdbmongo.objectid_printer import MongoOID
29+
from gdbmongo.printer_protocol import SupportsToString
2930
from gdbmongo.string_data_printer import MongoStringData
3031

3132

@@ -212,3 +213,62 @@ def to_value(self) -> gdb.Value:
212213

213214
setattr(MongoBSONSymbol, "_fields_",
214215
[(field.name, field.type) for field in dataclasses.fields(MongoBSONSymbol)])
216+
217+
218+
# pylint: disable-next=too-few-public-methods
219+
class UndefinedLabelerPrinter(SupportsToString):
220+
# pylint: disable=missing-function-docstring
221+
"""Pretty-printer for literal undefined value."""
222+
223+
def __init__(self, val: gdb.Value, /) -> None:
224+
self.val = val
225+
226+
def to_string(self) -> str:
227+
return "undefined"
228+
229+
230+
# pylint: disable-next=too-few-public-methods
231+
class NullLabelerPrinter(SupportsToString):
232+
# pylint: disable=missing-function-docstring
233+
"""Pretty-printer for literal null value."""
234+
235+
def __init__(self, val: gdb.Value, /) -> None:
236+
self.val = val
237+
238+
def to_string(self) -> str:
239+
return "null"
240+
241+
242+
# pylint: disable-next=too-few-public-methods
243+
class MinKeyLabelerPrinter(SupportsToString):
244+
# pylint: disable=missing-function-docstring
245+
"""Pretty-printer for literal MinKey value."""
246+
247+
def __init__(self, val: gdb.Value, /) -> None:
248+
self.val = val
249+
250+
def to_string(self) -> str:
251+
return "MinKey()"
252+
253+
254+
# pylint: disable-next=too-few-public-methods
255+
class MaxKeyLabelerPrinter(SupportsToString):
256+
# pylint: disable=missing-function-docstring
257+
"""Pretty-printer for literal MaxKey value."""
258+
259+
def __init__(self, val: gdb.Value, /) -> None:
260+
self.val = val
261+
262+
def to_string(self) -> str:
263+
return "MaxKey()"
264+
265+
266+
def add_printers(pretty_printer: gdb.printing.RegexpCollectionPrettyPrinter, /) -> None:
267+
"""Add the BSON-related printers to the pretty printer collection given."""
268+
pretty_printer.add_printer("mongo::MaxKeyLabeler", "^mongo::MaxKeyLabeler$",
269+
MaxKeyLabelerPrinter)
270+
pretty_printer.add_printer("mongo::MinKeyLabeler", "^mongo::MinKeyLabeler$",
271+
MinKeyLabelerPrinter)
272+
pretty_printer.add_printer("mongo::NullLabeler", "^mongo::NullLabeler$", NullLabelerPrinter)
273+
pretty_printer.add_printer("mongo::UndefinedLabeler", "^mongo::UndefinedLabeler$",
274+
UndefinedLabelerPrinter)

gdbmongo/interaction.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
import gdb
2323
import gdb.printing
2424

25-
from gdbmongo import (abseil_printers, date_printer, decorable_printer, lock_manager_printer,
26-
objectid_printer, string_data_printer, timestamp_printer)
25+
from gdbmongo import (abseil_printers, bsonmisc_printer, date_printer, decorable_printer,
26+
lock_manager_printer, objectid_printer, string_data_printer,
27+
timestamp_printer)
2728
from gdbmongo.detect_toolchain import ToolchainVersionDetector
2829
from gdbmongo.printer_protocol import SupportsChildren, SupportsToString
2930
from gdbmongo.stdlib_printers_loader import resolve_import
@@ -83,6 +84,7 @@ def register_printers(*, essentials: bool = True, stdlib: bool = False, abseil:
8384

8485
if mongo_extras:
8586
pretty_printer_mongo_extras = RegexpCollectionPrettyPrinter("gdbmongo-mongo-extras")
87+
bsonmisc_printer.add_printers(pretty_printer_mongo_extras)
8688
date_printer.add_printers(pretty_printer_mongo_extras)
8789
decorable_printer.add_printers(pretty_printer_mongo_extras)
8890
objectid_printer.add_printers(pretty_printer_mongo_extras)

0 commit comments

Comments
 (0)