From: John Mulligan Date: Mon, 30 Jun 2025 21:18:36 +0000 (-0400) Subject: mgr/smb: add a wrapper_type attribute for resourcelib fields X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=4be5d428ca9705b1b05360ac1a0a9aa59a57259f;p=ceph.git mgr/smb: add a wrapper_type attribute for resourcelib fields Add a new wrapper_type attribute for resourcelib fields that will be used for better YAML representations in a future change. The wrapper_type attribute will be applied during the simplification phase to prep the resulting object for serialization. It can be set using the special _customize_resource function. Signed-off-by: John Mulligan --- diff --git a/src/pybind/mgr/smb/resourcelib.py b/src/pybind/mgr/smb/resourcelib.py index ea7a82263bd0d..7c76660015a7b 100644 --- a/src/pybind/mgr/smb/resourcelib.py +++ b/src/pybind/mgr/smb/resourcelib.py @@ -223,6 +223,11 @@ class Field: default: Any = _unset quiet: bool = False # customization value keep_none: bool = False # customization value + # wrapper_type can be used to customize the output type of a scalar value. + # Typically this is used to assist YAML serialization. + # IMPORTANT: make sure the type produces valid YAML and JSON, ideally as + # subclass as a native type. + wrapper_type: Optional[Callable] = None def optional(self) -> bool: """Return true if the type of the field is Optional.""" @@ -462,11 +467,15 @@ class Resource: } return + if fld.wrapper_type: + _out = fld.wrapper_type + else: + _out = lambda v: v # noqa: E731 if isinstance(value, str): - data[fld.name] = str(value) + data[fld.name] = _out(str(value)) return if isinstance(value, (int, float)): - data[fld.name] = value + data[fld.name] = _out(value) return raise ResourceTypeError(f'unexpected type for field {fld.name}')