Skip to content

Commit

Permalink
Clean up / refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rchache committed Dec 25, 2023
1 parent 5351512 commit dc54402
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import software.amazon.smithy.model.traits.synthetic.SyntheticEnumTrait;
import software.amazon.smithy.model.validation.Severity;
import software.amazon.smithy.model.validation.ValidationEvent;
import software.amazon.smithy.model.validation.ValidationUtils;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.SetUtils;
import software.amazon.smithy.utils.StringUtils;
Expand Down Expand Up @@ -227,7 +228,7 @@ List<ValidationEvent> validate(
}

String message;
String pretty = Node.prettyPrintJsonWithBackticks(right.toNode());
String pretty = ValidationUtils.tickedPrettyPrintedNode(right);
if (path.isEmpty()) {
message = String.format("Added trait `%s` with value %s", trait, pretty);
} else {
Expand Down Expand Up @@ -260,7 +261,7 @@ List<ValidationEvent> validate(
return Collections.emptyList();
}

String pretty = Node.prettyPrintJsonWithBackticks(left.toNode());
String pretty = ValidationUtils.tickedPrettyPrintedNode(left);
String message;
if (path.isEmpty()) {
message = String.format("Removed trait `%s`. Previous trait value: %s", trait, pretty);
Expand Down Expand Up @@ -294,8 +295,8 @@ List<ValidationEvent> validate(
return Collections.emptyList();
}

String leftPretty = Node.prettyPrintJsonWithBackticks(left.toNode());
String rightPretty = Node.prettyPrintJsonWithBackticks(right.toNode());
String leftPretty = ValidationUtils.tickedPrettyPrintedNode(left);
String rightPretty = ValidationUtils.tickedPrettyPrintedNode(right);
String message;
if (path.isEmpty()) {
message = String.format("Changed trait `%s` from %s to %s", trait, leftPretty, rightPretty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import software.amazon.smithy.model.traits.Trait;
import software.amazon.smithy.model.traits.TraitDefinition;
import software.amazon.smithy.model.validation.ValidationEvent;
import software.amazon.smithy.model.validation.ValidationUtils;
import software.amazon.smithy.utils.StringUtils;

/**
Expand Down Expand Up @@ -214,15 +215,15 @@ private TraitDefinition.ChangeType isChangeBreaking(TraitDefinition.ChangeType t
}

private String createBreakingMessage(TraitDefinition.ChangeType type, String path, Node left, Node right) {
String leftPretty = Node.prettyPrintJsonWithBackticks(left.toNode());
String rightPretty = Node.prettyPrintJsonWithBackticks(right.toNode());
String leftPretty = ValidationUtils.tickedPrettyPrintedNode(left);
String rightPretty = ValidationUtils.tickedPrettyPrintedNode(right);

switch (type) {
case ADD:
if (!path.isEmpty()) {
return String.format("Added trait contents to `%s` at path `%s` with value %s",
trait.getId(), path, rightPretty);
} else if (Node.objectNode().equals(right.toNode())) {
} else if (Node.objectNode().equals(right)) {
return String.format("Added trait `%s`", trait.getId());
} else {
return String.format("Added trait `%s` with value %s", trait.getId(), rightPretty);
Expand All @@ -231,7 +232,7 @@ private String createBreakingMessage(TraitDefinition.ChangeType type, String pat
if (!path.isEmpty()) {
return String.format("Removed trait contents from `%s` at path `%s`. Removed value: %s",
trait.getId(), path, leftPretty);
} else if (Node.objectNode().equals(left.toNode())) {
} else if (Node.objectNode().equals(left)) {
return String.format("Removed trait `%s`", trait.getId());
} else {
return String.format("Removed trait `%s`. Previous trait value: %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ public void findsDifferencesInListTraitValues() {
.assemble()
.unwrap();
List<ValidationEvent> events = TestHelper.findEvents(ModelDiff.compare(modelA, modelB), "ModifiedTrait");
List<String> messages = events.stream().map(ValidationEvent::getMessage).collect(Collectors.toList());
List<String> messages = events.stream().map(ValidationEvent::getMessage)
.map(message -> message.replaceAll("\r\n", "\n")) // normalize line separators
.collect(Collectors.toList());

assertThat(events, hasSize(4));
assertThat(events.stream().filter(e -> e.getMessage().contains("Removed"))
Expand Down Expand Up @@ -232,7 +234,9 @@ public void findsDifferencesInSetTraitValues() {
.assemble()
.unwrap();
List<ValidationEvent> events = TestHelper.findEvents(ModelDiff.compare(modelA, modelB), "ModifiedTrait");
List<String> messages = events.stream().map(ValidationEvent::getMessage).collect(Collectors.toList());
List<String> messages = events.stream().map(ValidationEvent::getMessage)
.map(message -> message.replaceAll("\r\n", "\n")) // normalize line separators
.collect(Collectors.toList());

assertThat(events, hasSize(4));
assertThat(events.stream().filter(e -> e.getMessage().contains("Removed"))
Expand Down Expand Up @@ -262,7 +266,9 @@ public void findsDifferencesInMapTraitValues() {
.assemble()
.unwrap();
List<ValidationEvent> events = TestHelper.findEvents(ModelDiff.compare(modelA, modelB), "ModifiedTrait");
List<String> messages = events.stream().map(ValidationEvent::getMessage).collect(Collectors.toList());
List<String> messages = events.stream().map(ValidationEvent::getMessage)
.map(message -> message.replaceAll("\r\n", "\n")) // normalize line separators
.collect(Collectors.toList());

assertThat(events, hasSize(4));
assertThat(events.stream().filter(e -> e.getMessage().contains("Removed"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,27 +155,6 @@ public static String prettyPrintJson(Node node, String indentString) {
return NodeHandler.prettyPrint(node, indentString);
}

/**
* Writes the contents of a Node to a pretty-printed JSON string,
* and surrounds the result in backticks.
*
* @param node Node to write.
* @return Returns a string suitable for Markdown rendering.
*/
public static String prettyPrintJsonWithBackticks(Node node) {
String prettyPrinted = prettyPrintJson(node);
if (prettyPrinted.contains(System.lineSeparator()) || prettyPrinted.contains("\n")) {
return System.lineSeparator() + "```" + System.lineSeparator()
+ prettyPrinted + System.lineSeparator()
+ "```";
} else if (prettyPrinted.startsWith("\"") && prettyPrinted.endsWith("\"")) {
// for pure strings, replace the quotes with backticks
return "`" + prettyPrinted.substring(1, prettyPrinted.length() - 1) + "`";
} else {
return "`" + prettyPrinted + "`";
}
}

/**
* Writes the contents of a Node to a non-pretty-printed JSON string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.ToShapeId;

Expand Down Expand Up @@ -89,6 +90,26 @@ public static String tickedList(Stream<?> values) {
return values.map(Object::toString).sorted().collect(Collectors.joining("`, `", "`", "`"));
}

/**
* Writes the contents of a Node to a pretty-printed JSON string surrounded in backticks.
*
* @param node Node to write.
* @return Returns a string suitable for Markdown rendering.
*/
public static String tickedPrettyPrintedNode(Node node) {
String prettyPrinted = Node.prettyPrintJson(node);
if (prettyPrinted.contains(System.lineSeparator()) || prettyPrinted.contains("\n")) {
return System.lineSeparator() + "```" + System.lineSeparator()
+ prettyPrinted + System.lineSeparator()
+ "```";
} else if (prettyPrinted.startsWith("\"") && prettyPrinted.endsWith("\"")) {
// for pure strings, replace the quotes with backticks
return "`" + prettyPrinted.substring(1, prettyPrinted.length() - 1) + "`";
} else {
return "`" + prettyPrinted + "`";
}
}

@Deprecated
public static <T extends ToShapeId> Map<String, List<ShapeId>> findDuplicateShapeNames(Collection<T> shapes) {
return shapes.stream()
Expand Down

0 comments on commit dc54402

Please sign in to comment.