MapStruct: How to map all attributes to first element of a list?












0















I need a mapping to achieve this:



@Mapping(source = "a", target = "result.transaction[0].a"),
@Mapping(source = "b", target = "result.transaction[0].b"),
@Mapping(source = "c", target = "result.transaction[0].c"),
...
Response dataToResponse(DataModel model);


But this syntax does not work (btw: This works with Spring Bean wrapper).
A solution like this is just a half-cooked solution:



@AsList
public <T> List<T> asList( T in ) {
List<T> result = new ArrayList<T>();
if ( in!=null ) {
result.add(in);
}
return result;
}


This only works for exactly one attribute since it always creates a new list for each attribute. I don't need to map each attribute to the first element of a new list. The list must be reused but I don't know how this works. What is the proper way to achieve that?
I thought about something like this:



@Mapping(source = "a", target = "transaction.a"),
@Mapping(source = "b", target = "transaction.b"),
@Mapping(source = "c", target = "transaction.c"),
...
Transaction dataToTransaction(DataModel model);


and then...



@Mapping([use Transaction from b4], target = "result");


But how can I pass the already mapped fields from above?
(I'm using the latest final release 1.1.0.Final)










share|improve this question





























    0















    I need a mapping to achieve this:



    @Mapping(source = "a", target = "result.transaction[0].a"),
    @Mapping(source = "b", target = "result.transaction[0].b"),
    @Mapping(source = "c", target = "result.transaction[0].c"),
    ...
    Response dataToResponse(DataModel model);


    But this syntax does not work (btw: This works with Spring Bean wrapper).
    A solution like this is just a half-cooked solution:



    @AsList
    public <T> List<T> asList( T in ) {
    List<T> result = new ArrayList<T>();
    if ( in!=null ) {
    result.add(in);
    }
    return result;
    }


    This only works for exactly one attribute since it always creates a new list for each attribute. I don't need to map each attribute to the first element of a new list. The list must be reused but I don't know how this works. What is the proper way to achieve that?
    I thought about something like this:



    @Mapping(source = "a", target = "transaction.a"),
    @Mapping(source = "b", target = "transaction.b"),
    @Mapping(source = "c", target = "transaction.c"),
    ...
    Transaction dataToTransaction(DataModel model);


    and then...



    @Mapping([use Transaction from b4], target = "result");


    But how can I pass the already mapped fields from above?
    (I'm using the latest final release 1.1.0.Final)










    share|improve this question



























      0












      0








      0








      I need a mapping to achieve this:



      @Mapping(source = "a", target = "result.transaction[0].a"),
      @Mapping(source = "b", target = "result.transaction[0].b"),
      @Mapping(source = "c", target = "result.transaction[0].c"),
      ...
      Response dataToResponse(DataModel model);


      But this syntax does not work (btw: This works with Spring Bean wrapper).
      A solution like this is just a half-cooked solution:



      @AsList
      public <T> List<T> asList( T in ) {
      List<T> result = new ArrayList<T>();
      if ( in!=null ) {
      result.add(in);
      }
      return result;
      }


      This only works for exactly one attribute since it always creates a new list for each attribute. I don't need to map each attribute to the first element of a new list. The list must be reused but I don't know how this works. What is the proper way to achieve that?
      I thought about something like this:



      @Mapping(source = "a", target = "transaction.a"),
      @Mapping(source = "b", target = "transaction.b"),
      @Mapping(source = "c", target = "transaction.c"),
      ...
      Transaction dataToTransaction(DataModel model);


      and then...



      @Mapping([use Transaction from b4], target = "result");


      But how can I pass the already mapped fields from above?
      (I'm using the latest final release 1.1.0.Final)










      share|improve this question
















      I need a mapping to achieve this:



      @Mapping(source = "a", target = "result.transaction[0].a"),
      @Mapping(source = "b", target = "result.transaction[0].b"),
      @Mapping(source = "c", target = "result.transaction[0].c"),
      ...
      Response dataToResponse(DataModel model);


      But this syntax does not work (btw: This works with Spring Bean wrapper).
      A solution like this is just a half-cooked solution:



      @AsList
      public <T> List<T> asList( T in ) {
      List<T> result = new ArrayList<T>();
      if ( in!=null ) {
      result.add(in);
      }
      return result;
      }


      This only works for exactly one attribute since it always creates a new list for each attribute. I don't need to map each attribute to the first element of a new list. The list must be reused but I don't know how this works. What is the proper way to achieve that?
      I thought about something like this:



      @Mapping(source = "a", target = "transaction.a"),
      @Mapping(source = "b", target = "transaction.b"),
      @Mapping(source = "c", target = "transaction.c"),
      ...
      Transaction dataToTransaction(DataModel model);


      and then...



      @Mapping([use Transaction from b4], target = "result");


      But how can I pass the already mapped fields from above?
      (I'm using the latest final release 1.1.0.Final)







      mapstruct






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 28 '17 at 6:40







      Bevor

















      asked Sep 27 '17 at 12:43









      BevorBevor

      4,081854104




      4,081854104
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Obviously there is no clean solution for that. So I had to workaround it by exclude the following mapping into a separate mapper:



          @Mapping(source = "a", target = "transaction.a"),
          @Mapping(source = "b", target = "transaction.b"),
          @Mapping(source = "c", target = "transaction.c"),
          Transaction dataToTransaction(DataModel model);


          In the main mapper, I execute the separate mapper and convert it into a list by expression:



          @Mapping(expression = "java(Arrays.asList(SubMapper.INSTANCE.dataToTransaction(model)))", target = "result.transactions")





          share|improve this answer































            0














            Just came across this question. A bit more elegant solution would be something like this:



            @Mapping(source = "a", target = "transaction.a"),
            @Mapping(source = "b", target = "transaction.b"),
            @Mapping(source = "c", target = "transaction.c"),
            Transaction dataToTransaction(DataModel model);



            • this is the same as in the previous answer


            What remains to do is to map single transaction to a one-element-list:



            default List<Transaction> mapTransactionToList(Transaction source) {
            return ImmutableList.of(source);
            }


            Now you can simply map model to List<Transaction> in your @Mapping definition and MapStruct should figure out what to do.



            I think this looks better and is less error-prone than "expression" based solution.



            Please note that you can include the code in your Mapper interface.






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46448219%2fmapstruct-how-to-map-all-attributes-to-first-element-of-a-list%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Obviously there is no clean solution for that. So I had to workaround it by exclude the following mapping into a separate mapper:



              @Mapping(source = "a", target = "transaction.a"),
              @Mapping(source = "b", target = "transaction.b"),
              @Mapping(source = "c", target = "transaction.c"),
              Transaction dataToTransaction(DataModel model);


              In the main mapper, I execute the separate mapper and convert it into a list by expression:



              @Mapping(expression = "java(Arrays.asList(SubMapper.INSTANCE.dataToTransaction(model)))", target = "result.transactions")





              share|improve this answer




























                0














                Obviously there is no clean solution for that. So I had to workaround it by exclude the following mapping into a separate mapper:



                @Mapping(source = "a", target = "transaction.a"),
                @Mapping(source = "b", target = "transaction.b"),
                @Mapping(source = "c", target = "transaction.c"),
                Transaction dataToTransaction(DataModel model);


                In the main mapper, I execute the separate mapper and convert it into a list by expression:



                @Mapping(expression = "java(Arrays.asList(SubMapper.INSTANCE.dataToTransaction(model)))", target = "result.transactions")





                share|improve this answer


























                  0












                  0








                  0







                  Obviously there is no clean solution for that. So I had to workaround it by exclude the following mapping into a separate mapper:



                  @Mapping(source = "a", target = "transaction.a"),
                  @Mapping(source = "b", target = "transaction.b"),
                  @Mapping(source = "c", target = "transaction.c"),
                  Transaction dataToTransaction(DataModel model);


                  In the main mapper, I execute the separate mapper and convert it into a list by expression:



                  @Mapping(expression = "java(Arrays.asList(SubMapper.INSTANCE.dataToTransaction(model)))", target = "result.transactions")





                  share|improve this answer













                  Obviously there is no clean solution for that. So I had to workaround it by exclude the following mapping into a separate mapper:



                  @Mapping(source = "a", target = "transaction.a"),
                  @Mapping(source = "b", target = "transaction.b"),
                  @Mapping(source = "c", target = "transaction.c"),
                  Transaction dataToTransaction(DataModel model);


                  In the main mapper, I execute the separate mapper and convert it into a list by expression:



                  @Mapping(expression = "java(Arrays.asList(SubMapper.INSTANCE.dataToTransaction(model)))", target = "result.transactions")






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 27 '17 at 15:50









                  BevorBevor

                  4,081854104




                  4,081854104

























                      0














                      Just came across this question. A bit more elegant solution would be something like this:



                      @Mapping(source = "a", target = "transaction.a"),
                      @Mapping(source = "b", target = "transaction.b"),
                      @Mapping(source = "c", target = "transaction.c"),
                      Transaction dataToTransaction(DataModel model);



                      • this is the same as in the previous answer


                      What remains to do is to map single transaction to a one-element-list:



                      default List<Transaction> mapTransactionToList(Transaction source) {
                      return ImmutableList.of(source);
                      }


                      Now you can simply map model to List<Transaction> in your @Mapping definition and MapStruct should figure out what to do.



                      I think this looks better and is less error-prone than "expression" based solution.



                      Please note that you can include the code in your Mapper interface.






                      share|improve this answer




























                        0














                        Just came across this question. A bit more elegant solution would be something like this:



                        @Mapping(source = "a", target = "transaction.a"),
                        @Mapping(source = "b", target = "transaction.b"),
                        @Mapping(source = "c", target = "transaction.c"),
                        Transaction dataToTransaction(DataModel model);



                        • this is the same as in the previous answer


                        What remains to do is to map single transaction to a one-element-list:



                        default List<Transaction> mapTransactionToList(Transaction source) {
                        return ImmutableList.of(source);
                        }


                        Now you can simply map model to List<Transaction> in your @Mapping definition and MapStruct should figure out what to do.



                        I think this looks better and is less error-prone than "expression" based solution.



                        Please note that you can include the code in your Mapper interface.






                        share|improve this answer


























                          0












                          0








                          0







                          Just came across this question. A bit more elegant solution would be something like this:



                          @Mapping(source = "a", target = "transaction.a"),
                          @Mapping(source = "b", target = "transaction.b"),
                          @Mapping(source = "c", target = "transaction.c"),
                          Transaction dataToTransaction(DataModel model);



                          • this is the same as in the previous answer


                          What remains to do is to map single transaction to a one-element-list:



                          default List<Transaction> mapTransactionToList(Transaction source) {
                          return ImmutableList.of(source);
                          }


                          Now you can simply map model to List<Transaction> in your @Mapping definition and MapStruct should figure out what to do.



                          I think this looks better and is less error-prone than "expression" based solution.



                          Please note that you can include the code in your Mapper interface.






                          share|improve this answer













                          Just came across this question. A bit more elegant solution would be something like this:



                          @Mapping(source = "a", target = "transaction.a"),
                          @Mapping(source = "b", target = "transaction.b"),
                          @Mapping(source = "c", target = "transaction.c"),
                          Transaction dataToTransaction(DataModel model);



                          • this is the same as in the previous answer


                          What remains to do is to map single transaction to a one-element-list:



                          default List<Transaction> mapTransactionToList(Transaction source) {
                          return ImmutableList.of(source);
                          }


                          Now you can simply map model to List<Transaction> in your @Mapping definition and MapStruct should figure out what to do.



                          I think this looks better and is less error-prone than "expression" based solution.



                          Please note that you can include the code in your Mapper interface.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 21 '18 at 9:42









                          Antonín KarásekAntonín Karásek

                          115




                          115






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46448219%2fmapstruct-how-to-map-all-attributes-to-first-element-of-a-list%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                              Npm cannot find a required file even through it is in the searched directory