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

Generate Kotlin data classes #6345

Closed
eiswind opened this issue Jun 12, 2017 · 15 comments
Closed

Generate Kotlin data classes #6345

eiswind opened this issue Jun 12, 2017 · 15 comments

Comments

@eiswind
Copy link

eiswind commented Jun 12, 2017

I found #3883 today, so I hope there is something going on at this point. I found it nearly impossible to implement a jooq generated interface with a kotlin data class (compiler conflicts on the overriden get/set methods).
For Kotlin users it would be great if one could work with gererated kotlin data classes.

@lukaseder
Copy link
Member

I agree that would be very desirable. May I ask you for some more details about the conflicts you've got? Perhaps, there's some easy solution to the problems you were having...

@eiswind
Copy link
Author

eiswind commented Jun 15, 2017 via email

@lukaseder
Copy link
Member

@eiswind Oh interesting, thanks for elaborating. Yes, I've added my 2¢ to that Kotlin issue.

A workaround for the time being would be to generate data classes that do not implement the generated interfaces, or generate Kotlin data classes that contain delegate getters and setters...

Unfortunately, I don't think we can make a thorough fix in jOOQ 3.10 anymore. We'll definitely look into Kotlin code generation improvements in jOOQ 3.11, though.

@niiamon-rag
Copy link

@eiswind Hey Thomas! Were you able to move this forward a bit? In general, how are you doing Kotlin code generation? I want to give it a shot.

@bastman
Copy link

bastman commented Jan 9, 2018

Currently we have a few big issues that hold us back from using jooq:

  • The code generator does not generate immutable data classes in kotlin
  • Nullability information for columns is not provided by the code generator ( How about generating jsr-305 annotations derived from not-null-db-constraints? Or generate kotlin with nullable-/not-nullable types).
  • Java 8 Time API: We would like to use "Instant", rather than LocalDateTime to represent SQL DateTime / Timestamp. Since "Instant" is the perfect match for UTC epochmillis.

Is there any chance to get these features implemented one day? I would love to get rid of hibernate.

Thanks a lot. Jooq is great and might become awesome ;)

@lukaseder
Copy link
Member

Thanks for your feedback, @bastman. Let me show you how none of these issues will hold you back:

The code generator does not generate immutable data classes in kotlin

Create your own for the time being! It's really simple, just extend the JavaGenerator in jooq-codegen and you're done!

That said, let me increase the priority of this particular feature request (out of the box kotlin data class generation)

Nullability information for columns is not provided by the code generator ( How about generating jsr-305 annotations derived from not-null-db-constraints? Or generate kotlin with nullable-/not-nullable types).

While I appreciate that people used to the convenience of Kotlin (or Scala, Ceylon, etc.) nullability guarantees would like to see this functionality also in Java, it is unreasonable to expect this from a SQL API. This is explained here:
https://blog.jooq.org/2017/07/25/5-things-you-may-not-have-known-about-jooq/#thing1

Anyway, this discussion has been had before many times, e.g. #4748 and #6244. JSR-305 is currently not an option for jOOQ's core, and if you like to have those annotations on your kotlin data classes, then see my previous answer, it'll be easy to generate them yourself.

Java 8 Time API: We would like to use "Instant",

Easy. Just write your converter: https://www.jooq.org/doc/latest/manual/code-generation/custom-data-types/

rather than LocalDateTime to represent SQL DateTime / Timestamp. Since "Instant" is the perfect match for UTC epochmillis.

Be careful though. Instant is not the perfect match for TIMESTAMP (without time zone!), although it might be the perfect match for TIMESTAMP WITH TIME ZONE, e.g. in PostgreSQL

Is there any chance to get these features implemented one day?

Sure. "One day" is a very broad specification, so I can certainly say yes to this question

I would love to get rid of hibernate.

None of the issues you've mentioned should keep you from doing that. The overall gain is still magnificent if you're sure that jOOQ is a better fit than Hibernate - because these design decision are much more higher level than individual data types.

Besides, from the way you phrased this, I'm sure you've already decided anyway.

Hope this helps :-)

@lukaseder
Copy link
Member

We're definitely (finally) going to implement a new way generating DTO types in jOOQ's code generator, perhaps even replacing the term POJO (whose J means Java) by DTO in generated package names and API. The main issue here is #3821, in whose context Kotlin data classes will also be supported.

No existing logic will be removed, it will just be reorganised to better allow for further extensions, like this one.

@Sethuraman
Copy link

@lukaseder - any update on the Kotlin data class for pojos? Is that a part of the 3.13 release?

@lukaseder
Copy link
Member

Haha, promises... Every. Single. Time. Why do I keep making them? No, this is not part of any release yet.

I recognise a lot of people want this.

@lukaseder
Copy link
Member

In the meantime, I've invested some time in a new jOOQ-kotlin module: #6256. Its purpose is to offer a better out of the box experience by providing a few extension methods for useful things.

Any feedback in that area that you folks may have is very welcome!

@lukaseder
Copy link
Member

I will address this now for jOOQ 3.14 via #6248. I don't think that generating data classes in isolation makes sense. I'd like to offer the same feature set as for Scala, a complete KotlinGenerator that generates kotlin code for all objects.

@lukaseder
Copy link
Member

Implemented via #6248 (push to github pending)

@lukaseder
Copy link
Member

A nice feature when implementing an entire KotlinGenerator rather than "just" data class support is that we can use Kotlin properties also in interfaces and records. This will lead to a more consistent, complete experience, rather than having to wrestle with interop things like https://youtrack.jetbrains.com/issue/KT-6653

I mean, why not:

ctx.batchStore(
  BookRecord(id = 1, title = "Effective jOOQ")
  BookRecord(id = 2, title = "Just use SQL")
)

@lukaseder
Copy link
Member

When generating Kotlin data classes with optional parameters, such as:

data class T(
      val i: Int? = null
    , val j: String? = null
)

Then, the existing RecordMapper implementation is wrong and no longer works. This is independent of adding better code generation support for Kotlin and can happen with manually written data classes as well. I've created an issue for this: #10194

@lukaseder
Copy link
Member

There will be a flag to opt out of this in the KotlinGenerator: #10288

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

No branches or pull requests

5 participants