Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for enum #282

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tests/unit/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,30 @@ def test_convert_not_sequence(self):
# Verify
assert value == "1"

def test_convert_enum(self):
try:
from enum import Enum

class OptionType(Enum):
OP1 = "FirstOp"
OP2 = "SecondOp"
OP3 = "ThirdOp"

# Setup
registry = converters.ConverterFactoryRegistry(
(converters.StandardConverter(),)
)
key = converters.keys.Sequence(converters.keys.CONVERT_TO_STRING)

# Run
converter = registry[key](None)
value = converter(OptionType.OP1)

# Verify
assert value == "FirstOp"
except ImportError:
pass

def test_eq(self):
assert converters.keys.Sequence(0) == converters.keys.Sequence(0)
assert not (converters.keys.Sequence(1) == converters.keys.Sequence(0))
Expand Down
7 changes: 7 additions & 0 deletions uplink/converters/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"""
# Standard library imports
import functools
try:
from enum import Enum
except ImportError: # pragma: no cover
# Enum is added on version 3.4
Enum = type(None)

# Local imports

Expand Down Expand Up @@ -86,6 +91,8 @@ class Sequence(CompositeKey):
def convert(self, converter, value):
if isinstance(value, (list, tuple)):
return list(map(converter, value))
elif isinstance(value, Enum) and hasattr(value, 'value'):
return value.value
else:
return converter(value)

Expand Down