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

Make "choice" field creation configurable #639

Closed
hcw70 opened this issue Jan 3, 2022 · 2 comments · Fixed by #641
Closed

Make "choice" field creation configurable #639

hcw70 opened this issue Jan 3, 2022 · 2 comments · Fixed by #641
Labels
enhancement New feature or request

Comments

@hcw70
Copy link

hcw70 commented Jan 3, 2022

I dont like the fact that sometimes
"choice" is generated as a attribute containing child elements, and sometimes it is called
"a_Or_b_Or_c" ...

I would like to have the
AttributeCompoundChoiceHandler
always emit "choice" (at least if a config option is set) instead of the "a_Or_b_Or_c" case as in

    def choose_name(self, target: Class, names: List[str]) -> str:
        reserved = set(map(get_slug, self.base_attrs(target)))
        reserved.update(map(get_slug, target.attrs))

        if len(names) > 3 or len(names) != len(set(names)):
            name = "choice"
        else:
            name = "_Or_".join(names)

        return ClassUtils.unique_name(name, reserved)

So make this something like:


    def choose_name(self, target: Class, names: List[str]) -> str:
        reserved = set(map(get_slug, self.base_attrs(target)))
        reserved.update(map(get_slug, target.attrs))

        if len(names) > 3 or len(names) != len(set(names)) or self.always_use_choice:
            name = "choice"
        else:
            name = "_Or_".join(names)

        return ClassUtils.unique_name(name, reserved)

I need to copy subtrees of xsdata instances, and so i need to fill the "choice" attribute of the parent_copy.
But if that attribute name is not constant, i ran into problems determining the container attribute of each element
separately.

@tefra
Copy link
Owner

tefra commented Jan 8, 2022

Awesome suggestion @hcw70, the new config options are now on master

@tefra tefra added the enhancement New feature or request label Jan 8, 2022
@tefra tefra closed this as completed in #641 Jan 8, 2022
tefra added a commit that referenced this issue Jan 8, 2022
@hcw70
Copy link
Author

hcw70 commented Jan 10, 2022

Nice! Quick! and good! (as usual)!

However, dunno why you have twice "false" in
<CompoundFields defaultName="choice" forceDefaultName="false">false</CompoundFields>
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants