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

Immutability #149

Closed
lorenzleutgeb opened this issue May 19, 2020 · 17 comments · Fixed by #175
Closed

Immutability #149

lorenzleutgeb opened this issue May 19, 2020 · 17 comments · Fixed by #175
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@lorenzleutgeb
Copy link
Contributor

lorenzleutgeb commented May 19, 2020

I'd like the DTOs generated by graphql-java-codegen to optionally be "immutable".

By "immutable" I mean that the generated classes do not have any setters. Very similar to to what Lombok does with its @Value annotation.

A generated class with this feature enabled should look like:

public class Person {
    // Everything as usual, except...
    // NO SETTERS IN HERE
}

If you approve the addition of this feature, I am happy to implement and PR it.

@kobylynskyi
Copy link
Owner

Hi @lorenzleutgeb
We can have another mapping config option (something like generateModelsImmutable with default = false)
We need to also consider that get() methods that return collection should return a copy of the collection.
If you would like to work on this feature, then please assign it to yourself. Thanks!

@kobylynskyi kobylynskyi added the enhancement New feature or request label May 19, 2020
@lorenzleutgeb
Copy link
Contributor Author

You're right. I might do it, but not today...

@joffrey-bion
Copy link
Contributor

joffrey-bion commented May 29, 2020

We need to also consider that get() methods that return collection should return a copy of the collection.

Maybe we could simply make an immutable copy of the collection at construction time?
That would avoid creating new objects at every get call (but it would eagerly duplicate collection properties even if never accessed).

@lorenzleutgeb
Copy link
Contributor Author

lorenzleutgeb commented May 29, 2020

@joffrey-bion it would be interesting to see benchmarks, this is certainly not the first time someone ha to make this decision... My uneducated guess would be to favour eager duplication in the constructor, because that would already break even in the case where every property is accessed at least once, right?

@xenoterracide
Copy link

I'm mostly looking for setter-less DTO's first, not too worried about the Array itself

@xenoterracide
Copy link

though maybe this code generator, could integrate with another code generator like immutables, don't know if that's possible.

@lorenzleutgeb
Copy link
Contributor Author

Since this project already depends on Lombok, I quickly checked whether Lombok's @Value could be used. Turns out that it does not generate unmodifiable copies of collections, and even though requested, this feature will not be implemented. So, I'd like to say "let's just do what Lombok does" and propose to implement the feature without the rather complicated requirement of making collections unmodifiable.

@joffrey-bion
Copy link
Contributor

@xenoterracide Yeah I wouldn't worry too much about defensive copies of collections, either.

@lorenzleutgeb Just to clarify a bit, the fact that this project depends on Lombok should be irrelevant, because we're talking about the generated code that will belong to the target project.
Also, Lombok is mostly a tool to help write and maintain more easily the code that people write by hand. Generating code that contains Lombok annotations is sort of pointless because it would need to then go through Lombok's generation, adding an extra step for no added maintainability.

@lorenzleutgeb
Copy link
Contributor Author

Also, Lombok is mostly a tool to help write and maintain more easily the code that people write by hand. Generating code that contains Lombok annotations is sort of pointless because it would need to then go through Lombok's generation, adding an extra step for no added maintainability.

Yeah I'm aware. I didn't mean to suggest to generate code with Lombok annotations, but to generate code that is essentially equivalent to what Lombok would generate if @Value would be present.

@joffrey-bion
Copy link
Contributor

@lorenzleutgeb Yes, I was just answering the first part of your comment. But I agree with you on your conclusion ;)

@kobylynskyi
Copy link
Owner

Hey, @lorenzleutgeb
In the description of this feature request, you mentioned that you might work on it. Do you still want to do that? I would appreciate your contribution. Thanks.

@lorenzleutgeb
Copy link
Contributor Author

Yeah, I'd do it if you drop the requirement of instantiating unmodifiable collections and instead allow me to do it "the Lombok way", effectively just dropping setters but not having special getter implementations.

@kobylynskyi
Copy link
Owner

I am fine with Lombok approach

@lorenzleutgeb
Copy link
Contributor Author

In order to avoid changes later on, let's also fix the "user interface": How would you like the configuration parameter to be called? I'd call it generateValue which might be a bit unintuitive.

@kobylynskyi
Copy link
Owner

How about ‘generateImmutableModels’ or ‘generateModelsImmutable’?

@lorenzleutgeb
Copy link
Contributor Author

I think that generateImmutableModels (verb, adjective, noun) better fits other, already existing, names such as generateAsyncApi, so I'll choose that.

@lorenzleutgeb
Copy link
Contributor Author

lorenzleutgeb commented Jun 19, 2020

Just to confirm: Starting from version 2, together with the feature described in #148, MapStruct can now target immutable DTOs and will instantiate them via the builder 🎊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants