Skip to content
This repository was archived by the owner on May 10, 2024. It is now read-only.

Commit d38f773

Browse files
committed
paddle-php-sdk PR# 35 PaddleEnum (in our case, PaddleStrEnum)
1 parent 31814e9 commit d38f773

File tree

5 files changed

+87
-10
lines changed

5 files changed

+87
-10
lines changed

paddle_billing/PaddleStrEnum.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from enum import EnumMeta, StrEnum
2+
3+
from paddle_billing.Undefined import Undefined
4+
5+
6+
def _is_dunder(name): # Copied from enum.py because IDE complains when calling protected class super()._is_dunder
7+
"""
8+
Returns True if a __dunder__ name, False otherwise.
9+
"""
10+
return (
11+
len(name) > 4 and
12+
name[:2] == name[-2:] == '__' and
13+
name[2] != '_' and
14+
name[-3] != '_'
15+
)
16+
17+
18+
19+
class PaddleStrEnumMeta(EnumMeta):
20+
"""
21+
Returns Undefined() by default when a property doesn't exist
22+
This provides some flexibility for the SDK to handle Paddle adding new properties to their APIspec
23+
"""
24+
25+
def __getattr__(cls, name):
26+
if _is_dunder(name):
27+
raise AttributeError(name)
28+
29+
try:
30+
return super().__getattr__(name)
31+
except AttributeError:
32+
return Undefined()
33+
34+
35+
36+
class PaddleStrEnum(StrEnum, metaclass=PaddleStrEnumMeta):
37+
"""
38+
Subclass of StrEnum but uses PaddleStrEnumMeta metaclass to set Undefined() value to undefined enum keys
39+
"""
40+
41+
@classmethod
42+
def is_known(cls) -> bool:
43+
"""Not yet implemented"""

paddle_billing/StrEnumFromValue.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

paddle_billing/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# The order of these three imports matters
12
from paddle_billing.Environment import Environment
23
from paddle_billing.Options import Options
34
from paddle_billing.Client import Client

tests/Unit/PaddleStrEnum/__init__.py

Whitespace-only changes.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from dataclasses import asdict, dataclass, is_dataclass
2+
3+
from paddle_billing.PaddleStrEnum import PaddleStrEnum
4+
from paddle_billing.Undefined import Undefined
5+
6+
7+
8+
def test_paddle_str_enum_works_as_expected():
9+
class TestCountryCodesEnum(PaddleStrEnum):
10+
CA = 'canada'
11+
US = 'usa'
12+
13+
14+
assert isinstance(TestCountryCodesEnum.CA, PaddleStrEnum)
15+
assert isinstance(TestCountryCodesEnum.US, PaddleStrEnum)
16+
assert TestCountryCodesEnum.CA == 'canada'
17+
assert TestCountryCodesEnum.US == 'usa'
18+
assert TestCountryCodesEnum('canada') == 'canada'
19+
assert TestCountryCodesEnum('usa') == 'usa'
20+
21+
22+
23+
def test_dataclass_asdict_returns_expected_paddle_str_enum_value():
24+
class TestCountryCodesEnum(PaddleStrEnum):
25+
CA = 'canada'
26+
US = 'usa'
27+
28+
29+
@dataclass
30+
class TestDataclass:
31+
country_code: TestCountryCodesEnum | Undefined = Undefined()
32+
33+
def get_parameters(self) -> dict:
34+
return asdict(self)
35+
36+
37+
test_dataclass = TestDataclass(TestCountryCodesEnum.CA)
38+
39+
assert is_dataclass(test_dataclass)
40+
assert isinstance(test_dataclass.get_parameters(), dict)
41+
assert test_dataclass.get_parameters().get('country_code', None) is not None
42+
assert type(test_dataclass.get_parameters().get('country_code', None)) == TestCountryCodesEnum
43+
assert test_dataclass.get_parameters().get('country_code', None) == 'canada'

0 commit comments

Comments
 (0)