You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HowTo use Builders for immutable mapping? (missing examples) #1755
I'm trying to use mapstruct on Android for mapping immutable AutoValue objects, but cannot for the live of me figure out how. There are no examples in mapstruct-examples which contain 'InjectionStrategy.CONSTRUCTOR' or '@MapperConfig' or 'immutable' or '@autovalue'. I found one class container 'builder' namely ProtobufAccessorNamingStrategy but that didn't help me much.
Nor could I find anything helpful on stackoverflow. The documentation mentions builder but not how to use it, it mentions BuilderProvider which is not mentioned elsewhere in reference-guide.
When trying to create a mapper for AutoValue I get
The return type com.example.mapstructtest.model.MyValueDto is an abstract class or interface. Provide a non abstract / non interface result type or a factory method.
but then the impl generated tries to do this which is not the autovallue it should use create method
private final Builder builder1 = new Builder();
I'll ask elsewhere for help. The purpose if this is to point to the lack of documentation.
Best Alex
Ps: I've forked an https://github.com/arberg/mapstruct-test which is an example project for mapstruct for Android. I could update it with AutoValue example, and it could also be copied into mapstruct-examples.
I finally figured out it was because I was using the fluent naming convention. I have added examples here for getter-naming convention with AutoValue (and Android).
I finally figured out it was because I was using the fluent naming convention. I have added examples here for getter-naming convention with AutoValue (and Android).
I do find that my comments regarding lack of documentation stil stands.
It's sometimes hard to write good documentation. We are (almost always) short of time to do the stuff, so feedback but also improvements are very much appreciated. Would you be able to issue a PR on our documentation to improve here?
Also, your example could help others. Can you strip it down to it essence and donate it to the examples repo (issue a PR)? Then its possible to refer to it from our documentation.
Last, but not least 😄 we use SO so that users can help themselves as much as is possible. Would you be able to answer your own question to add to the knowledge base?
Yeah, I get documentation can be hard to get time for and to make it clear. It always takes a couple of rolls.
I think I can do something. I would like to add the android example project as a subfolder with test of immutable @autovalue builders and demonstrate it just works on Android. There are other stuff in mapstruct, which I suspect do not work, name the /services text-file. I might also change a few lines in the reference documentation.
By donating the example, did you mean adding it as subfolder to https://github.com/mapstruct/mapstruct-examples
I could do that, I think that would be a great idea, it would have helped me a lot, and taken my frustration out of it, I'm sure :)
But before this, I have a few questions, so I can get the documentation and examples right.
I cannot get constructors to work. Here's the example code:
Is it correct that mapstruct as of 1.3 does not in any way support fluent getter-syntax for builders? I can see its possible somehowe with some NamingStrategy to get fluent accressors to work for getter/setter mapping, but a in reference says its not supported.
Your welcome, thank you for the positive response.
But before this, I have a few questions, so I can get the documentation and examples right.
I cannot get constructors to work. Here's the example code:
The injectionStrategy is intended for CDI / Spring / JSR303. So you cannot mix that with the default component model. We should probably put an error on that combination.
Is it correct that mapstruct as of 1.3 does not in any way support fluent getter-syntax for builders? I can see its possible somehowe with some NamingStrategy to get fluent accressors to work for getter/setter mapping, but a in reference says its not supported.
There's a difference by " support" and "used". Yes, we recognise fluent getters (accessors) and will generate code for that. No, the generated code will not be in fluent style. That will be far too tricky with all the null checks, nesting, etc.
thx for responses. I have enough for what I need for my project, but I'll keep going with questions for the examples. We can stop anytime you want.
The injectionStrategy is intended for CDI / Spring / JSR303. So you cannot mix that with the default component model. We should probably put an error on that combination.
Ahh, I see. I tried removing that from my builder-based mapper, and indeed it works without. Thank you.
There's a difference by " support" and "used". Yes, we recognise fluent getters (accessors) and will generate code for that. No, the generated code will not be in fluent style
I'm not sure what you mean by that, since you just generate the mapping, not the data classes themselves. I don't care that much about how your generated code looks, as long as it does its job. I have put together a few examples. If you could take a quick peek, I'm sure that would make it clear whether you support that style or not.
Here an Immutable constructor style mapping that does not work. Code is generated, but it just creates the objcects, it makes no mapping. I created constructor which takes no arguments, because the mapping annotation compiler complained. I just got the idea from the reference that this constructor style immutability should be supported
I just got the idea from the reference that this constructor style immutability should be supported
Where? Constructor immutability is not supported. It is high on our wish list 😄.
@filiphr .. do you see chance to take a look at this stuff? I think we should have some good examples on the builders in combination with different technologies (Lombok, AutoValue, etc).
Having good examples and reference documentation is always hard. @arberg we will really appreciate if you can prepare an example that would work for Android and AutoValue and submit a PR for our https://github.com/mapstruct/mapstruct-examples.
We also need to mention in the documentation (in the Supported builder frameworks in the Using Builders section of our documentation) that fluent getters do not work for AutoValue. @arberg would you like to contribute this to our documentation as well?
@sjaakd currently we do have integration tests with all the different supported frameworks. Here is the one for auto value. The rest are here
Is it correct that mapstruct as of 1.3 does not in any way support fluent getter-syntax for builders? I can see its possible somehowe with some NamingStrategy to get fluent accressors to work for getter/setter mapping, but a in reference says its not supported.
Yes MapStruct does not support fluent getter-syntax. What it does support is a fluent setter. A fluent setter is a setter that returns the same type as the type being modified. At the moment I don't see possibility for MapStruct to support fluent getters. The reason for this is that in this case MapStruct would consider all methods that return something as a property and that would be to invasive. We already have some problems with the fluent setters (although they are more constraint and easier to detect).
I just got the idea from the reference that this constructor style immutability should be supported
Yes this is not supported, but very high on our wishlist. I would suggest to you to follow #73 for this.
Thank you both. I have made a push request for example and reference documentation. I have included a fluent-setter example, and noted how it does not work for fluent getters. I have added a section between 3.5 and 3.6, and 3.5 was about direct access.
Regarding fluent access, I changed a line in ref doc referring to FreeBuilder that had me confused. I think I understood it correctly so I dared correct the wording, but please check. Its the ending statement of this section (I have quoted my new version)
FreeBuilder - When FreeBuilder is present on the annotation processor path then the FreeBuilderAccessorNamingStrategy would be used by default. When using FreeBuilder then the JavaBean convention should be followed with setName/getName, as MapStruct does not support fluent getter access String name().
I think I got mightily confused about from notes on dependency injection constructors, as my mind was on immutable objects when reading it. I have now explicitely stated it does not support immutable constructors.
I have also linked to the integration tests from the readme in examples, because there are actually quite great examples, that I think could have helped me had I noticed them.
Activity
arberg commentedon Mar 17, 2019
I finally figured out it was because I was using the fluent naming convention. I have added examples here for getter-naming convention with AutoValue (and Android).
https://github.com/arberg/mapstruct-test
I do find that my comments regarding lack of documentation stil stands.
sjaakd commentedon Mar 17, 2019
@arberg ... Thanks for your feedback.
It's sometimes hard to write good documentation. We are (almost always) short of time to do the stuff, so feedback but also improvements are very much appreciated. Would you be able to issue a PR on our documentation to improve here?
Also, your example could help others. Can you strip it down to it essence and donate it to the examples repo (issue a PR)? Then its possible to refer to it from our documentation.
Last, but not least 😄 we use SO so that users can help themselves as much as is possible. Would you be able to answer your own question to add to the knowledge base?
Thanks for taking the effort to report this.
arberg commentedon Mar 18, 2019
Yeah, I get documentation can be hard to get time for and to make it clear. It always takes a couple of rolls.
I think I can do something. I would like to add the android example project as a subfolder with test of immutable @autovalue builders and demonstrate it just works on Android. There are other stuff in mapstruct, which I suspect do not work, name the /services text-file. I might also change a few lines in the reference documentation.
By donating the example, did you mean adding it as subfolder to https://github.com/mapstruct/mapstruct-examples
I could do that, I think that would be a great idea, it would have helped me a lot, and taken my frustration out of it, I'm sure :)
But before this, I have a few questions, so I can get the documentation and examples right.
https://github.com/arberg/mapstruct-test/blob/master/app/src/main/java/com/example/mapstructtest/model/mapping/ImmutableConstructableCarMapper.java
Your welcome, thank you for the positive response.
Best Alex
sjaakd commentedon Mar 18, 2019
The
injectionStrategyis intended for CDI / Spring / JSR303. So you cannot mix that with the default component model. We should probably put an error on that combination.There's a difference by " support" and "used". Yes, we recognise fluent getters (accessors) and will generate code for that. No, the generated code will not be in fluent style. That will be far too tricky with all the null checks, nesting, etc.
arberg commentedon Mar 19, 2019
thx for responses. I have enough for what I need for my project, but I'll keep going with questions for the examples. We can stop anytime you want.
Ahh, I see. I tried removing that from my builder-based mapper, and indeed it works without. Thank you.
I'm not sure what you mean by that, since you just generate the mapping, not the data classes themselves. I don't care that much about how your generated code looks, as long as it does its job. I have put together a few examples. If you could take a quick peek, I'm sure that would make it clear whether you support that style or not.
Here an Immutable constructor style mapping that does not work. Code is generated, but it just creates the objcects, it makes no mapping. I created constructor which takes no arguments, because the mapping annotation compiler complained. I just got the idea from the reference that this constructor style immutability should be supported
https://github.com/arberg/mapstruct-test/blob/master/app/src/main/java/com/example/mapstructtest/model/mapping/ImmutableConstructableCarMapper.java
Generated code by mapstruct
Here's a fluent example
https://github.com/arberg/mapstruct-test/blob/master/app/src/main/java/com/example/mapstructtest/model/mapping/AutoValueFluentMapper.java
Generated code is exactly same style as above, it creates object, does no mapping.
In both cases, the dto classes is in the parent directory.
If both are not supported, I'll just document that, and get on with my life :)
sjaakd commentedon Mar 23, 2019
Where? Constructor immutability is not supported. It is high on our wish list 😄.
@filiphr .. do you see chance to take a look at this stuff? I think we should have some good examples on the builders in combination with different technologies (Lombok, AutoValue, etc).
filiphr commentedon Mar 23, 2019
Having good examples and reference documentation is always hard. @arberg we will really appreciate if you can prepare an example that would work for Android and AutoValue and submit a PR for our https://github.com/mapstruct/mapstruct-examples.
We also need to mention in the documentation (in the Supported builder frameworks in the Using Builders section of our documentation) that fluent getters do not work for AutoValue. @arberg would you like to contribute this to our documentation as well?
@sjaakd currently we do have integration tests with all the different supported frameworks. Here is the one for auto value. The rest are here
filiphr commentedon Mar 23, 2019
I didn't finish my thought and pressed comment 😄.
@arberg regarding
Yes MapStruct does not support fluent getter-syntax. What it does support is a fluent setter. A fluent setter is a setter that returns the same type as the type being modified. At the moment I don't see possibility for MapStruct to support fluent getters. The reason for this is that in this case MapStruct would consider all methods that return something as a property and that would be to invasive. We already have some problems with the fluent setters (although they are more constraint and easier to detect).
Yes this is not supported, but very high on our wishlist. I would suggest to you to follow #73 for this.
arberg commentedon Mar 24, 2019
Thank you both. I have made a push request for example and reference documentation. I have included a fluent-setter example, and noted how it does not work for fluent getters. I have added a section between 3.5 and 3.6, and 3.5 was about direct access.
Regarding fluent access, I changed a line in ref doc referring to FreeBuilder that had me confused. I think I understood it correctly so I dared correct the wording, but please check. Its the ending statement of this section (I have quoted my new version)
I think I got mightily confused about from notes on dependency injection constructors, as my mind was on immutable objects when reading it. I have now explicitely stated it does not support immutable constructors.
I have also linked to the integration tests from the readme in examples, because there are actually quite great examples, that I think could have helped me had I noticed them.
arberg commentedon Mar 31, 2019
I've created pull requests here
mapstruct/mapstruct-examples#61
and
#1769