Skip to content

Commit

Permalink
fixes #165 OverwriteHandler flag needs to be at individual handler
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehu committed Aug 22, 2018
1 parent 2a3f6f4 commit 7e8ed7b
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 261 deletions.
13 changes: 13 additions & 0 deletions codegen-core/src/main/java/com/networknt/codegen/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,17 @@ default void transfer(String folder, String path, String filename, DefaultRocker
}
}

/**
* This is a default method to check if a handler or handler test exists or not before making overwrite decision.
*
* @param folder The output folder of the project
* @param path Current file path in the output folder
* @param filename Current filename in the output folder
* @throws IOException throws IOException
*/
default boolean checkExist(String folder, String path, String filename) throws IOException {
String absPath = folder + (path.isEmpty()? "" : separator + path) + separator + filename;
return Files.exists(Paths.get(absPath));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep
for(Any item : items) {
Any any = item.get("example");
String example = any.valueType() != ValueType.INVALID ? StringEscapeUtils.escapeJson(any.toString()).trim() : "";
if(overwriteHandler) transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), item.get("handler") + ".java", templates.hybrid.handler.template(handlerPackage, host, service, item, example));
if(!overwriteHandler && checkExist(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), item.get("handler") + ".java")) {
continue;
}
transfer(targetPath, ("src.main.java." + handlerPackage).replace(".", separator), item.get("handler") + ".java", templates.hybrid.handler.template(handlerPackage, host, service, item, example));
String serviceId = host + "/" + service + "/" + item.get("name") + "/" + item.get("version");
Map<String, Object> map = new HashMap<>();
map.put("schema", item.get("schema"));
Expand All @@ -92,10 +95,11 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep

// handler test cases
transfer(targetPath, ("src.test.java." + handlerPackage + ".").replace(".", separator), "TestServer.java", templates.hybrid.testServer.template(handlerPackage));
if(overwriteHandlerTest) {
for(Any item : items) {
transfer(targetPath, ("src.test.java." + handlerPackage).replace(".", separator), item.get("handler") + "Test.java", templates.hybrid.handlerTest.template(handlerPackage, host, service, item));
for(Any item : items) {
if(!overwriteHandlerTest && checkExist(targetPath, ("src.test.java." + handlerPackage).replace(".", separator), item.get("handler") + "Test.java")) {
continue;
}
transfer(targetPath, ("src.test.java." + handlerPackage).replace(".", separator), item.get("handler") + "Test.java", templates.hybrid.handlerTest.template(handlerPackage, host, service, item));
}
}

Expand Down
Loading

0 comments on commit 7e8ed7b

Please sign in to comment.