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

staged Builder for Factories #1112

Open
bmarwell opened this issue Oct 25, 2019 · 17 comments
Open

staged Builder for Factories #1112

bmarwell opened this issue Oct 25, 2019 · 17 comments
Milestone

Comments

@bmarwell
Copy link

Hi,

I'd love to see https://immutables.github.io/factory.html supporting a staged builder.

Caveat: in #761 you argued that external builders and staged builders combined make no sense. Please do look at the last comment. I think this can be implemented (for both Factory and Immutables).

@dsinghvi
Copy link

dsinghvi commented Apr 6, 2022

+1 this would be great for my use case

@dsinghvi
Copy link

dsinghvi commented Apr 6, 2022

@elucash would you accept a contribution?

@bmarwell
Copy link
Author

@elucash would you consider this? There is even @dsinghvi who would contribute.

@bmarwell
Copy link
Author

Hi @elucash any chance on this?

@elucash
Copy link
Member

elucash commented Dec 13, 2022

Hi @bmarwell I'll look into this (i.e. what can we do). Wanted to carve a day to work on Immutables, which seems like today :)

@bmarwell
Copy link
Author

Woohooo 🎉
Let me know if I can help (eg docs or writing a test).

@elucash
Copy link
Member

elucash commented Dec 14, 2022

I would definitely ask for help with documentation/guide. But as of now I'm quite puzzled how to change builder generation for factory (digging our code and trying to restore the picture) from top level to nested, we cannot put stage interfaces inside builder as it's prohibited for a type to extend/implement the type nested inside it (while the other way around is not a problem)

@elucash
Copy link
Member

elucash commented Dec 14, 2022

Experimental implementation PR, need more testing, more changes than expected. The solution requires now to have @Value.Enclosing annotation in the class with factories, otherwise it's quite difficult to arrange proper generation.

@Value.Enclosing
@Value.Style(stagedBuilder = true)
public class StagedFactory {
	@Builder.Factory
	public static String superstring(int theory, String reality, @Nullable Void evidence) {
		return theory + " != " + reality + ", " + evidence;
	}
}

  // generates the following methods/interfaces
  ImmutableStagedFactory.SuperstringTheoryBuildStage a = ImmutableStagedFactory.superstringBuilder();
  ImmutableStagedFactory.SuperstringRealityBuildStage b = a.theory(1);
  ImmutableStagedFactory.SuperstringBuildFinal c = b.reality("x");
  ImmutableStagedFactory.SuperstringBuildFinal d = c.evidence(null);
  String superstring = d.build();

Immutable* pattern for enclosing generated class can be changed using Style.typeImmutableEnclosing

will get back to it in a couple of days, need a fresh look

@bmarwell
Copy link
Author

Hey! Thanks, will take a look at it in a few days (or maybe after vacation). Thank you so much for getting started! 👍🏻

@bmarwell
Copy link
Author

bmarwell commented Feb 1, 2023

Hey @elucash, I cannot compile your branch :(

[ERROR] $HOME/git/immutables/mirror/target/generated-sources/annotations/org/immutables/mirror/processor/Generator_Mirrors.java:[238,93] error: ';' expected

@elucash
Copy link
Member

elucash commented Feb 1, 2023

Hm.. I'm doing mvn clean install and it works for me and for CI https://github.com/immutables/immutables/actions/runs/3699719352/jobs/6267370633
I appreciate If anyone chime in and help to understand what the problem is. I'm not saying there's no problem, but I cannot reproduce one with a clean checkout. (Maybe Windows specific stuff, not a clean checkout, build cache of some sort?

@bmarwell
Copy link
Author

Will try again!

elucash added a commit that referenced this issue Mar 22, 2023
Experimental #1112 requiring Enclosing annotation
@bmarwell
Copy link
Author

Is 5eb1de3 on any version in central I can try?

@elucash
Copy link
Member

elucash commented Jul 13, 2023

Probably, 2.10.0-rc0 contains this already

@elucash elucash added this to the 2.10.0 milestone Oct 12, 2023
@bmarwell
Copy link
Author

bmarwell commented Nov 4, 2023

Will try, sorry for the delay

@bmarwell
Copy link
Author

bmarwell commented Nov 6, 2023

Hey @elucash!

tested with 2.10.0.

The annotated Constructor of "MyObject" will result in a compile error in the return line:

    public void build() {
      checkRequiredAttributes();
      return MyObject.<init>(str1, s2, s3);
    }

// and last method in class MyObjectBuildFinal

    /* Builds a new {@link void !should_not_be_used_in_generated_code!}. */
    void build();

@bmarwell
Copy link
Author

bmarwell commented Nov 6, 2023

The Factory builder works flawlessly, thanks! :)

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

No branches or pull requests

3 participants