-
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.
Signed-off-by: Jalander Ramagiri <[email protected]>
- Loading branch information
Jalander Ramagiri
committed
May 22, 2024
1 parent
ddd02fb
commit 0b345f8
Showing
161 changed files
with
12,804 additions
and
537 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
66 changes: 66 additions & 0 deletions
66
generator/src/main/java/dev/cdevents/generator/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,66 @@ | ||
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); | ||
} | ||
} | ||
} | ||
} |
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.