-
Notifications
You must be signed in to change notification settings - Fork 70
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
language agnostic templates #158
Comments
Please note that CobiGen could ship a minimal library for java intentionally containing a class
Then the CobiGen Java Plugin that parses the template could understand this as template macro and interpret it while the template can be processed by any java compiler and gets full IDE support with completion etc. |
This is so damn cool. Give me some time and a room with food and drinks and let me implement this. I am dying for all this :) |
I did design the API. Have a look at this cool example template to get what I am dreaming of: This is fully compliant Java Code that compiles and can be refactored. |
A new challenge when implementing this in CobiGen will be that the templates defined as Java have to be unloaded and reloaded in its own classloader realm as they can frequently change. |
One thing to clarify for Currently I am out of ideas and maybe better go to bed... |
Just discovered that the following chars can be used in java identifiers: |
|
We discussed about the design of this issue and came to the following conclusion:
|
As my initial proposal code was unfortunately lost, I did a little coding from scratch with hopefully perfect design now: |
Here is an update to the latest status. I created a github project as sandbox for the vision how templates could look like with this new feature: For the AST lib (mmm-code) I made great progress and is already working quite well (see linked JUnits and Types as demo):
Review feedback is most welcome. Crucial aspects therefore are:
|
Replacement of dots with new variable syntax has a stupid bug where the re-substitution is not assigned back to the variable. Feature was obviously never tested before. Since I am working on #158 I hit the bug and wanna have it fixed.
In our latest workshop we came to the conclusion to define an interface for marcros that will be implemented. In templates you just provide the class references to the template annotation. CobiGen would provide ready to use macros for generation of getters, setters, equals, hashCode, etc. but users could write their own marcos for advanced use-cases. Templates themselves will remain rather simple and therefore easy to read. |
After some more hacking I came to the conclusion that we should create an abstract class
The "parameters" are set before by CobiGen via the setters of the parent class and are thereby stored in protected members and hence easily accessible (alternatively via getters to even avoid collision with user defined members). |
To take this even further think of that we would also provide UML models or potentially even OpenAPI contracts via our AST. Generating from an input type like a data model class from Java code, C# code, TypeScript code, UML, or OpenAPI could work using the same templates. Nice... |
During my experiments I came back to the point why it is better to introduce a variable type for dynamic type expressions instead of using a concrete type with an annotation containing the expression:
If you do not always use the exact same combination it will not work as expected. With my suggestion the expression and type hierarchy is bundled in a single point of information.
and:
or maybe we can even combine both to allow the planned reuse but also having what you suggested:
Please also note that I suggested to use a syntax like |
Still I am not finally convinced regarding our variable syntax. IMHO it is readable but however, IDE support is not working fine this way. Eclipse often messes up imports or fails with auto-completion. Maybe we should come back to original suggestions like |
I still like |
* #158: only reformatted code with devonfw standard formatter as preparation to make diffs readable * #158: changed underscore syntax for language-agnostic templates Co-authored-by: Malte Brunnlieb <[email protected]>
* #158: only reformatted code with devonfw standard formatter as preparation to make diffs readable * #158: changed underscore syntax for language-agnostic templates Co-authored-by: Malte Brunnlieb <[email protected]>
… vision for future with alternative ETO templates, advanced CobiGenModel, etc
Note: This is a visionary feature request that will cause a lot of effort and refactoring. However, I think that it would cause a huge benefit.
It would be awesome if the templates could be compatible with the language they produce. The template-engine could have a grammar configuration for each language that defines how the language syntax is used to express templating logic.
E.g. for Java this would mean:
x_varname_x
instead of${varname}
what would also be allowed in package segments, type names, methods, fields, etc.UPDATE: initially proposed
$_varname_$
or__varname__
but due to strange problems with tools (eclipse, git) we changed to prevent problems.The new variable syntax with underscores could also express the case-transformation (assuming variable names are then treated case-insensitive):
x_varname_x
for lowercaseX_VarName_X
for PascalCasex_varName_x
for camlCaseX_VARNAME_X
for UPPERCASEX_VAR_NAME_X
for CAP_CASEx_var-name_x
for train-case(Please note that I am intentionally mixing concepts of velocity and freemarker, because I think both have pros and cons but we need to create something new and better).
${var}.getFoo().getBar()
and the type of${var}
is also dynamic it can get really tricky. However I assume thatgetFoo()
has to assume at least a common supertype to work on and then you could also doIf the type is easy to extend you could also create a subclass X_VarType_X in the CobiGen templates that extends from Supertype and mark it as not being a template so the class itself will be ignored on generation.
It would be nice to make some experiments and prototyping in this area to get more insights.
The same concept would easily work with JS, .NET, PHP, python, ruby, scala, etc.
Using this strange v_var_v approach might look ugly but it would be a very simple way to solve big problems.
Assume you can refactor your CobiGen Templates with Java in a typesafe way...
What I like very much about velocity and miss a lot in freemarker is direct access to java calls:
foo.bar.some
forfoo.getBar().getSome()
foo.myMethod()
forfoo.myMethod()
This way we could simply wrap the input object (pojo) into a JavaContainer and provide rich navigations and operations on that and allow simple and native invocations on Class such as
getSimpleName()
orgetName()
such obvious things are so very tricky in freemarker.The text was updated successfully, but these errors were encountered: