Skip to content

Commit

Permalink
Merge pull request #55 from amplify-education/hotfix/add-to_bool-func…
Browse files Browse the repository at this point in the history
…tion

Add `to_bool` function
  • Loading branch information
rddimon committed Jul 11, 2023
2 parents da94405 + edd3fc2 commit ced2c2f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
13 changes: 12 additions & 1 deletion amplify_aws_utils/resource_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def chunker(sequence, size):
# [15, 16, 17, 18, 19]
"""
return (
sequence[position : position + size]
sequence[position: position + size]
for position in range(0, len(sequence), size)
)

Expand Down Expand Up @@ -457,3 +457,14 @@ def lambda_handler(event, context):
) from exc

return None


def to_bool(value: Any) -> bool:
"""
Convert `value` to the lower string and compare with the list ["yes", "y", "true", "t", "on", "1"]
If in the list then `true` else `false`
"""
if str(value).lower() in ["yes", "y", "true", "t", "on", "1"]:
return True

return False
2 changes: 1 addition & 1 deletion amplify_aws_utils/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Place of record for the package version"""

__version__ = "0.4.3"
__version__ = "0.4.4"
__git_hash__ = "GIT_HASH"
20 changes: 20 additions & 0 deletions test/unit/test_resource_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
wait_for_sshable,
wait_for_state,
wait_for_state_boto3,
to_bool,
)


Expand Down Expand Up @@ -350,6 +351,25 @@ def test_dynamodb_record_to_dict(self):
actual,
)

def test_to_bool(self):
"""Test to_bool happy"""
# true values
self.assertEqual(to_bool("t"), True)
self.assertEqual(to_bool("true"), True)
self.assertEqual(to_bool("yes"), True)
self.assertEqual(to_bool("y"), True)
self.assertEqual(to_bool("on"), True)
self.assertEqual(to_bool("1"), True)
self.assertEqual(to_bool(1), True)
# false values
self.assertEqual(to_bool("0"), False)
self.assertEqual(to_bool(0), False)
self.assertEqual(to_bool(None), False)
self.assertEqual(to_bool("any"), False)
self.assertEqual(to_bool("n"), False)
self.assertEqual(to_bool(False), False)
self.assertEqual(to_bool("False"), False)


# aws_lambda_powertools.middleware_factory.factory.logger is being patched but not referenced.
# pylint: disable=unused-argument,no-value-for-parameter
Expand Down

0 comments on commit ced2c2f

Please sign in to comment.