Skip to content
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

feat: Allow config substitutions to specify re replace strings #755

Merged
merged 1 commit into from
Feb 25, 2023
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
6 changes: 6 additions & 0 deletions tests/formats/dataclass/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ def test_constant_value(self):
attr = AttrFactory.create(types=[AttrTypeFactory.create(alias="alias")])
self.assertEqual("Alias", self.filters.constant_value(attr))

def test_apply_substitutions_with_regexes(self):
self.filters.substitutions[ObjectType.CLASS]["(.*)Class"] = "\\1Type"

actual = self.filters.apply_substitutions("FooClass", ObjectType.CLASS)
self.assertEqual("FooType", actual)

@mock.patch.object(Filters, "field_default_value")
def test_field_definition(self, mock_field_default_value):
mock_field_default_value.side_effect = [1, False]
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_create(self):
' <Substitution type="package" search="http://schemas.xmlsoap.org/wsdl/soap/" replace="soap"/>\n'
' <Substitution type="package" search="http://schemas.xmlsoap.org/wsdl/soap12/" replace="soap12"/>\n'
' <Substitution type="package" search="http://schemas.xmlsoap.org/soap/envelope/" replace="soapenv"/>\n'
' <Substitution type="class" search="Class" replace="Type"/>\n'
' <Substitution type="class" search="(.*)Class$" replace="\\1Type"/>\n'
" </Substitutions>\n"
"</Config>\n"
)
Expand Down
2 changes: 1 addition & 1 deletion xsdata/formats/dataclass/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def class_name(self, name: str) -> str:

def apply_substitutions(self, name: str, obj_type: ObjectType) -> str:
for search, replace in self.substitutions[obj_type].items():
name = re.sub(search, replace, name)
name = re.sub(rf"{search}", rf"{replace}", name)

return name

Expand Down
4 changes: 3 additions & 1 deletion xsdata/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,9 @@ def create(cls) -> "GeneratorConfig":
)

obj.substitutions.substitution.append(
GeneratorSubstitution(type=ObjectType.CLASS, search="Class", replace="Type")
GeneratorSubstitution(
type=ObjectType.CLASS, search="(.*)Class$", replace="\\1Type"
)
)

return obj
Expand Down