]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/smb: add a wrapper_type attribute for resourcelib fields
authorJohn Mulligan <jmulligan@redhat.com>
Mon, 30 Jun 2025 21:18:36 +0000 (17:18 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Tue, 12 Aug 2025 14:24:49 +0000 (10:24 -0400)
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 <jmulligan@redhat.com>
src/pybind/mgr/smb/resourcelib.py

index ea7a82263bd0ddf7051c4b7c5241bde7e0369eac..7c76660015a7b627dff74e67cbe4c0e307110425 100644 (file)
@@ -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}')