-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add serialize_where.py #7
Labels
Comments
Doing the same manually in GPT-4 chat: from typing import Any, Union
from collections.abc import Iterable
_boolExpFields = {} # Assuming this is a global variable in your JavaScript code
_serialize = {} # Assuming this is a global variable in your JavaScript code
def serialize_where(exp: Any, env: str = 'links') -> Any:
if isinstance(exp, list):
return [serialize_where(e, env) for e in exp]
elif isinstance(exp, dict):
keys = exp.keys()
result = {}
for key in keys:
key_type = type(exp[key])
setted = False
is_id_field = key in ['type_id', 'from_id', 'to_id']
if env == 'links':
if key_type in (str, int):
if key == 'value' or key == key_type.__name__:
setted = result[key_type.__name__] = {'value': {'_eq': exp[key]}}
else:
setted = result[key] = {'_eq': exp[key]}
elif key not in _boolExpFields and isinstance(exp[key], list):
setted = result[key] = serialize_where(path_to_where(*exp[key]))
elif env == 'value':
if key_type in (str, int):
setted = result[key] = {'_eq': exp[key]}
ids = [
'rule_id', 'action_id', 'subject_id', 'object_id',
'link_id', 'tree_id', 'root_id', 'parent_id'
]
if (
key_type == dict
and '_type_of' in exp[key]
and (
(env == 'links' and (is_id_field or key == 'id')) or
(env == 'selector' and key == 'item_id') or
(env == 'can' and key in ids) or
(env == 'tree' and key in ids) or
(env == 'value' and key == 'link_id')
)
):
_temp = setted = {
'_by_item': {
'path_item_id': {'_eq': exp[key]['_type_of']},
'group_id': {'_eq': 0}
}
}
if key == 'id':
result['_and'] = [*result.get('_and', []), _temp]
else:
result[key[:-3]] = _temp
elif (
key_type == dict
and '_id' in exp[key]
and (
(env == 'links' and (is_id_field or key == 'id')) or
(env == 'selector' and key == 'item_id') or
(env == 'can' and key in ids) or
(env == 'tree' and key in ids) or
(env == 'value' and key == 'link_id')
)
and isinstance(exp[key]['_id'], list)
and len(exp[key]['_id']) >= 1
):
_temp = setted = serialize_where(
path_to_where(exp[key]['_id'][0], *exp[key]['_id'][1:]), 'links'
)
if key == 'id':
result['_and'] = [*result.get('_and', []), _temp]
else:
result[key[:-3]] = _temp
if not setted:
_temp = (
serialize_where(exp[key], env) if key in _boolExpFields else (
serialize_where(exp[key], _serialize.get(env, {}).get('relations', {}).get(key))
if _serialize.get(env, {}).get('relations', {}).get(key) else exp[key]
)
)
if key == '_and':
if '_and' in result:
result['_and'].extend(_temp)
else:
result['_and'] = _temp
else:
result[key] = _temp
return result
else:
if exp is None:
raise ValueError('undefined in query')
return exp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Translate this code to python and put it into serialize_where.py file
The text was updated successfully, but these errors were encountered: