Skip to content

Commit a0318e2

Browse files
authored
chore(codegen): apply schema naming convention to only exported schema objects (#1813)
* use $ suffix
1 parent c7b3402 commit a0318e2

File tree

5 files changed

+77
-75
lines changed

5 files changed

+77
-75
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ test-protocols:
2626
(cd ./private/smithy-rpcv2-cbor && npx vitest run --globals && yarn test:index)
2727
(cd ./private/smithy-rpcv2-cbor-schema && npx vitest run --globals && yarn test:index)
2828

29+
# "build generate test"
30+
bgt:
31+
make build generate-protocol-tests test-protocols
32+
2933
test-unit:
3034
yarn g:vitest run -c vitest.config.mts
3135

private/my-local-model-schema/src/schemas/schemas_0.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,14 @@ import { TypeRegistry } from "@smithy/core/schema";
3434
import type { StaticErrorSchema, StaticOperationSchema, StaticStructureSchema } from "@smithy/types";
3535

3636
import {
37-
CodedThrottlingError as __CodedThrottlingError,
38-
HaltError as __HaltError,
39-
MainServiceLinkedError as __MainServiceLinkedError,
40-
MysteryThrottlingError as __MysteryThrottlingError,
41-
RetryableError as __RetryableError,
42-
XYZServiceServiceException as __XYZServiceServiceException,
37+
CodedThrottlingError,
38+
HaltError,
39+
MainServiceLinkedError,
40+
MysteryThrottlingError,
41+
RetryableError,
42+
XYZServiceServiceException,
4343
} from "../models/errors";
44-
import {
45-
XYZServiceSyntheticServiceException as __XYZServiceSyntheticServiceException,
46-
} from "../models/XYZServiceSyntheticServiceException";
44+
import { XYZServiceSyntheticServiceException } from "../models/XYZServiceSyntheticServiceException";
4745

4846
/* eslint no-var: 0 */
4947
export var Alpha$: StaticStructureSchema = [3, n0, _A,
@@ -56,7 +54,7 @@ export var CodedThrottlingError$: StaticErrorSchema = [-3, n0, _CTE,
5654
[],
5755
[]
5856
];
59-
TypeRegistry.for(n0).registerError(CodedThrottlingError$, __CodedThrottlingError);
57+
TypeRegistry.for(n0).registerError(CodedThrottlingError$, CodedThrottlingError);
6058
export var GetNumbersRequest$: StaticStructureSchema = [3, n0, _GNR,
6159
0,
6260
[_bD, _bI, _fWM, _fWMi],
@@ -72,25 +70,25 @@ export var HaltError$: StaticErrorSchema = [-3, n0, _HE,
7270
[],
7371
[]
7472
];
75-
TypeRegistry.for(n0).registerError(HaltError$, __HaltError);
73+
TypeRegistry.for(n0).registerError(HaltError$, HaltError);
7674
export var MainServiceLinkedError$: StaticErrorSchema = [-3, n0, _MSLE,
7775
{ [_e]: _c, [_hE]: 400 },
7876
[],
7977
[]
8078
];
81-
TypeRegistry.for(n0).registerError(MainServiceLinkedError$, __MainServiceLinkedError);
79+
TypeRegistry.for(n0).registerError(MainServiceLinkedError$, MainServiceLinkedError);
8280
export var MysteryThrottlingError$: StaticErrorSchema = [-3, n0, _MTE,
8381
{ [_e]: _c },
8482
[],
8583
[]
8684
];
87-
TypeRegistry.for(n0).registerError(MysteryThrottlingError$, __MysteryThrottlingError);
85+
TypeRegistry.for(n0).registerError(MysteryThrottlingError$, MysteryThrottlingError);
8886
export var RetryableError$: StaticErrorSchema = [-3, n0, _RE,
8987
{ [_e]: _c },
9088
[],
9189
[]
9290
];
93-
TypeRegistry.for(n0).registerError(RetryableError$, __RetryableError);
91+
TypeRegistry.for(n0).registerError(RetryableError$, RetryableError);
9492
export var TradeEventStreamRequest$: StaticStructureSchema = [3, n0, _TESR,
9593
0,
9694
[_eS],
@@ -106,10 +104,10 @@ export var XYZServiceServiceException$: StaticErrorSchema = [-3, n0, _XYZSSE,
106104
[],
107105
[]
108106
];
109-
TypeRegistry.for(n0).registerError(XYZServiceServiceException$, __XYZServiceServiceException);
107+
TypeRegistry.for(n0).registerError(XYZServiceServiceException$, XYZServiceServiceException);
110108
var __Unit = "unit" as const;
111-
export var XYZServiceSyntheticServiceException: StaticErrorSchema = [-3, _s, "XYZServiceSyntheticServiceException", 0, [], []];
112-
TypeRegistry.for(_s).registerError(XYZServiceSyntheticServiceException, __XYZServiceSyntheticServiceException);
109+
export var XYZServiceSyntheticServiceException$: StaticErrorSchema = [-3, _s, "XYZServiceSyntheticServiceException", 0, [], []];
110+
TypeRegistry.for(_s).registerError(XYZServiceSyntheticServiceException$, XYZServiceSyntheticServiceException);
113111
export var TradeEvents$: StaticStructureSchema = [3, n0, _TE,
114112
{ [_st]: 1 },
115113
[_a, _b, _g],

private/smithy-rpcv2-cbor-schema/src/schemas/schemas_0.ts

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,17 @@ import type {
140140
StaticStructureSchema,
141141
} from "@smithy/types";
142142

143-
import {
144-
ComplexError as __ComplexError,
145-
InvalidGreeting as __InvalidGreeting,
146-
ValidationException as __ValidationException,
147-
} from "../models/errors";
148-
import {
149-
RpcV2ProtocolServiceException as __RpcV2ProtocolServiceException,
150-
} from "../models/RpcV2ProtocolServiceException";
143+
import { ComplexError, InvalidGreeting, ValidationException } from "../models/errors";
144+
import { RpcV2ProtocolServiceException } from "../models/RpcV2ProtocolServiceException";
151145

152146
/* eslint no-var: 0 */
153147
var __Unit = "unit" as const;
154148
export var ValidationException$: StaticErrorSchema = [-3, n0, _VE,
155149
{ [_e]: _c },
156150
[_m, _fL],
157-
[0, () => ValidationExceptionFieldList$]
151+
[0, () => ValidationExceptionFieldList]
158152
];
159-
TypeRegistry.for(n0).registerError(ValidationException$, __ValidationException);
153+
TypeRegistry.for(n0).registerError(ValidationException$, ValidationException);
160154
export var ValidationExceptionField$: StaticStructureSchema = [3, n0, _VEF,
161155
0,
162156
[_p, _m],
@@ -172,7 +166,7 @@ export var ComplexError$: StaticErrorSchema = [-3, n1, _CE,
172166
[_TL, _N],
173167
[0, () => ComplexNestedErrorData$]
174168
];
175-
TypeRegistry.for(n1).registerError(ComplexError$, __ComplexError);
169+
TypeRegistry.for(n1).registerError(ComplexError$, ComplexError);
176170
export var ComplexNestedErrorData$: StaticStructureSchema = [3, n1, _CNED,
177171
0,
178172
[_F],
@@ -208,7 +202,7 @@ export var InvalidGreeting$: StaticErrorSchema = [-3, n1, _IG,
208202
[_M],
209203
[0]
210204
];
211-
TypeRegistry.for(n1).registerError(InvalidGreeting$, __InvalidGreeting);
205+
TypeRegistry.for(n1).registerError(InvalidGreeting$, InvalidGreeting);
212206
export var OperationWithDefaultsInput$: StaticStructureSchema = [3, n1, _OWDI,
213207
0,
214208
[_de, _cOD, _tLD, _oTLD],
@@ -237,17 +231,17 @@ export var RecursiveShapesInputOutputNested2$: StaticStructureSchema = [3, n1, _
237231
export var RpcV2CborDenseMapsInputOutput$: StaticStructureSchema = [3, n1, _RVCDMIO,
238232
0,
239233
[_dSM, _dNM, _dBM, _dSMe, _dSMen],
240-
[() => DenseStructMap$, 128 | 1, 128 | 2, 128 | 0, [2, n1, _DSM, 0, 0, 64 | 0]]
234+
[() => DenseStructMap, 128 | 1, 128 | 2, 128 | 0, [2, n1, _DSM, 0, 0, 64 | 0]]
241235
];
242236
export var RpcV2CborListInputOutput$: StaticStructureSchema = [3, n1, _RVCLIO,
243237
0,
244238
[_sL, _sS, _iL, _bL, _tL, _eL, _iEL, _nSL, _sLt, _bLl],
245-
[64 | 0, 64 | 0, 64 | 1, 64 | 2, 64 | 4, 64 | 0, 64 | 1, [1, n2, _NSL, 0, 64 | 0], () => StructureList$, 64 | 21]
239+
[64 | 0, 64 | 0, 64 | 1, 64 | 2, 64 | 4, 64 | 0, 64 | 1, [1, n2, _NSL, 0, 64 | 0], () => StructureList, 64 | 21]
246240
];
247241
export var RpcV2CborSparseMapsInputOutput$: StaticStructureSchema = [3, n1, _RVCSMIO,
248242
0,
249243
[_sSM, _sNM, _sBM, _sSMp, _sSMpa],
250-
[[() => SparseStructMap$, 0], [() => SparseNumberMap$, 0], [() => SparseBooleanMap$, 0], [() => SparseStringMap$, 0], [() => SparseSetMap$, 0]]
244+
[[() => SparseStructMap, 0], [() => SparseNumberMap, 0], [() => SparseBooleanMap, 0], [() => SparseStringMap, 0], [() => SparseSetMap, 0]]
251245
];
252246
export var SimpleScalarStructure$: StaticStructureSchema = [3, n1, _SSS,
253247
0,
@@ -262,7 +256,7 @@ export var SimpleStructure$: StaticStructureSchema = [3, n1, _SS,
262256
export var SparseNullsOperationInputOutput$: StaticStructureSchema = [3, n1, _SNOIO,
263257
0,
264258
[_sSL, _sSMp],
265-
[[() => SparseStringList$, 0], [() => SparseStringMap$, 0]]
259+
[[() => SparseStringList, 0], [() => SparseStringMap, 0]]
266260
];
267261
export var StructureListMember$: StaticStructureSchema = [3, n1, _SLM,
268262
0,
@@ -274,52 +268,52 @@ export var GreetingStruct$: StaticStructureSchema = [3, n2, _GS,
274268
[_h],
275269
[0]
276270
];
277-
export var RpcV2ProtocolServiceException: StaticErrorSchema = [-3, _sC, "RpcV2ProtocolServiceException", 0, [], []];
278-
TypeRegistry.for(_sC).registerError(RpcV2ProtocolServiceException, __RpcV2ProtocolServiceException);
279-
var ValidationExceptionFieldList$: StaticListSchema = [1, n0, _VEFL,
271+
export var RpcV2ProtocolServiceException$: StaticErrorSchema = [-3, _sC, "RpcV2ProtocolServiceException", 0, [], []];
272+
TypeRegistry.for(_sC).registerError(RpcV2ProtocolServiceException$, RpcV2ProtocolServiceException);
273+
var ValidationExceptionFieldList: StaticListSchema = [1, n0, _VEFL,
280274
0, () => ValidationExceptionField$
281275
];
282-
var StructureList$: StaticListSchema = [1, n1, _SL,
276+
var StructureList: StaticListSchema = [1, n1, _SL,
283277
0, () => StructureListMember$
284278
];
285-
var TestStringList$ = 64 | 0;
286-
var BlobList$ = 64 | 21;
287-
var BooleanList$ = 64 | 2;
288-
var FooEnumList$ = 64 | 0;
289-
var IntegerEnumList$ = 64 | 1;
290-
var IntegerList$ = 64 | 1;
291-
var NestedStringList$: StaticListSchema = [1, n2, _NSL,
279+
var TestStringList = 64 | 0;
280+
var BlobList = 64 | 21;
281+
var BooleanList = 64 | 2;
282+
var FooEnumList = 64 | 0;
283+
var IntegerEnumList = 64 | 1;
284+
var IntegerList = 64 | 1;
285+
var NestedStringList: StaticListSchema = [1, n2, _NSL,
292286
0, 64 | 0
293287
];
294-
var SparseStringList$: StaticListSchema = [1, n2, _SSL,
288+
var SparseStringList: StaticListSchema = [1, n2, _SSL,
295289
{ [_s]: 1 }, 0
296290
];
297-
var StringList$ = 64 | 0;
298-
var StringSet$ = 64 | 0;
299-
var TimestampList$ = 64 | 4;
300-
var DenseBooleanMap$ = 128 | 2;
301-
var DenseNumberMap$ = 128 | 1;
302-
var DenseSetMap$: StaticMapSchema = [2, n1, _DSM,
291+
var StringList = 64 | 0;
292+
var StringSet = 64 | 0;
293+
var TimestampList = 64 | 4;
294+
var DenseBooleanMap = 128 | 2;
295+
var DenseNumberMap = 128 | 1;
296+
var DenseSetMap: StaticMapSchema = [2, n1, _DSM,
303297
0, 0, 64 | 0
304298
];
305-
var DenseStringMap$ = 128 | 0;
306-
var DenseStructMap$: StaticMapSchema = [2, n1, _DSMe,
299+
var DenseStringMap = 128 | 0;
300+
var DenseStructMap: StaticMapSchema = [2, n1, _DSMe,
307301
0, 0, () => GreetingStruct$
308302
];
309-
var SparseBooleanMap$: StaticMapSchema = [2, n1, _SBM,
303+
var SparseBooleanMap: StaticMapSchema = [2, n1, _SBM,
310304
{ [_s]: 1 }, 0, 2
311305
];
312-
var SparseNumberMap$: StaticMapSchema = [2, n1, _SNM,
306+
var SparseNumberMap: StaticMapSchema = [2, n1, _SNM,
313307
{ [_s]: 1 }, 0, 1
314308
];
315-
var SparseSetMap$: StaticMapSchema = [2, n1, _SSM,
309+
var SparseSetMap: StaticMapSchema = [2, n1, _SSM,
316310
{ [_s]: 1 }, 0, 64 | 0
317311
];
318-
var SparseStructMap$: StaticMapSchema = [2, n1, _SSMp,
312+
var SparseStructMap: StaticMapSchema = [2, n1, _SSMp,
319313
{ [_s]: 1 }, 0, () => GreetingStruct$
320314
];
321-
var TestStringMap$ = 128 | 0;
322-
var SparseStringMap$: StaticMapSchema = [2, n2, _SSMpa,
315+
var TestStringMap = 128 | 0;
316+
var SparseStringMap: StaticMapSchema = [2, n2, _SSMpa,
323317
{ [_s]: 1 }, 0, 0
324318
];
325319
export var EmptyInputOutput$: StaticOperationSchema = [9, n1, _EIO,

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/knowledge/ServiceClosure.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,18 @@ public String getShapeSchemaVariableName(Shape shape, StringStore store) {
209209
symbolName += "_" + store.var(shape.getId().getNamespace(), "n");
210210
}
211211
/*
212-
* The schema suffix allows what would otherwise conflict with the
213-
* shape's interface symbol name to be exported at the top level of the same package.
212+
* Although exporting a type and value with the same name is allowed by TS, we
213+
* do not want to do this because the structure's interface is not that
214+
* of the schema object.
215+
*
216+
* The name transform deconflicts the interface and structure variable names.
217+
* for export at the top level of the same package.
214218
*/
215-
String schemaSuffix = "$";
216-
return symbolName + schemaSuffix;
219+
String suffix = "";
220+
if (shape.isStructureShape() || shape.isUnionShape() || shape.isOperationShape()) {
221+
suffix = "$";
222+
}
223+
return symbolName + suffix;
217224
}
218225

219226
/**

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaGenerator.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,10 @@ private void writeSimpleSchema(Shape shape) {
125125

126126
private void writeStructureSchema(StructureShape shape) {
127127
checkedWriteSchema(shape, () -> {
128-
String symbolName = ServiceClosure.RESERVED_WORDS.escape(shape.getId().getName());
128+
String exceptionCtorSymbolName = ServiceClosure.RESERVED_WORDS.escape(shape.getId().getName());
129129
if (shape.hasTrait(ErrorTrait.class)) {
130-
String exceptionCtorSymbolName = "__" + symbolName;
131130
writer.addTypeImport("StaticErrorSchema", null, TypeScriptDependency.SMITHY_TYPES);
132-
writer.addRelativeImport(symbolName, exceptionCtorSymbolName, Paths.get("..", "models", "errors"));
131+
writer.addRelativeImport(exceptionCtorSymbolName, null, Paths.get("..", "models", "errors"));
133132
writer.openBlock(
134133
"""
135134
export var $L: StaticErrorSchema = [-3, $L, $L,""",
@@ -167,33 +166,33 @@ private void writeStructureSchema(StructureShape shape) {
167166
*/
168167
private void writeBaseError() {
169168
String serviceName = CodegenUtils.getServiceName(settings, model, symbolProvider);
170-
String serviceExceptionName = CodegenUtils.getSyntheticBaseExceptionName(serviceName, model);
169+
String syntheticBaseExceptionName = CodegenUtils.getSyntheticBaseExceptionName(serviceName, model);
170+
String schemaSymbolName = syntheticBaseExceptionName + "$";
171171

172172
String namespace = settings.getService(model).getId().getNamespace();
173173

174-
String exceptionCtorSymbolName = "__" + serviceExceptionName;
175174
writer.addTypeImport("StaticErrorSchema", null, TypeScriptDependency.SMITHY_TYPES);
176175
writer.addRelativeImport(
177-
serviceExceptionName,
178-
exceptionCtorSymbolName,
179-
Paths.get("..", "models", serviceExceptionName)
176+
syntheticBaseExceptionName,
177+
null,
178+
Paths.get("..", "models", syntheticBaseExceptionName)
180179
);
181180

182181
String syntheticNamespace = store.var("smithy.ts.sdk.synthetic." + namespace);
183182
writer.write(
184183
"""
185184
export var $L: StaticErrorSchema = [-3, $L, $S, 0, [], []];""",
186-
serviceExceptionName,
185+
schemaSymbolName,
187186
syntheticNamespace,
188-
serviceExceptionName
187+
syntheticBaseExceptionName
189188
);
190189
writer.addImportSubmodule("TypeRegistry", null, TypeScriptDependency.SMITHY_CORE, "/schema");
191190
writer.write(
192191
"""
193192
TypeRegistry.for($L).registerError($L, $L);""",
194193
syntheticNamespace,
195-
serviceExceptionName,
196-
exceptionCtorSymbolName
194+
schemaSymbolName,
195+
syntheticBaseExceptionName
197196
);
198197
}
199198

0 commit comments

Comments
 (0)