Skip to content

Commit

Permalink
Accept bool, int, and float as string in configs
Browse files Browse the repository at this point in the history
These types can all be converted trivially to a string and their
values are also valid strings.

Change-Id: I3f7dafe16761ab258fd1b797a21813f97379efdd
  • Loading branch information
joeyparrish committed Oct 18, 2019
1 parent 465fda0 commit ad6a124
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions streamer/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,19 +331,27 @@ def _check_and_convert_type(self, field, key, value):

# For enums or RuntimeMapTypes, try to cast the value to the enum type, and
# raise a WrongType error if this fails.
if (issubclass(field.type, enum.Enum) or
issubclass(field.type, RuntimeMapType)):
if issubclass(field.type, (enum.Enum, RuntimeMapType)):
try:
return field.type(value)
except ValueError:
raise WrongType(self.__class__, key, field) from None

# A float should be permissive and accept an int, as well.
if field.type is float:
if not isinstance(value, float) and not isinstance(value, int):
if not isinstance(value, (float, int)):
raise WrongType(self.__class__, key, field)
return value

# For strings, accept bools, ints, and floats, too. For example, "true",
# "7", and "10.2" are all valid strings, but will come out of the YAML
# parser as a bool, and int, and a float, respectively. Convert these to
# string.
if field.type is str:
if isinstance(value, (bool, float, int, str)):
return str(value)
raise WrongType(self.__class__, key, field)

# These aren't true types, but validation classes for specific types of
# limited input. Run the validate() method to check the input.
if issubclass(field.type, ValidatingType):
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function errorTests() {
field_name: 'filters',
}));

inputConfig.inputs[0].filters = [1, 2, 3];
inputConfig.inputs[0].filters = [{}];

await expectAsync(startStreamer(inputConfig, minimalPipelineConfig))
.toBeRejectedWith(jasmine.objectContaining({
Expand Down

0 comments on commit ad6a124

Please sign in to comment.