-
Notifications
You must be signed in to change notification settings - Fork 115
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
Add ConverterProvider type to handle Converter retrieval for parameterized type #578
Comments
I am unclear about your use case. As for your suggestion, it's unclear who implement the spi mentioned in your issue. Once you got hold of the converter, in what use case you are using it for? |
Let me clarify my needs. At properties injection point, I would like to be able to convert properties coming from my
It seems to me that there is for now nothing in the spec allowing/describing how to do that. The only option for now seems to be to create a type like following
Then create and implement a
So to my mind this is a bit frustrating to not be able to take advantage of the generic type without being forced to subtype it. To sum up, my proposal is to provide a simple way for user to convert properties into their own custom parameterized type in addition to the Array, List and Set that are by default supported according to the spec. At this point am I wrong or am i missing something ?
The For my previous example it could be something like:
For all properties injection point discovered by the Mp-Config implementation, the Mp-Config implementation will have to invoke all Is it clearer or am I missing something ? Thanks |
IMHO you're not missing anything. Your use case is highlighting a gap (perhaps) in the specification: the specification does not say anything about a Your proposal, though, as written, wouldn't handle something like My personal side project MicroProfile Config implementation does all this to a limited extent; you can see examples of it here: https://github.com/microbean/microbean-microprofile-config/blob/master/src/test/java/org/microbean/microprofile/config/TestConverters.java. The {holds nose} 😄 code that accomplishes this probably not as well as it should is here: https://github.com/microbean/microbean-microprofile-config/blob/master/src/main/java/org/microbean/microprofile/config/ConversionHub.java#L366-L487 It has been a while since I've looked at it so it should be used just as an example of how an implementation could do this, and not as an example of any best practices! Note that this sort of thing doesn't require a separate interface. The specification would just have to require implementations to handle type resolution properly when discovering converters. Currently it does not. |
Hey @ljnelson, Happy to read that you spotted the same little "gap" in the specification.
Actually my proposal handles this case too. If the injection point is like:
Then the MP-config implementation will invoke user provided
I agree with you. But as you may have experiment with your side project, trying to produce a generic code to deal with all possible parameterized type injection cases can be really complex. Another thing in favor of the |
Obviously either approach then works. I am always personally in favor of fewer constructs in a specification. Fewer things to maintain is usually better. Lastly, most MicroProfile vendors are already familiar with generic type wrangling since they have to work with JAXB and CDI; I don't think it's an undue burden to make them (us!) do this work (there's probably whole swaths of code that can be reused). That's just my opinion, of course. |
Actually, our two approaches to fix this gap can both be part of the spec and work together. I explain. Suppose the following injection points:
To provide converters for those injection points, the developper can either
and
and in this case your proposal fit perfectly.
and then provide a
The concept of having both WDYT ? |
This seems very similar with the behaviour of Maybe we should consider using the same approach? |
Yes, just like in JAX-RS this issue has two chapters:
About the "second chapter" I agree with you that |
Thanks @NicoNes for further explaining this! Let's agree on the use case: As an end user, I want to have the Type parameter honoured on performing injection or programmantic lookup.
As for the solution to the above use case, for simplicity, we should put the support in the implementation like what @ljnelson suggested. I only expect the end user to provide a converter to convert String to MyObject. The implementation should do an exact search first if not found out. Then fallback to assignable operation. |
@Emily-Jiang The use case are correct !
Actually when doing:
The So my proposal based on @radcortez suggestion is to rely on either
and add a method in
|
We probably need to stay away from adding a direct dependency to CDI, so my preference would be to add a custom the like JAX-RS. |
I am not convinced whether we need to add TypeLiteral as yet. I think for the programmatic look, the runtime can get the type parameter from the field type. I think the Converter api needs to be updated to take in the Type Parameter, which was discussed in the past. I think with this change the use case should be fulfilled. |
@Emily-Jiang I don't get your point. How it is possible ? |
The <T> T getValue(String property, Type genericType); This still allows to use JAX-RS List<Integer> list = config.getValue("prop", new GenericType<List<Integer>>{}.getType()) but it also allows to use other helpers that provide So far this is fairly straight forward. One detail question might be whether to return The harder problem is how to make conversion fully generic. The proposed provider to me falls short as it does not allow composition based on the converters defined within the registry. One aspect that has not been raised are upper bound types.This would also include |
All of this. +1000. Java Language Specification-compliant type assignability using |
@jbee, about using this:
What happen if user invokes this method with its own I personnaly prefer :
Except the fact that using EDIT: About all other points I agree with you and @ljnelson that MP-Config vendors can get the I said exact match because if user adds following
and ask for following conversion:
The |
Independent of how it is passed you cannot be sure where the underlying This usually isn't a problem (and you will find that there are many implementation classes for the |
Well I'm not sure you get my point. As a user I can create this valid following
If I use it to invoke So to not have to deal with this kind of problem my proposal is to rely on So it just my opinion but I think it's one of the reaon why Am I missing something ? |
If
Either |
Well sorry guys, I deleted my previous answer because it tooks me long to get your point @jbee about your following statement but I think I get it now:
So if my understanding is right, you are saying that If my understanding is right I agree with you on this one.
I agree with you here too. But I still got a problem with your signature proposal It does not enforce type inference at all in all cases. It works fine for the following statetement:
But it does not automatically works for following one and requires user to use type witness (
So unless I am wrong about the type inference issue, I think the MP-Config spec should globally tries it best to ensure type inference when possible. So I think that a custom
This could be a good compromise to solve both the issue you spotted in my proposal with
WDYT? |
In summary, as @NicoNes points out the downside of |
Description
Suppose that I create a parameterized type as follow:
If I want to use this parameterized type in the following injection points
I have two provide a global converter or an implicit converter.
But the problem with both of them is they won't tell nothing me about the type argument of the parameterized type at the given injection point.
Proposal
So my proposal would be to add a
ConverterProvider
type as follow to handle this caseJust like
Converter
thisConverterProvider
would be discovered by SPI or provided programatically.Then it would be invoked when resolving injection points to retrieve the right
Converter
.If no
ConverterProvider
is available or if it does not provide no eligibleConverter
then the currentConverter
retrieval mechanism is applied.The concept of
ConverterProvider
is taken form JAX-RS spec where the equivalent isParamConverterProvider
.WDYT ? Am I missing something ?
Thanks
The text was updated successfully, but these errors were encountered: