]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/smb: add a new BigString helper type for serializing yaml
authorJohn Mulligan <jmulligan@redhat.com>
Wed, 2 Jul 2025 21:41:26 +0000 (17:41 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Tue, 12 Aug 2025 14:24:49 +0000 (10:24 -0400)
Add a new BigString type that serves to help serialize resources to
YAML, causing it to use the multi-line literal style. A BigString is
a string so it should be transparent to other serializations, but
note that it should only be used in "simplified" structures.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
src/pybind/mgr/smb/resources.py

index 89d83281d8ec22723bed3042a230292d3e3fe988..b00c20b9ca48eca57021279b7c2a2adeeaac5891 100644 (file)
@@ -94,6 +94,29 @@ class InvalidInputError(ValueError, ErrorResponseBase):
         return -errno.EINVAL, data, "Invalid input"
 
 
+class BigString(str):
+    """A subclass of str that exists specifally to assit the YAML
+    formatting of longer strings (SSL/TLS certs). Because the
+    python YAML lib makes doing this automatically very awkward.
+    """
+
+    @staticmethod
+    def yaml_representer(
+        dumper: yaml.SafeDumper, data: 'BigString'
+    ) -> yaml.ScalarNode:
+        _type = 'tag:yaml.org,2002:str'
+        data = str(data)
+        if '\n' in data or len(data) >= 80:
+            return dumper.represent_scalar(_type, data, style='|')
+        return dumper.represent_scalar(_type, data)
+
+
+# thanks yaml lib for your odd api.
+# Maybe this should be part of object_format.py? If this could be useful
+# elsewhere, perhaps lift this.
+yaml.SafeDumper.add_representer(BigString, BigString.yaml_representer)
+
+
 class _RBase:
     # mypy doesn't currently (well?) support class decorators adding methods
     # so we use a base class to add this method to all our resource classes.