blob: b13d6678603a885f314467995f27018fc4451d2d [file] [log] [blame]
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Conversions between snake-, camel-, and shouty-case names."""
from enum import Enum
class Case(str, Enum):
SNAKE = "snake_case"
SHOUTY = "SHOUTY_CASE"
CAMEL = "CamelCase"
K_CAMEL = "kCamelCase"
# Map of (from, to) cases to their conversion function. Initially only contains
# identity case conversions, additional conversions are added with the
# _case_conversion decorator.
_case_conversions = {(case.value, case.value): lambda x: x for case in Case}
def _case_conversion(case_from, case_to):
"""Decorator to dynamically dispatch case conversions at runtime."""
def _func(f):
_case_conversions[case_from, case_to] = f
return f
return _func
@_case_conversion(Case.SNAKE, Case.CAMEL)
@_case_conversion(Case.SHOUTY, Case.CAMEL)
def snake_to_camel(name):
"""Convert from snake_case to CamelCase. Also works from SHOUTY_CASE."""
return "".join(word.capitalize() for word in name.split("_"))
@_case_conversion(Case.CAMEL, Case.K_CAMEL)
def camel_to_k_camel(name):
"""Convert from CamelCase to kCamelCase."""
return "k" + name
@_case_conversion(Case.SNAKE, Case.K_CAMEL)
@_case_conversion(Case.SHOUTY, Case.K_CAMEL)
def snake_to_k_camel(name):
"""Converts from snake_case to kCamelCase. Also works from SHOUTY_CASE."""
return camel_to_k_camel(snake_to_camel(name))
def convert_case(case_from, case_to, value):
"""Converts cases based on runtime case values.
Note: Cases can be strings or enum values."""
return _case_conversions[case_from, case_to](value)
def is_case_conversion_supported(case_from, case_to):
"""Determines if a case conversion would be supported"""
return (case_from, case_to) in _case_conversions