Skip to content

Commit

Permalink
Add module preprocessor and fix linter issues
Browse files Browse the repository at this point in the history
Signed-off-by: Jalander Ramagiri <[email protected]>
  • Loading branch information
Jalander Ramagiri committed May 23, 2024
1 parent 0b345f8 commit 5a2f5fc
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 249 deletions.
5 changes: 3 additions & 2 deletions .github/linters/sun_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@
<module name="IllegalImport"/>
<!-- defaults to sun.* packages -->
<module name="RedundantImport"/>
<module name="UnusedImports">
<!-- Commenting for now, not suitable for generated events -->
<!--<module name="UnusedImports">
<property name="processJavadoc" value="false"/>
</module>
</module>-->

<!-- Checks for Size Violations. -->
<!-- See https://checkstyle.org/config_sizes.html -->
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Lint Code Base
uses: github/super-linter/slim@v4
env:
FILTER_REGEX_INCLUDE: .*sdk/src/main/.*|.*generator/src/main/.*
FILTER_REGEX_INCLUDE: .*sdk/src/main/.*|.*generator/src/main/.*|.*preprocessor/src/main/.*
VALIDATE_ALL_CODEBASE: false
DEFAULT_BRANCH: main
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
8 changes: 4 additions & 4 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ To run all targets, before creating a commit:
```

## Upgrade SDK to the Latest CDEvents Spec
The [Java SDK Generator](./generator) is utilized to create events from the [CDEvents Spec schemas](./spec/schemas),
The [Java SDK Generator](./generator) is utilized to create events from the CDEvents Spec schemas,
and the [CDEvents Spec repository](https://github.com/cdevents/spec/) is added as a Git Submodule to this repository which needs to be updated with latest Spec release.
- Steps involved to generate latest SDK
1. Update spec submodule to the latest release
Expand All @@ -93,8 +93,8 @@ and the [CDEvents Spec repository](https://github.com/cdevents/spec/) is added a
cd ../
````
- Verify the latest Spec is applied and Push the modified spec to your private working branch
2. Generate model classes from [CDEvents Spec schemas](./spec/schemas) using maven plugin `jsonschema2pojo`
- Compare the existing `jsonschema2pojo` plugin configuration in the [generator's pom.xml file](./generator/pom.xml) with the available [CDEvents Spec schemas](./spec/schemas)
2. Generate model classes from CDEvents Spec schemas using maven plugin `jsonschema2pojo`
- Compare the existing `jsonschema2pojo` plugin configuration in the [generator's pom.xml file](./generator/pom.xml) with the available CDEvents Spec schemas
- Add/update executions for `jsonschema2pojo` plugin configuration in the [generator's pom.xml file](./generator/pom.xml) as below,
````pom
<execution>
Expand All @@ -111,7 +111,7 @@ and the [CDEvents Spec repository](https://github.com/cdevents/spec/) is added a
</configuration>
</execution>
````
- Where, `id`, `sourcePath` needs to be updated with the actual file name and `targetPackage` directory structure needs to be created in [sdk models](./sdk/src/main/java/dev/cdevents/models) for each new schema file if you find in [CDEvents Spec schemas](./spec/schemas)
- Where, `id`, `sourcePath` needs to be updated with the actual file name and `targetPackage` directory structure needs to be created in [sdk models](./sdk/src/main/java/dev/cdevents/models) for each new schema file if you find in CDEvents Spec schemas

2. Generate CDEvent classes using [mustache template](./generator/src/main/resources/template/event-template.mustache)
- [event-template.mustache](./generator/src/main/resources/template/event-template.mustache) needs an update If there is any change in the `context` or `subject` of a CDEvent
Expand Down
20 changes: 0 additions & 20 deletions generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,6 @@

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>run-PreprocessSchemas-main-class</id>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>dev.cdevents.generator.PreprocessSchemas</mainClass>
<arguments>
<argument>${parent.project.dir}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private static void updateSubjectContentProperties(SchemaData schemaData, JsonNo
} else if (contentNode.get("type").asText().equals("array")) {
JsonNode itemsNode = contentNode.get("items");
dataType = itemsNode.get("type").asText();
if(dataType.equals("string")){
if (dataType.equals("string")) {
dataType = "List<String>";
}
} else {
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
</scm>

<modules>
<module>preprocessor</module>
<module>generator</module>
<module>sdk</module>
</modules>
Expand Down
70 changes: 70 additions & 0 deletions preprocessor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>dev.cdevents</groupId>
<artifactId>cdevents-sdk-java-parent</artifactId>
<version>0.3.2-SNAPSHOT</version>
</parent>

<artifactId>cdevents-sdk-java-preprocessor</artifactId>

<name>cdevents-sdk-java-preprocessor</name>
<description>Preprocessor for SDK Source code generator</description>
<url>https://github.com/cdevents</url>

<properties>
<maven.javadoc.skip>true</maven.javadoc.skip>
<maven.source.skip>true</maven.source.skip>
<maven.deploy.skip>true</maven.deploy.skip>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<sdk.project.dir>${project.basedir}/../sdk</sdk.project.dir>
<parent.project.dir>${project.basedir}/../</parent.project.dir>
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>run-PreprocessSchemas-main-class</id>
<phase>process-classes</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>dev.cdevents.preprocessor.PreprocessSchemas</mainClass>
<arguments>
<argument>${parent.project.dir}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,66 +1,79 @@
package dev.cdevents.generator;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;

public class PreprocessSchemas {

private static final ObjectMapper objectMapper = new ObjectMapper();

private static final Logger log = LoggerFactory.getLogger(PreprocessSchemas.class);

public static void main(String[] args) throws IOException {
if (args == null || args.length != 1) {
System.err.println("Usage: PreprocessSchemas <schema_directory_path>");
throw new IllegalArgumentException("Schema directory path arguments not passed to PreprocessSchemas");
}
String parentBaseDir = args[0];
String specSchemaDir = parentBaseDir + File.separator + "spec" + File.separator + "schemas";
Files.walk(Paths.get(specSchemaDir))
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".json"))
.forEach(PreprocessSchemas::processFile);
}

private static void processFile(Path filePath) {
log.info("processing schema file {} to update $ref path with json extension.", filePath.getFileName());
try {
JsonNode rootNode = objectMapper.readTree(filePath.toFile());
updateRefs(rootNode);
objectMapper.writerWithDefaultPrettyPrinter().writeValue(filePath.toFile(), rootNode);
} catch (IOException e) {
log.error("Exception occurred while process schema file to update ref {}", e.getMessage());
throw new IllegalStateException("Exception occurred while process schema file to update ref ", e);
}
}

private static void updateRefs(JsonNode node) {
if (node.isObject()) {
ObjectNode objectNode = (ObjectNode) node;
Iterator<String> fieldNames = objectNode.fieldNames();
while (fieldNames.hasNext()) {
String fieldName = fieldNames.next();
JsonNode childNode = objectNode.get(fieldName);
if ("$ref".equals(fieldName) && !childNode.asText().endsWith(".json")) {
objectNode.put(fieldName, childNode.asText() + ".json");
} else {
updateRefs(childNode);
}
}
} else if (node.isArray()) {
for (JsonNode arrayItem : node) {
updateRefs(arrayItem);
}
}
}
}
package dev.cdevents.preprocessor;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;

public class PreprocessSchemas {

private PreprocessSchemas() {
}

private static ObjectMapper objectMapper = new ObjectMapper();

private static Logger log = LoggerFactory.getLogger(PreprocessSchemas.class);

/**
*
* Main method to update schema files.
* @param args [0] - parent directory for the cdevents-sdk-java
*/
public static void main(String[] args) {
if (args == null || args.length != 1) {
System.err.println("Usage: PreprocessSchemas <schema_directory_path>");
throw new IllegalArgumentException("Prent directory path argument not passed to PreprocessSchemas");
}
String parentBaseDir = args[0];
String specSchemaDir = parentBaseDir + File.separator + "spec" + File.separator + "schemas";
try {
Files.walk(Paths.get(specSchemaDir))
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".json"))
.forEach(PreprocessSchemas::processFile);
} catch (IOException e) {
log.error("Exception occurred while processing schema directory {}", e.getMessage());
throw new IllegalStateException("Exception occurred while processing schema directory", e);
}
}

private static void processFile(Path filePath) {
log.info("processing schema file {} to update $ref path with json extension.", filePath.getFileName());
try {
JsonNode rootNode = objectMapper.readTree(filePath.toFile());
updateRefs(rootNode);
objectMapper.writerWithDefaultPrettyPrinter().writeValue(filePath.toFile(), rootNode);
} catch (IOException e) {
log.error("Exception occurred while process schema file to update ref {}", e.getMessage());
throw new IllegalStateException("Exception occurred while process schema file to update ref ", e);
}
}

private static void updateRefs(JsonNode node) {
if (node.isObject()) {
ObjectNode objectNode = (ObjectNode) node;
Iterator<String> fieldNames = objectNode.fieldNames();
while (fieldNames.hasNext()) {
String fieldName = fieldNames.next();
JsonNode childNode = objectNode.get(fieldName);
if ("$ref".equals(fieldName) && !childNode.asText().endsWith(".json")) {
objectNode.put(fieldName, childNode.asText() + ".json");
} else {
updateRefs(childNode);
}
}
} else if (node.isArray()) {
for (JsonNode arrayItem : node) {
updateRefs(arrayItem);
}
}
}
}
2 changes: 1 addition & 1 deletion sdk/src/main/java/dev/cdevents/CDEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static boolean validateCDEventJson(String cdEventJson) {
private static Set<ValidationMessage> getJsonSchemaValidationMessages(CDEvent cdEvent) {
Map<String, String> schemaMap = new HashMap<>();
schemaMap.put(cdEvent.schemaURL(), SCHEMA_CLASSPATH + cdEvent.schemaFileName());
schemaMap.put(cdEvent.baseURI() + "links/embeddedlinksarray", SCHEMA_CLASSPATH+ "links/embeddedlinksarray.json");
schemaMap.put(cdEvent.baseURI() + "links/embeddedlinksarray", SCHEMA_CLASSPATH + "links/embeddedlinksarray.json");
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012, builder ->
// This creates a mapping from $id which starts with https://cdevents.dev/0.4.0/schema to the retrieval URI classpath:schema/
builder.schemaMappers(schemaMappers -> schemaMappers.mappings(schemaMap))
Expand Down
Loading

0 comments on commit 5a2f5fc

Please sign in to comment.