Skip to content

1574: Introducing AnnotateWith with values.#2792

Merged
filiphr merged 55 commits into
mapstruct:mainfrom
Zegveld:feature/1574
Aug 20, 2022
Merged

1574: Introducing AnnotateWith with values.#2792
filiphr merged 55 commits into
mapstruct:mainfrom
Zegveld:feature/1574

Conversation

@Zegveld

@Zegveld Zegveld commented Mar 18, 2022

Copy link
Copy Markdown
Contributor

introducing @AnnotateWith for adding custom annotations to classes or methods.

closes #1574

Ben Zegveld added 14 commits March 7, 2022 23:54
Still need to implement the following:
1. succesful situations for all types except class and string.
2. succesful situations for all types with multiple values (array format).
3. add compilation error when multiple value types are filled.
4. check if behavior works correct for incorrect type with array format.
@Zegveld

Zegveld commented Mar 18, 2022

Copy link
Copy Markdown
Contributor Author

@filiphr , do we want to call the additional arguments (key/value pairs) that you can place at an annotation, properties or parameters?
I noticed that currently I'm using both. The annotation has @Parameter while the code talks about properties.

@tthornton3-chwy

Copy link
Copy Markdown

@filiphr @Zegveld this change would help us greatly in a couple of places, is there any movement or anything needed for this one?

@filiphr

filiphr commented May 6, 2022

Copy link
Copy Markdown
Member

This is a really good improvement indeed @tthornton3-chwy. I've been pretty busy with other activities and haven't had the chance to review it properly. I hope that I'll be able to give a proper pass soon.

Comment thread core/src/main/java/org/mapstruct/AnnotateWith.java Outdated
@sjaakd

sjaakd commented Jun 1, 2022

Copy link
Copy Markdown
Contributor

@Zegveld I added some initial findings. Need to do a real review though. Will be back for more thorough analysis.

@Zegveld

Zegveld commented Jun 1, 2022

Copy link
Copy Markdown
Contributor Author

Did quite a bit of renaming. Might need another round of renames, but at least it's getting closer to the java spec ;) instead of just key or Property/parameters :) Resolving the old remarks concerning renames.

Ben Zegveld and others added 6 commits June 4, 2022 01:52
… import the internal anotations, but mention the containing class at the '@GemDefinition' annotation.
* Remove TargetGem and rely on it being present
* Use Type#describe for the error messages
* Remove findAllAnnotationParameters from the AdditionalAnnotationsBuilder and extract the needed information in the builder itself

@filiphr filiphr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really great job on this one @Zegveld.

I took the liberty to do some small polishing in a separate PR. Please have a look at it, and if you do not like it let me know. I felt like I could do a bit more in AdditionalAnnotationsBuilder and perhaps simplify certain things, move things around the checks of the annotation parameters in one method, but I didn't want to change a lot before talking to you.

I have also left some inline comments.

Some more general things that we need to clear out:


Enum API

The API around enums feels a bit strange. If you want to define multiple enums you will need to write:

@Element(name = "enumArray",
        enums = {
            @AnnotateWith.EnumElement(enumClass = AnnotateWithEnum.class, name = "EXISTING"),
            @AnnotateWith.EnumElement(enumClass = AnnotateWithEnum.class, name = "EXISTING")
        }
    )

Basically you'll need to define the enumClass twice. I think that we need to revisit this API.

e.g.

@Element( name = "enumArray", 
        enumValue = @AnnotateWith.EnumElement( 
            enumClass = AnnotateWithEnum.class,
            names = {
                "EXISTING",
                "EXISTING"
            }
        )
    ),

or

@Element( name = "enumArray",
        enumClass = AnnotateWithEnum.class,
        enumNames = {
            "EXISTING",
            "EXISTING"
        }
    )

With the second approach we can get rid of the @EnumElement.

What do you think?


Define same element multiple times

You can now write:

@AnnotateWith.Element( name = "stringParam", strings = "value1" ),
@AnnotateWith.Element( name = "stringParam", strings = "value2" ),

From what I could see in the code, we are going to pick value1 and nothing will happen with value2. I think that we should not allow this. Same as we are doing for Mapping#target.

