- Notifications
You must be signed in to change notification settings - Fork673
fix: use the [] after key names for array variables inparams
#1699
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
15 changes: 12 additions & 3 deletionsgitlab/mixins.py
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
22 changes: 15 additions & 7 deletionsgitlab/types.py
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 |
---|---|---|
@@ -63,8 +63,8 @@ def get(self) -> Any: | ||
def set_from_cli(self, cli_value: Any) -> None: | ||
self._value = cli_value | ||
def get_for_api(self, *, key: str) ->Tuple[str,Any]: | ||
return(key,self._value) | ||
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
class _ListArrayAttribute(GitlabAttribute): | ||
@@ -76,20 +76,28 @@ def set_from_cli(self, cli_value: str) -> None: | ||
else: | ||
self._value = [item.strip() for item in cli_value.split(",")] | ||
def get_for_api(self, *, key: str) ->Tuple[str, str]: | ||
# Do not comma-split single value passed as string | ||
if isinstance(self._value, str): | ||
return(key,self._value) | ||
if TYPE_CHECKING: | ||
assert isinstance(self._value, list) | ||
return(key,",".join([str(x) for x in self._value])) | ||
class ArrayAttribute(_ListArrayAttribute): | ||
"""To support `array` types as documented in | ||
https://docs.gitlab.com/ee/api/#array""" | ||
def get_for_api(self, *, key: str) -> Tuple[str, Any]: | ||
if isinstance(self._value, str): | ||
return (f"{key}[]", self._value) | ||
if TYPE_CHECKING: | ||
assert isinstance(self._value, list) | ||
return (f"{key}[]", self._value) | ||
class CommaSeparatedListAttribute(_ListArrayAttribute): | ||
"""For values which are sent to the server as a Comma Separated Values | ||
@@ -98,8 +106,8 @@ class CommaSeparatedListAttribute(_ListArrayAttribute): | ||
class LowercaseStringAttribute(GitlabAttribute): | ||
def get_for_api(self, *, key: str) ->Tuple[str, str]: | ||
return(key,str(self._value).lower()) | ||
class FileAttribute(GitlabAttribute): | ||
39 changes: 30 additions & 9 deletionsgitlab/utils.py
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 |
---|---|---|
@@ -55,34 +55,53 @@ def response_content( | ||
def _transform_types( | ||
data: Dict[str, Any], | ||
custom_types: dict, | ||
*, | ||
transform_data: bool, | ||
transform_files: Optional[bool] = True, | ||
) -> Tuple[dict, dict]: | ||
"""Copy the data dict with attributes that have custom types and transform them | ||
before being sent to the server. | ||
``transform_files``: If ``True`` (default), also populates the ``files`` dict for | ||
FileAttribute types with tuples to prepare fields for requests' MultipartEncoder: | ||
https://toolbelt.readthedocs.io/en/latest/user.html#multipart-form-data-encoder | ||
``transform_data``: If ``True`` transforms the ``data`` dict with fields | ||
suitable for encoding as query parameters for GitLab's API: | ||
https://docs.gitlab.com/ee/api/#encoding-api-parameters-of-array-and-hash-types | ||
Returns: | ||
A tuple of the transformed data dict and files dict""" | ||
# Duplicate data to avoid messing with what the user sent us | ||
data = data.copy() | ||
if not transform_files and not transform_data: | ||
return data, {} | ||
files = {} | ||
for attr_name,attr_class in custom_types.items(): | ||
if attr_name not in data: | ||
continue | ||
gitlab_attribute =attr_class(data[attr_name]) | ||
# if the typeis FileAttribute we need to pass the data as file | ||
if isinstance(gitlab_attribute, types.FileAttribute) and transform_files: | ||
key =gitlab_attribute.get_file_name(attr_name) | ||
files[attr_name] = (key, data.pop(attr_name)) | ||
continue | ||
if not transform_data: | ||
continue | ||
if isinstance(gitlab_attribute, types.GitlabAttribute): | ||
key, value = gitlab_attribute.get_for_api(key=attr_name) | ||
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if key != attr_name: | ||
del data[attr_name] | ||
data[key] = value | ||
return data, files | ||
@@ -94,6 +113,8 @@ def copy_dict( | ||
) -> None: | ||
for k, v in src.items(): | ||
if isinstance(v, dict): | ||
# NOTE(jlvillal): This provides some support for the `hash` type | ||
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
# https://docs.gitlab.com/ee/api/#hash | ||
# Transform dict values to new attributes. For example: | ||
# custom_attributes: {'foo', 'bar'} => | ||
# "custom_attributes['foo']": "bar" | ||
5 changes: 5 additions & 0 deletionstests/functional/api/test_groups.py
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
38 changes: 38 additions & 0 deletionstests/unit/mixins/test_mixin_methods.py
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
18 changes: 9 additions & 9 deletionstests/unit/test_types.py
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
28 changes: 25 additions & 3 deletionstests/unit/test_utils.py
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
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.