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

Make generateModelBuilder generate deep builders (instead of shallow) #680

Merged
merged 1 commit into from
Oct 8, 2017

Conversation

sav007
Copy link
Contributor

@sav007 sav007 commented Oct 5, 2017

For each builder generate additional setter with Mutator<*.Builder> as an argument:

public Builder hero(@Nonnull Mutator<Hero.Builder> mutator) {
  Hero.Builder builder = this.hero != null ? this.hero.toBuilder() : Hero.builder();

  mutator.accept(builder);

  this.hero = builder.build();
  return this;
}

That will allow user to do next:

data.toBuilder()
  .hero(someHero)
  // or
  .hero(new Consumer<>() {
    void accept(Hero.Builder builder) {
      builder.name("Joe")
       .age(31)
    }
  })

This will be even better with lambda support:

data.toBuilder()
  .hero(someHero)
  // or
  .hero(builder -> builder
       .name("Joe")
       .age(31)
  )

Closes #643

@sav007
Copy link
Contributor Author

sav007 commented Oct 5, 2017

cc: @martinbonnin

@sav007 sav007 force-pushed the feature/463/deep-copy-builders branch from 86e3163 to 34e3c18 Compare October 5, 2017 04:26
@martinbonnin
Copy link
Contributor

martinbonnin commented Oct 5, 2017

Just to let you know I started looking into this but I have troubles importing the snapshot into my project. Somehow the Consumer<> class is not exported not sure exactly why... Okay, rm -rf ~/.gradle and it's working :)

Also it would help to have an integration test where we could demonstrate the usage of this. Not really sure how to do this without adding another submodule as it requires changing some compiler flags.

Anyways, I'll post again when I get it working

*
* @param <T> the type of the input to the operation
*/
public interface Consumer<T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you use Consumer on purpose ? Why not call the class Mutator or BuilderMutator so as to avoid confusion with the other Consumers (reactivex, java.util, com.annimon.stream, maybe others ?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just copied from Java8, but yeah we can rename it

For each builder generate additional setter with `Consumer<*.Builder>` as an argument:

```
public Builder hero(@nonnull Consumer<Hero.Builder> mutator) {
  Hero.Builder builder = this.hero != null ? this.hero.toBuilder() : Hero.builder();

  mutator.accept(builder);

  this.hero = builder.build();
  return this;
}
```

That will allow user to do next:
```
data.toBuilder()
  .hero(someHero)
  // or
  .hero(new Consumer<>() {
    void accept(Hero.Builder builder) {
      builder.name("Joe")
       .age(31)
    }
  })
```

This will be even better with lambda support:
```
data.toBuilder()
  .hero(someHero)
  // or
  .hero(builder -> builder
       .name("Joe")
       .age(31)
    }
  })
```

Closes apollographql#643
@sav007 sav007 force-pushed the feature/463/deep-copy-builders branch from 34e3c18 to c606d30 Compare October 6, 2017 03:06
@sav007 sav007 merged commit 18c54e4 into apollographql:master Oct 8, 2017
@sav007 sav007 deleted the feature/463/deep-copy-builders branch October 8, 2017 03:55
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.

2 participants