explodinglabs / jsonrpcserver

Process incoming JSON-RPC requests in Python
MIT License
186 stars 40 forks source link

Add convert case feature back #216

Closed bcb closed 1 year ago

bcb commented 3 years ago

Re discussion #215, add the feature back to convert method and parameter names to snake case.

bcb commented 1 year ago

The user can do their own modifications to the request before calling dispatch.

This was the code used in version 4.

def convert_camel_case_string(name: str) -> str:
    """Convert camel case string to snake case"""
    string = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
    return re.sub("([a-z0-9])([A-Z])", r"\1_\2", string).lower()

def convert_camel_case_keys(original_dict: Dict[str, Any]) -> Dict[str, Any]:
    """Converts all keys of a dict from camel case to snake case, recursively"""
    new_dict = dict()
    for key, val in original_dict.items():
        if isinstance(val, dict):
            # Recurse
            new_dict[convert_camel_case_string(key)] = convert_camel_case_keys(val)
        else:
            new_dict[convert_camel_case_string(key)] = val
    return new_dict