Skip to content

Commit

Permalink
Update PreprocessSchemas with interface implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
  • Loading branch information
Jalander Ramagiri committed Jun 13, 2024
1 parent 88d3bdd commit bbd4279
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 78 deletions.
2 changes: 1 addition & 1 deletion preprocessor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<goal>java</goal>
</goals>
<configuration>
<mainClass>dev.cdevents.preprocessor.PreprocessSchemas</mainClass>
<mainClass>dev.cdevents.PreprocessSchemas</mainClass>
<arguments>
<argument>${spec.schemas.dir}</argument>
</arguments>
Expand Down
38 changes: 38 additions & 0 deletions preprocessor/src/main/java/dev/cdevents/PreprocessSchemas.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.cdevents;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import dev.cdevents.process.ProcessSchemas;
import dev.cdevents.process.ProcessSchemasImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
* Adapts incoming schemas to a format that jsonschema2pojo
*/
public final class PreprocessSchemas {

private PreprocessSchemas() {
}
/**
*
* Main method to update schema files.
* @param args [0] - spec schemas directory for the cdevents-sdk-java
*/
public static void main(String[] args) {
if (args == null || args.length != 1) {
System.err.println("Usage: PreprocessSchemas <spec_schemas_directory_path>");
throw new IllegalArgumentException("spec schemas directory path argument not passed to PreprocessSchemas");
}
String specSchemasDir = args[0];
ProcessSchemas processSchemas = new ProcessSchemasImpl();
processSchemas.updateSchemas(specSchemasDir);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
*
*/

package dev.cdevents.preprocessor;
package dev.cdevents;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.cdevents.process;

public interface ProcessSchemas {

/**
* Updates schemas in the specified input schemasPath.
* @param schemasPath
*/
void updateSchemas(String schemasPath);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dev.cdevents.process;

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.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;

public class ProcessSchemasImpl implements ProcessSchemas {

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

/**
* Updates schemas in the specified input schemasPath.
* @param schemasPath
*/
@Override
public void updateSchemas(String schemasPath) {
try {
Files.walk(Paths.get(schemasPath))
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".json"))
.forEach(ProcessSchemasImpl::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());
ObjectMapper objectMapper = new ObjectMapper();
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);
}
}
}
}
17 changes: 17 additions & 0 deletions preprocessor/src/main/java/dev/cdevents/process/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright 2024-Present https://cdevents.dev/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/

package dev.cdevents.process;

0 comments on commit bbd4279

Please sign in to comment.