Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions private/my-local-model-schema/src/models/models_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ export interface GetNumbersResponse {
/**
* @public
*/
export interface Unit {
}
export interface Unit {}

/**
* @public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
MysteryThrottlingError$,
RetryableError,
RetryableError$,
TradeEvents$,
TradeEventStream$,
TradeEventStreamCommand,
TradeEventStreamRequest$,
TradeEventStreamResponse$,
TradeEvents$,
XYZService,
XYZServiceClient,
XYZServiceServiceException,
Expand Down
3 changes: 1 addition & 2 deletions private/my-local-model/src/models/models_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ export interface GetNumbersResponse {
/**
* @public
*/
export interface Unit {
}
export interface Unit {}

/**
* @public
Expand Down
2 changes: 1 addition & 1 deletion private/my-local-model/src/protocols/Rpcv2cbor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import type {
Endpoint as __Endpoint,
EventStreamSerdeContext as __EventStreamSerdeContext,
HeaderBag as __HeaderBag,
MessageHeaders as __MessageHeaders,
Message as __Message,
MessageHeaders as __MessageHeaders,
ResponseMetadata as __ResponseMetadata,
SerdeContext as __SerdeContext,
} from "@smithy/types";
Expand Down
3 changes: 1 addition & 2 deletions private/smithy-rpcv2-cbor-schema/src/models/models_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ export interface GreetingStruct {
/**
* @public
*/
export interface EmptyStructure {
}
export interface EmptyStructure {}

/**
* @public
Expand Down
3 changes: 1 addition & 2 deletions private/smithy-rpcv2-cbor/src/models/models_0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ export interface GreetingStruct {
/**
* @public
*/
export interface EmptyStructure {
}
export interface EmptyStructure {}

/**
* @public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -493,7 +494,7 @@ private void generateEndpointParameterInstructionProvider() {
);
});
}
writer.write("})").dedent();
writer.dedent().write("})");
}

private void generateCommandMiddlewareResolver(String configType) {
Expand Down Expand Up @@ -710,21 +711,24 @@ private void addCommandSpecificPlugins() {
}
writer.pushState();
writer.putContext(symbolMap);
writer.openBlock("$pluginFn:T(config", "),", () -> {
List<String> additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap);
Map<String, CommandWriterConsumer> clientAddParamsWriterConsumers =
plugin.getOperationAddParamsWriterConsumers();
if (additionalParameters.isEmpty() && clientAddParamsWriterConsumers.isEmpty()) {
return;
}
writer.openBlock(", { ", " }", () -> {
// caution: using String.join instead of templating
// because additionalParameters may contain Smithy syntax.

List<String> additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap);
Map<String, CommandWriterConsumer> clientAddParamsWriterConsumers =
plugin.getOperationAddParamsWriterConsumers();
boolean hasAddParams =
!additionalParameters.isEmpty() || !clientAddParamsWriterConsumers.isEmpty();

if (!hasAddParams) {
writer.write("$pluginFn:T(config),");
} else {
writer.openBlock("$pluginFn:T(config, {", "}),", () -> {
if (!additionalParameters.isEmpty()) {
writer.writeInline(String.join(", ", additionalParameters) + ", ");
// caution: using String.join instead of templating
// because additionalParameters may contain Smithy syntax.
writer.write(String.join(",\n", additionalParameters) + ",");
}
clientAddParamsWriterConsumers.forEach((key, consumer) -> {
writer.writeInline(
writer.write(
"$L: $C,",
key,
(Consumer<TypeScriptWriter>) (w -> {
Expand All @@ -743,7 +747,7 @@ private void addCommandSpecificPlugins() {
);
});
});
});
}
writer.popState();
});
}
Expand Down Expand Up @@ -801,7 +805,13 @@ static void writeIndex(
TypeScriptWriter writer = new TypeScriptWriter("");

TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
Set<OperationShape> containedOperations = new TreeSet<>(
Comparator.comparing(opShape -> symbolProvider.toSymbol(opShape).getName())
);
containedOperations.addAll(
topDownIndex.getContainedOperations(service)
);

if (containedOperations.isEmpty()) {
writer.write("export {};");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,19 @@ private void generateNamedEnum() {
.sorted(Comparator.comparing(e -> e.getName().get()))
.forEach(this::writeNamedEnumConstant);
});

String inline = """
export type %s = (typeof %s)[keyof typeof %s];""".formatted(
symbol.getName(),
symbol.getName(),
symbol.getName()
);
String breakline = inline.replace("= ", "=\n ");

writer
.writeDocs("@public")
.write(
"export type $L = (typeof $L)[keyof typeof $L];",
symbol.getName(),
symbol.getName(),
symbol.getName()
inline.length() <= TypeScriptWriter.LINE_WIDTH ? inline : breakline
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
Expand All @@ -24,6 +25,42 @@
@SmithyInternalApi
final class ImportDeclarations implements ImportContainer {

/**
* External package first, relative last, otherwise by case-insensitive alphabetical ordering.
*/
private static final Comparator<String> MODULES_ORDERING = (a, b) -> {
if (a.startsWith(".") && !b.startsWith(".")) {
return 1;
}
if (!a.startsWith(".") && b.startsWith(".")) {
return -1;
}
if (a.equalsIgnoreCase(b)) {
return a.compareTo(b);
}
return a.toLowerCase().compareTo(b.toLowerCase());
};

/**
* Type imports first, otherwise by symbol name, case-insensitively, ignoring alias.
*/
private static final Comparator<String> IMPORTS_ORDERING = (a, b) -> {
boolean aType = a.startsWith("type ");
boolean bType = b.startsWith("type ");
if (aType && !bType) {
return -1;
}
if (!aType && bType) {
return 1;
}
String normalA = a.replaceAll("(^type )|( as (.*?)$)", "").toLowerCase();
String normalB = b.replaceAll("(^type )|( as (.*?)$)", "").toLowerCase();
if (normalA.equals(normalB)) {
return a.compareTo(b);
}
return normalA.compareTo(normalB);
};

private final String moduleNameString;
private final String relativize;
private final Map<String, Pair<String, Ignore>> defaultImports = new TreeMap<>();
Expand Down Expand Up @@ -136,18 +173,7 @@ private static void createImports(
Map<String, Map<String, String>> namedTypeImports,
StringBuilder buffer
) {
TreeSet<String> mergedModuleKeys = new TreeSet<>((a, b) -> {
if (a.startsWith(".") && !b.startsWith(".")) {
return 1;
}
if (!a.startsWith(".") && b.startsWith(".")) {
return -1;
}
if (a.equalsIgnoreCase(b)) {
return a.compareTo(b);
}
return a.toLowerCase().compareTo(b.toLowerCase());
});
TreeSet<String> mergedModuleKeys = new TreeSet<>(MODULES_ORDERING);
mergedModuleKeys.addAll(namedImports.keySet());
mergedModuleKeys.addAll(namedTypeImports.keySet());

Expand All @@ -170,22 +196,7 @@ private static void createImports(
mergedSymbolKeys.addAll(moduleImports.keySet());
mergedSymbolKeys.addAll(typeImports.keySet());

Set<String> imports = new TreeSet<>((a, b) -> {
boolean aType = a.startsWith("type ");
boolean bType = b.startsWith("type ");
if (aType && !bType) {
return -1;
}
if (!aType && bType) {
return 1;
}
String normalA = a.replaceAll("(type )|( as (.*?))", "");
String normalB = b.replaceAll("(type )|( as (.*?))", "");
if (normalA.equals(normalB)) {
return a.compareTo(b);
}
return normalA.compareTo(normalB);
});
Set<String> imports = new TreeSet<>(IMPORTS_ORDERING);

for (String alias : mergedSymbolKeys) {
String runtimeSymbol = moduleImports.get(alias);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ private static String getModulePath(String fileLocation) {

static void writeIndex(Model model, ServiceShape service, FileManifest fileManifest) {
TypeScriptWriter writer = new TypeScriptWriter("");
writer.write("export * from \"./$L\"", getModulePath(PAGINATION_INTERFACE_FILE));
writer.write("export * from \"./$L\";", getModulePath(PAGINATION_INTERFACE_FILE));

TopDownIndex topDownIndex = TopDownIndex.of(model);
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
for (OperationShape operation : containedOperations) {
if (operation.hasTrait(PaginatedTrait.ID)) {
String outputFilepath = PaginationGenerator.getOutputFilelocation(operation);
writer.write("export * from \"./$L\"", getModulePath(outputFilepath));
writer.write("export * from \"./$L\";", getModulePath(outputFilepath));
}
}

Expand Down Expand Up @@ -190,14 +190,11 @@ private void writePager() {
config: ${aggClient:L}PaginationConfiguration,
input: ${inputType:L},
...rest: any[]
) => Paginator<${outputType:L}> =
createPaginator<${paginationType:L}, ${inputType:L}, ${outputType:L}>(
${serviceTypeName:L},
${operationName:L},
${inputToken:S},
${outputToken:S},
${pageSizeMember:S}
);
) => Paginator<${outputType:L}> = createPaginator<
${paginationType:L},
${inputType:L},
${outputType:L}
>(${serviceTypeName:L}, ${operationName:L}, ${inputToken:S}, ${outputToken:S}, ${pageSizeMember:S});
"""
)
.popState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,26 @@ private void renderNonErrorStructure() {
.map(SymbolReference::getAlias)
.collect(Collectors.joining(", "));

if (extendsFrom.isEmpty()) {
writer.openBlock("export interface $L {", symbol.getName());
} else {
writer.openBlock("export interface $L extends $L {", symbol.getName(), extendsFrom);
}

StructuredMemberWriter config = new StructuredMemberWriter(
model,
symbolProvider,
shape.getAllMembers().values(),
this.requiredMemberMode,
sensitiveDataFinder
);
config.writeMembers(writer, shape);
writer.closeBlock("}");

if (config.members.isEmpty()) {
writer.write("export interface $L {}", symbol.getName());
} else {
if (extendsFrom.isEmpty()) {
writer.openBlock("export interface $L {", symbol.getName());
} else {
writer.openBlock("export interface $L extends $L {", symbol.getName(), extendsFrom);
}
config.writeMembers(writer, shape);
writer.closeBlock("}");
}

writer.write("");
renderStructureNamespace(config, includeValidation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,29 @@ public TypeScriptWriter openCollapsibleBlock(
return openCollapsibleBlock(open, close, condition, new Object[] {arg1}, runnable);
}

public TypeScriptWriter openCollapsibleBlock(
String open,
String close,
boolean condition,
Object arg1,
Object arg2,
Runnable runnable
) {
return openCollapsibleBlock(open, close, condition, new Object[] {arg1, arg2}, runnable);
}

public TypeScriptWriter openCollapsibleBlock(
String open,
String close,
boolean condition,
Object arg1,
Object arg2,
Object arg3,
Runnable runnable
) {
return openCollapsibleBlock(open, close, condition, new Object[] {arg1, arg2, arg3}, runnable);
}

/**
* Modifies and writes shape documentation comments if docs are present.
*
Expand Down
Loading