What do you think?

Comment thread core/src/main/java/org/mapstruct/AnnotateWith.java
Comment thread processor/src/test/java/org/mapstruct/ap/test/annotatewith/DeprecateMapper.java Outdated
Comment thread core/src/main/java/org/mapstruct/AnnotateWith.java Outdated
Comment thread processor/src/test/java/org/mapstruct/ap/test/annotatewith/AnnotateWithTest.java Outdated
Comment thread processor/src/test/java/org/mapstruct/ap/test/annotatewith/AnnotateWithTest.java Outdated
Comment thread processor/src/test/java/org/mapstruct/ap/test/annotatewith/AnnotateWithTest.java Outdated
Comment thread core/src/main/java/org/mapstruct/AnnotateWith.java Outdated
@filiphr

filiphr commented Jun 6, 2022

Copy link
Copy Markdown
Member

I did a small pass over some of the errors @Zegveld let me know if you agree with it or not.

I also changed one from error to warning, because I thought that having an error for that is a bit too strict.

@Zegveld

Zegveld commented Jun 6, 2022

Copy link
Copy Markdown
Contributor Author

I did a small pass over some of the errors @Zegveld let me know if you agree with it or not.

I also changed one from error to warning, because I thought that having an error for that is a bit too strict.

changes look good, time to add fixture tests.

@filiphr

filiphr commented Jun 6, 2022

Copy link
Copy Markdown
Member

I forgot the HiddenEnumForAnnotationDefault. Can we do this in the same way NullEnum is done in Junit Jupiter and put it in its own file with package-protected visibility.

@filiphr

filiphr commented Jun 18, 2022

Copy link
Copy Markdown
Member

@Zegveld I did one more pass and add some small changes. Let me know if you do not agree with them.

I also saw how the generated annotations look like and I created #2895 so we can improve that a bit.

@Zegveld

Zegveld commented Jun 18, 2022

Copy link
Copy Markdown
Contributor Author

@Zegveld I did one more pass and add some small changes. Let me know if you do not agree with them.

Looking good to me.

I also saw how the generated annotations look like and I created #2895 so we can improve that a bit.

Sounds good.

@filiphr

filiphr commented Jun 18, 2022

Copy link
Copy Markdown
Member

@Zegveld one last thing, I saw that ErroneousMapperWithTooManyParameterValues is not used anywhere. Are we missing some test case perhaps?

@Zegveld

Zegveld commented Jun 18, 2022

Copy link
Copy Markdown
Contributor Author

@Zegveld one last thing, I saw that ErroneousMapperWithTooManyParameterValues is not used anywhere. Are we missing some test case perhaps?

I think we can remove that one, as it gives the same error as ErroneousMapperWithWrongParameter. Or do we want a different error if there are too many parameters opposed to wrong parameter?
Then we need to add a test for it, and probably another compiler error message.

@filiphr

filiphr commented Jun 18, 2022

Copy link
Copy Markdown
Member

I think that we need to provide a different compiler error message if they have defined multiple parameters. Similar like if you do it for the @Mapping

@filiphr

filiphr commented Jul 8, 2022

Copy link
Copy Markdown
Member

@Zegveld should we do the last change for this one and then merge this to main?

@Zegveld

Zegveld commented Jul 25, 2022

Copy link
Copy Markdown
Contributor Author

@Zegveld should we do the last change for this one and then merge this to main?

Sure, I've been busy and haven't had time to work on the last changes yet. Feel free to pick it up otherwise I'll try to do it next weekend if I have some time then.

@Zegveld

Zegveld commented Aug 15, 2022

Copy link
Copy Markdown
Contributor Author

@filiphr could you take a look at the new error message. I feel like it isn't quite correct, but can't figure out what. If you think it is fine as it is, I think it can then be merged.
Unless there was something else that I forgot to do ofcourse ;)

@filiphr

filiphr commented Aug 20, 2022

Copy link
Copy Markdown
Member

Thanks a lot for your final changes @Zegveld. I've squashed and integrated this into main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add annotations to Generated code

4 participants