From 324891f471daa711828f9854a66ce8aca6a1483f Mon Sep 17 00:00:00 2001 From: Michael Dowling <mtdowling@gmail.com> Date: Wed, 28 Aug 2019 14:38:45 -0700 Subject: [PATCH] Add expectShapeId for fully-qualified shape ID --- .../amazon/smithy/model/node/StringNode.java | 13 +++++++++++++ .../amazon/smithy/model/node/StringNodeTest.java | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/node/StringNode.java b/smithy-model/src/main/java/software/amazon/smithy/model/node/StringNode.java index 1a7b200c690..803e8b1104d 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/node/StringNode.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/node/StringNode.java @@ -147,6 +147,19 @@ public ShapeId expectShapeId(String namespace) { } } + /** + * Expects that the value of the string is a fully-qualified Shape ID. + * + * @return Returns the parsed Shape ID. + */ + public ShapeId expectShapeId() { + try { + return ShapeId.from(getValue()); + } catch (ShapeIdSyntaxException e) { + throw new SourceException(e.getMessage(), this); + } + } + @Override public boolean equals(Object other) { return other instanceof StringNode && value.equals(((StringNode) other).getValue()); diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/node/StringNodeTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/node/StringNodeTest.java index 5abc1092462..08ce4b898ee 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/node/StringNodeTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/node/StringNodeTest.java @@ -27,6 +27,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import software.amazon.smithy.model.SourceLocation; +import software.amazon.smithy.model.shapes.ShapeId; public class StringNodeTest { @@ -138,4 +139,12 @@ public void equalityAndHashCodeTest() { public void convertsToStringNode() { assertTrue(Node.from("foo").asStringNode().isPresent()); } + + @Test + public void parsesShapeIds() { + ShapeId expected = ShapeId.from("foo.baz#Bar"); + + assertEquals(expected, Node.from("foo.baz#Bar").expectStringNode().expectShapeId()); + assertEquals(expected, Node.from("foo.baz#Bar").expectStringNode().expectShapeId("notfoo")); + } }