Skip to content
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

#1924 Correct handling of org.immutables.value.Value.Style.typeImmutable #3189

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Drevsh
Copy link

@Drevsh Drevsh commented Mar 8, 2023

Immutables allows adapting the name of the generated classes via @org.immutables.value.Value.Style
Also see: https://immutables.github.io/style.html

This PR adds correct handling for prefix/infix/postfix handling.
Example:

@Value.Immutable
@Value.Style(defaultAsDefault = true, typeImmutable = "_*_")
public interface NotificationMessageDtoInifx {
	String getMessage();
	String getTableId();
	String getType();
}

@Value.Immutable
@Value.Style(defaultAsDefault = true, typeImmutable = "*_")
public interface NotificationMessageDtoPostfix {
	String getMessage();
	String getTableId();
	String getType();
}

@Value.Immutable
@Value.Style(defaultAsDefault = true, typeImmutable = "_*")
public interface NotificationMessageDtoPrefix {
	String getMessage();
	String getTableId();
	String getType();
}

Now results in the correct Builder:

public class MessageMapperInfixImpl implements MessageMapperInfix {

    @Override
    public NotificationMessageDto map(NotificationMessageInfix msg) {
        if ( msg == null ) {
            return null;
        }

        _NotificationMessageDto_.Builder notificationMessageDto = _NotificationMessageDto_.builder();

        notificationMessageDto.message( msg.getMessage() );
        notificationMessageDto.tableId( msg.getTableId() );
        notificationMessageDto.type( msg.getType() );

        return notificationMessageDto.build();
    }
}

public class MessageMapperPostfixImpl implements MessageMapperPostfix {

    @Override
    public NotificationMessageDto map(NotificationMessagePostfix msg) {
        if ( msg == null ) {
            return null;
        }

        NotificationMessageDto_.Builder notificationMessageDto = NotificationMessageDto_.builder();

        notificationMessageDto.message( msg.getMessage() );
        notificationMessageDto.tableId( msg.getTableId() );
        notificationMessageDto.type( msg.getType() );

        return notificationMessageDto.build();
    }
}

public class MessageMapperPrefixImpl implements MessageMapperPrefix {

    @Override
    public NotificationMessageDto map(NotificationMessagePrefix msg) {
        if ( msg == null ) {
            return null;
        }

        _NotificationMessageDto.Builder notificationMessageDto = _NotificationMessageDto.builder();

        notificationMessageDto.message( msg.getMessage() );
        notificationMessageDto.tableId( msg.getTableId() );
        notificationMessageDto.type( msg.getType() );

        return notificationMessageDto.build();
    }
}

@sleicht
Copy link

sleicht commented Mar 20, 2023

Thanks for this improved handling of immutables. I created out of your examples some IT cases: sleicht@ed826f3 You can cherrypick or copy them as you like.

@Drevsh
Copy link
Author

Drevsh commented Mar 21, 2023

Nice I could not find the correct integration test (where I would have had access to the immutables annotations).
Will add them to the PR first thin tomorrow.

@Drevsh
Copy link
Author

Drevsh commented Mar 23, 2023

Could you give me a hint on how to setup the integration tests correctly? I'm unable to correctly import the tests. I tried to import them via Eclipse and Intelij but both IDEs seem to struggle with the project hierarchy... and having the tests in the resource folder

@sleicht
Copy link

sleicht commented Mar 24, 2023

Unfortunately I did not find a documentation how to do it "the right way". So I had to fiddle around a little bit.
After applying this patch you can import the folder integrationtest/src/test/resources/immutablesBuilderTest into intellij. With those further steps I could then run the 'ImmutablesMapperTest' in IntelliJ:

  • cd <project_root>/integrationtest/src/test/resources/immutablesBuilderTest
  • mvn clean compile -P generate-via-compiler-plugin

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.

None yet

2 participants