-
Notifications
You must be signed in to change notification settings - Fork 18
Handle AWS error codes in documents #495
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b0690f3
Only have default discriminator for struct docs
JordonPhillips aa72972
Use shared settings for JSON codec
JordonPhillips 5e2e774
Pass JSON document class through settings
JordonPhillips 9ee1229
Handle AWS error codes in documents
JordonPhillips cc0b232
Fix typo in protocols docs
JordonPhillips File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from smithy_core.documents import Document | ||
from smithy_core.shapes import ShapeID, ShapeType | ||
|
||
|
||
def parse_document_discriminator( | ||
document: Document, default_namespace: str | None | ||
) -> ShapeID | None: | ||
if document.shape_type is ShapeType.MAP: | ||
map_document = document.as_map() | ||
code = map_document.get("__type") | ||
if code is None: | ||
code = map_document.get("code") | ||
if code is not None and code.shape_type is ShapeType.STRING: | ||
return parse_error_code(code.as_string(), default_namespace) | ||
|
||
return None | ||
|
||
|
||
def parse_error_code(code: str, default_namespace: str | None) -> ShapeID | None: | ||
if not code: | ||
return None | ||
|
||
code = code.split(":")[0] | ||
if "#" in code: | ||
return ShapeID(code) | ||
|
||
if not code or not default_namespace: | ||
return None | ||
|
||
return ShapeID.from_parts(name=code, namespace=default_namespace) |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pytest | ||
from smithy_aws_core.utils import parse_document_discriminator, parse_error_code | ||
from smithy_core.documents import Document | ||
from smithy_core.shapes import ShapeID | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"document, expected", | ||
[ | ||
({"__type": "FooError"}, "com.test#FooError"), | ||
({"__type": "com.test#FooError"}, "com.test#FooError"), | ||
( | ||
{ | ||
"__type": "FooError:http://internal.amazon.com/coral/com.amazon.coral.validate/" | ||
}, | ||
"com.test#FooError", | ||
), | ||
( | ||
{ | ||
"__type": "com.test#FooError:http://internal.amazon.com/coral/com.amazon.coral.validate" | ||
}, | ||
"com.test#FooError", | ||
), | ||
({"code": "FooError"}, "com.test#FooError"), | ||
({"code": "com.test#FooError"}, "com.test#FooError"), | ||
( | ||
{ | ||
"code": "FooError:http://internal.amazon.com/coral/com.amazon.coral.validate/" | ||
}, | ||
"com.test#FooError", | ||
), | ||
( | ||
{ | ||
"code": "com.test#FooError:http://internal.amazon.com/coral/com.amazon.coral.validate" | ||
}, | ||
"com.test#FooError", | ||
), | ||
({"__type": "FooError", "code": "BarError"}, "com.test#FooError"), | ||
("FooError", None), | ||
({"__type": None}, None), | ||
({"__type": ""}, None), | ||
({"__type": ":"}, None), | ||
], | ||
) | ||
def test_aws_json_document_discriminator( | ||
document: dict[str, str], expected: ShapeID | None | ||
) -> None: | ||
actual = parse_document_discriminator(Document(document), "com.test") | ||
assert actual == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"code, expected", | ||
[ | ||
("FooError", "com.test#FooError"), | ||
( | ||
"FooError:http://internal.amazon.com/coral/com.amazon.coral.validate/", | ||
"com.test#FooError", | ||
), | ||
( | ||
"com.test#FooError:http://internal.amazon.com/coral/com.amazon.coral.validate", | ||
"com.test#FooError", | ||
), | ||
("", None), | ||
(":", None), | ||
], | ||
) | ||
def test_parse_error_code(code: str, expected: ShapeID | None) -> None: | ||
actual = parse_error_code(code, "com.test") | ||
assert actual == expected | ||
|
||
|
||
def test_parse_error_code_without_default_namespace() -> None: | ||
actual = parse_error_code("FooError", None) | ||
assert actual is None |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.