Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class TableErrorsArgs extends BaseArgs<TableErrorsCtx> {
names = { "-o", "--output" },
description = "Output AsciiDoc file",
paramLabel = "OUTPUT_FILE",
defaultValue = "build/docs/table-errors"
defaultValue = "../docs-common/modules/data-api/partials/dataapi-errors.adoc"
)
public String $output;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,13 @@ public class TableErrorsCmd extends BaseCmd<TableErrorsCtx> {
@Mixin @Getter
private TableErrorsArgs $args;

private static final String[] ASCIIDOC_PREFIX = {
"= {data-api} error codes",
"",
"The {data-api} can return the following errors:",
"",
"// Note: The following table is autogenerated from the actual Data API error codes and should not be edited manually.",
"// If there is a typo or an issue with description clarity, the source Data API code should be updated instead.",
"// To regenerate, run 'dh docgen table-errors' from the datastax/astra-client-docs-tests repo. Pass the raw 'src/main/resources/errors.yaml' file from the stargate/data-api repo corresponding to the desired release.",
""
};

private static final String[] ASCIIDOC_SUFFIX = {};
private static final String TAG_START = "// tag::generated-table[]";
private static final String TAG_END = "// end::generated-table[]";

private static final Pattern TABLE_REGION_PATTERN = Pattern.compile(
Pattern.quote(TAG_START) + ".*?" + Pattern.quote(TAG_END),
Pattern.DOTALL
);

private static final Map<String, String[]> ERROR_CODE_SEE_ALSO_MAP = Map.of(
"DOCUMENT_REPLACE_DIFFERENT_DOCID", new String[] {
Expand Down Expand Up @@ -75,39 +70,42 @@ protected int run() {
.sorted(Comparator.comparing(n -> n.path("code").asString("")))
.toList();

if (output.getParent() != null) {
Files.createDirectories(output.getParent());
}

try (val writer = Files.newBufferedWriter(output)) {
for (val line : ASCIIDOC_PREFIX) {
writer.write(line + "\n");
}

writer.write("[cols=\"1,1,2,3\"]\n|===\n| Error code | Scope | Title | Description\n\n");
// Build the replacement block (everything between and including the tags)
val sb = new StringBuilder();
sb.append(TAG_START).append("\n");
sb.append("[cols=\"1,1,2,3\"]\n|===\n| Error code | Scope | Title | Description\n\n");

for (val entry : errors) {
val code = entry.path("code").asString("");
val scope = entry.hasNonNull("scope") ? entry.get("scope").asString() : "n/a";
val title = escapePipe(entry.path("title").asString(""));
val desc = escapePipe(PLACEHOLDER_PATTERN.matcher(entry.path("body").asString("").split("\r?\n", 2)[0].trim()).replaceAll("**[$1]**"));
for (val entry : errors) {
val code = entry.path("code").asString("");
val scope = entry.hasNonNull("scope") ? entry.get("scope").asString() : "n/a";
val title = escapePipe(entry.path("title").asString(""));
val desc = escapePipe(PLACEHOLDER_PATTERN.matcher(entry.path("body").asString("").split("\r?\n", 2)[0].trim()).replaceAll("**[$1]**"));

writer.write(String.format("| `%s`\n| %s\n| %s\na| %s\n", code, scope, title, desc));
sb.append(String.format("| `%s`\n| %s\n| %s\na| %s\n", code, scope, title, desc));

if (ERROR_CODE_SEE_ALSO_MAP.containsKey(code)) {
String[] linkInfo = ERROR_CODE_SEE_ALSO_MAP.get(code);
writer.write(String.format("\nSee also %s[%s].\n", linkInfo[0], linkInfo[1]));
}
writer.write("\n");
if (ERROR_CODE_SEE_ALSO_MAP.containsKey(code)) {
String[] linkInfo = ERROR_CODE_SEE_ALSO_MAP.get(code);
sb.append(String.format("\nSee also %s[%s].\n", linkInfo[0], linkInfo[1]));
}
sb.append("\n");
}

writer.write("|===\n");
sb.append("|===\n");
sb.append(TAG_END);

for (val line : ASCIIDOC_SUFFIX) {
writer.write(line + "\n");
}
// Read the existing file and splice in the new table
val existing = Files.readString(output);
val matcher = TABLE_REGION_PATTERN.matcher(existing);

if (!matcher.find()) {
throw new IllegalStateException("Could not find generated-table tags in " + output + ". Expected markers: '" + TAG_START + "' and '" + TAG_END + "'");
}

val updated = new StringBuilder(existing);
updated.replace(matcher.start(), matcher.end(), sb.toString());

Files.writeString(output, updated.toString());

CliLogger.println(false, "@|green ✓|@ Written " + errors.size() + " rows to " + output);

return 0;
Expand Down
Loading