-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to CBOR trait from configured formats
This commit refactors the trait implementation to be specific to the CBOR wire format instead of taking a collection of possible wire formats, simplifying the process of resolving the protocol to use for generated code. It also moves the package to `smithy-protocol-traits`, removing the pluralization on `protocols`.
- Loading branch information
Showing
24 changed files
with
301 additions
and
459 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
description = "This module provides the implementation of protocol traits for Smithy." | ||
|
||
ext { | ||
displayName = "Smithy :: Protocol Traits" | ||
moduleName = "software.amazon.smithy.protocol.traits" | ||
} | ||
|
||
dependencies { | ||
api project(":smithy-utils") | ||
api project(":smithy-model") | ||
} |
148 changes: 148 additions & 0 deletions
148
...-protocol-traits/src/main/java/software/amazon/smithy/protocol/traits/Rpcv2CborTrait.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.protocol.traits; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
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.traits.AbstractTrait; | ||
import software.amazon.smithy.model.traits.AbstractTraitBuilder; | ||
import software.amazon.smithy.model.traits.Trait; | ||
import software.amazon.smithy.utils.ListUtils; | ||
import software.amazon.smithy.utils.ToSmithyBuilder; | ||
|
||
public final class Rpcv2CborTrait extends AbstractTrait implements ToSmithyBuilder<Rpcv2CborTrait> { | ||
|
||
public static final ShapeId ID = ShapeId.from("smithy.protocols#rpcv2Cbor"); | ||
|
||
private static final String HTTP = "http"; | ||
private static final String EVENT_STREAM_HTTP = "eventStreamHttp"; | ||
|
||
private final List<String> http; | ||
private final List<String> eventStreamHttp; | ||
|
||
private Rpcv2CborTrait(Builder builder) { | ||
super(ID, builder.getSourceLocation()); | ||
http = ListUtils.copyOf(builder.http); | ||
eventStreamHttp = ListUtils.copyOf(builder.eventStreamHttp); | ||
} | ||
|
||
/** | ||
* Creates a new {@code Builder}. | ||
*/ | ||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
/** | ||
* Updates the builder from a Node. | ||
* | ||
* @param node Node object that must be a valid {@code ObjectNode}. | ||
* @return Returns the updated builder. | ||
*/ | ||
public static Rpcv2CborTrait fromNode(Node node) { | ||
Builder builder = builder().sourceLocation(node); | ||
ObjectNode objectNode = node.expectObjectNode(); | ||
objectNode.getArrayMember(HTTP).map(values -> Node.loadArrayOfString(HTTP, values)) | ||
.ifPresent(builder::http); | ||
objectNode.getArrayMember(EVENT_STREAM_HTTP).map(values -> Node.loadArrayOfString(EVENT_STREAM_HTTP, values)) | ||
.ifPresent(builder::eventStreamHttp); | ||
return builder.build(); | ||
} | ||
|
||
/** | ||
* Gets the priority ordered list of supported HTTP protocol versions. | ||
* | ||
* @return Returns the supported HTTP protocol versions. | ||
*/ | ||
public List<String> getHttp() { | ||
return http; | ||
} | ||
|
||
/** | ||
* Gets the priority ordered list of supported HTTP protocol versions that are required when | ||
* using event streams. | ||
* | ||
* @return Returns the supported event stream HTTP protocol versions. | ||
*/ | ||
public List<String> getEventStreamHttp() { | ||
return eventStreamHttp; | ||
} | ||
|
||
@Override | ||
protected Node createNode() { | ||
ObjectNode.Builder builder = Node.objectNodeBuilder().sourceLocation(getSourceLocation()); | ||
if (!getHttp().isEmpty()) { | ||
builder.withMember(HTTP, Node.fromStrings(getHttp())); | ||
} | ||
if (!getEventStreamHttp().isEmpty()) { | ||
builder.withMember(EVENT_STREAM_HTTP, Node.fromStrings(getEventStreamHttp())); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
public Builder toBuilder() { | ||
return builder().http(http).eventStreamHttp(eventStreamHttp); | ||
} | ||
|
||
/** | ||
* Builder for creating a {@code Rpcv2CborTrait}. | ||
*/ | ||
public static final class Builder extends AbstractTraitBuilder<Rpcv2CborTrait, Builder> { | ||
|
||
private final List<String> http = new ArrayList<>(); | ||
private final List<String> eventStreamHttp = new ArrayList<>(); | ||
|
||
@Override | ||
public Rpcv2CborTrait build() { | ||
return new Rpcv2CborTrait(this); | ||
} | ||
|
||
/** | ||
* Sets the list of supported HTTP protocols. | ||
* | ||
* @param http HTTP protocols to set and replace. | ||
* @return Returns the builder. | ||
*/ | ||
public Builder http(List<String> http) { | ||
this.http.clear(); | ||
this.http.addAll(http); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the list of supported event stream HTTP protocols. | ||
* | ||
* @param eventStreamHttp Event stream HTTP protocols to set and replace. | ||
* @return Returns the builder. | ||
*/ | ||
public Builder eventStreamHttp(List<String> eventStreamHttp) { | ||
this.eventStreamHttp.clear(); | ||
this.eventStreamHttp.addAll(eventStreamHttp); | ||
return this; | ||
} | ||
} | ||
|
||
/** | ||
* Implements the {@code AbstractTrait.Provider}. | ||
*/ | ||
public static final class Provider extends AbstractTrait.Provider { | ||
|
||
public Provider() { | ||
super(ID); | ||
} | ||
|
||
@Override | ||
public Trait createTrait(ShapeId target, Node value) { | ||
Rpcv2CborTrait result = fromNode(value); | ||
result.setNodeCache(value); | ||
return result; | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...-traits/src/main/java/software/amazon/smithy/protocol/traits/Rpcv2CborTraitValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.protocol.traits; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.ServiceShape; | ||
import software.amazon.smithy.model.validation.AbstractValidator; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
/** | ||
* Validates models implementing the {@code Rpcv2CborTrait} against its constraints by: | ||
* | ||
* - Ensuring that every entry in {@code eventStreamHttp} also appears in the {@code http} property | ||
* of a protocol trait. | ||
*/ | ||
@SmithyInternalApi | ||
public final class Rpcv2CborTraitValidator extends AbstractValidator { | ||
|
||
@Override | ||
public List<ValidationEvent> validate(Model model) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
for (ServiceShape serviceShape : model.getServiceShapesWithTrait(Rpcv2CborTrait.class)) { | ||
Rpcv2CborTrait protocolTrait = serviceShape.expectTrait(Rpcv2CborTrait.class); | ||
|
||
List<String> invalid = new ArrayList<>(protocolTrait.getEventStreamHttp()); | ||
invalid.removeAll(protocolTrait.getHttp()); | ||
if (!invalid.isEmpty()) { | ||
events.add(error(serviceShape, protocolTrait, | ||
String.format("The following values of the `eventStreamHttp` property do " | ||
+ "not also appear in the `http` property of the %s protocol " | ||
+ "trait: %s", protocolTrait.toShapeId(), invalid))); | ||
} | ||
} | ||
return events; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...hy-protocol-traits/src/main/java/software/amazon/smithy/protocol/traits/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* Defines protocols for Smithy. | ||
*/ | ||
@SmithyUnstableApi | ||
package software.amazon.smithy.protocol.traits; | ||
|
||
import software.amazon.smithy.utils.SmithyUnstableApi; |
1 change: 1 addition & 0 deletions
1
...its/src/main/resources/META-INF/services/software.amazon.smithy.model.traits.TraitService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
software.amazon.smithy.protocol.traits.Rpcv2CborTrait$Provider |
1 change: 1 addition & 0 deletions
1
...ts/src/main/resources/META-INF/services/software.amazon.smithy.model.validation.Validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
software.amazon.smithy.protocol.traits.Rpcv2CborTraitValidator |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...tocol-traits/src/test/java/software/amazon/smithy/protocol/traits/Rpcv2CborTraitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.protocol.traits; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.model.traits.Trait; | ||
import software.amazon.smithy.model.traits.TraitFactory; | ||
import java.util.Optional; | ||
|
||
public class Rpcv2CborTraitTest { | ||
|
||
@Test | ||
public void loadsTraitWithDefaults() { | ||
Node node = Node.objectNode(); | ||
TraitFactory provider = TraitFactory.createServiceFactory(); | ||
Optional<Trait> trait = provider.createTrait(Rpcv2CborTrait.ID, ShapeId.from("ns.foo#foo"), node); | ||
|
||
Assertions.assertTrue(trait.isPresent()); | ||
Assertions.assertTrue(trait.get() instanceof Rpcv2CborTrait); | ||
Rpcv2CborTrait smithyRpcV2Trait = (Rpcv2CborTrait) trait.get(); | ||
Assertions.assertEquals(smithyRpcV2Trait.toNode(), node); | ||
} | ||
} |
14 changes: 3 additions & 11 deletions
14
...ithy/protocols/traits/TestRunnerTest.java → ...mithy/protocol/traits/TestRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...files/eventStreamHttp-matches-http.errors → ...files/eventStreamHttp-matches-http.errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[ERROR] smithy.example#InvalidService1: The following values of the `eventStreamHttp` property do not also appear in the `http` property of the smithy.protocols#rpcv2 protocol trait: [http/1.1] | Rpcv2Trait | ||
[ERROR] smithy.example#InvalidService2: The following values of the `eventStreamHttp` property do not also appear in the `http` property of the smithy.protocols#rpcv2 protocol trait: [http/1.1] | Rpcv2Trait | ||
[ERROR] smithy.example#InvalidService3: The following values of the `eventStreamHttp` property do not also appear in the `http` property of the smithy.protocols#rpcv2 protocol trait: [http/1.1, h2c] | Rpcv2Trait | ||
[ERROR] smithy.example#InvalidService1: The following values of the `eventStreamHttp` property do not also appear in the `http` property of the smithy.protocols#rpcv2Cbor protocol trait: [http/1.1] | Rpcv2CborTrait | ||
[ERROR] smithy.example#InvalidService2: The following values of the `eventStreamHttp` property do not also appear in the `http` property of the smithy.protocols#rpcv2Cbor protocol trait: [http/1.1] | Rpcv2CborTrait | ||
[ERROR] smithy.example#InvalidService3: The following values of the `eventStreamHttp` property do not also appear in the `http` property of the smithy.protocols#rpcv2Cbor protocol trait: [http/1.1, h2c] | Rpcv2CborTrait |
40 changes: 40 additions & 0 deletions
40
...ces/software/amazon/smithy/protocol/traits/errorfiles/eventStreamHttp-matches-http.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
$version: "2.0" | ||
|
||
namespace smithy.example | ||
|
||
use smithy.protocols#rpcv2Cbor | ||
|
||
@rpcv2Cbor(http: ["h2", "http/1.1"], eventStreamHttp: ["h2"]) | ||
service ValidService1 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2Cbor(http: ["h2"], eventStreamHttp: ["h2"]) | ||
service ValidService2 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2Cbor(http: [], eventStreamHttp: []) | ||
service ValidService3 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2Cbor(http: ["http/1.1"], eventStreamHttp: []) | ||
service ValidService4 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2Cbor(eventStreamHttp: ["http/1.1"]) | ||
service InvalidService1 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2Cbor(http: ["h2"], eventStreamHttp: ["http/1.1"]) | ||
service InvalidService2 { | ||
version: "2023-02-10" | ||
} | ||
|
||
@rpcv2Cbor(http: ["h2"], eventStreamHttp: ["h2", "http/1.1", "h2c"]) | ||
service InvalidService3 { | ||
version: "2023-02-10" | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.