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

Tolerate unknown relationship types #377

Merged
merged 1 commit into from
Apr 16, 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
6 changes: 6 additions & 0 deletions docs/source/spec/core/selectors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ The table below lists the labeled directed relationships from each shape.
- The shape targeted by the member. Note that member targets have no
relationship name.

.. important::

Implementations MUST tolerate parsing unknown relationship types. When
evaluated, the directed traversal of unknown relationship types matches
no shapes.


Functions
=========
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from "[foo^foo]": Unable to deserialize Node using fromNode method: Syntax error at character 1 of 9, near `foo^foo]`: Expected one of the following tokens: `id`, `id|member`, `id|name`, `id|namespace`, `service|version`, `trait|`; expression `[foo^foo]` | Model
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from ":each(:not(string) > list": Unable to deserialize Node using fromNode method: Syntax error at character 25 of 25, near ``: Expected one of the following tokens: `)`, `,`; expression `:each(:not(string) > list` | Model
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from "foo -[]->": Unable to deserialize Node using fromNode method: Syntax error at character 0 of 9, near `foo -[]->`: Expected one of the following tokens: `*`, `-[`, `:`, `>`, `[`, `bigDecimal`, `bigInteger`, `blob`, `boolean`, `byte`, `collection`, `document`, `double`, `float`, `integer`, `list`, `long`, `map`, `member`, `number`, `operation`, `resource`, `service`, `set`, `short`, `simpleType`, `string`, `structure`, `timestamp`, `union`; expression `foo -[]->` | Model
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from "foo -[foo]->": Unable to deserialize Node using fromNode method: Syntax error at character 0 of 12, near `foo -[foo]->`: Expected one of the following tokens: `*`, `-[`, `:`, `>`, `[`, `bigDecimal`, `bigInteger`, `blob`, `boolean`, `byte`, `collection`, `document`, `double`, `float`, `integer`, `list`, `long`, `map`, `member`, `number`, `operation`, `resource`, `service`, `set`, `short`, `simpleType`, `string`, `structure`, `timestamp`, `union`; expression `foo -[foo]->` | Model
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from "foo -[input]->": Unable to deserialize Node using fromNode method: Syntax error at character 0 of 14, near `foo -[input]->`: Expected one of the following tokens: `*`, `-[`, `:`, `>`, `[`, `bigDecimal`, `bigInteger`, `blob`, `boolean`, `byte`, `collection`, `document`, `double`, `float`, `integer`, `list`, `long`, `map`, `member`, `number`, `operation`, `resource`, `service`, `set`, `short`, `simpleType`, `string`, `structure`, `timestamp`, `union`; expression `foo -[input]->` | Model
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from ":not": Unable to deserialize Node using fromNode method: Syntax error at character 4 of 4, near ``: Expected one of the following tokens: `(`; expression `:not` | Model
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from ":not(": Unable to deserialize Node using fromNode method: Syntax error at character 5 of 5, near ``: Expected one of the following tokens: `*`, `-[`, `:`, `>`, `[`, `bigDecimal`, `bigInteger`, `blob`, `boolean`, `byte`, `collection`, `document`, `double`, `float`, `integer`, `list`, `long`, `map`, `member`, `number`, `operation`, `resource`, `service`, `set`, `short`, `simpleType`, `string`, `structure`, `timestamp`, `union`; expression `:not(` | Model
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from ":not()": Unable to deserialize Node using fromNode method: Syntax error at character 5 of 6, near `)`: Expected one of the following tokens: `*`, `-[`, `:`, `>`, `[`, `bigDecimal`, `bigInteger`, `blob`, `boolean`, `byte`, `collection`, `document`, `double`, `float`, `integer`, `list`, `long`, `map`, `member`, `number`, `operation`, `resource`, `service`, `set`, `short`, `simpleType`, `string`, `structure`, `timestamp`, `union`; expression `:not()` | Model
Expand All @@ -119,7 +119,6 @@
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from ":each(string, ": Unable to deserialize Node using fromNode method: Syntax error at character 14 of 14, near ``: Expected one of the following tokens: `*`, `-[`, `:`, `>`, `[`, `bigDecimal`, `bigInteger`, `blob`, `boolean`, `byte`, `collection`, `document`, `double`, `float`, `integer`, `list`, `long`, `map`, `member`, `number`, `operation`, `resource`, `service`, `set`, `short`, `simpleType`, `string`, `structure`, `timestamp`, `union`; expression `:each(string, ` | Model
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from ":each(string, )": Unable to deserialize Node using fromNode method: Syntax error at character 14 of 15, near `)`: Expected one of the following tokens: `*`, `-[`, `:`, `>`, `[`, `bigDecimal`, `bigInteger`, `blob`, `boolean`, `byte`, `collection`, `document`, `double`, `float`, `integer`, `list`, `long`, `map`, `member`, `number`, `operation`, `resource`, `service`, `set`, `short`, `simpleType`, `string`, `structure`, `timestamp`, `union`; expression `:each(string, )` | Model
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from ":each(string, :not())": Unable to deserialize Node using fromNode method: Syntax error at character 19 of 21, near `))`: Expected one of the following tokens: `*`, `-[`, `:`, `>`, `[`, `bigDecimal`, `bigInteger`, `blob`, `boolean`, `byte`, `collection`, `document`, `double`, `float`, `integer`, `list`, `long`, `map`, `member`, `number`, `operation`, `resource`, `service`, `set`, `short`, `simpleType`, `string`, `structure`, `timestamp`, `union`; expression `:each(string, :not())` | Model
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from "service -[foo]-> *": Unable to deserialize Node using fromNode method: Syntax error at character 10 of 18, near `foo]-> *`: Expected one of the following tokens: `bound`, `collectionOperation`, `create`, `delete`, `error`, `identifier`, `input`, `instanceOperation`, `list`, `member`, `operation`, `output`, `put`, `read`, `resource`, `update`; expression `service -[foo]-> *` | Model
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from "[foo]": Unable to deserialize Node using fromNode method: Syntax error at character 1 of 5, near `foo]`: Expected one of the following tokens: `id`, `id|member`, `id|name`, `id|namespace`, `service|version`, `trait|`; expression `[foo]` | Model
[ERROR] -: Error creating `EmitEachSelector` validator: Deserialization error at (/selector): unable to create software.amazon.smithy.model.selector.Selector from "[foo=baz]": Unable to deserialize Node using fromNode method: Syntax error at character 1 of 9, near `foo=baz]`: Expected one of the following tokens: `id`, `id|member`, `id|name`, `id|namespace`, `service|version`, `trait|`; expression `[foo=baz]` | Model
[NOTE] ns.foo#Long: The long shape is not connected to from any service shape. | UnreferencedShape
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
"id": "invalid",
"name": "EmitEachSelector",
"configuration": {
"selector": "foo -[foo]->"
"selector": "foo -[input]->"
}
},
{
Expand Down Expand Up @@ -578,13 +578,6 @@
"selector": "service -[resource]-> resource"
}
},
{
"id": "noMatchInvalidRel",
"name": "EmitEachSelector",
"configuration": {
"selector": "service -[foo]-> *"
}
},
{
"id": "any",
"name": "EmitEachSelector",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.logging.Logger;
import software.amazon.smithy.model.neighbor.RelationshipType;
import software.amazon.smithy.model.shapes.CollectionShape;
import software.amazon.smithy.model.shapes.NumberShape;
Expand All @@ -35,6 +36,8 @@
* Parses a selector expression.
*/
final class Parser {

private static final Logger LOGGER = Logger.getLogger(Parser.class.getName());
private static final Set<Character> BREAK_TOKENS = SetUtils.of(',', ']', ')');
private static final Set<String> REL_TYPES = new HashSet<>();
private static final List<String> FUNCTIONS = ListUtils.of("test", "each", "of", "not");
Expand Down Expand Up @@ -155,7 +158,14 @@ private Selector parseMultiEdgeDirectedNeighbor() {
String next;
do {
// Requires at least one relationship type.
relationships.add(expect(REL_TYPES));
next = parseIdentifier();
relationships.add(next);
// Tolerate unknown relationships, but log a warning.
if (!REL_TYPES.contains(next)) {
LOGGER.warning(String.format(
"Unknown relationship type '%s' found near %s. Expected one of: %s",
next, position - next.length(), REL_TYPES));
}
next = expect(MULTI_EDGE_NEXT_ARG_TOKEN);
} while (!next.equals("]->"));
return new NeighborSelector(relationships);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;

import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -67,4 +68,18 @@ public void directedMultiEdgeTraversal() {

assertThat(result, contains("smithy.example#Input", "smithy.example#Output"));
}

@Test
public void skipsUnknownRelationships() {
Set<String> result = selectIds("operation -[input, output, foo]->");

assertThat(result, contains("smithy.example#Input", "smithy.example#Output"));
}

@Test
public void returnsEmptyForUnknownRelationships() {
Set<String> result = selectIds("operation -[foo]->");

assertThat(result, empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

import java.util.Set;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -112,4 +113,12 @@ public void detectsUnclosedMultiEdgeNeighbor() {
public void detectsUnclosedMultiEdgeNeighborTrailingComma() {
Assertions.assertThrows(SelectorSyntaxException.class, () -> Selector.parse("operation -[input, "));
}

@Test
public void toleratesUnexpectedRelationshipTypes() {
String expr = "operation -[foo]-> *";
Selector selector = Selector.parse(expr);

assertThat(expr, equalTo(selector.toString()));
}
}