From: John Mulligan Date: Wed, 2 Jul 2025 21:41:26 +0000 (-0400) Subject: mgr/smb: add a new BigString helper type for serializing yaml X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=39f73e2b7926e2f717947243a24c8743e4dcb815;p=ceph.git mgr/smb: add a new BigString helper type for serializing yaml 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 --- diff --git a/src/pybind/mgr/smb/resources.py b/src/pybind/mgr/smb/resources.py index 89d83281d8ec2..b00c20b9ca48e 100644 --- a/src/pybind/mgr/smb/resources.py +++ b/src/pybind/mgr/smb/resources.py @@ -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.