Skip to content

Commit 9f392c7

Browse files
committed
subordinate ProtocolPriorityConfig to TypeScriptSettings
1 parent 552e4d6 commit 9f392c7

File tree

4 files changed

+273
-133
lines changed

4 files changed

+273
-133
lines changed

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,21 @@ By default, the Smithy TypeScript code generators provide the code generation fr
185185
186186
[`TypeScriptSettings`](smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptSettings.java) contains all of the settings enabled from `smithy-build.json` and helper methods and types. The up-to-date list of top-level properties enabled for `typescript-client-codegen` can be found in `TypeScriptSettings.ArtifactType.CLIENT`.
187187

188-
|Setting|Required|Description|
189-
|---|---|---|
190-
|`package`|Yes|Name of the package in `package.json`.|
191-
|`packageVersion`|Yes|Version of the package in `package.json`.|
192-
|`packageDescription`|No|Description of the package in `package.json`. The default value is `${package} client`|
193-
|`packageJson`|No|Custom `package.json` properties that will be merged with the base `package.json`. The default value is an empty object.|
194-
|`packageManager`|No|Configured package manager for the package. The default value is `yarn`.|
195-
|`service`|No|The Shape ID of the service to generate a client for. If not provided, the code generator will attempt to infer the service Shape ID. If there is exactly 1 service found in the model, then the service is used as the inferred Shape ID. If no services are found, then code generation fails. If more than 1 service is found, then code generation fails.|
196-
|`protocol`|No|The Shape ID of the protocol used to generate serialization and deserialization. If not provided, the code generator will attempt to resolve the highest priority service protocol supported in code generation (registered through `TypeScriptIntegration`). If no protocols are found, code generation will use serialization and deserialization error stubs.|
197-
|`private`|No|Whether the package is `private` in `package.json`. The default value is `false`.|
198-
|`requiredMemberMode`|No|**NOT RECOMMENDED DUE TO BACKWARD COMPATIBILITY CONCERNS.** Sets whether members marked with the `@required` trait are allowed to be `undefined`. See more details on the risks in `TypeScriptSettings.RequiredMemberMode`. The default value is `nullable`.|
199-
|`createDefaultReadme`|No|Whether to generate a default `README.md` for the package. The default value is `false`.|
200-
|`useLegacyAuth`|No|**NOT RECOMMENDED, AVAILABLE ONLY FOR BACKWARD COMPATIBILITY CONCERNS.** Flag that enables using legacy auth. When in doubt, use the default identity and auth behavior (not configuring `useLegacyAuth`) as the golden path.|
188+
| Setting |Required| Description |
189+
|---------------------------|---|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
190+
| `package` |Yes| Name of the package in `package.json`. |
191+
| `packageVersion` |Yes| Version of the package in `package.json`. |
192+
| `packageDescription` |No| Description of the package in `package.json`. The default value is `${package} client` |
193+
| `packageJson` |No| Custom `package.json` properties that will be merged with the base `package.json`. The default value is an empty object. |
194+
| `packageManager` |No| Configured package manager for the package. The default value is `yarn`. |
195+
| `service` |No| The Shape ID of the service to generate a client for. If not provided, the code generator will attempt to infer the service Shape ID. If there is exactly 1 service found in the model, then the service is used as the inferred Shape ID. If no services are found, then code generation fails. If more than 1 service is found, then code generation fails. |
196+
| `protocol` |No| The Shape ID of the protocol used to generate serialization and deserialization. If not provided, the code generator will attempt to resolve the highest priority service protocol supported in code generation (registered through `TypeScriptIntegration`). If no protocols are found, code generation will use serialization and deserialization error stubs. |
197+
| `private` |No| Whether the package is `private` in `package.json`. The default value is `false`. |
198+
| `requiredMemberMode` |No| **NOT RECOMMENDED DUE TO BACKWARD COMPATIBILITY CONCERNS.** Sets whether members marked with the `@required` trait are allowed to be `undefined`. See more details on the risks in `TypeScriptSettings.RequiredMemberMode`. The default value is `nullable`. |
199+
| `createDefaultReadme` |No| Whether to generate a default `README.md` for the package. The default value is `false`. |
200+
| `useLegacyAuth` |No| **NOT RECOMMENDED, AVAILABLE ONLY FOR BACKWARD COMPATIBILITY CONCERNS.** Flag that enables using legacy auth. When in doubt, use the default identity and auth behavior (not configuring `useLegacyAuth`) as the golden path. |
201+
| `serviceProtocolPriority` |No| Map of service `ShapeId` strings to lists of protocol `ShapeId` strings. Used to override protocol selection behavior. |
202+
| `defaultProtocolPriority` |No| List of protocol `ShapeId` strings. Lower precedence than `serviceProtocolPriority` but applies to all services. |
201203

