-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add EnumNamingStrategy SPI #2100
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
Conversation
Add a new EnumNamingStrategy SPI which can be used for customizing the way enums are matched by name. It is similar to the AccessorNamingStrategy such that it allows implementors to provide a custom way of defining a property.
|
@filiphr I've tested your proposed solution this morning, and it is getting there. However, I have identified 2 problems that we need to resolve: The default value This means that there are 2 enum constants that map to On the "java side" there are no such values, but standard practice is to not populate the field at all ("null") The current generated implementation picks one of these (seems to be the last parsed) for the generated code, and that is This however is an illegal value to set, and at runtime the stub will throw an exception; Therefore in order to make it correct the EnumNamingStrategy should expose something like NullValueCheckStrategy |
|
Thanks for the feedback @seime. I have some questions: The default value So you are saying that nothing should map to NullValueCheckStrategy Not following here. If you defined |
|
@chris922 the difference between this and #2089 is that for the previous PR you have to explicitly implement how you want to do a transformation. e.g. This allows defining a prefix, suffix, etc. on a case by case basis. With this PR you can implement a custom name based strategy based on certain rules. For example the style guide for Protobuf. |
|
Okay, thanks for the clarification. Then I got it correctly. Basically #2089 is
vs. here
.. that will be used for all enum mappings |
Correct @filiphr . If an enum has not been set, it will have the value 0. proto definition: Generated java stub: Generated enum mapping:
The mapper definiton: The code generated code : Please see the comment |
|
I think that now I understand what you mean. You expect the mapping from With this PR: Expected: Am I on the right track? Does this branch from your example generate 100% what you expect? I tried to be a bit smart and automatically detect the mapping from Regarding the |
|
@filiphr Your current PR generates the following (on my computer, I have not checked how you select which EnumStatus constant to generate - maybe indeterministic?) but I do want this which is the opposite of what you wrote (you probably copy pasted wrongly?) I pushed my updated branch based on this PR: The code here https://github.com/entur/mapstruct-spi-protobuf/blob/valuemapping_accessor_spi/spi-impl/src/main/java/no/entur/mapstruct/example/protobuf/ProtobufEnumConstantNamingStrategy.java Ref |
|
Thanks a lot for the patience @seime. I understand it now. I wanted to avoid the I will remove this smartness and add a method for that. However, I'll keep the special |
|
@seime I've updated this PR by adding the new method. I think that now it covers your use case 100% |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Filip,
I think you need to update the specifications.. Also, I'm not entirely sure whether this is the proper way of handling. I can imagine that sometimes you don't want to apply the strategy and now its suddenly always there.
For instance,, suppose you would apply this strategy to your organisation. Would I expect, working in a team totally not related to a team working on protobuf functionality this kind of behaviour? I'm still not sure whether SPI is a proper way forward.
| public class CheeseTypeMapperImpl implements CheeseTypeMapper { | ||
| @Override | ||
| public CheeseType map(CustomCheeseType cheese) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't CheeseTypeMapperImpl not implement CheeseTypeMapper?
The mapper you defined has signature
@Mapper
public interface CheeseTypeMapper {
CheeseType map(CheeseTypeProtobuf cheese);
CheeseTypeProtobuf map(CheeseType cheese);
}I'm reading this now several times, and I don't understand
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like I have made a mistake there. Thanks for spotting it
processor/src/test/java/org/mapstruct/ap/test/value/spi/CustomEnumNamingStrategyTest.java
Show resolved
Hide resolved
|
Thanks a lot for the review @sjaakd.
Yes I'll update the documentation and I'll try to make it clearer. This strategy passes the enum you are trying to map so I see it similar as an
That's why the protobuf functionality will only be applied if the enum is a protobuf enum. Btw we don't provide out of the box support for this. On top of it you can still use the original names of the enums in |
|
@sjaakd, I've updated the documentation. Does it make more sense now? I removed the mention of Protobuf in order to keep it simpler. |
|
Hey Filip.. I think its ok.. I understand that the strategy allows for spotting certain enum types and based on the type adapt the enum name. It's ok. Although I think that the use cases is (in the end) very protobuf specific (I wonder whether there would be even other cases, but I'd like to be proven wrong). .. I guess the protobuf one would end up somewhere in our examples or even as a pre-delivered SPI?. |
Hey Filip.. did you push your changes? |
Yes the use case is very protobuf specific right now. Let's see what the future brings, there might be some other use cases as well. Remember
Seems like I forgot 🤦. Just pushed now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.. looks way better :) nice job.
|
@filiphr LGTM! |
|
Thanks a lot for the review @seime |
Add a new EnumNamingStrategy SPI which can be used for customizing the way enums are matched by name.
It is similar to the AccessorNamingStrategy such that it allows implementors to provide a custom way of defining a property.
Related to #796, #1220, #1789 and #1667
@seime this is the PR with the SPI allowing custom way of name based mapping enums. I took the interface you did in #2045 and iterated with my ideas on top of it. Can you please have a look and tell me how it looks to you.