-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: python handles arrays of deep object query parameters (#…
- Loading branch information
1 parent
c6a7748
commit a9e5871
Showing
49 changed files
with
1,396 additions
and
411 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,56 @@ | ||
from collections import ChainMap | ||
from typing import Any, Dict, Optional | ||
from typing import Any, Dict, List, Optional, Tuple | ||
|
||
import pydantic | ||
|
||
|
||
# Flattens dicts to be of the form {"key[subkey][subkey2]": value} where value is not a dict | ||
def traverse_query_dict(dict_flat: Dict[str, Any], key_prefix: Optional[str] = None) -> Dict[str, Any]: | ||
result = {} | ||
def traverse_query_dict(dict_flat: Dict[str, Any], key_prefix: Optional[str] = None) -> List[Tuple[str, Any]]: | ||
result = [] | ||
for k, v in dict_flat.items(): | ||
key = f"{key_prefix}[{k}]" if key_prefix is not None else k | ||
if isinstance(v, dict): | ||
result.update(traverse_query_dict(v, key)) | ||
result.extend(traverse_query_dict(v, key)) | ||
elif isinstance(v, list): | ||
for arr_v in v: | ||
if isinstance(arr_v, dict): | ||
result.extend(traverse_query_dict(arr_v, key)) | ||
else: | ||
result.append((key, arr_v)) | ||
else: | ||
result[key] = v | ||
result.append((key, v)) | ||
return result | ||
|
||
|
||
def single_query_encoder(query_key: str, query_value: Any) -> Dict[str, Any]: | ||
def single_query_encoder(query_key: str, query_value: Any) -> List[Tuple[str, Any]]: | ||
if isinstance(query_value, pydantic.BaseModel) or isinstance(query_value, dict): | ||
if isinstance(query_value, pydantic.BaseModel): | ||
obj_dict = query_value.dict(by_alias=True) | ||
else: | ||
obj_dict = query_value | ||
return traverse_query_dict(obj_dict, query_key) | ||
elif isinstance(query_value, list): | ||
encoded_values: List[Tuple[str, Any]] = [] | ||
for value in query_value: | ||
if isinstance(value, pydantic.BaseModel) or isinstance(value, dict): | ||
if isinstance(value, pydantic.BaseModel): | ||
obj_dict = value.dict(by_alias=True) | ||
elif isinstance(value, dict): | ||
obj_dict = value | ||
|
||
return {query_key: query_value} | ||
encoded_values.extend(single_query_encoder(query_key, obj_dict)) | ||
else: | ||
encoded_values.append((query_key, value)) | ||
|
||
return encoded_values | ||
|
||
def encode_query(query: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any]]: | ||
return dict(ChainMap(*[single_query_encoder(k, v) for k, v in query.items()])) if query is not None else None | ||
return [(query_key, query_value)] | ||
|
||
|
||
def encode_query(query: Optional[Dict[str, Any]]) -> Optional[List[Tuple[str, Any]]]: | ||
if query is None: | ||
return None | ||
|
||
encoded_query = [] | ||
for k, v in query.items(): | ||
encoded_query.extend(single_query_encoder(k, v)) | ||
return encoded_query |
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 +1 @@ | ||
3.10.2 | ||
3.10.3 |
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
45 changes: 35 additions & 10 deletions
45
seed/python-sdk/alias-extends/src/seed/core/query_encoder.py
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
34 changes: 26 additions & 8 deletions
34
seed/python-sdk/alias-extends/tests/utils/test_query_encoding.py
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
45 changes: 35 additions & 10 deletions
45
seed/python-sdk/api-wide-base-path/src/seed/core/query_encoder.py
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.