Skip to content

Commit 082e534

Browse files
committed
Add fromNode to ValidationEvent
1 parent cc26fd6 commit 082e534

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

smithy-model/src/main/java/software/amazon/smithy/model/validation/ValidationEvent.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import software.amazon.smithy.model.SourceException;
2525
import software.amazon.smithy.model.SourceLocation;
2626
import software.amazon.smithy.model.node.Node;
27+
import software.amazon.smithy.model.node.ObjectNode;
28+
import software.amazon.smithy.model.node.StringNode;
2729
import software.amazon.smithy.model.node.ToNode;
2830
import software.amazon.smithy.model.shapes.Shape;
2931
import software.amazon.smithy.model.shapes.ShapeId;
@@ -171,6 +173,35 @@ public Node toNode() {
171173
.build();
172174
}
173175

176+
public static ValidationEvent fromNode(Node node) {
177+
ObjectNode objectNode = node.expectObjectNode();
178+
179+
// A source location should always have at least a filename in the node
180+
// representation of a ValidationEvent. Expect that and default the
181+
// other properties.
182+
SourceLocation location = new SourceLocation(
183+
objectNode.expectStringMember("filename").getValue(),
184+
objectNode.getNumberMemberOrDefault("line", 0).intValue(),
185+
objectNode.getNumberMemberOrDefault("column", 0).intValue());
186+
187+
// Set required properties.
188+
Builder builder = builder()
189+
.id(objectNode.expectStringMember("id").getValue())
190+
.severity(Severity.valueOf(objectNode.expectStringMember("severity").getValue()))
191+
.message(objectNode.expectStringMember("message").getValue())
192+
.sourceLocation(location);
193+
194+
// Set optional properties.
195+
objectNode.getStringMember("suppressionReason").map(StringNode::getValue)
196+
.ifPresent(builder::suppressionReason);
197+
objectNode.getStringMember("shapeId")
198+
.map(StringNode::getValue)
199+
.map(ShapeId::from)
200+
.ifPresent(builder::shapeId);
201+
202+
return builder.build();
203+
}
204+
174205
/**
175206
* @return The location at which the event occurred.
176207
*/

smithy-model/src/test/java/software/amazon/smithy/model/validation/ValidationEventTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.jupiter.api.Assertions;
2525
import org.junit.jupiter.api.Test;
2626
import software.amazon.smithy.model.SourceLocation;
27+
import software.amazon.smithy.model.node.Node;
2728
import software.amazon.smithy.model.node.ObjectNode;
2829
import software.amazon.smithy.model.shapes.ShapeId;
2930
import software.amazon.smithy.model.shapes.StringShape;
@@ -72,6 +73,21 @@ public void suppressionIsOnlyValidWithSuppress() {
7273
});
7374
}
7475

76+
@Test
77+
public void loadsWithFromNode() {
78+
ShapeId id = ShapeId.from("ns.foo#baz");
79+
ValidationEvent event = ValidationEvent.fromNode(Node.parse(
80+
"{\"id\": \"abc.foo\", \"severity\": \"SUPPRESSED\", \"suppressionReason\": \"my reason\", "
81+
+ "\"shapeId\": \"ns.foo#baz\", \"message\": \"The message\", "
82+
+ "\"filename\": \"/path/to/file.smithy\", \"line\": 7, \"column\": 2}"));
83+
84+
assertThat(event.getSeverity(), equalTo(Severity.SUPPRESSED));
85+
assertThat(event.getMessage(), equalTo("The message"));
86+
assertThat(event.getId(), equalTo("abc.foo"));
87+
assertThat(event.getSuppressionReason().get(), equalTo("my reason"));
88+
assertThat(event.getShapeId().get(), is(id));
89+
}
90+
7591
@Test
7692
public void hasGetters() {
7793
ShapeId id = ShapeId.from("ns.foo#baz");

0 commit comments

Comments
 (0)