-
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.
Update PreprocessSchemas with interface implementation
Signed-off-by: Jalander Ramagiri <jalander.ramagiri@est.tech>
- Loading branch information
Jalander Ramagiri
committed
Jun 13, 2024
1 parent
88d3bdd
commit bbd4279
Showing
7 changed files
with
136 additions
and
78 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
38 changes: 38 additions & 0 deletions
38
preprocessor/src/main/java/dev/cdevents/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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -14,4 +14,4 @@ | |
* | ||
*/ | ||
|
||
package dev.cdevents.preprocessor; | ||
package dev.cdevents; |
76 changes: 0 additions & 76 deletions
76
preprocessor/src/main/java/dev/cdevents/preprocessor/PreprocessSchemas.java
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
preprocessor/src/main/java/dev/cdevents/process/ProcessSchemas.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package dev.cdevents.process; | ||
|
||
public interface ProcessSchemas { | ||
|
||
/** | ||
* Updates schemas in the specified input schemasPath. | ||
* @param schemasPath | ||
*/ | ||
void updateSchemas(String schemasPath); | ||
} |
69 changes: 69 additions & 0 deletions
69
preprocessor/src/main/java/dev/cdevents/process/ProcessSchemasImpl.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 |
---|---|---|
@@ -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
17
preprocessor/src/main/java/dev/cdevents/process/package-info.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 |
---|---|---|
@@ -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; |