Skip to content

Commit fba1006

Browse files
authored
feat: add rpcv2 cbor support (#509)
1 parent c5b40f4 commit fba1006

Some content is hidden

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

47 files changed

+6471
-12
lines changed

codegen/smithy-go-codegen/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ dependencies {
2727
api("org.jsoup:jsoup:1.14.1")
2828
api("software.amazon.smithy:smithy-rules-engine:$smithyVersion")
2929
implementation("software.amazon.smithy:smithy-protocol-test-traits:$smithyVersion")
30+
api("software.amazon.smithy:smithy-protocol-traits:$smithyVersion")
3031
}

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/AddOperationShapes.java

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.TreeSet;
1919
import java.util.logging.Logger;
20+
import software.amazon.smithy.go.codegen.trait.BackfilledInputOutputTrait;
2021
import software.amazon.smithy.model.Model;
2122
import software.amazon.smithy.model.knowledge.TopDownIndex;
2223
import software.amazon.smithy.model.shapes.AbstractShapeBuilder;
@@ -83,6 +84,7 @@ private static StructureShape emptyOperationStructure(ServiceShape service, Shap
8384
return StructureShape.builder()
8485
.id(ShapeId.fromParts(CodegenUtils.getSyntheticTypeNamespace(), opShapeId.getName(service) + suffix))
8586
.addTrait(Synthetic.builder().build())
87+
.addTrait(new BackfilledInputOutputTrait())
8688
.build();
8789
}
8890

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/EventStreamGenerator.java

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import software.amazon.smithy.utils.StringUtils;
3232

3333
public final class EventStreamGenerator {
34+
public static final String AMZ_CONTENT_TYPE = "application/vnd.amazon.eventstream";
35+
3436
private static final String EVENT_STREAM_FILE = "eventstream.go";
3537

3638
private final GoSettings settings;

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoDependency.java

+36
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,42 @@ public Symbol valueSymbol(String name) {
156156
return SymbolUtils.createValueSymbolBuilder(name, this).build();
157157
}
158158

159+
/**
160+
* Creates a Symbol for a `const` exported by this package.
161+
* @param name The name.
162+
* @return The symbol.
163+
*/
164+
public Symbol constSymbol(String name) {
165+
return SymbolUtils.createValueSymbolBuilder(name, this).build();
166+
}
167+
168+
/**
169+
* Creates a Symbol for a `func` exported by this package.
170+
* @param name The name.
171+
* @return The symbol.
172+
*/
173+
public Symbol func(String name) {
174+
return SymbolUtils.createValueSymbolBuilder(name, this).build();
175+
}
176+
177+
/**
178+
* Creates a Symbol for a `struct` exported by this package.
179+
* @param name The name.
180+
* @return The symbol.
181+
*/
182+
public Symbol struct(String name) {
183+
return SymbolUtils.createPointableSymbolBuilder(name, this).build();
184+
}
185+
186+
/**
187+
* Creates a Symbol for a `Value` exported by this package.
188+
* @param name The name.
189+
* @return The symbol.
190+
*/
191+
public Symbol interfaceSymbol(String name) {
192+
return SymbolUtils.createValueSymbolBuilder(name, this).build();
193+
}
194+
159195
/**
160196
* Creates a pointable Symbol for a name exported by this package.
161197
* @param name The name.

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoSettings.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,35 @@
1919
import java.util.Objects;
2020
import java.util.Optional;
2121
import java.util.Set;
22+
import software.amazon.smithy.aws.traits.protocols.AwsJson1_0Trait;
23+
import software.amazon.smithy.aws.traits.protocols.AwsJson1_1Trait;
24+
import software.amazon.smithy.aws.traits.protocols.AwsQueryTrait;
25+
import software.amazon.smithy.aws.traits.protocols.Ec2QueryTrait;
26+
import software.amazon.smithy.aws.traits.protocols.RestJson1Trait;
27+
import software.amazon.smithy.aws.traits.protocols.RestXmlTrait;
2228
import software.amazon.smithy.codegen.core.CodegenException;
2329
import software.amazon.smithy.model.Model;
2430
import software.amazon.smithy.model.knowledge.ServiceIndex;
2531
import software.amazon.smithy.model.node.ObjectNode;
2632
import software.amazon.smithy.model.shapes.ServiceShape;
2733
import software.amazon.smithy.model.shapes.ShapeId;
34+
import software.amazon.smithy.protocol.traits.Rpcv2CborTrait;
2835
import software.amazon.smithy.utils.SmithyInternalApi;
2936

3037
/**
3138
* Settings used by {@link GoCodegenPlugin}.
3239
*/
3340
@SmithyInternalApi
3441
public final class GoSettings {
42+
public static final Set<ShapeId> PROTOCOLS_BY_PRIORITY = Set.of(
43+
Rpcv2CborTrait.ID,
44+
AwsJson1_0Trait.ID,
45+
AwsJson1_1Trait.ID,
46+
RestJson1Trait.ID,
47+
RestXmlTrait.ID,
48+
AwsQueryTrait.ID,
49+
Ec2QueryTrait.ID
50+
);
3551

3652
private static final String SERVICE = "service";
3753
private static final String MODULE_NAME = "module";
@@ -247,7 +263,10 @@ public ShapeId resolveServiceProtocol(
247263

248264
Set<ShapeId> resolvedProtocols = serviceIndex.getProtocols(service).keySet();
249265

250-
return resolvedProtocols.stream()
266+
var byPriority = PROTOCOLS_BY_PRIORITY.stream()
267+
.filter(resolvedProtocols::contains)
268+
.toList();
269+
return byPriority.stream()
251270
.filter(supportedProtocolTraits::contains)
252271
.findFirst()
253272
.orElseThrow(() -> new UnresolvableProtocolException(String.format(

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/GoStdlibTypes.java

+14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
public final class GoStdlibTypes {
2525
private GoStdlibTypes() { }
2626

27+
public static final class Bytes {
28+
public static final Symbol NewReader = SmithyGoDependency.BYTES.valueSymbol("NewReader");
29+
}
30+
2731
public static final class Context {
2832
public static final Symbol Context = SmithyGoDependency.CONTEXT.valueSymbol("Context");
2933
public static final Symbol Background = SmithyGoDependency.CONTEXT.valueSymbol("Background");
@@ -55,13 +59,23 @@ public static final class Fmt {
5559
public static final Symbol Sprintf = SmithyGoDependency.FMT.valueSymbol("Sprintf");
5660
}
5761

62+
public static final class Io {
63+
public static final Symbol ReadAll = SmithyGoDependency.IO.valueSymbol("ReadAll");
64+
public static final Symbol Copy = SmithyGoDependency.IO.valueSymbol("Copy");
65+
66+
public static final class IoUtil {
67+
public static final Symbol Discard = SmithyGoDependency.IOUTIL.valueSymbol("Discard");
68+
}
69+
}
70+
5871
public static final class Net {
5972
public static final class Http {
6073
public static final Symbol Request = SmithyGoDependency.NET_HTTP.pointableSymbol("Request");
6174
public static final Symbol Response = SmithyGoDependency.NET_HTTP.pointableSymbol("Response");
6275
public static final Symbol Server = SmithyGoDependency.NET_HTTP.pointableSymbol("Server");
6376
public static final Symbol Handler = SmithyGoDependency.NET_HTTP.valueSymbol("Handler");
6477
public static final Symbol ResponseWriter = SmithyGoDependency.NET_HTTP.valueSymbol("ResponseWriter");
78+
public static final Symbol MethodPost = SmithyGoDependency.NET_HTTP.valueSymbol("MethodPost");
6579
}
6680
}
6781

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoDependency.java

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public final class SmithyGoDependency {
6060
public static final GoDependency SMITHY_HTTP_BINDING = smithy("encoding/httpbinding");
6161
public static final GoDependency SMITHY_JSON = smithy("encoding/json", "smithyjson");
6262
public static final GoDependency SMITHY_XML = smithy("encoding/xml", "smithyxml");
63+
public static final GoDependency SMITHY_CBOR = smithy("encoding/cbor", "smithycbor");
6364
public static final GoDependency SMITHY_IO = smithy("io", "smithyio");
6465
public static final GoDependency SMITHY_LOGGING = smithy("logging");
6566
public static final GoDependency SMITHY_PTR = smithy("ptr");
@@ -68,6 +69,7 @@ public final class SmithyGoDependency {
6869
public static final GoDependency SMITHY_WAITERS = smithy("waiter", "smithywaiter");
6970
public static final GoDependency SMITHY_DOCUMENT = smithy("document", "smithydocument");
7071
public static final GoDependency SMITHY_DOCUMENT_JSON = smithy("document/json", "smithydocumentjson");
72+
public static final GoDependency SMITHY_DOCUMENT_CBOR = smithy("document/cbor", "smithydocumentcbor");
7173
public static final GoDependency SMITHY_SYNC = smithy("sync", "smithysync");
7274
public static final GoDependency SMITHY_AUTH = smithy("auth", "smithyauth");
7375
public static final GoDependency SMITHY_AUTH_BEARER = smithy("auth/bearer");

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoTypes.java

+39-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static final class Smithy {
2828
public static final Symbol Properties = SmithyGoDependency.SMITHY.pointableSymbol("Properties");
2929
public static final Symbol OperationError = SmithyGoDependency.SMITHY.pointableSymbol("OperationError");
3030
public static final Symbol InvalidParamsError = SmithyGoDependency.SMITHY.pointableSymbol("InvalidParamsError");
31+
public static final Symbol SerializationError = SmithyGoDependency.SMITHY.pointableSymbol("SerializationError");
3132

3233
public static final class Document {
3334
public static final Symbol NoSerde = SmithyGoDependency.SMITHY_DOCUMENT.pointableSymbol("NoSerde");
@@ -39,15 +40,47 @@ public static final class Time {
3940
public static final Symbol FormatDateTime = SmithyGoDependency.SMITHY_TIME.valueSymbol("FormatDateTime");
4041
}
4142

43+
public static final class Rand {
44+
public static final Symbol NewUUID = SmithyGoDependency.SMITHY_RAND.valueSymbol("NewUUID");
45+
}
46+
4247
public static final class Encoding {
4348
public static final class Json {
4449
public static final Symbol NewEncoder = SmithyGoDependency.SMITHY_JSON.valueSymbol("NewEncoder");
4550
public static final Symbol Value = SmithyGoDependency.SMITHY_JSON.valueSymbol("Value");
4651
}
52+
53+
public static final class Cbor {
54+
public static final Symbol Encode = SmithyGoDependency.SMITHY_CBOR.valueSymbol("Encode");
55+
public static final Symbol Decode = SmithyGoDependency.SMITHY_CBOR.valueSymbol("Decode");
56+
public static final Symbol Value = SmithyGoDependency.SMITHY_CBOR.valueSymbol("Value");
57+
public static final Symbol Uint = SmithyGoDependency.SMITHY_CBOR.valueSymbol("Uint");
58+
public static final Symbol NegInt = SmithyGoDependency.SMITHY_CBOR.valueSymbol("NegInt");
59+
public static final Symbol Slice = SmithyGoDependency.SMITHY_CBOR.valueSymbol("Slice");
60+
public static final Symbol String = SmithyGoDependency.SMITHY_CBOR.valueSymbol("String");
61+
public static final Symbol List = SmithyGoDependency.SMITHY_CBOR.valueSymbol("List");
62+
public static final Symbol Map = SmithyGoDependency.SMITHY_CBOR.valueSymbol("Map");
63+
public static final Symbol Tag = SmithyGoDependency.SMITHY_CBOR.pointableSymbol("Tag");
64+
public static final Symbol Bool = SmithyGoDependency.SMITHY_CBOR.valueSymbol("Bool");
65+
public static final Symbol Nil = SmithyGoDependency.SMITHY_CBOR.pointableSymbol("Nil");
66+
public static final Symbol Undefined = SmithyGoDependency.SMITHY_CBOR.pointableSymbol("Undefined");
67+
public static final Symbol Float32 = SmithyGoDependency.SMITHY_CBOR.valueSymbol("Float32");
68+
public static final Symbol Float64 = SmithyGoDependency.SMITHY_CBOR.valueSymbol("Float64");
69+
public static final Symbol EncodeRaw = SmithyGoDependency.SMITHY_CBOR.valueSymbol("EncodeRaw");
70+
public static final Symbol AsInt8 = SmithyGoDependency.SMITHY_CBOR.valueSymbol("AsInt8");
71+
public static final Symbol AsInt16 = SmithyGoDependency.SMITHY_CBOR.valueSymbol("AsInt16");
72+
public static final Symbol AsInt32 = SmithyGoDependency.SMITHY_CBOR.valueSymbol("AsInt32");
73+
public static final Symbol AsInt64 = SmithyGoDependency.SMITHY_CBOR.valueSymbol("AsInt64");
74+
public static final Symbol AsFloat32 = SmithyGoDependency.SMITHY_CBOR.valueSymbol("AsFloat32");
75+
public static final Symbol AsFloat64 = SmithyGoDependency.SMITHY_CBOR.valueSymbol("AsFloat64");
76+
public static final Symbol AsTime = SmithyGoDependency.SMITHY_CBOR.valueSymbol("AsTime");
77+
}
4778
}
4879

49-
public static final class Rand {
50-
public static final Symbol NewUUID = SmithyGoDependency.SMITHY_RAND.valueSymbol("NewUUID");
80+
public static final class Document {
81+
public static final class Cbor {
82+
public static final Symbol NewEncoder = SmithyGoDependency.SMITHY_DOCUMENT_CBOR.valueSymbol("NewEncoder");
83+
}
5184
}
5285

5386
public static final class Ptr {
@@ -57,6 +90,9 @@ public static final class Ptr {
5790
public static final Symbol Int16 = SmithyGoDependency.SMITHY_PTR.valueSymbol("Int16");
5891
public static final Symbol Int32 = SmithyGoDependency.SMITHY_PTR.valueSymbol("Int32");
5992
public static final Symbol Int64 = SmithyGoDependency.SMITHY_PTR.valueSymbol("Int64");
93+
public static final Symbol Float32 = SmithyGoDependency.SMITHY_PTR.valueSymbol("Float32");
94+
public static final Symbol Float64 = SmithyGoDependency.SMITHY_PTR.valueSymbol("Float64");
95+
public static final Symbol Time = SmithyGoDependency.SMITHY_PTR.valueSymbol("Time");
6096
}
6197

6298
public static final class Middleware {
@@ -84,6 +120,7 @@ public static final class Middleware {
84120
public static final class Transport {
85121
public static final class Http {
86122
public static final Symbol Request = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.pointableSymbol("Request");
123+
public static final Symbol Response = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.pointableSymbol("Response");
87124
public static final Symbol NewStackRequest = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("NewStackRequest");
88125
public static final Symbol NewClientHandler = SmithyGoDependency.SMITHY_HTTP_TRANSPORT.valueSymbol("NewClientHandler");
89126

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SymbolUtils.java

+13
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,17 @@ public static Symbol getReference(Symbol symbol) {
210210
public static Symbol buildPackageSymbol(String name) {
211211
return Symbol.builder().name(name).build();
212212
}
213+
214+
public static Symbol buildSymbol(String name, String namespace) {
215+
return Symbol.builder()
216+
.name(name)
217+
.namespace(namespace, ".")
218+
.build();
219+
}
220+
221+
public static boolean isNilable(Symbol symbol) {
222+
return isPointable(symbol)
223+
|| symbol.getProperty(SymbolUtils.GO_SLICE).isPresent()
224+
|| symbol.getProperty(SymbolUtils.GO_MAP).isPresent();
225+
}
213226
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.smithy.go.codegen.integration;
17+
18+
import java.util.List;
19+
import software.amazon.smithy.go.codegen.protocol.rpc2.cbor.Rpc2CborProtocolGenerator;
20+
import software.amazon.smithy.utils.ListUtils;
21+
22+
public class DefaultProtocols implements GoIntegration {
23+
@Override
24+
public List<ProtocolGenerator> getProtocolGenerators() {
25+
return ListUtils.of(new Rpc2CborProtocolGenerator());
26+
}
27+
}

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/HttpProtocolUnitTestRequestGenerator.java

+5
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ protected void generateTestCaseValues(GoWriter writer, HttpRequestTestCase testC
190190
"return smithytesting.CompareURLFormReaderBytes(actual, []byte(`%s`))",
191191
body);
192192
break;
193+
case "application/cbor":
194+
compareFunc = String.format(
195+
"return smithytesting.CompareCBOR(actual, `%s`)",
196+
body);
197+
break;
193198
default:
194199
compareFunc = String.format(
195200
"return smithytesting.CompareReaderBytes(actual, []byte(`%s`))",

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/HttpProtocolUnitTestResponseErrorGenerator.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,20 @@ protected void generateTestCaseValues(GoWriter writer, HttpResponseTestCase test
107107
writeStructField(writer, "BodyMediaType", "$S", mediaType);
108108
});
109109
testCase.getBody().ifPresent(body -> {
110-
writeStructField(writer, "Body", "[]byte(`$L`)", body);
110+
var mediaType = testCase.getBodyMediaType().orElse("");
111+
if (mediaType.equalsIgnoreCase("application/cbor")) {
112+
writeStructField(writer, "Body", """
113+
func() []byte {
114+
p, err := $T.DecodeString(`$L`)
115+
if err != nil {
116+
panic(err)
117+
}
118+
119+
return p
120+
}()""", SmithyGoDependency.BASE64.func("StdEncoding"), body);
121+
} else {
122+
writeStructField(writer, "Body", "[]byte(`$L`)", body);
123+
}
111124
});
112125

113126
writeStructField(writer, "ExpectError", errorShape, testCase.getParams());

codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/HttpProtocolUnitTestResponseGenerator.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,20 @@ protected void generateTestCaseValues(GoWriter writer, HttpResponseTestCase test
9797
writeStructField(writer, "BodyMediaType", "$S", mediaType);
9898
});
9999
testCase.getBody().ifPresent(body -> {
100-
writeStructField(writer, "Body", "[]byte(`$L`)", body);
100+
var mediaType = testCase.getBodyMediaType().orElse("");
101+
if (mediaType.equalsIgnoreCase("application/cbor")) {
102+
writeStructField(writer, "Body", """
103+
func() []byte {
104+
p, err := $T.DecodeString(`$L`)
105+
if err != nil {
106+
panic(err)
107+
}
108+
109+
return p
110+
}()""", SmithyGoDependency.BASE64.func("StdEncoding"), body);
111+
} else {
112+
writeStructField(writer, "Body", "[]byte(`$L`)", body);
113+
}
101114
});
102115

103116
writeStructField(writer, "ExpectResult", outputShape, testCase.getParams());

0 commit comments

Comments
 (0)