16
16
package software .amazon .smithy .typescript .codegen ;
17
17
18
18
import java .util .ArrayList ;
19
+ import java .util .Collection ;
20
+ import java .util .HashMap ;
19
21
import java .util .List ;
20
22
import java .util .Map ;
21
23
import java .util .Objects ;
22
- import java .util .Optional ;
23
24
import java .util .ServiceLoader ;
24
25
import java .util .Set ;
25
26
import java .util .TreeSet ;
26
- import java .util .function .Function ;
27
27
import java .util .logging .Logger ;
28
- import java .util .stream .Collectors ;
29
28
import software .amazon .smithy .build .FileManifest ;
30
29
import software .amazon .smithy .build .PluginContext ;
31
30
import software .amazon .smithy .codegen .core .Symbol ;
43
42
import software .amazon .smithy .model .shapes .StructureShape ;
44
43
import software .amazon .smithy .model .shapes .UnionShape ;
45
44
import software .amazon .smithy .model .traits .EnumTrait ;
46
- import software .amazon .smithy .model .traits .ProtocolsTrait ;
47
45
import software .amazon .smithy .typescript .codegen .integration .ProtocolGenerator ;
48
46
import software .amazon .smithy .typescript .codegen .integration .RuntimeClientPlugin ;
49
47
import software .amazon .smithy .typescript .codegen .integration .TypeScriptIntegration ;
@@ -70,6 +68,7 @@ class CodegenVisitor extends ShapeVisitor.Default<Void> {
70
68
private final TypeScriptDelegator writers ;
71
69
private final List <TypeScriptIntegration > integrations = new ArrayList <>();
72
70
private final List <RuntimeClientPlugin > runtimePlugins = new ArrayList <>();
71
+ private final ProtocolGenerator protocolGenerator ;
73
72
private final ApplicationProtocol applicationProtocol ;
74
73
75
74
CodegenVisitor (PluginContext context ) {
@@ -100,8 +99,37 @@ class CodegenVisitor extends ShapeVisitor.Default<Void> {
100
99
}
101
100
symbolProvider = SymbolProvider .cache (resolvedProvider );
102
101
102
+ // Resolve the nullable protocol generator and application protocol.
103
+ protocolGenerator = resolveProtocolGenerator (integrations , service , settings );
104
+ applicationProtocol = protocolGenerator == null
105
+ ? ApplicationProtocol .createDefaultHttpApplicationProtocol ()
106
+ : protocolGenerator .getApplicationProtocol ();
107
+
103
108
writers = new TypeScriptDelegator (settings , model , fileManifest , symbolProvider , integrations );
104
- applicationProtocol = ApplicationProtocol .resolve (settings , service , integrations );
109
+ }
110
+
111
+ private static ProtocolGenerator resolveProtocolGenerator (
112
+ Collection <TypeScriptIntegration > integrations ,
113
+ ServiceShape service ,
114
+ TypeScriptSettings settings
115
+ ) {
116
+ // Collect all of the supported protocol generators.
117
+ Map <String , ProtocolGenerator > generators = new HashMap <>();
118
+ for (TypeScriptIntegration integration : integrations ) {
119
+ for (ProtocolGenerator generator : integration .getProtocolGenerators ()) {
120
+ generators .put (generator .getName (), generator );
121
+ }
122
+ }
123
+
124
+ String protocolName ;
125
+ try {
126
+ protocolName = settings .resolveServiceProtocol (service , generators .keySet ());
127
+ } catch (UnresolvableProtocolException e ) {
128
+ LOGGER .warning ("Unable to find a protocol generator for " + service .getId () + ": " + e .getMessage ());
129
+ protocolName = null ;
130
+ }
131
+
132
+ return protocolName != null ? generators .get (protocolName ) : null ;
105
133
}
106
134
107
135
void execute () {
@@ -119,12 +147,8 @@ void execute() {
119
147
// Generate the client Node and Browser configuration files. These
120
148
// files are switched between in package.json based on the targeted
121
149
// environment.
122
- String defaultProtocolName = getDefaultGenerator ().map (ProtocolGenerator ::getName ).orElse (null );
123
- LOGGER .fine ("Resolved the default protocol of the client to " + defaultProtocolName );
124
-
125
- // Generate each runtime config file for targeted platforms.
126
150
RuntimeConfigGenerator configGenerator = new RuntimeConfigGenerator (
127
- settings , model , symbolProvider , defaultProtocolName , writers , integrations );
151
+ settings , model , symbolProvider , writers , integrations );
128
152
for (LanguageTarget target : LanguageTarget .values ()) {
129
153
LOGGER .fine ("Generating " + target + " runtime configuration" );
130
154
configGenerator .generate (target );
@@ -151,19 +175,6 @@ void execute() {
151
175
settings , fileManifest , SymbolDependency .gatherDependencies (dependencies .stream ()));
152
176
}
153
177
154
- // Finds the first listed protocol from the service that has a
155
- // discovered protocol generator that matches the name.
156
- private Optional <ProtocolGenerator > getDefaultGenerator () {
157
- List <String > protocols = settings .resolveServiceProtocols (service );
158
- Map <String , ProtocolGenerator > generators = integrations .stream ()
159
- .flatMap (integration -> integration .getProtocolGenerators ().stream ())
160
- .collect (Collectors .toMap (ProtocolGenerator ::getName , Function .identity ()));
161
- return protocols .stream ()
162
- .filter (generators ::containsKey )
163
- .map (generators ::get )
164
- .findFirst ();
165
- }
166
-
167
178
@ Override
168
179
protected Void getDefault (Shape shape ) {
169
180
return null ;
@@ -249,35 +260,27 @@ public Void serviceShape(ServiceShape shape) {
249
260
for (OperationShape operation : topDownIndex .getContainedOperations (service )) {
250
261
writers .useShapeWriter (operation , commandWriter -> new CommandGenerator (
251
262
settings , model , operation , symbolProvider , commandWriter ,
252
- runtimePlugins , applicationProtocol ).run ());
263
+ runtimePlugins , protocolGenerator , applicationProtocol ).run ());
253
264
}
254
265
255
- // Generate each protocol.
256
- shape .getTrait (ProtocolsTrait .class ).ifPresent (protocolsTrait -> {
257
- LOGGER .info ("Looking for protocol generators for protocols: " + protocolsTrait .getProtocolNames ());
258
- for (TypeScriptIntegration integration : integrations ) {
259
- for (ProtocolGenerator generator : integration .getProtocolGenerators ()) {
260
- if (protocolsTrait .hasProtocol (generator .getName ())) {
261
- LOGGER .info ("Generating serde for protocol " + generator .getName () + " on " + shape .getId ());
262
- String fileRoot = "protocols/" + ProtocolGenerator .getSanitizedName (generator .getName ());
263
- String namespace = "./" + fileRoot ;
264
- TypeScriptWriter writer = new TypeScriptWriter (namespace );
265
- ProtocolGenerator .GenerationContext context = new ProtocolGenerator .GenerationContext ();
266
- context .setProtocolName (generator .getName ());
267
- context .setIntegrations (integrations );
268
- context .setModel (model );
269
- context .setService (shape );
270
- context .setSettings (settings );
271
- context .setSymbolProvider (symbolProvider );
272
- context .setWriter (writer );
273
- generator .generateRequestSerializers (context );
274
- generator .generateResponseDeserializers (context );
275
- generator .generateSharedComponents (context );
276
- fileManifest .writeFile (fileRoot + ".ts" , writer .toString ());
277
- }
278
- }
279
- }
280
- });
266
+ if (protocolGenerator != null ) {
267
+ LOGGER .info ("Generating serde for protocol " + protocolGenerator .getName () + " on " + shape .getId ());
268
+ String fileRoot = "protocols/" + ProtocolGenerator .getSanitizedName (protocolGenerator .getName ());
269
+ String namespace = "./" + fileRoot ;
270
+ TypeScriptWriter writer = new TypeScriptWriter (namespace );
271
+ ProtocolGenerator .GenerationContext context = new ProtocolGenerator .GenerationContext ();
272
+ context .setProtocolName (protocolGenerator .getName ());
273
+ context .setIntegrations (integrations );
274
+ context .setModel (model );
275
+ context .setService (shape );
276
+ context .setSettings (settings );
277
+ context .setSymbolProvider (symbolProvider );
278
+ context .setWriter (writer );
279
+ protocolGenerator .generateRequestSerializers (context );
280
+ protocolGenerator .generateResponseDeserializers (context );
281
+ protocolGenerator .generateSharedComponents (context );
282
+ fileManifest .writeFile (fileRoot + ".ts" , writer .toString ());
283
+ }
281
284
282
285
return null ;
283
286
}
0 commit comments