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

Add fromNode to ValidationEvent #518

Merged
merged 1 commit into from
Jul 30, 2020
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 @@ -24,6 +24,8 @@
import software.amazon.smithy.model.SourceException;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.node.StringNode;
import software.amazon.smithy.model.node.ToNode;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
Expand Down Expand Up @@ -171,6 +173,35 @@ public Node toNode() {
.build();
}

public static ValidationEvent fromNode(Node node) {
ObjectNode objectNode = node.expectObjectNode();

// A source location should always have at least a filename in the node
// representation of a ValidationEvent. Expect that and default the
// other properties.
SourceLocation location = new SourceLocation(
objectNode.expectStringMember("filename").getValue(),
objectNode.getNumberMemberOrDefault("line", 0).intValue(),
objectNode.getNumberMemberOrDefault("column", 0).intValue());

// Set required properties.
Builder builder = builder()
.id(objectNode.expectStringMember("id").getValue())
.severity(Severity.valueOf(objectNode.expectStringMember("severity").getValue()))
.message(objectNode.expectStringMember("message").getValue())
.sourceLocation(location);

// Set optional properties.
objectNode.getStringMember("suppressionReason").map(StringNode::getValue)
.ifPresent(builder::suppressionReason);
objectNode.getStringMember("shapeId")
.map(StringNode::getValue)
.map(ShapeId::from)
.ifPresent(builder::shapeId);

return builder.build();
}

/**
* @return The location at which the event occurred.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.StringShape;
Expand Down Expand Up @@ -72,6 +73,21 @@ public void suppressionIsOnlyValidWithSuppress() {
});
}

@Test
public void loadsWithFromNode() {
ShapeId id = ShapeId.from("ns.foo#baz");
ValidationEvent event = ValidationEvent.fromNode(Node.parse(
"{\"id\": \"abc.foo\", \"severity\": \"SUPPRESSED\", \"suppressionReason\": \"my reason\", "
+ "\"shapeId\": \"ns.foo#baz\", \"message\": \"The message\", "
+ "\"filename\": \"/path/to/file.smithy\", \"line\": 7, \"column\": 2}"));

assertThat(event.getSeverity(), equalTo(Severity.SUPPRESSED));
assertThat(event.getMessage(), equalTo("The message"));
assertThat(event.getId(), equalTo("abc.foo"));
assertThat(event.getSuppressionReason().get(), equalTo("my reason"));
assertThat(event.getShapeId().get(), is(id));
}

@Test
public void hasGetters() {
ShapeId id = ShapeId.from("ns.foo#baz");
Expand Down