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

Qute: shorter enum constant access #21854

Closed
FroMage opened this issue Dec 1, 2021 · 4 comments · Fixed by #22024
Closed

Qute: shorter enum constant access #21854

FroMage opened this issue Dec 1, 2021 · 4 comments · Fixed by #22024
Assignees
Labels
area/qute The template engine kind/enhancement New feature or request
Milestone

Comments

@FroMage
Copy link
Member

FroMage commented Dec 1, 2021

Description

At the moment we make enum values accessible in views using this declaration:

@TemplateData
public enum ContentStatus {
    NEW,
    ACCEPTED,
    REJECTED,
    DELETED;
}

Which then exposes values such as fr_epardaud_model_ContentStatus:NEW which is handy in case of name conflict, but not so much if you don't have any conflict.

The shorter behaviour can already be obtained with @TemplateData(namespace = TemplateData.SIMPLENAME) but I'd like to have another annotation that would be an alias for this. Perhaps @TemplateEnum?

@TemplateEnum
public enum ContentStatus {
    NEW,
    ACCEPTED,
    REJECTED,
    DELETED;
}

This would be usable in the views as ContentStatus:NEW.

Note that ATM I have a special case for accessing the values() enum static member:

    @TemplateExtension(namespace = "ReputationGainReason", matchName = "*")
    static ReputationGainReason reputaionGainReason(String value) {
       return ReputationGainReason.valueOf(value);
    }

    @TemplateExtension(namespace = "ReputationGainReason", matchName = "values", priority = 6)
    static ReputationGainReason[] reputaionGainReasonValues() {
       return ReputationGainReason.values();
    }

And I wonder if we should not automatically add it as for for all enums declared with @TemplateData and @TemplateEnum? So ContentStatus:values would call the .values() static method. WDYT?

Implementation ideas

No response

@FroMage FroMage added kind/enhancement New feature or request area/qute The template engine labels Dec 1, 2021
@quarkus-bot
Copy link

quarkus-bot bot commented Dec 1, 2021

/cc @mkouba

@mkouba
Copy link
Contributor

mkouba commented Dec 7, 2021

I like the idea of @TemplateEnum with behavior equivalent to @TemplateData(namespace = TemplateData.SIMPLENAME).

And I wonder if we should not automatically add it as for for all enums declared with @templatedata and @TemplateEnum? So ContentStatus:values would call the .values() static method. WDYT?

This already works with @TemplateData(namespace = TemplateData.SIMPLENAME), just try {#each ContentStatus:values}{it}{/} or ContentStatus:values[0].

@mkouba
Copy link
Contributor

mkouba commented Dec 7, 2021

In general, @TemplateData(namespace = TemplateData.SIMPLENAME) makes all public static members accessible...

@mkouba mkouba self-assigned this Dec 7, 2021
mkouba added a commit to mkouba/quarkus that referenced this issue Dec 8, 2021
mkouba added a commit to mkouba/quarkus that referenced this issue Dec 9, 2021
@quarkus-bot quarkus-bot bot added this to the 2.7 - main milestone Dec 9, 2021
Postremus pushed a commit to Postremus/quarkus that referenced this issue Dec 10, 2021
@FroMage
Copy link
Member Author

FroMage commented Dec 13, 2021

Thanks!

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

Successfully merging a pull request may close this issue.

2 participants