Skip to content

Commit d0dc52c

Browse files
kstichsrchase
authored andcommitted
Update for Smithy 1.0.0
This commit updates the build plugin to version 0.2.0. This commit updates to the GA version of Smithy tooling. It includes necessary updates to handle changes in protocol traits, enums, streaming components, and other assorted breaks. Minor updates to the HttpProtocolTestGenerator have been made so that tests are generated for supported protocols even if not specified explicitly in the smithy-build.json file. A .travis.yml configuration file has also been added.
1 parent 5e2c9e1 commit d0dc52c

File tree

51 files changed

+330
-239
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+330
-239
lines changed

.travis.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: java
2+
3+
jdk:
4+
- oraclejdk8
5+
- openjdk8
6+
- openjdk11
7+
8+
sudo: true
9+
10+
dist: trusty
11+
12+
install: /bin/true
13+
14+
script: ./gradlew clean build -Plog-tests

build.gradle.kts

+14-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ plugins {
2525

2626
allprojects {
2727
group = "software.amazon.smithy"
28-
version = "0.1.0"
28+
version = "0.2.0"
2929
}
3030

3131
// The root project doesn't produce a JAR.
@@ -204,6 +204,19 @@ subprojects {
204204

205205
tasks["checkstyleTest"].enabled = false
206206

207+
/*
208+
* Tests
209+
* ====================================================
210+
*
211+
* Configure the running of tests.
212+
*/
213+
// Log on passed, skipped, and failed test events if the `-Plog-tests` property is set.
214+
if (project.hasProperty("log-tests")) {
215+
tasks.test {
216+
testLogging.events("passed", "skipped", "failed")
217+
}
218+
}
219+
207220
/*
208221
* Code coverage
209222
* ====================================================

smithy-typescript-codegen-test/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extra["moduleName"] = "software.amazon.smithy.typescript.codegen.test"
1919
tasks["jar"].enabled = false
2020

2121
plugins {
22-
id("software.amazon.smithy").version("0.4.3")
22+
id("software.amazon.smithy").version("0.5.1")
2323
}
2424

2525
repositories {
@@ -29,5 +29,5 @@ repositories {
2929

3030
dependencies {
3131
implementation(project(":smithy-typescript-codegen"))
32-
implementation("software.amazon.smithy:smithy-protocol-test-traits:0.9.9")
32+
implementation("software.amazon.smithy:smithy-protocol-test-traits:1.0.5")
3333
}

smithy-typescript-codegen-test/model/main.smithy

+52-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
$version: "0.4.0"
1+
$version: "1.0"
22
namespace example.weather
33

44
use smithy.test#httpRequestTests
55
use smithy.test#httpResponseTests
66

77
/// Provides weather forecasts.
8-
@protocols([{name: "aws.rest-json-1.1"}])
8+
@fakeProtocol
99
@paginated(inputToken: "nextToken", outputToken: "nextToken", pageSize: "pageSize")
1010
service Weather {
1111
version: "2006-03-01",
@@ -18,6 +18,7 @@ resource City {
1818
read: GetCity,
1919
list: ListCities,
2020
resources: [Forecast, CityImage],
21+
operations: [GetCityAnnouncements]
2122
}
2223

2324
resource Forecast {
@@ -47,7 +48,7 @@ apply GetCity @httpRequestTests([
4748
{
4849
id: "WriteGetCityAssertions",
4950
documentation: "Does something",
50-
protocol: "aws.rest-json-1.1",
51+
protocol: "example.weather#fakeProtocol",
5152
method: "GET",
5253
uri: "/cities/123",
5354
body: "",
@@ -61,7 +62,7 @@ apply GetCity @httpResponseTests([
6162
{
6263
id: "WriteGetCityResponseAssertions",
6364
documentation: "Does something",
64-
protocol: "aws.rest-json-1.1",
65+
protocol: "example.weather#fakeProtocol",
6566
code: 200,
6667
body: """
6768
{
@@ -139,7 +140,7 @@ apply NoSuchResource @httpResponseTests([
139140
{
140141
id: "WriteNoSuchResourceAssertions",
141142
documentation: "Does something",
142-
protocol: "aws.rest-json-1.1",
143+
protocol: "example.weather#fakeProtocol",
143144
code: 404,
144145
body: """
145146
{
@@ -168,7 +169,7 @@ apply ListCities @httpRequestTests([
168169
{
169170
id: "WriteListCitiesAssertions",
170171
documentation: "Does something",
171-
protocol: "aws.rest-json-1.1",
172+
protocol: "example.weather#fakeProtocol",
172173
method: "GET",
173174
uri: "/cities",
174175
body: "",
@@ -241,6 +242,7 @@ structure GetForecastInput {
241242

242243
structure GetForecastOutput {
243244
chanceOfRain: Float,
245+
precipitation: Precipitation,
244246
}
245247

246248
union Precipitation {
@@ -251,15 +253,16 @@ union Precipitation {
251253
mixed: TypedYesNo,
252254
other: OtherStructure,
253255
blob: Blob,
256+
foo: example.weather.nested#Foo,
254257
baz: example.weather.nested.more#Baz,
255258
}
256259

257260
structure OtherStructure {}
258261

259-
@enum("YES": {}, "NO": {})
262+
@enum([{value: "YES"}, {value: "NO"}])
260263
string SimpleYesNo
261264

262-
@enum("YES": {name: "YES"}, "NO": {name: "NO"})
265+
@enum([{value: "YES", name: "YES"}, {value: "NO", name: "NO"}])
263266
string TypedYesNo
264267

265268
map StringMap {
@@ -281,9 +284,49 @@ structure GetCityImageInput {
281284
}
282285

283286
structure GetCityImageOutput {
284-
@streaming
285287
@httpPayload
286288
image: CityImageData,
287289
}
288290

291+
@streaming
289292
blob CityImageData
293+
294+
@readonly
295+
@http(method: "GET", uri: "/cities/{cityId}/announcements")
296+
operation GetCityAnnouncements {
297+
input: GetCityAnnouncementsInput,
298+
output: GetCityAnnouncementsOutput,
299+
errors: [NoSuchResource]
300+
}
301+
302+
303+
structure GetCityAnnouncementsInput {
304+
@required
305+
@httpLabel
306+
cityId: CityId,
307+
}
308+
309+
structure GetCityAnnouncementsOutput {
310+
@httpHeader("x-last-updated")
311+
lastUpdated: Timestamp,
312+
313+
@httpPayload
314+
announcements: Announcements
315+
}
316+
317+
@streaming
318+
union Announcements {
319+
police: Message,
320+
fire: Message,
321+
health: Message
322+
}
323+
324+
structure Message {
325+
message: String,
326+
author: String
327+
}
328+
329+
// Define a fake protocol trait for use.
330+
@trait
331+
@protocolDefinition
332+
structure fakeProtocol {}

smithy-typescript-codegen-test/model/more-nesting.smithy

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$version: "0.4.0"
1+
$version: "1.0"
22
namespace example.weather.nested.more
33

44
structure Baz {

smithy-typescript-codegen-test/model/nested.smithy

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$version: "0.4.0"
1+
$version: "1.0"
22
namespace example.weather.nested
33

44
structure Foo {

smithy-typescript-codegen-test/smithy-build.json

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"targetNamespace": "Weather",
77
"package": "weather",
88
"packageVersion": "0.0.1",
9-
"protocol": "aws.rest-json-1.1",
109
"packageJson": {
1110
"license": "Apache-2.0"
1211
}

smithy-typescript-codegen/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ extra["displayName"] = "Smithy :: Typescript :: Codegen"
1818
extra["moduleName"] = "software.amazon.smithy.typescript.codegen"
1919

2020
dependencies {
21-
api("software.amazon.smithy:smithy-codegen-core:0.9.9")
22-
implementation("software.amazon.smithy:smithy-protocol-test-traits:0.9.9")
21+
api("software.amazon.smithy:smithy-codegen-core:1.0.5")
22+
implementation("software.amazon.smithy:smithy-protocol-test-traits:1.0.5")
2323
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenVisitor.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,22 @@ class CodegenVisitor extends ShapeVisitor.Default<Void> {
115115
writers = new TypeScriptDelegator(settings, model, fileManifest, symbolProvider, integrations);
116116
}
117117

118-
private static ProtocolGenerator resolveProtocolGenerator(
118+
private ProtocolGenerator resolveProtocolGenerator(
119119
Collection<TypeScriptIntegration> integrations,
120120
ServiceShape service,
121121
TypeScriptSettings settings
122122
) {
123123
// Collect all of the supported protocol generators.
124-
Map<String, ProtocolGenerator> generators = new HashMap<>();
124+
Map<ShapeId, ProtocolGenerator> generators = new HashMap<>();
125125
for (TypeScriptIntegration integration : integrations) {
126126
for (ProtocolGenerator generator : integration.getProtocolGenerators()) {
127-
generators.put(generator.getName(), generator);
127+
generators.put(generator.getProtocol(), generator);
128128
}
129129
}
130130

131-
String protocolName;
131+
ShapeId protocolName;
132132
try {
133-
protocolName = settings.resolveServiceProtocol(service, generators.keySet());
133+
protocolName = settings.resolveServiceProtocol(model, service, generators.keySet());
134134
} catch (UnresolvableProtocolException e) {
135135
LOGGER.warning("Unable to find a protocol generator for " + service.getId() + ": " + e.getMessage());
136136
protocolName = null;
@@ -178,7 +178,10 @@ void execute() {
178178
IndexGenerator.writeIndex(settings, model, symbolProvider, fileManifest);
179179

180180
// Generate protocol tests IFF found in the model.
181-
new HttpProtocolTestGenerator(settings, model, symbolProvider, writers).run();
181+
if (protocolGenerator != null) {
182+
ShapeId protocol = protocolGenerator.getProtocol();
183+
new HttpProtocolTestGenerator(settings, model, protocol, symbolProvider, writers).run();
184+
}
182185

183186
// Write each pending writer.
184187
LOGGER.fine("Flushing TypeScript writers");
@@ -256,7 +259,7 @@ public Void stringShape(StringShape shape) {
256259
@Override
257260
public Void serviceShape(ServiceShape shape) {
258261
if (!Objects.equals(service, shape)) {
259-
LOGGER.fine(() -> "Skipping `" + service.getId() + "` because it is not `" + service.getId() + "`");
262+
LOGGER.fine(() -> "Skipping `" + shape.getId() + "` because it is not `" + service.getId() + "`");
260263
return null;
261264
}
262265

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java

+16-10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import software.amazon.smithy.model.shapes.MemberShape;
2626
import software.amazon.smithy.model.shapes.OperationShape;
2727
import software.amazon.smithy.model.shapes.ServiceShape;
28+
import software.amazon.smithy.model.shapes.Shape;
2829
import software.amazon.smithy.model.shapes.StructureShape;
2930
import software.amazon.smithy.model.traits.StreamingTrait;
3031
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
@@ -172,11 +173,11 @@ private void addInputAndOutputTypes() {
172173
private void writeInputType(String typeName, Optional<StructureShape> inputShape) {
173174
if (inputShape.isPresent()) {
174175
StructureShape input = inputShape.get();
175-
List<MemberShape> streamingMembers = getStreamingMembers(input);
176-
if (streamingMembers.isEmpty()) {
176+
List<MemberShape> blobStreamingMembers = getBlobStreamingMembers(input);
177+
if (blobStreamingMembers.isEmpty()) {
177178
writer.write("export type $L = $T;", typeName, symbolProvider.toSymbol(input));
178179
} else {
179-
writeStreamingInputType(typeName, input, streamingMembers.get(0));
180+
writeStreamingInputType(typeName, input, blobStreamingMembers.get(0));
180181
}
181182
} else {
182183
// If the input is non-existent, then use an empty object.
@@ -196,8 +197,14 @@ private void writeOutputType(String typeName, Optional<StructureShape> outputSha
196197
}
197198
}
198199

199-
private List<MemberShape> getStreamingMembers(StructureShape shape) {
200-
return shape.getAllMembers().values().stream().filter(memberShape -> memberShape.hasTrait(StreamingTrait.class))
200+
private List<MemberShape> getBlobStreamingMembers(StructureShape shape) {
201+
return shape.getAllMembers().values().stream()
202+
.filter(memberShape -> {
203+
// Streaming blobs need to have their types modified
204+
// See `writeStreamingInputType`
205+
Shape target = model.expectShape(memberShape.getTarget());
206+
return target.isBlobShape() && target.hasTrait(StreamingTrait.class);
207+
})
201208
.collect(Collectors.toList());
202209
}
203210

@@ -209,11 +216,10 @@ private List<MemberShape> getStreamingMembers(StructureShape shape) {
209216
*/
210217
private void writeStreamingInputType(String typeName, StructureShape inputShape, MemberShape streamingMember) {
211218
Symbol inputSymbol = symbolProvider.toSymbol(inputShape);
212-
writer.openBlock("export type $L = Omit<$T, $S> & {", "};", typeName, inputSymbol,
213-
streamingMember.getMemberName(), () -> {
214-
writer.write("$1L$2L: $3T[$1S]|string|Uint8Array|Buffer;", streamingMember.getMemberName(),
215-
streamingMember.isRequired() ? "" : "?", inputSymbol);
216-
});
219+
String memberName = streamingMember.getMemberName();
220+
String optionalSuffix = streamingMember.isRequired() ? "" : "?";
221+
writer.openBlock("export type $L = Omit<$T, $S> & {", "};", typeName, inputSymbol, memberName, () ->
222+
writer.write("$1L$2L: $3T[$1S]|string|Uint8Array|Buffer;", memberName, optionalSuffix, inputSymbol));
217223
}
218224

219225
private void addCommandSpecificPlugins() {

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/EnumGenerator.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.util.Comparator;
1919
import software.amazon.smithy.codegen.core.Symbol;
2020
import software.amazon.smithy.model.shapes.StringShape;
21-
import software.amazon.smithy.model.traits.EnumConstantBody;
21+
import software.amazon.smithy.model.traits.EnumDefinition;
2222
import software.amazon.smithy.model.traits.EnumTrait;
2323

2424
/**
@@ -78,7 +78,7 @@ public void run() {
7878

7979
// Unnamed enums generate a union of string literals.
8080
private void generateUnnamedEnum() {
81-
String variants = TypeScriptUtils.getEnumVariants(enumTrait.getValues().keySet());
81+
String variants = TypeScriptUtils.getEnumVariants(enumTrait.getEnumDefinitionValues());
8282
writer.write("export type $L = $L", symbol.getName(), variants);
8383
}
8484

@@ -88,18 +88,17 @@ private void generateNamedEnum() {
8888
// Sort the named values to ensure a stable order and sane diffs.
8989
// TODO: Should we just sort these in the trait itself?
9090
enumTrait.getValues()
91-
.entrySet()
9291
.stream()
93-
.sorted(Comparator.comparing(e -> e.getValue().getName().get()))
94-
.forEach(entry -> writeNamedEnumConstant(entry.getKey(), entry.getValue()));
92+
.sorted(Comparator.comparing(e -> e.getName().get()))
93+
.forEach(this::writeNamedEnumConstant);
9594
});
9695
}
9796

98-
private void writeNamedEnumConstant(String value, EnumConstantBody body) {
97+
private void writeNamedEnumConstant(EnumDefinition body) {
9998
assert body.getName().isPresent();
10099

101100
String name = body.getName().get();
102101
body.getDocumentation().ifPresent(writer::writeDocs);
103-
writer.write("$L = $S,", TypeScriptUtils.sanitizePropertyName(name), value);
102+
writer.write("$L = $S,", TypeScriptUtils.sanitizePropertyName(name), body.getValue());
104103
}
105104
}

0 commit comments

Comments
 (0)