Skip to content

Fix bug when field gets converted to blacklisted word #36

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

Merged
merged 1 commit into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions json_to_models/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ def __init__(

@cached_method
def convert_class_name(self, name):
return prepare_label(name, convert_unicode=self.convert_unicode)
return prepare_label(name, convert_unicode=self.convert_unicode, to_snake_case=False)

@cached_method
def convert_field_name(self, name):
return inflection.underscore(prepare_label(name, convert_unicode=self.convert_unicode))
return prepare_label(name, convert_unicode=self.convert_unicode, to_snake_case=True)

def generate(self, nested_classes: List[str] = None, bases: str = None, extra: str = "") \
-> Tuple[ImportPathList, str]:
Expand Down Expand Up @@ -279,13 +279,15 @@ def sort_kwargs(kwargs: dict, ordering: Iterable[Iterable[str]]) -> dict:
return sorted_dict


def prepare_label(s: str, convert_unicode: bool) -> str:
if s in blacklist_words:
s += "_"
def prepare_label(s: str, convert_unicode: bool, to_snake_case: bool) -> str:
if convert_unicode:
s = unidecode(s)
s = re.sub(r"\W", "", s)
if not ('a' <= s[0].lower() <= 'z'):
if '0' <= s[0] <= '9':
s = ones[int(s[0])] + "_" + s[1:]
if to_snake_case:
s = inflection.underscore(s)
if s in blacklist_words:
s += "_"
return s
8 changes: 7 additions & 1 deletion test/test_code_generation/test_attrs_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def test_attrib_kwargs_sort():
assert 0, "XPass"



def field_meta(original_name):
return f"metadata={{'{METADATA_FIELD_NAME}': '{original_name}'}}"

Expand Down Expand Up @@ -90,6 +89,7 @@ class Test:
"qwerty": FloatString,
"asdfg": DOptional(int),
"dict": DDict(int),
"ID": int,
"not": bool,
"1day": int,
"день_недели": str,
Expand Down Expand Up @@ -125,6 +125,11 @@ class Test:
"type": "Dict[str, int]",
"body": f"attr.ib({field_meta('dict')})"
},
"ID": {
"name": "id_",
"type": "int",
"body": f"attr.ib({field_meta('ID')})"
},
"not": {
"name": "not_",
"type": "bool",
Expand Down Expand Up @@ -155,6 +160,7 @@ class Test:
foo: int = attr.ib()
qwerty: FloatString = attr.ib()
dict_: Dict[str, int] = attr.ib({field_meta('dict')})
id_: int = attr.ib({field_meta('ID')})
not_: bool = attr.ib({field_meta('not')})
one_day: int = attr.ib({field_meta('1day')})
den_nedeli: str = attr.ib({field_meta('день_недели')})
Expand Down