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

Google AutoValue support #802

Closed
natros opened this Issue Apr 15, 2016 · 9 comments

Comments

Projects
None yet
7 participants
@natros

natros commented Apr 15, 2016

AutoValue is used to create immutable objects using an annotation processor. It would be nice if mapstruct would support this mapping.

AutoValue
AutoValue with Builders

@gunnarmorling

This comment has been minimized.

Show comment
Hide comment
@gunnarmorling

gunnarmorling Apr 15, 2016

Member

Hey, any thoughts on how this support should look like? Is it "just" support for builders? Or anything else? I like the idea (there is an issue for builder-style support somewhere already), but the details are still a bit vague to me.

Member

gunnarmorling commented Apr 15, 2016

Hey, any thoughts on how this support should look like? Is it "just" support for builders? Or anything else? I like the idea (there is an issue for builder-style support somewhere already), but the details are still a bit vague to me.

@natros

This comment has been minimized.

Show comment
Hide comment
@natros

natros Apr 15, 2016

With jackson I can do this:

@AutoValue
@JsonDeserialize(builder = AutoValue_User.Builder.class)
public abstract class User {
    @JsonProperty
    public abstract Integer userId();

    @JsonProperty
    public abstract String login();

    @JsonProperty
    public abstract String name();

    public static Builder builder() {
        return new AutoValue_User.Builder();
    }

    @AutoValue.Builder
    public abstract static class Builder {
        @JsonProperty
        public abstract Builder userId(Integer userId);

        @JsonProperty
        public abstract Builder login(String login);

        @JsonProperty
        public abstract Builder name(String name);

        public abstract User build();
    }
}

        User user1 = User.builder().userId(1).login("fsousa").name("Filipe Sousa").build();
        System.out.println("user1 = " + user1);

        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(user1);
        System.out.println("s = " + s);

        User user2 = objectMapper.readValue(s, User.class);
        System.out.println("user2 = " + user2);

In this example jackson is able to serialize and deserialize based on the builder.
Maybe something like this should be possible to use.

@Mapper
public interface UserMapper {
    @MapstructBuilder(builder = AutoValue_User.Builder.class)
    UserDto toUserDto(User user);
}

natros commented Apr 15, 2016

With jackson I can do this:

@AutoValue
@JsonDeserialize(builder = AutoValue_User.Builder.class)
public abstract class User {
    @JsonProperty
    public abstract Integer userId();

    @JsonProperty
    public abstract String login();

    @JsonProperty
    public abstract String name();

    public static Builder builder() {
        return new AutoValue_User.Builder();
    }

    @AutoValue.Builder
    public abstract static class Builder {
        @JsonProperty
        public abstract Builder userId(Integer userId);

        @JsonProperty
        public abstract Builder login(String login);

        @JsonProperty
        public abstract Builder name(String name);

        public abstract User build();
    }
}

        User user1 = User.builder().userId(1).login("fsousa").name("Filipe Sousa").build();
        System.out.println("user1 = " + user1);

        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(user1);
        System.out.println("s = " + s);

        User user2 = objectMapper.readValue(s, User.class);
        System.out.println("user2 = " + user2);

In this example jackson is able to serialize and deserialize based on the builder.
Maybe something like this should be possible to use.

@Mapper
public interface UserMapper {
    @MapstructBuilder(builder = AutoValue_User.Builder.class)
    UserDto toUserDto(User user);
}
@gunnarmorling

This comment has been minimized.

Show comment
Hide comment
@gunnarmorling

gunnarmorling Aug 24, 2016

Member

The Immutables project would be another interesting case in the same category as AutoValue it seems.

Member

gunnarmorling commented Aug 24, 2016

The Immutables project would be another interesting case in the same category as AutoValue it seems.

@natros

This comment has been minimized.

Show comment
Hide comment
@natros

natros commented Aug 25, 2016

And there is also https://github.com/google/FreeBuilder from google.

@ericmartineau

This comment has been minimized.

Show comment
Hide comment
@ericmartineau

ericmartineau Jun 23, 2017

I submitted a PR that would support this use case, as well as lombok @builder

#1229

ericmartineau commented Jun 23, 2017

I submitted a PR that would support this use case, as well as lombok @builder

#1229

@rmannibucau

This comment has been minimized.

Show comment
Hide comment
@rmannibucau

rmannibucau Sep 22, 2017

+1 to support it, @autovalue generation is particular enough to not make it easy to bridge with a custom strategy (in particular the final fields handling)

rmannibucau commented Sep 22, 2017

+1 to support it, @autovalue generation is particular enough to not make it easy to bridge with a custom strategy (in particular the final fields handling)

@maciekpapiez

This comment has been minimized.

Show comment
Hide comment
@maciekpapiez

maciekpapiez commented Oct 2, 2017

👍

@ugrave

This comment has been minimized.

Show comment
Hide comment
@ugrave

ugrave Oct 4, 2017

Support for the Builders generated by Lombok would also be nice.

ugrave commented Oct 4, 2017

Support for the Builders generated by Lombok would also be nice.

@filiphr filiphr added this to the 1.3.x milestone Oct 17, 2017

@filiphr filiphr self-assigned this Apr 2, 2018

@filiphr

This comment has been minimized.

Show comment
Hide comment
@filiphr

filiphr Apr 4, 2018

Member

With #1373 support for builders has been added to master. We are going to release 1.3.0 Beta so you can give it a shot soon.

Member

filiphr commented Apr 4, 2018

With #1373 support for builders has been added to master. We are going to release 1.3.0 Beta so you can give it a shot soon.

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