diff --git a/turms-server-common/src/test/java/ResponseStatusCodeFileGenerator.java b/turms-server-common/src/test/java/ResponseStatusCodeFileGenerator.java index c989833ff9..1c28183f94 100644 --- a/turms-server-common/src/test/java/ResponseStatusCodeFileGenerator.java +++ b/turms-server-common/src/test/java/ResponseStatusCodeFileGenerator.java @@ -49,22 +49,25 @@ public static void main(String[] args) { List lines = Files.readAllLines(path); // cpp - generateSourceCodeFile("h", lines, 0, (statusCodeName, statusCode, isGroupFirstCode) -> { - String codeName = convertToCamelCase(statusCodeName); - return "const int k" - + codeName.substring(0, 1) - .toUpperCase() - + codeName.substring(1) - + " = " - + statusCode - + ";"; - }); + generateSourceCodeFile("h", + lines, + 0, + (statusCodeName, statusCode, groupName, isFirstGroupCode) -> { + String codeName = convertToCamelCase(statusCodeName); + return "const int k" + + codeName.substring(0, 1) + .toUpperCase() + + codeName.substring(1) + + " = " + + statusCode + + ";"; + }); // dart generateSourceCodeFile("dart", lines, 2, - (statusCodeName, statusCode, isGroupFirstCode) -> "static const " + (statusCodeName, statusCode, groupName, isFirstGroupCode) -> "static const " + convertToCamelCase(statusCodeName) + " = " + statusCode @@ -74,7 +77,7 @@ public static void main(String[] args) { generateSourceCodeFile("kt", lines, 4, - (statusCodeName, statusCode, isGroupFirstCode) -> "const val " + (statusCodeName, statusCode, groupName, isFirstGroupCode) -> "const val " + statusCodeName + " = " + statusCode); @@ -83,7 +86,7 @@ public static void main(String[] args) { generateSourceCodeFile("ts", lines, 4, - (statusCodeName, statusCode, isGroupFirstCode) -> isGroupFirstCode + (statusCodeName, statusCode, groupName, isFirstGroupCode) -> isFirstGroupCode ? statusCodeName + " = " + statusCode @@ -95,9 +98,9 @@ public static void main(String[] args) { generateSourceCodeFile("swift", lines, 4, - (statusCodeName, statusCode, isGroupFirstCode) -> { + (statusCodeName, statusCode, groupName, isFirstGroupCode) -> { String codeName = convertToCamelCase(statusCodeName); - return isGroupFirstCode + return isFirstGroupCode ? "case " + codeName + " = " @@ -105,6 +108,25 @@ public static void main(String[] args) { : "case " + codeName; }); + + // markdown + generateSourceCodeFile("md", + lines, + 0, + false, + (statusCodeName, statusCode, groupName, isFirstGroupCode) -> isFirstGroupCode + ? "|" + + groupName + + "|" + + statusCodeName + + "|" + + statusCode + + "|" + : "||" + + statusCodeName + + "|" + + statusCode + + "|"); } private static String convertToCamelCase(String input) { @@ -128,10 +150,20 @@ private static void generateSourceCodeFile( List lines, int indent, StatusCodeTransformer transformer) throws IOException { + generateSourceCodeFile(fileSuffix, lines, indent, true, transformer); + } + + private static void generateSourceCodeFile( + String fileSuffix, + List lines, + int indent, + boolean outputComments, + StatusCodeTransformer transformer) throws IOException { StringBuilder builder = new StringBuilder(1024 * 16); boolean start = false; int lastStatusCode = Integer.MIN_VALUE; String indentStr = " ".repeat(indent); + String groupName = null; for (String line : lines) { line = line.trim(); if (!start) { @@ -139,16 +171,22 @@ private static void generateSourceCodeFile( continue; } if (line.isBlank()) { - builder.append('\n'); + if (outputComments) { + builder.append('\n'); + } continue; } if (line.contains("TODO")) { continue; } if (line.startsWith("//")) { - builder.append(indentStr) - .append(line) - .append('\n'); + if (outputComments) { + builder.append(indentStr) + .append(line) + .append('\n'); + } + groupName = line.substring(2) + .trim(); continue; } if (line.endsWith(";")) { @@ -172,23 +210,34 @@ private static void generateSourceCodeFile( + "\" as number", e); } - String newLine = transformer - .transform(statusCodeName, statusCode, statusCode != lastStatusCode + 1); + String newLine = transformer.transform(statusCodeName, + statusCode, + groupName, + statusCode != lastStatusCode + 1); builder.append(indentStr) .append(newLine) .append('\n'); lastStatusCode = statusCode; } String code = builder.toString(); - Files.writeString(Path.of("./code." - + fileSuffix), + Path path = Path.of("./code." + + fileSuffix); + Files.writeString(path, code, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); + + System.out.println("Generated a file: " + + path.toAbsolutePath() + .normalize()); } private interface StatusCodeTransformer { - String transform(String statusCodeName, int statusCode, boolean isGroupFirstCode); + String transform( + String statusCodeName, + int statusCode, + String groupName, + boolean isFirstGroupCode); } } \ No newline at end of file