-
Notifications
You must be signed in to change notification settings - Fork 69
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
jte-models: Add support to generate Kotlin code #282
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments and questions.
try { | ||
language = Language.valueOf(configuredLanguage); | ||
} catch (IllegalArgumentException ex) { | ||
// how to report wrong conciguration? fail or default to Java? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@casid, I'm unsure how (or even if) to do this. It is an open question for this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion you should fail the build in this case. Building Java when a different language was specified is not what the user wants.
I think throwing an exception with a good description would be good enough, but you should test that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 440c1f0.
@@ -21,7 +21,7 @@ public class ModelExtension implements JteExtension { | |||
|
|||
@Override | |||
public String name() { | |||
return "Generate type-safe model facade for templates"; | |||
return "Generate type-safe model facade for templates in Java"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also unsure about the impact of changing this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name isn't actually used anywhere at the moment.
@@ -23,17 +23,27 @@ public class ModelGenerator { | |||
private final String targetClassName; | |||
private final String interfaceName; | |||
|
|||
private final Language language; | |||
|
|||
public ModelGenerator(TemplateEngine engine, String templateSubDirectory, String targetClassName, String interfaceName) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept this constructor for compatibility reasons. But it may be unnecessary now since it is not used in the code base.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ModelGenerator is internal implementation, so it is not necessary to keep the constructor overload.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed in aac2a43.
Looks good to me. Just one question: since Kotlin can interoperate with Java classes, what is the benefit of generating Kotlin versions of the classes? (I'm not much of a Kotlin user) |
Interoperability is quite good, but there are places where it fails. For example, if you have a Kte template such as: @param messages: List<String>
<html lang="pt">
<head>
<title>First Page</title>
</head>
<body>
<h1>message</h1>
</body>
</html>
package br.ufpe.liber;
import gg.jte.models.runtime.*;
public interface Templates {
@JteView("index.kte")
JteModel index(List<String> messages);
} Compilation error:
There are workarounds, but then the user needs to use them constantly, which impacts the developer experience. |
Thanks for reviewing, @edward3h. I pushed a couple of commits after your feedback. :-) |
Looks good. Thank you for your contribution. |
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #282 +/- ##
============================================
+ Coverage 91.09% 91.13% +0.03%
- Complexity 1181 1186 +5
============================================
Files 75 76 +1
Lines 3100 3113 +13
Branches 479 480 +1
============================================
+ Hits 2824 2837 +13
+ Misses 169 168 -1
- Partials 107 108 +1
☔ View full report in Codecov by Sentry. |
Thanks for the contribution @marcospereira and thanks for the review @edward3h. I know it is against the Java conventions, but so far enum constants in the jte project have been camel case. Could we rename the Language enum values to I think it is also okay to be case sensitive here when we parse the setting and do a Other than that, this looks fantastic! |
Thanks, @casid.
Done in a059cd3.
Done in 1888f90. This should be good to go now. :-) |
Wow, that was quick! I'll merge this as soon as the CI build is done. |
Thanks! Also, when can we have a release with those changes? |
There haven't been any changes since the last release, so there's nothing blocking us from releasing this asap. Will have a look at it this weekend. |
Thank you. No rush here, though. :-) By the way, I just submitted a follow-up pr to update the docs. See #283. :-) |
@marcospereira I published version 3.1.2 which contains this feature! |
What?
This adds support to generate Kotlin code (instead of Java) when using the
jte-models
extension. There are a few inconveniences when mixing both languages (clashing names such asList
, which are different for both, not having to import standard-lib classes in Kotlin that would break the Java generate code), and having this will solve that.How
Simply by providing a
language
configuration to the extension. The default is Java, so people upgrading won't have to change anything in their build files. An example:Generate code
The generated code looks like this: