Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit Error Message for Mismatch BEGIN and END Aliases #3889

Merged
merged 4 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -13,11 +13,30 @@
*/
public final class SnippetDictionary {
private final Map<String, List<String>> snippetDictionary = new HashMap<>();
private final List<String> missingBeginTag = new ArrayList<>();

public boolean isActive() {
return !snippetDictionary.isEmpty();
}

/**
* Gets all codesnippet aliases that are missing a corresponding end tag, aka all begin tags missing an end tag.
*
* @return All codesnippet aliases that are missing an end tag.
*/
public List<String> getMissingEndTags() {
return new ArrayList<>(snippetDictionary.keySet());
}

/**
* Gets all codesnippet aliases that are missing a corresponding begin tag, aka all end tags missing a begin tag.
*
* @return All codesnippet aliases that are missing a begin tag.
*/
public List<String> getMissingBeginTags() {
return missingBeginTag;
}

public void beginSnippet(String key) {
if (!this.snippetDictionary.containsKey((key))) {
this.snippetDictionary.put(key, new ArrayList<>());
Expand All @@ -31,9 +50,18 @@ public void processLine(String line) {
}

public List<String> finalizeSnippet(String key) {
List<String> value = this.snippetDictionary.get(key);
this.snippetDictionary.remove(key);
List<String> value = null;

// Check the dictionary for containing the key.
if (snippetDictionary.containsKey(key)) {
// If it does contain the key return the codesnippet for the key.
value = snippetDictionary.remove(key);
} else {
// Otherwise add the codesnippet to the list of keys that have missing begin tags.
missingBeginTag.add(key);
}

// Return no matter what, if begin tags are missing an exception will be thrown later.
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public final class SnippetReplacer {

/**
* The "verification" operation encapsulated by this function is as follows.
*
* <p>
* 1. Scan under the target direction for all discovered code snippet DEFINITIONS 2. Examine all snippet CALLS,
* finding where updates are needed. 3. Report all discovered snippets in need of update as well as all bad snippet
* calls
*
* <p>
* A "bad snippet call" is simply calling for a snippet whose ID has no definition.
*
* <p>
* See {@link #updateCodesnippets(RootAndGlob, List, RootAndGlob, boolean, RootAndGlob, List, boolean, int, boolean, Log)}
* for details on actually defining and calling snippets.
*/
Expand Down Expand Up @@ -405,6 +405,8 @@ private static List<CodesnippetError> verifySnippets(Path file, Pattern beginReg
static Map<String, Codesnippet> getAllSnippets(List<Path> snippetSources)
throws IOException, MojoExecutionException {
Map<String, List<Codesnippet>> codesnippets = new HashMap<>();
Map<String, List<String>> missingBeginTag = new HashMap<>();
Map<String, List<String>> missingEndTag = new HashMap<>();

for (Path samplePath : snippetSources) {
List<String> fileContent = Files.readAllLines(samplePath, StandardCharsets.UTF_8);
Expand Down Expand Up @@ -432,9 +434,17 @@ static Map<String, Codesnippet> getAllSnippets(List<Path> snippetSources)
snippetReader.processLine(line);
}
}

if (!snippetReader.getMissingEndTags().isEmpty()) {
missingEndTag.put(samplePath.toString(), snippetReader.getMissingEndTags());
}

if (!snippetReader.getMissingBeginTags().isEmpty()) {
missingBeginTag.put(samplePath.toString(), snippetReader.getMissingBeginTags());
}
}

String potentialErrorMessage = createDuplicateCodesnippetErrorMessage(codesnippets);
String potentialErrorMessage = createInvalidSnippetsErrorMessage(codesnippets, missingEndTag, missingBeginTag);
if (!potentialErrorMessage.isEmpty()) {
throw new MojoExecutionException(potentialErrorMessage);
}
Expand All @@ -443,7 +453,8 @@ static Map<String, Codesnippet> getAllSnippets(List<Path> snippetSources)
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().get(0)));
}

private static String createDuplicateCodesnippetErrorMessage(Map<String, List<Codesnippet>> codesnippets) {
private static String createInvalidSnippetsErrorMessage(Map<String, List<Codesnippet>> codesnippets,
Map<String, List<String>> missingEndTags, Map<String, List<String>> missingBeginTags) {
StringBuilder errorMessage = new StringBuilder();

for (Map.Entry<String, List<Codesnippet>> codesnippetsById : codesnippets.entrySet()) {
Expand All @@ -470,6 +481,32 @@ private static String createDuplicateCodesnippetErrorMessage(Map<String, List<Co
errorMessage.append(System.lineSeparator());
}

for (Map.Entry<String, List<String>> missingEndTag : missingEndTags.entrySet()) {
errorMessage.append("The following codesnippet aliases in file' ")
.append(missingEndTag.getKey())
.append("' didn't have a matching END alias:")
.append(System.lineSeparator());

for (String alias : missingEndTag.getValue()) {
errorMessage.append(" - ").append(alias).append(System.lineSeparator());
}

errorMessage.append(System.lineSeparator());
}

for (Map.Entry<String, List<String>> missingBeginTag : missingBeginTags.entrySet()) {
errorMessage.append("The following codesnippet aliases in file' ")
.append(missingBeginTag.getKey())
.append("' didn't have a matching BEGIN alias:")
.append(System.lineSeparator());

for (String alias : missingBeginTag.getValue()) {
errorMessage.append(" - ").append(alias).append(System.lineSeparator());
}

errorMessage.append(System.lineSeparator());
}

return errorMessage.toString();
}

Expand Down