From 74fa8481a91ebb6502cec5652a4f97a3aeb6b997 Mon Sep 17 00:00:00 2001 From: Jacob Tomlinson Date: Tue, 11 Jun 2024 11:30:49 +0100 Subject: [PATCH] Allow explicit passing of plural name to new_class instead of suffix (#403) --- kr8s/_objects.py | 9 +++++---- kr8s/tests/test_objects.py | 31 +++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/kr8s/_objects.py b/kr8s/_objects.py index 8d3abe1c..a53ac859 100644 --- a/kr8s/_objects.py +++ b/kr8s/_objects.py @@ -1624,7 +1624,7 @@ def new_class( namespaced=True, scalable: Optional[bool] = None, scalable_spec: Optional[str] = None, - plural_suffix: str = "s", + plural: Optional[str] = None, ) -> Type[APIObject]: """Create a new APIObject subclass. @@ -1635,7 +1635,7 @@ def new_class( namespaced: Whether the resource is namespaced or not. scalable: Whether the resource is scalable or not. scalable_spec: The name of the field to use for scaling. - plural_suffix: The suffix to use for the plural form of the resource. + plural: The plural form of the resource. Returns: A new APIObject subclass. @@ -1644,6 +1644,7 @@ def new_class( kind, version = kind.split(".", 1) if version is None: version = "v1" + plural = plural or kind.lower() + "s" newcls = type( kind, (APIObject,), @@ -1651,8 +1652,8 @@ def new_class( "kind": kind, "version": version, "_asyncio": asyncio, - "endpoint": kind.lower() + plural_suffix, - "plural": kind.lower() + plural_suffix, + "endpoint": plural.lower(), + "plural": plural.lower(), "singular": kind.lower(), "namespaced": namespaced, "scalable": scalable or False, diff --git a/kr8s/tests/test_objects.py b/kr8s/tests/test_objects.py index 58a32a6f..9b4867fb 100644 --- a/kr8s/tests/test_objects.py +++ b/kr8s/tests/test_objects.py @@ -1068,12 +1068,31 @@ def test_sync_new_class_is_sync(): def test_new_class_plural_suffix(): - MyPlural = new_class( - kind="MyPlural", + MyFoo = new_class( + kind="MyFoo", version="newclass.example.com/v1", namespaced=True, - plural_suffix="es", ) - instance = MyPlural({}) - assert instance.plural.endswith("es") - assert instance.endpoint.endswith("es") + instance = MyFoo({}) + assert instance.plural == "myfoos" + assert instance.endpoint == "myfoos" + + MyClass = new_class( + kind="MyClass", + plural="MyClasses", + version="newclass.example.com/v1", + namespaced=True, + ) + instance = MyClass({}) + assert instance.plural == "myclasses" + assert instance.endpoint == "myclasses" + + MyPolicy = new_class( + kind="MyPolicy", + plural="MyPolicies", + version="newclass.example.com/v1", + namespaced=True, + ) + instance = MyPolicy({}) + assert instance.plural == "mypolicies" + assert instance.endpoint == "mypolicies"