Skip to content

Commit

Permalink
Support generating Markdown table for response status codes automatic…
Browse files Browse the repository at this point in the history
…ally
  • Loading branch information
JamesChenX committed Dec 25, 2023
1 parent 9148daf commit 9db96f6
Showing 1 changed file with 73 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,25 @@ public static void main(String[] args) {
List<String> 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
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -95,16 +98,35 @@ 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
+ " = "
+ statusCode
: "case "
+ codeName;
});

// markdown
generateSourceCodeFile("md",
lines,
0,
false,
(statusCodeName, statusCode, groupName, isFirstGroupCode) -> isFirstGroupCode
? "|"
+ groupName
+ "|"
+ statusCodeName
+ "|"
+ statusCode
+ "|"
: "||"
+ statusCodeName
+ "|"
+ statusCode
+ "|");
}

private static String convertToCamelCase(String input) {
Expand All @@ -128,27 +150,43 @@ private static void generateSourceCodeFile(
List<String> lines,
int indent,
StatusCodeTransformer transformer) throws IOException {
generateSourceCodeFile(fileSuffix, lines, indent, true, transformer);
}

private static void generateSourceCodeFile(
String fileSuffix,
List<String> 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) {
start = line.startsWith("public enum ResponseStatusCode {");
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(";")) {
Expand All @@ -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);
}

}

0 comments on commit 9db96f6

Please sign in to comment.