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

Keep the same "case" in the class names #498

Closed
skinkie opened this issue May 25, 2021 · 5 comments · Fixed by #499
Closed

Keep the same "case" in the class names #498

skinkie opened this issue May 25, 2021 · 5 comments · Fixed by #499

Comments

@skinkie
Copy link
Contributor

skinkie commented May 25, 2021

At this moment it seems that xsData is transforming the casing of an element to something "pythonish" would it be possible to keep the "original" element casing as it matches the casing in the XML Schema and obviously the XML itself?

@tefra
Copy link
Owner

tefra commented May 25, 2021

Check the generator config , you can select the name case for classes and fields, set the class name case to originalCase

@skinkie
Copy link
Contributor Author

skinkie commented May 25, 2021

Below works, for me but when changing FieldName to originalCase as well, I end up with:

Traceback (most recent call last):
  File "/home/skinkie/.local/bin/xsdata", line 33, in <module>
    sys.exit(load_entry_point('xsdata', 'console_scripts', 'xsdata')())
  File "/mnt/storage/home/skinkie/Sources/xsdata/xsdata/__main__.py", line 8, in main
    cli()
  File "/usr/lib/python3.9/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/mnt/storage/home/skinkie/Sources/xsdata/xsdata/cli.py", line 195, in generate
    transformer.process(list(uris))
  File "/mnt/storage/home/skinkie/Sources/xsdata/xsdata/codegen/transformer.py", line 107, in process
    self.process_classes()
  File "/mnt/storage/home/skinkie/Sources/xsdata/xsdata/codegen/transformer.py", line 190, in process_classes
    writer.write(classes)
  File "/mnt/storage/home/skinkie/Sources/xsdata/xsdata/codegen/writer.py", line 33, in write
    for result in self.generator.render(classes):
  File "/mnt/storage/home/skinkie/Sources/xsdata/xsdata/formats/dataclass/generator.py", line 57, in render
    source=self.render_module(resolver, cluster),
  File "/mnt/storage/home/skinkie/Sources/xsdata/xsdata/formats/dataclass/generator.py", line 91, in render_module
    output = self.render_classes(resolver.sorted_classes())
  File "/mnt/storage/home/skinkie/Sources/xsdata/xsdata/formats/dataclass/generator.py", line 121, in render_classes
    return "\n\n\n".join(map(render_class, classes)) + "\n"
  File "/mnt/storage/home/skinkie/Sources/xsdata/xsdata/formats/dataclass/generator.py", line 114, in render_class
    load(template)
  File "/usr/lib/python3.9/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/lib/python3.9/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/lib/python3.9/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "/mnt/storage/home/skinkie/Sources/xsdata/xsdata/formats/dataclass/templates/class.jinja2", line 34, in top-level template code
    {{ attr.name|field_name(obj.name) }}: {{ attr|field_type(parents) }} = field(
  File "/mnt/storage/home/skinkie/Sources/xsdata/xsdata/formats/dataclass/filters.py", line 107, in field_name
    return self.field_case(safe_name, class_name=class_name)
  File "/mnt/storage/home/skinkie/Sources/xsdata/xsdata/models/config.py", line 88, in __call__
    return self.callback(string, **kwargs)
TypeError: return_input() got an unexpected keyword argument 'class_name'

xsdata generate -c config.xml -p onix --structure-style single-package ONIX_BookProduct_3.0_reference.xsd

<?xml version="1.0" encoding="UTF-8"?>
<Config xmlns="http://pypi.org/project/xsdata" version="21.4">
  <Output maxLineLength="79">
    <Package>generated</Package>
    <Format>dataclasses</Format>
    <Structure>filenames</Structure>
    <DocstringStyle>reStructuredText</DocstringStyle>
    <CompoundFields>false</CompoundFields>
  </Output>
  <Conventions>
    <ClassName case="originalCase" safePrefix="type"/>
    <FieldName case="snakeCase" safePrefix="value"/>
    <ConstantName case="screamingSnakeCase" safePrefix="value"/>
    <ModuleName case="snakeCase" safePrefix="mod"/>
    <PackageName case="snakeCase" safePrefix="pkg"/>
  </Conventions>
</Config>

@skinkie
Copy link
Contributor Author

skinkie commented May 25, 2021

In addition to that, the generated code has syntax errors;

Traceback (most recent call last):
  File "/mnt/storage/home/skinkie/Sources/onix/loader.py", line 2, in <module>
    from onix import ONIXMessage
  File "/mnt/storage/home/skinkie/Sources/onix/onix.py", line 17162
    class area attlist_nohref(Enum):
               ^
SyntaxError: invalid syntax

@skinkie
Copy link
Contributor Author

skinkie commented May 25, 2021

I have been using 52, but there is a new source release here: https://www.editeur.org/files/ONIX%203/ONIX_BookProduct_XSD_schema+codes_Issue_53.zip

@tefra
Copy link
Owner

tefra commented May 25, 2021

For the exception during the generation, there is a fix on master for that, thank you for reporting, I guess no-one tried it before for the field names.

For the second part unfortunately that's what the original case is supposed to do, return the original string without any modifications, even if the produced code has syntax errors.

Try one of the mixed cases mixedCase|mixedSnakeCase|mixedPascalCase that protect the original words naming case.

@tefra tefra closed this as completed May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants