Skip to content

Commit

Permalink
fix: stringcase replaced by casefy. Closes #341 (#347)
Browse files Browse the repository at this point in the history
Co-authored-by: Marcos Schroh <marcos.schroh@kpn.com>
  • Loading branch information
marcosschroh and marcosschroh authored Jun 15, 2023
1 parent f3e6c6a commit 9891f87
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 64 deletions.
52 changes: 27 additions & 25 deletions dataclasses_avroschema/case.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import typing

import stringcase
import casefy

from .field_utils import ENUM

# Summary from https://github.com/okunishinishi/python-stringcase
# stringcase.camelcase('foo_bar_baz') # => "fooBarBaz"
# stringcase.capitalcase('foo_bar_baz') # => "Foo_bar_baz"
# stringcase.constcase('FooBarBaz') # => "_FOO_BAR_BAZ"
# stringcase.lowercase('FooBarBaz') # => "foobarbaz"
# stringcase.pascalcase('FooBarBaz') # => "FooBarBaz"
# stringcase.pathcase('foo_bar_baz') # => "foo/bar/baz"
# stringcase.snakecase('FooBarBaz') # => "foo_bar_baz"
# stringcase.spinalcase('FooBarBaz') # => "-foo-bar-baz"
# stringcase.trimcase('FooBarBaz') # => "FooBarBaz"
# stringcase.uppercase('FooBarBaz') # => "FOOBARBAZ"
# stringcase.alphanumcase('Foo_123 Bar!') # =>'Foo123Bar'
# casefy.camelcase('foo_bar_baz') # => "fooBarBaz"
# casefy.capitalcase('foo_bar_baz') # => "Foo_bar_baz"
# casefy.constcase('FooBarBaz') # => "_FOO_BAR_BAZ"
# casefy.lowercase('FooBarBaz') # => "foobarbaz"
# casefy.pascalcase('FooBarBaz') # => "FooBarBaz"
# casefy.pathcase('foo_bar_baz') # => "foo/bar/baz"
# casefy.snakecase('FooBarBaz') # => "foo_bar_baz"
# casefy.kebabcase('FooBarBaz') # => "-foo-bar-baz"
# casefy.upperkebabcase('FooBarBaz') # => "FOO-BAR"
# casefy.trimcase('FooBarBaz') # => "FooBarBaz"
# casefy.uppercase('FooBarBaz') # => "FOOBARBAZ"
# casefy.alphanumcase('Foo_123 Bar!') # =>'Foo123Bar'

CAMELCASE = "camelcase"
CAPITALCASE = "capitalcase"
Expand All @@ -25,23 +25,25 @@
PATHCASE = "PATHCASE"
SNAKECASE = "snakecase"
SPINALCASE = "spinalcase"
UPPERSPINALCASE = "upperkebabcase"
TRIMCASE = "trimcase"
UPPERCASE = "uppercase"
ALPHANUMCASE = "alphanumcase"


CASE_TO_FUNC = {
CAMELCASE: stringcase.camelcase,
CAPITALCASE: stringcase.capitalcase,
CONSTCASE: stringcase.constcase,
LOWERCASE: stringcase.lowercase,
PASCALCASE: stringcase.pascalcase,
PATHCASE: stringcase.pathcase,
SNAKECASE: stringcase.snakecase,
SPINALCASE: stringcase.spinalcase,
TRIMCASE: stringcase.trimcase,
UPPERCASE: stringcase.uppercase,
ALPHANUMCASE: stringcase.alphanumcase,
CAMELCASE: casefy.camelcase,
CAPITALCASE: casefy.capitalcase,
CONSTCASE: casefy.constcase,
LOWERCASE: casefy.lowercase,
PASCALCASE: casefy.pascalcase,
PATHCASE: lambda value: casefy.separatorcase(value, separator="/"),
SNAKECASE: casefy.snakecase,
SPINALCASE: casefy.kebabcase,
UPPERSPINALCASE: casefy.upperkebabcase,
TRIMCASE: str.strip,
UPPERCASE: casefy.uppercase,
ALPHANUMCASE: casefy.alphanumcase,
}


