-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from Avivsalem/staging
Staging
- Loading branch information
Showing
6 changed files
with
73 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,56 @@ | ||
import json | ||
|
||
import numbers | ||
from typing import Any, Dict, TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from mypy_boto3_sqs.type_defs import MessageAttributeValueQueueTypeDef | ||
from mypy_boto3_sqs.type_defs import MessageAttributeValueQueueTypeDef, MessageAttributeValueTypeDef | ||
|
||
|
||
def get_aws_data_type(value: Any) -> str: | ||
if isinstance(value, (list, set, frozenset, tuple)): | ||
return "String.Array" | ||
elif isinstance(value, bool): | ||
if isinstance(value, str): | ||
return "String" | ||
elif isinstance(value, (int, float)): | ||
elif isinstance(value, numbers.Number): | ||
return "Number" | ||
elif isinstance(value, bytes): | ||
return "Binary" | ||
elif isinstance(value, bool): | ||
return "String.bool" | ||
elif isinstance(value, (list, set, frozenset, tuple)): | ||
return "String.list" | ||
else: | ||
return "String" | ||
return "String.other" | ||
|
||
|
||
def generate_message_attributes(headers: Dict[str, Any]) -> Dict[str, 'MessageAttributeValueQueueTypeDef']: | ||
results: Dict[str, 'MessageAttributeValueQueueTypeDef'] = {} | ||
for key, value in headers.items(): | ||
data_type = get_aws_data_type(value) | ||
results[key] = {"DataType": data_type} | ||
|
||
if data_type == "String": | ||
results[key]["StringValue"] = value | ||
|
||
elif data_type == "Binary": | ||
results[key]["BinaryValue"] = value | ||
|
||
else: | ||
results[key]["StringValue"] = json.dumps(value) | ||
|
||
return results | ||
|
||
|
||
def decode_message_attributes(message_attributes: Dict[str, 'MessageAttributeValueTypeDef']) -> Dict[str, Any]: | ||
result: Dict[str, Any] = {} | ||
for key, value in message_attributes.items(): | ||
decoded_val: Any | ||
if value["DataType"] == "String": | ||
decoded_val = value["StringValue"] | ||
|
||
elif value["DataType"] == "Binary": | ||
decoded_val = value["BinaryValue"] | ||
else: | ||
decoded_val = json.loads(value["StringValue"]) | ||
|
||
result[key] = decoded_val | ||
|
||
def generate_message_attributes(attributes: Dict[str, Any]) -> Dict[str, 'MessageAttributeValueQueueTypeDef']: | ||
return { | ||
key: { | ||
"DataType": get_aws_data_type(value), | ||
"StringValue": value if isinstance(value, str) else json.dumps(value) # to avoid double encoding | ||
} | ||
for key, value in attributes.items() | ||
} | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters