-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module preprocessor and fix linter issues
Signed-off-by: Jalander Ramagiri <[email protected]>
- Loading branch information
Jalander Ramagiri
committed
May 23, 2024
1 parent
0b345f8
commit 5a2f5fc
Showing
11 changed files
with
280 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
145 changes: 79 additions & 66 deletions
145
...cdevents/generator/PreprocessSchemas.java → ...vents/preprocessor/PreprocessSchemas.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.