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 events using jsonschema2pojo and mustache template #58

Merged
merged 27 commits into from
Oct 2, 2023

Conversation

rjalander
Copy link
Contributor

@rjalander rjalander commented Jun 26, 2023

Initial changes to generate events using jsonschema2pojo and mustache template, /src/main/resources/template/event-template.mustache

pom.xml has the plugin to generate jsonschema2pojo and plugin to run the main class from CDEventsGenerator to create events using mustache template.

Running ./mvnw verify

  1. Generates models using jsonschema2pojo for each event under - dev.cdevents.models.generated.subject.predicate
  2. Generates events from mustache template and placed under - dev.cdevents.events.generated

Note:

  1. Generating only 3 events for an initial review and schema files for the same are placed under - /src/main/resources/schema (This will essentially point to spec repo to generate all the events once reviewed)
  2. Removed current Events(dev.cdevents.events) and Models(dev.cdevents.models) that are manually created earlier.

Copy link
Contributor

@lapentad lapentad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm
Just some comments on the CDEventsGenerator class which hold most of the logic here. But I don't see anything disruptive, apart from indentation/whitespaces/formatting (So anything you can see from the linting log)
I'll take a second look another day

} catch (IOException e) {
throw new CDEventsException("Exception occurred while generating class file from Json schema ", e);
}
System.out.println("Rendered event-template has been written to file - "+classFile.getAbsolutePath());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be logged with a logger or printing is fine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, will be changing to logger for all the required logs

