Skip to content

Commit

Permalink
Merge pull request #488 from beer-garden/map-parameter-type-to-string
Browse files Browse the repository at this point in the history
Parameter decorator map literal to string
  • Loading branch information
1maple1 authored Jul 10, 2024
2 parents 40e59b4 + 2c3bbf7 commit 8960f2e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Brewtils Changelog
------
TBD

- Update Parameter decorator to map parameter type from literal to string value if possible
- Added Subscriber Types to Subscriber model
- Added Prefix Topics to System model
- Support adding Prefix Topics for the Generated Subscribers. It is supported through the `@client` or SystemClient inputs or beer.conf
Expand Down
12 changes: 10 additions & 2 deletions brewtils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,20 @@ def echo(self, message):
"""

# Allowing type matching: string == String == STRING
if type and type not in Parameter.TYPES:
# Validate type against permitted string literals
if not isinstance(type, str):
# Try to map literal to string equivalent
temp_type = _format_type(type)
if temp_type in Parameter.TYPES:
type = temp_type
elif type not in Parameter.TYPES:
# Allowing type matching: string == String == STRING
for parameter_type in Parameter.TYPES:
if type.upper() == parameter_type.upper():
type = parameter_type
break
if type not in Parameter.TYPES:
raise ValueError(f"Unable to map type {type} to string literal")

if _wrapped is None:
return functools.partial(
Expand Down
48 changes: 48 additions & 0 deletions test/decorators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,54 @@ def cmd3(foo):
assert cmd2.parameters[0].type == "String"
assert cmd3.parameters[0].type == "String"

def test_literal_mapping(self, basic_param):

del basic_param["type"]

@parameter(**basic_param, type=str)
def cmd1(foo):
return foo

@parameter(**basic_param, type=int)
def cmd2(foo):
return foo

@parameter(**basic_param, type=float)
def cmd3(foo):
return foo

@parameter(**basic_param, type=bool)
def cmd4(foo):
return foo

@parameter(**basic_param, type=dict)
def cmd5(foo):
return foo

@parameter(**basic_param)
def cmd6(foo):
return foo

assert cmd1.parameters[0].type == "String"
assert cmd2.parameters[0].type == "Integer"
assert cmd3.parameters[0].type == "Float"
assert cmd4.parameters[0].type == "Boolean"
assert cmd5.parameters[0].type == "Dictionary"
assert cmd6.parameters[0].type == "Any"

with pytest.raises(ValueError):

class BadType:
bad = True

@parameter(**basic_param, type=BadType)
def cmd_bad_1(foo):
return foo

@parameter(**basic_param, type="Bad Type")
def cmd_bad_2(foo):
return foo


class TestParameters(object):
@pytest.fixture(autouse=True)
Expand Down

0 comments on commit 8960f2e

Please sign in to comment.