202204
#### `typescript-client-codegen` plugin artifacts
203205

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222
import java.util.Objects;
23+
import java.util.Optional;
2324
import java.util.Set;
2425
import java.util.function.BiFunction;
2526
import java.util.logging.Logger;
@@ -28,6 +29,7 @@
2829
import software.amazon.smithy.codegen.core.SymbolProvider;
2930
import software.amazon.smithy.model.Model;
3031
import software.amazon.smithy.model.knowledge.ServiceIndex;
32+
import software.amazon.smithy.model.node.ArrayNode;
3133
import software.amazon.smithy.model.node.BooleanNode;
3234
import software.amazon.smithy.model.node.Node;
3335
import software.amazon.smithy.model.node.ObjectNode;
@@ -37,7 +39,7 @@
3739
import software.amazon.smithy.model.shapes.ShapeId;
3840
import software.amazon.smithy.model.traits.DefaultTrait;
3941
import software.amazon.smithy.model.traits.RequiredTrait;
40-
import software.amazon.smithy.typescript.codegen.protocols.ProtocolPriority;
42+
import software.amazon.smithy.typescript.codegen.protocols.ProtocolPriorityConfig;
4143
import software.amazon.smithy.utils.SmithyUnstableApi;
4244

4345
/**
@@ -61,6 +63,8 @@ public final class TypeScriptSettings {
6163
private static final String CREATE_DEFAULT_README = "createDefaultReadme";
6264
private static final String USE_LEGACY_AUTH = "useLegacyAuth";
6365
private static final String GENERATE_TYPEDOC = "generateTypeDoc";
66+
private static final String SERVICE_PROTOCOL_PRIORITY = "serviceProtocolPriority";
67+
private static final String DEFAULT_PROTOCOL_PRIORITY = "defaultProtocolPriority";
6468

6569
private String packageName;
6670
private String packageDescription = "";
@@ -79,6 +83,7 @@ public final class TypeScriptSettings {
7983
private boolean createDefaultReadme = false;
8084
private boolean useLegacyAuth = false;
8185
private boolean generateTypeDoc = false;
86+
private final ProtocolPriorityConfig protocolPriorityConfig = new ProtocolPriorityConfig();
8287

8388
@Deprecated
8489
public static TypeScriptSettings from(Model model, ObjectNode config) {
@@ -130,6 +135,9 @@ public static TypeScriptSettings from(Model model, ObjectNode config, ArtifactTy
130135
.orElse(RequiredMemberMode.NULLABLE));
131136

132137
settings.setPluginSettings(config);
138+
139+
settings.readProtocolPriorityConfiguration(config);
140+
133141
return settings;
134142
}
135143

@@ -452,7 +460,7 @@ public ShapeId resolveServiceProtocol(Model model, ServiceShape service, Set<Sha
452460
+ "generate in smithy-build.json to generate this service.");
453461
}
454462

455-
List<ShapeId> protocolPriority = ProtocolPriority.getProtocolPriority(service.toShapeId());
463+
List<ShapeId> protocolPriority = this.protocolPriorityConfig.getProtocolPriority(service.toShapeId());
456464
List<ShapeId> protocolPriorityList = protocolPriority != null && !protocolPriority.isEmpty()
457465
? protocolPriority
458466
: new ArrayList<>(supportedProtocols);
@@ -489,6 +497,13 @@ public String getDefaultSigningName() {
489497
return defaultSigningName;
490498
}
491499

500+
/**
501+
* @return config container for service and/or default protocol selection priority overrides.
502+
*/
503+
public ProtocolPriorityConfig getProtocolPriority() {
504+
return protocolPriorityConfig;
505+
}
506+
492507
/**
493508
* An enum indicating the type of artifact the code generator will produce.
494509
*/
@@ -593,4 +608,46 @@ public static PackageManager fromString(String s) {
593608
throw new CodegenException(String.format("Unsupported package manager: %s", s));
594609
}
595610
}
611+
612+
/**
613+
* Reads serviceProtocolPriority and defaultProtocolPriority configuration fields.
614+
* {
615+
* serviceProtocolPriority: {
616+
* "namespace#Service": ["namespace#Protocol1", "namespace#Protocol2"]
617+
* },
618+
* defaultProtocolPriority: ["namespace#Protocol"]
619+
* }
620+
*/
621+
private void readProtocolPriorityConfiguration(ObjectNode config) {
622+
try {
623+
Optional<ObjectNode> protocolPriorityNode = config.getObjectMember(SERVICE_PROTOCOL_PRIORITY);
624+
if (protocolPriorityNode.isPresent()) {
625+
ObjectNode objectNode = protocolPriorityNode.get();
626+
objectNode.getMembers().forEach((StringNode k, Node v) -> {
627+
ShapeId serviceShapeId = ShapeId.from(k.getValue());
628+
List<ShapeId> protocolList = v.asArrayNode().get().getElementsAs(
629+
e -> ShapeId.from(e.asStringNode().get().getValue())
630+
);
631+
protocolPriorityConfig.setProtocolPriority(
632+
serviceShapeId,
633+
protocolList
634+
);
635+
});
636+
}
637+
Optional<ArrayNode> defaultProtocolPriorityOpt = config.getArrayMember(DEFAULT_PROTOCOL_PRIORITY);
638+
if (defaultProtocolPriorityOpt.isPresent()) {
639+
ArrayNode defaultProtocolPriorityStringArr = defaultProtocolPriorityOpt.get();
640+
protocolPriorityConfig.setCustomDefaultProtocolPriority(
641+
defaultProtocolPriorityStringArr.getElementsAs(
642+
e -> ShapeId.from(e.asStringNode().get().getValue())
643+
)
644+
);
645+
}
646+
} catch (Exception e) {
647+
throw new IllegalArgumentException(
648+
"Error while parsing serviceProtocolPriority or defaultProtocolPriority configuration fields",
649+
e
650+
);
651+
}
652+
}
596653
}
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,31 @@
1515
/**
1616
* Allows customization of protocol selection for specific services or a global default ordering.
1717
*/
18-
public final class ProtocolPriority {
19-
private static final Map<ShapeId, List<ShapeId>> SERVICE_PROTOCOL_PRIORITY_CUSTOMIZATIONS = new HashMap<>();
20-
private static List<ShapeId> customDefaultPriority = null;
21-
22-
private ProtocolPriority() {}
18+
public final class ProtocolPriorityConfig {
19+
private final Map<ShapeId, List<ShapeId>> serviceProtocolPriorityCustomizations = new HashMap<>();
20+
private List<ShapeId> customDefaultPriority = null;
2321

2422
/**
2523
* @param serviceShapeId - service scope.
2624
* @param protocolPriorityOrder - priority order of protocols.
2725
*/
28-
public static void setProtocolPriority(ShapeId serviceShapeId, List<ShapeId> protocolPriorityOrder) {
29-
SERVICE_PROTOCOL_PRIORITY_CUSTOMIZATIONS.put(serviceShapeId, protocolPriorityOrder);
26+
public void setProtocolPriority(ShapeId serviceShapeId, List<ShapeId> protocolPriorityOrder) {
27+
serviceProtocolPriorityCustomizations.put(serviceShapeId, protocolPriorityOrder);
3028
}
3129

3230
/**
3331
* @param defaultProtocolPriorityOrder - use for all services that don't have a more specific priority order.
3432
*/
35-
public static void setCustomDefaultProtocolPriority(List<ShapeId> defaultProtocolPriorityOrder) {
33+
public void setCustomDefaultProtocolPriority(List<ShapeId> defaultProtocolPriorityOrder) {
3634
customDefaultPriority = new ArrayList<>(defaultProtocolPriorityOrder);
3735
}
3836

3937
/**
4038
* @param serviceShapeId - service scope.
4139
* @return priority order of protocols or null if no override exists.
4240
*/
43-
public static List<ShapeId> getProtocolPriority(ShapeId serviceShapeId) {
44-
return SERVICE_PROTOCOL_PRIORITY_CUSTOMIZATIONS.getOrDefault(
41+
public List<ShapeId> getProtocolPriority(ShapeId serviceShapeId) {
42+
return serviceProtocolPriorityCustomizations.getOrDefault(
4543
serviceShapeId,
4644
customDefaultPriority != null ? new ArrayList<>(customDefaultPriority) : null
4745
);
@@ -50,14 +48,14 @@ public static List<ShapeId> getProtocolPriority(ShapeId serviceShapeId) {
5048
/**
5149
* @param serviceShapeId - to unset.
5250
*/
53-
public static void deleteProtocolPriority(ShapeId serviceShapeId) {
54-
SERVICE_PROTOCOL_PRIORITY_CUSTOMIZATIONS.remove(serviceShapeId);
51+
public void deleteProtocolPriority(ShapeId serviceShapeId) {
52+
serviceProtocolPriorityCustomizations.remove(serviceShapeId);
5553
}
5654

5755
/**
5856
* Unset the custom default priority order.
5957
*/
60-
public static void deleteCustomDefaultProtocolPriority() {
58+
public void deleteCustomDefaultProtocolPriority() {
6159
customDefaultPriority = null;
6260
}
6361
}

0 commit comments

Comments
 (0)