]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commit
pybind/mgr: add ObjectFormatAdapter type to object_format.py
authorJohn Mulligan <jmulligan@redhat.com>
Sat, 9 Apr 2022 18:29:25 +0000 (14:29 -0400)
committerAdam King <adking@redhat.com>
Sat, 21 May 2022 23:20:43 +0000 (19:20 -0400)
commitf37af75b77d25e47689c2c5aee6115217c2c8a58
tree5cf31a4fb8915aa9a8cf6f55bb344ddf40029b7d
parent846629ca3534b0331f3f683831e10642f9da22de
pybind/mgr: add ObjectFormatAdapter type to object_format.py

The ObjectFormatAdapter fills the role for bridging between types
that can return a simplified representation of themselves and
actually formatting objects as JSON and YAML.

Note that we do not want generally want types that serialize themselves
to JSON/YAML strings. That approach makes it harder to standardize on
the final output formatting (indentation, multiple yaml docs, etc).
Additionally, we do not want the types to need to specialize between
JSON and YAML. So, by default, we try to use a method `to_simplified`
which is not specific to any serialization format.  However, for
backwards compatibility with types that already have methods *that
return dicts/lists/etc* under the names `to_json` or `to_yaml` we
support using the `compatible` flag to enable the use of those methods.
If the adaptor fails to find a conversion method on the object,
serialization of the object itself is attempted - this way return values
of simple lists, dicts, etc also works.

An earlier version of this patch tried to share the JSON/YAML
serialization logic found in src/pybind/mgr/orchestrator/module.py.
However, this approach was deemed too complicated and we also preferred
to use yaml safe dumping whenever possible.  This does lead to a level
of code duplication. Dealing with this duplication is a task left for
the future.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
(cherry picked from commit 5a3f2ae916146a13290695e141c789722dbd9ad9)
src/pybind/mgr/object_format.py