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

Enum mapping in provided bindings #6

Open
Paul2708 opened this issue Apr 5, 2020 · 4 comments
Open

Enum mapping in provided bindings #6

Paul2708 opened this issue Apr 5, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@Paul2708
Copy link

Paul2708 commented Apr 5, 2020

What are your thoughts on adding a default mapping for enums to ProvidedBindings?

Describe the solution you'd like
I already tried to do so here in an other project.
In this approach the argument will be mapped by the enum constant.

Describe alternatives you've considered
Another idea is to assume that the enum has two methods enum fromText(arg : String) and String toText() that will be called by reflections.
I'm not sure on how the performance will be influenced by reflection calls. So another idea would be to assume that the enum implements an interface with those methods. This would only work for enums, that are written by the end-user and not existing ones (obviously).

@Paul2708 Paul2708 added the enhancement New feature or request label Apr 5, 2020
@JohnnyJayJay
Copy link
Member

Good idea, this should be very straight-forward to implement. I don't think there is need for reflection or additional interfaces, I'll just use Enum.valueOf(Class, String) and Enum#name() respectively. The question is whether it should always call toUpperCase() on the argument or not.

@Paul2708
Copy link
Author

Paul2708 commented Apr 5, 2020

Well, by convention every enum constant is unique by its constant name in upper case.
Although the following code will work as well:

public enum Sample {
  test,
  TEST
}

So calling toUpperCase() will match two constants..

@JohnnyJayJay
Copy link
Member

Yes, that is the issue with this idea. On the other hand, making it very strict might result in the need to make own mapper implementations that are more convenient for end users in many cases.
I'm not really sure. I tend to the simple and strict solution. Theoretically speaking, you could also make a mapping interceptor that converts the next String to upper case or whatever is required by the enum, if you need this behaviour.

@JohnnyJayJay
Copy link
Member

EnumMappers are now part of the provided bindings and they use strict parsing. However, I also made it easier to request the mapping of different arguments, simplifying such things as string transformers, should you need them.
The EnumMapper itself is incredibly simple, fortunately.

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

No branches or pull requests

2 participants