Skip to content

Commit

Permalink
Allow explicit passing of plural name to new_class instead of suffix (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson authored Jun 11, 2024
1 parent 1f08653 commit 74fa848
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
9 changes: 5 additions & 4 deletions kr8s/_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -1644,15 +1644,16 @@ def new_class(
kind, version = kind.split(".", 1)
if version is None:
version = "v1"
plural = plural or kind.lower() + "s"
newcls = type(
kind,
(APIObject,),
{
"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,
Expand Down
31 changes: 25 additions & 6 deletions kr8s/tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 74fa848

Please sign in to comment.