From 4da4bb3c7146e0392d46fb9224364e7cdf7ce7f6 Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Fri, 21 Jul 2023 13:57:33 -0400 Subject: [PATCH] common/Formatter: add support for dumping null Signed-off-by: Patrick Donnelly (cherry picked from commit 5f57c526b22af16e8a079aacefcb26cc14202ad2) --- src/common/Formatter.cc | 18 ++++++++++++++++++ src/common/Formatter.h | 4 ++++ src/mgr/PyFormatter.cc | 5 +++++ src/mgr/PyFormatter.h | 1 + src/rgw/rgw_formats.cc | 5 +++++ src/rgw/rgw_formats.h | 1 + 6 files changed, 34 insertions(+) diff --git a/src/common/Formatter.cc b/src/common/Formatter.cc index 4ced59769b508..f121afa07a3e3 100644 --- a/src/common/Formatter.cc +++ b/src/common/Formatter.cc @@ -311,6 +311,11 @@ void JSONFormatter::add_value(std::string_view name, std::string_view val, bool } } +void JSONFormatter::dump_null(std::string_view name) +{ + add_value(name, "null"); +} + void JSONFormatter::dump_unsigned(std::string_view name, uint64_t u) { add_value(name, u); @@ -473,6 +478,14 @@ void XMLFormatter::add_value(std::string_view name, T val) m_ss << "\n"; } +void XMLFormatter::dump_null(std::string_view name) +{ + print_spaces(); + m_ss << "<" << get_xml_name(name) << " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\" />"; + if (m_pretty) + m_ss << "\n"; +} + void XMLFormatter::dump_unsigned(std::string_view name, uint64_t u) { add_value(name, u); @@ -845,6 +858,11 @@ void TableFormatter::add_value(std::string_view name, T val) { m_ss.str(""); } +void TableFormatter::dump_null(std::string_view name) +{ + add_value(name, "null"); +} + void TableFormatter::dump_unsigned(std::string_view name, uint64_t u) { add_value(name, u); diff --git a/src/common/Formatter.h b/src/common/Formatter.h index 879e1634d8141..14f30c84f3de7 100644 --- a/src/common/Formatter.h +++ b/src/common/Formatter.h @@ -80,6 +80,7 @@ namespace ceph { virtual void open_object_section(std::string_view name) = 0; virtual void open_object_section_in_ns(std::string_view name, const char *ns) = 0; virtual void close_section() = 0; + virtual void dump_null(std::string_view name) = 0; virtual void dump_unsigned(std::string_view name, uint64_t u) = 0; virtual void dump_int(std::string_view name, int64_t s) = 0; virtual void dump_float(std::string_view name, double d) = 0; @@ -149,6 +150,7 @@ namespace ceph { void open_object_section(std::string_view name) override; void open_object_section_in_ns(std::string_view name, const char *ns) override; void close_section() override; + void dump_null(std::string_view name) override; void dump_unsigned(std::string_view name, uint64_t u) override; void dump_int(std::string_view name, int64_t s) override; void dump_float(std::string_view name, double d) override; @@ -221,6 +223,7 @@ namespace ceph { void open_object_section(std::string_view name) override; void open_object_section_in_ns(std::string_view name, const char *ns) override; void close_section() override; + void dump_null(std::string_view name) override; void dump_unsigned(std::string_view name, uint64_t u) override; void dump_int(std::string_view name, int64_t s) override; void dump_float(std::string_view name, double d) override; @@ -277,6 +280,7 @@ namespace ceph { void open_object_section_with_attrs(std::string_view name, const FormatterAttrs& attrs) override; void close_section() override; + void dump_null(std::string_view name) override; void dump_unsigned(std::string_view name, uint64_t u) override; void dump_int(std::string_view name, int64_t s) override; void dump_float(std::string_view name, double d) override; diff --git a/src/mgr/PyFormatter.cc b/src/mgr/PyFormatter.cc index 8e58f6e9a84ab..6a7f3e9825552 100644 --- a/src/mgr/PyFormatter.cc +++ b/src/mgr/PyFormatter.cc @@ -37,6 +37,11 @@ void PyFormatter::open_object_section(std::string_view name) cursor = dict; } +void PyFormatter::dump_null(std::string_view name) +{ + dump_pyobject(name, Py_None); +} + void PyFormatter::dump_unsigned(std::string_view name, uint64_t u) { PyObject *p = PyLong_FromUnsignedLong(u); diff --git a/src/mgr/PyFormatter.h b/src/mgr/PyFormatter.h index 5e4c0a679ac34..b45fbf162e0b0 100644 --- a/src/mgr/PyFormatter.h +++ b/src/mgr/PyFormatter.h @@ -87,6 +87,7 @@ public: stack.pop(); } void dump_bool(std::string_view name, bool b) override; + void dump_null(std::string_view name) override; void dump_unsigned(std::string_view name, uint64_t u) override; void dump_int(std::string_view name, int64_t u) override; void dump_float(std::string_view name, double d) override; diff --git a/src/rgw/rgw_formats.cc b/src/rgw/rgw_formats.cc index 3affb5f141425..7ff3128021da3 100644 --- a/src/rgw/rgw_formats.cc +++ b/src/rgw/rgw_formats.cc @@ -113,6 +113,11 @@ void RGWFormatter_Plain::close_section() stack.pop_back(); } +void RGWFormatter_Plain::dump_null(std::string_view name) +{ + dump_value_int(name, "null"); /* I feel a little bad about this. */ +} + void RGWFormatter_Plain::dump_unsigned(std::string_view name, uint64_t u) { dump_value_int(name, "%" PRIu64, u); diff --git a/src/rgw/rgw_formats.h b/src/rgw/rgw_formats.h index d7e47259d5839..fd79e29671dac 100644 --- a/src/rgw/rgw_formats.h +++ b/src/rgw/rgw_formats.h @@ -38,6 +38,7 @@ public: void open_object_section(std::string_view name) override; void open_object_section_in_ns(std::string_view name, const char *ns) override; void close_section() override; + void dump_null(std::string_view name) override; void dump_unsigned(std::string_view name, uint64_t u) override; void dump_int(std::string_view name, int64_t u) override; void dump_float(std::string_view name, double d) override; -- 2.39.5