src/main/java/dev/cdevents/CDEventsGenerator.java Outdated Show resolved Hide resolved
String predicate = type[3];
String capitalizedSubject = StringUtils.capitalize(subject);
if(subject.equals("pipelinerun")){
capitalizedSubject = subject.substring(0, 1).toUpperCase() + subject.substring(1, 8) + subject.substring(8, 9).toUpperCase() + subject.substring(9);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you trying to capitalize only char 0 and char 8 in the string?

Suggested change
capitalizedSubject = subject.substring(0, 1).toUpperCase() + subject.substring(1, 8) + subject.substring(8, 9).toUpperCase() + subject.substring(9);
capitalizedSubject = StringUtils.capitalize(subject.substring(0, 8)) +
StringUtils.capitalize(subject.substring(8));

If you import a newer version of StringUtils you can capitalize the first character of a substring this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that's right using the StringUtils.capitalize() in the previous line, I can update to this simplified one based on your suggestion,
capitalizedSubject = capitalizedSubject.substring(0, 8) + StringUtils.capitalize(subject.substring(8));

src/main/java/dev/cdevents/CDEventsGenerator.java Outdated Show resolved Hide resolved
@afrittoli
Copy link
Contributor

@rjalander thank you, excited to see this happening!!
I haven't checked the code, but one comment I have is that it would be nice to add a CI job that generates the code and checks if the code is up to date and fails if not.

@rjalander
Copy link
Contributor Author

rjalander commented Jun 27, 2023

Thank you for your comment @afrittoli
Currently it generates the code from the Schemas as part of "Test/Unit Test" CI job above, and I can add more Unit tests to validate the generated code.

Build Job
image

@rjalander
Copy link
Contributor Author

@afrittoli is this a good idea to keep a release branch of Java-sdk as of now before this PR gets merged into main as generated code for v0.1.2 version ?

@afrittoli
Copy link
Contributor

I would recommend continuing regular development on main.
We can push a tag for v0.1.2 and if in future we need more v0.1.x patches we can create a branch for that.

pom.xml Outdated
@@ -105,6 +105,18 @@
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use latest versions as much as possible. In particular commons-lang3 supersedes commons-lang

https://mvnrepository.com/artifact/org.apache.commons/commons-lang3

pom.xml Outdated
@@ -128,6 +140,12 @@
<version>${packageurl.version}</version>
</dependency>

<dependency>
<groupId>com.github.spullara.mustache.java</groupId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this dependency required at runtime or only during code generation? If the latter then change to <scope>provided</scope> as well as adding <optional>true</optional>, as this dependency should not leak to consumers.

pom.xml Outdated
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the previous comment on the mustache dependency. Might be worth separating code generation from generated code consumption. That is, migrate this project to a multi module Maven setup, putting code generators in one module, and cdevents Java sdk in another. Otherwise we risk exposing the code generator and its dependencies as part of the runtime dependencies required by consumers of cdevents-java-sdk.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code as suggested with multi modules generator and sdk based on your commit

@rjalander rjalander requested a review from aalmiray July 12, 2023 17:05
Copy link
Contributor

@aalmiray aalmiray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. However, the release workflow in .github/workflows/release.yml and the root pom.xml require some changes:

release.yml

  • change ./mvnw -ntp -B --file pom.xml -Prelease to ./mvnw -ntp -B --file pom.xml -Prelease -pl :cdevents-sdk-java-parent

pom.xml

  • update version.jreleaser.plugin to 1.7.0
  • update <pluginManagement> by adding
                <plugin>
                    <groupId>org.jreleaser</groupId>
                    <artifactId>jreleaser-maven-plugin</artifactId>
                    <version>${version.jreleaser.plugin}</version>
                </plugin>

@aalmiray
Copy link
Contributor

Latest changes are 👍

@rjalander
Copy link
Contributor Author

Thank you all for your review and comments,

@aalmiray @afrittoli Can you please approve this PR If no other comments, I will be creating another PR which generates all other events pointing to v0.1.2 schemas of spec repository as submodule

Jalander Ramagiri and others added 20 commits July 13, 2023 11:16
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Co-authored-by: Lapenta Francesco Davide <37077655+lapentad@users.noreply.github.com>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Co-authored-by: Lapenta Francesco Davide <37077655+lapentad@users.noreply.github.com>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
<scope>runtime</scope>
</dependency>

<!-- Test dependencies -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generator Maven module needs no test dependencies. These can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I will create a Unit testing for generator code in the coming PR. Is that fine to keep this dependency.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's great that unit tests will be added, maybe you can add the dependency along with the tests?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. It's a good practice to add only the minimum reqs for a given unit of work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, Removed

sdk/pom.xml Outdated
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the library must remain Java 8 compatible. The parent pom defines compiler properties for Java 1.8. Remove this plugin definition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this plugin

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hold on a second. The moditect plugin was added because the codebase was set to Java 8 as a baseline. If the baseline moves to 11 then this plugin may be removed BUT and explicit module descriptor must be added.

@afrittoli has the Java 8 baseline changed recently?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plugin I requested for removal is the compiler plugin because it explicitly overrides source/target compatibility to 11.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not aware of any change from Java 8 to Java 11.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember we mentioned in the DEVELOPMENT readme "Java 9 is a minimum requirement to build the project",

@aalmiray is that readme need to be updated / any changes needed in the pom.xml ?

Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
pom.xml Show resolved Hide resolved
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
@rjalander
Copy link
Contributor Author

Thank you all for the review, @afrittoli @aalmiray Could you please approve If no other comments to address.

@rjalander
Copy link
Contributor Author

Also the Spinnaker PR; spinnaker/echo#1295, is dependent on the Java-SDK to produce CDevents from Spinnaker.
I will create another PR to generate all other events once this PR is merged.
Can we plan release of v0.1.2 to maven central to solve the Spinnaker dependency first?

@afrittoli
Copy link
Contributor

Also the Spinnaker PR; spinnaker/echo#1295, is dependent on the Java-SDK to produce CDevents from Spinnaker. I will create another PR to generate all other events once this PR is merged. Can we plan release of v0.1.2 to maven central to solve the Spinnaker dependency first?

Hello @rjalander - yes, this does not seem to me like a dependency for v0.1.2 - I think we already agreed that the SDK is ready for v0.1.2. Is there anything else holding the release? Would you be happy to do the release or would you like me to do it?

@rjalander
Copy link
Contributor Author

Ok, that will be great If we do release with the master changes as of now, yes can you please create a new release.

@aalmiray
Copy link
Contributor

Not a blocker for releasing 0.1.2 but, given the contributions I've made to the project so far, could I be added as a contributor to the root pom.xml? 🙏

Jalander Ramagiri added 2 commits August 8, 2023 16:58
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
@rjalander
Copy link
Contributor Author

@afrittoli @aalmiray please let me know If any other comments that needs to be addressed to get this PR approved/merged.

Copy link
Contributor

@afrittoli afrittoli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rjalander - it's great to see this happening!

I'm a bit worried about all the events being deleted and only a few ones being added as generated. All the tests are deleted too, and new one are added for the new generated code, which makes it harder to verify that the new code still behaves the same as the old one.

If you're confident that the new code preserves functionality, it's ok for me, hopefully this is a one-off for the transition to generated code.

@rjalander
Copy link
Contributor Author

In the next PR I will be adding other generated events which will have the same tests running to make sure the generated code works as expected.
I am good with the changes as this will have test for each generated event to verify in the next PR.

Copy link
Contributor

@afrittoli afrittoli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok - let's go for it!

@afrittoli
Copy link
Contributor

@aalmiray If it's ok for you I would go ahead and merge this.
@rjalander is it ok to squash this into one or do you want more commits? I would prefer not to keep all the 27 ones though

@rjalander
Copy link
Contributor Author

Yes we can Squash and merge.

@rjalander
Copy link
Contributor Author

@afrittoli can you please merge this PR, I will create a follow up PR for the next set of changes.

@afrittoli afrittoli merged commit 739fb09 into cdevents:main Oct 2, 2023
3 checks passed
@cdevents-bot cdevents-bot added the released Issue has been released label Jan 11, 2024
@cdevents-bot
Copy link
Collaborator

🎉 This issue has been resolved in v0.3.0 (Release Notes)

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

Successfully merging this pull request may close these issues.

5 participants