Expand All @@ -50,7 +52,7 @@ def case_item(item: typing.Dict, case_type: str) -> typing.Dict:
new_field = {}
for key, value in item.items():
if key == "name":
case_name = case_func(value)
case_name = case_func(value) # type: ignore
new_field[key] = case_name
elif isinstance(value, dict) and value.get("name"):
# means that it is a complex type with a record
Expand Down
12 changes: 6 additions & 6 deletions dataclasses_avroschema/model_generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from dataclasses import dataclass, field
from string import Template

import casefy
import fastavro
import stringcase

from dataclasses_avroschema import field_utils, serialization
from dataclasses_avroschema.types import JsonDict
Expand Down Expand Up @@ -121,7 +121,7 @@ def render_class(self, *, schema: JsonDict) -> str:
"""
Render the class generated from the schema
"""
name: str = stringcase.pascalcase(schema["name"])
name: str = casefy.pascalcase(schema["name"])
record_fields: typing.List[JsonDict] = schema["fields"]

# Sort the fields according whether it has a default value
Expand Down Expand Up @@ -222,7 +222,7 @@ def render_field(self, field: JsonDict, model_name: str) -> str:
record = f"\n{self.render_class(schema=field)}"
is_complex_type = True
self.extras.append(record)
language_type = stringcase.pascalcase(field["name"])
language_type = casefy.pascalcase(field["name"])
else:
# Native field or Logical type using a native
language_type = self.get_language_type(type=type, model_name=model_name)
Expand Down Expand Up @@ -378,10 +378,10 @@ def parse_enum(self, field: JsonDict) -> str:
self.imports.add("import enum")

field_name: str = field["name"]
enum_name = stringcase.pascalcase(field_name)
enum_name = casefy.pascalcase(field_name)
symbols = self.field_identation.join(
[
templates.enum_symbol_template.safe_substitute(key=stringcase.uppercase(symbol), value=f'"{symbol}"')
templates.enum_symbol_template.safe_substitute(key=casefy.uppercase(symbol), value=f'"{symbol}"')
for symbol in field["symbols"]
]
)
Expand Down Expand Up @@ -436,7 +436,7 @@ def get_field_default(self, *, field_type: str, default: typing.Any, name: str)
elif field_type == field_utils.BYTES:
return f'b"{default}"'
elif isinstance(field_type, dict) and field_type.get("type") == field_utils.ENUM:
return f"{stringcase.pascalcase(field_type['name'])}.{stringcase.uppercase(default)}"
return f"{casefy.pascalcase(field_type['name'])}.{casefy.uppercase(default)}"
elif isinstance(
default,
(
Expand Down
3 changes: 2 additions & 1 deletion docs/case.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ resulting in
|pascalcase|'FooBarBaz' # => "FooBarBaz"|
|pathcase|'foo_bar_baz' # => "foo/bar/baz"|
|snakecase|'FooBarBaz' # => "foo_bar_baz"|
|spinalcase|'FooBarBaz' # => "-foo-bar-baz"|
|upperspinalcase|'FooBarBaz' # => "-foo-bar-baz"|
|spinalcase|'FooBarBaz' # => "FOO-BAR"|
|trimcase|'FooBarBaz' # => "FooBarBaz"|
|uppercase|'FooBarBaz' # => "FOOBARBAZ|
|alphanumcase|'Foo_123 Bar!' # =>'Foo123Bar'|
25 changes: 13 additions & 12 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ python = "^3.7"
fastavro = "^1.7.3"
dacite = "^1.8.0"
faker = "^18.3.1"
stringcase = "^1.2.0"
inflector = "^3.1.0"
pydantic = {version = "^1.10.7", optional = true}
faust-streaming = {version = "^0.10.11", optional = true}
casefy = "^0.1.7"

[tool.poetry.group.dev.dependencies]
mypy = "^1"
Expand Down
Loading

0 comments on commit 9891f87

Please sign in to comment.