@azure-tools/typespec-go 0.8.3 → 0.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -1
- package/dist/codegen.go/src/clientFactory.d.ts.map +1 -1
- package/dist/codegen.go/src/clientFactory.js +32 -21
- package/dist/codegen.go/src/clientFactory.js.map +1 -1
- package/dist/codegen.go/src/cloudConfig.d.ts.map +1 -1
- package/dist/codegen.go/src/cloudConfig.js +7 -2
- package/dist/codegen.go/src/cloudConfig.js.map +1 -1
- package/dist/codegen.go/src/example.js +10 -4
- package/dist/codegen.go/src/example.js.map +1 -1
- package/dist/codegen.go/src/fake/servers.js +1 -1
- package/dist/codegen.go/src/fake/servers.js.map +1 -1
- package/dist/codegen.go/src/gomod.js +1 -1
- package/dist/codegen.go/src/helpers.d.ts +11 -2
- package/dist/codegen.go/src/helpers.d.ts.map +1 -1
- package/dist/codegen.go/src/helpers.js +53 -22
- package/dist/codegen.go/src/helpers.js.map +1 -1
- package/dist/codegen.go/src/license.d.ts +10 -0
- package/dist/codegen.go/src/license.d.ts.map +1 -0
- package/dist/codegen.go/src/license.js +45 -0
- package/dist/codegen.go/src/license.js.map +1 -0
- package/dist/codegen.go/src/operations.d.ts.map +1 -1
- package/dist/codegen.go/src/operations.js +74 -63
- package/dist/codegen.go/src/operations.js.map +1 -1
- package/dist/codemodel.go/src/client.d.ts +50 -40
- package/dist/codemodel.go/src/client.d.ts.map +1 -1
- package/dist/codemodel.go/src/client.js +38 -26
- package/dist/codemodel.go/src/client.js.map +1 -1
- package/dist/codemodel.go/src/examples.d.ts +15 -8
- package/dist/codemodel.go/src/examples.d.ts.map +1 -1
- package/dist/codemodel.go/src/examples.js +6 -0
- package/dist/codemodel.go/src/examples.js.map +1 -1
- package/dist/codemodel.go/src/index.d.ts +1 -0
- package/dist/codemodel.go/src/index.d.ts.map +1 -1
- package/dist/codemodel.go/src/index.js +1 -1
- package/dist/codemodel.go/src/index.js.map +1 -1
- package/dist/codemodel.go/src/package.d.ts +5 -0
- package/dist/codemodel.go/src/package.d.ts.map +1 -1
- package/dist/codemodel.go/src/package.js +1 -1
- package/dist/codemodel.go/src/package.js.map +1 -1
- package/dist/codemodel.go/src/param.d.ts +0 -14
- package/dist/codemodel.go/src/param.d.ts.map +1 -1
- package/dist/codemodel.go/src/param.js +0 -23
- package/dist/codemodel.go/src/param.js.map +1 -1
- package/dist/codemodel.go/src/type.d.ts +35 -16
- package/dist/codemodel.go/src/type.d.ts.map +1 -1
- package/dist/codemodel.go/src/type.js +25 -9
- package/dist/codemodel.go/src/type.js.map +1 -1
- package/dist/naming.go/src/index.d.ts +3 -0
- package/dist/naming.go/src/index.d.ts.map +1 -0
- package/dist/naming.go/src/index.js +7 -0
- package/dist/naming.go/src/index.js.map +1 -0
- package/dist/naming.go/src/mappings.d.ts +1 -0
- package/dist/naming.go/src/mappings.d.ts.map +1 -1
- package/dist/naming.go/src/mappings.js +5 -1
- package/dist/naming.go/src/mappings.js.map +1 -1
- package/dist/typespec-go/src/emitter.d.ts.map +1 -1
- package/dist/typespec-go/src/emitter.js +7 -0
- package/dist/typespec-go/src/emitter.js.map +1 -1
- package/dist/typespec-go/src/tcgcadapter/adapter.d.ts.map +1 -1
- package/dist/typespec-go/src/tcgcadapter/adapter.js +16 -14
- package/dist/typespec-go/src/tcgcadapter/adapter.js.map +1 -1
- package/dist/typespec-go/src/tcgcadapter/clients.d.ts.map +1 -1
- package/dist/typespec-go/src/tcgcadapter/clients.js +164 -99
- package/dist/typespec-go/src/tcgcadapter/clients.js.map +1 -1
- package/package.json +22 -22
|
@@ -91,13 +91,8 @@ export class clientAdapter {
|
|
|
91
91
|
AuthTypes[AuthTypes["Default"] = 0] = "Default";
|
|
92
92
|
AuthTypes[AuthTypes["NoAuth"] = 1] = "NoAuth";
|
|
93
93
|
AuthTypes[AuthTypes["WithAuth"] = 2] = "WithAuth";
|
|
94
|
-
AuthTypes[AuthTypes["OmitAuth"] = 4] = "OmitAuth";
|
|
95
94
|
})(AuthTypes || (AuthTypes = {}));
|
|
96
|
-
|
|
97
|
-
// an existing module. this is because the constructor(s) require
|
|
98
|
-
// the module name and version info, and we can't make any
|
|
99
|
-
// assumptions about the names/location.
|
|
100
|
-
let authType = (this.ta.codeModel.options.omitConstructors || this.ta.codeModel.options.containingModule) ? AuthTypes.OmitAuth : AuthTypes.Default;
|
|
95
|
+
let authType = AuthTypes.Default;
|
|
101
96
|
if (!this.ta.codeModel.options.omitConstructors && this.ta.codeModel.options.containingModule) {
|
|
102
97
|
// emit a diagnostic indicating that no ctors will be emitted due to containing-module.
|
|
103
98
|
this.ctx.program.reportDiagnostic({
|
|
@@ -107,6 +102,15 @@ export class clientAdapter {
|
|
|
107
102
|
target: sdkClient.__raw.type ?? NoTarget,
|
|
108
103
|
});
|
|
109
104
|
}
|
|
105
|
+
if ((this.ta.codeModel.options.omitConstructors || this.ta.codeModel.options.containingModule) && this.ta.codeModel.options.generateExamples) {
|
|
106
|
+
// emit a diagnostic indicating that no ctors will be emitted due to containing-module.
|
|
107
|
+
this.ctx.program.reportDiagnostic({
|
|
108
|
+
code: 'UnsupportedConfiguration',
|
|
109
|
+
severity: 'warning',
|
|
110
|
+
message: 'cannot emit examples when containing-module or omit-constructors is set',
|
|
111
|
+
target: sdkClient.__raw.type ?? NoTarget,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
110
114
|
/**
|
|
111
115
|
* processes a credendial, potentially adding its supporting client constructor
|
|
112
116
|
*
|
|
@@ -143,16 +147,23 @@ export class clientAdapter {
|
|
|
143
147
|
}
|
|
144
148
|
// anything other than public means non-instantiable client
|
|
145
149
|
if (sdkClient.clientInitialization.initializedBy & tcgc.InitializedByFlags.Individually) {
|
|
146
|
-
|
|
150
|
+
let constructable;
|
|
151
|
+
// we skip generating client constructors when emitting into
|
|
152
|
+
// an existing module. this is because the constructor(s) require
|
|
153
|
+
// the module name and version info, and we can't make any
|
|
154
|
+
// assumptions about the names/location.
|
|
155
|
+
if (!this.ta.codeModel.options.omitConstructors && !this.ta.codeModel.options.containingModule) {
|
|
156
|
+
constructable = new go.Constructable(go.newClientOptions(this.ta.codeModel.type, clientName));
|
|
157
|
+
}
|
|
147
158
|
for (const param of sdkClient.clientInitialization.parameters) {
|
|
148
159
|
switch (param.kind) {
|
|
149
160
|
case 'credential':
|
|
150
|
-
if (
|
|
161
|
+
if (!constructable) {
|
|
151
162
|
continue;
|
|
152
163
|
}
|
|
153
164
|
switch (param.type.kind) {
|
|
154
165
|
case 'credential':
|
|
155
|
-
authType |= processCredential(goClient,
|
|
166
|
+
authType |= processCredential(goClient, constructable, param.type.scheme);
|
|
156
167
|
break;
|
|
157
168
|
case 'union': {
|
|
158
169
|
const variantKinds = new Array();
|
|
@@ -161,7 +172,7 @@ export class clientAdapter {
|
|
|
161
172
|
// emit the support credential kinds and skip any unsupported ones.
|
|
162
173
|
// this prevents emitting the WithNoCredential constructor in cases
|
|
163
174
|
// where it might not actually be supported.
|
|
164
|
-
authType |= processCredential(goClient,
|
|
175
|
+
authType |= processCredential(goClient, constructable, variantType.scheme);
|
|
165
176
|
}
|
|
166
177
|
// no supported credential types were specified
|
|
167
178
|
if (authType === AuthTypes.Default) {
|
|
@@ -170,7 +181,7 @@ export class clientAdapter {
|
|
|
170
181
|
continue;
|
|
171
182
|
}
|
|
172
183
|
}
|
|
173
|
-
|
|
184
|
+
break;
|
|
174
185
|
case 'endpoint': {
|
|
175
186
|
if (this.ta.codeModel.type === 'azure-arm') {
|
|
176
187
|
// for ARM, the endpoint is handled via the azcore/arm.Client
|
|
@@ -201,23 +212,36 @@ export class clientAdapter {
|
|
|
201
212
|
adaptedParam.docs.summary = param.summary;
|
|
202
213
|
adaptedParam.docs.description = param.doc;
|
|
203
214
|
goClient.parameters.push(adaptedParam);
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
215
|
+
if (constructable) {
|
|
216
|
+
constructable.endpoint = new go.ClientEndpoint(adaptedParam);
|
|
217
|
+
// if the server's URL is *only* the endpoint parameter then we're done.
|
|
218
|
+
// this is the param.type.kind === 'endpoint' case.
|
|
219
|
+
if (endpointType.serverUrl === `{${templateArg.serializedName}}`) {
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
// there's either a suffix on the endpoint param, more template arguments, or both.
|
|
223
|
+
// either way we need to create supplemental info on the constructable.
|
|
224
|
+
// strip off the first segment which corresponds to the endpoint param as it's not needed.
|
|
225
|
+
const serverUrl = endpointType.serverUrl.replace(`{${templateArg.serializedName}}/`, '');
|
|
226
|
+
constructable.endpoint.supplemental = new go.SupplementalEndpoint(serverUrl);
|
|
208
227
|
}
|
|
209
|
-
// there's either a suffix on the endpoint param, more template arguments, or both.
|
|
210
|
-
// either way we need to create supplemental info on the constructable.
|
|
211
|
-
// strip off the first segment which corresponds to the endpoint param as it's not needed.
|
|
212
|
-
const serverUrl = endpointType.serverUrl.replace(`{${templateArg.serializedName}}/`, '');
|
|
213
|
-
goClient.instance.endpoint = new go.SupplementalEndpoint(serverUrl);
|
|
214
228
|
continue;
|
|
215
229
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
230
|
+
if (constructable) {
|
|
231
|
+
const adaptedParam = this.adaptURIParam(templateArg, false);
|
|
232
|
+
adaptedParam.docs.summary = templateArg.summary;
|
|
233
|
+
adaptedParam.docs.description = templateArg.doc;
|
|
234
|
+
adaptedParam.isApiVersion = templateArg.isApiVersionParam;
|
|
235
|
+
constructable.endpoint?.supplemental?.parameters.push(adaptedParam);
|
|
236
|
+
if (!go.isRequiredParameter(adaptedParam.style)) {
|
|
237
|
+
if (constructable.options.kind === 'clientOptions') {
|
|
238
|
+
constructable.options.parameters.push(adaptedParam);
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
throw new AdapterError('UnsupportedTsp', 'optional client parameters for ARM is not supported', templateArg.__raw?.node ?? NoTarget);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
221
245
|
}
|
|
222
246
|
break;
|
|
223
247
|
}
|
|
@@ -236,25 +260,22 @@ export class clientAdapter {
|
|
|
236
260
|
continue;
|
|
237
261
|
}
|
|
238
262
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
263
|
+
if (constructable) {
|
|
264
|
+
goClient.instance = constructable;
|
|
265
|
+
// if no authentication type was specified, or the noAuth scheme was
|
|
266
|
+
// explicitly specified, then include the WithNoCredential constructor
|
|
267
|
+
if (authType === AuthTypes.Default || (authType & AuthTypes.NoAuth) === AuthTypes.NoAuth) {
|
|
268
|
+
goClient.instance.constructors.push(new go.Constructor(`New${clientName}WithNoCredential`));
|
|
269
|
+
}
|
|
270
|
+
// propagate ctor params to all client ctors
|
|
271
|
+
for (const constructor of goClient.instance.constructors) {
|
|
272
|
+
constructor.parameters.push(...goClient.parameters);
|
|
248
273
|
}
|
|
249
|
-
}
|
|
250
|
-
// if we created constructors, propagate the persisted client params to them
|
|
251
|
-
for (const constructor of goClient.instance.constructors) {
|
|
252
|
-
constructor.parameters = goClient.parameters;
|
|
253
274
|
}
|
|
254
275
|
}
|
|
255
276
|
else if (parent) {
|
|
256
277
|
// this is a sub-client. it will share the client/host params of the parent.
|
|
257
|
-
// NOTE: we must propagate
|
|
278
|
+
// NOTE: we must propagate parent params before a potential recursive call
|
|
258
279
|
// to create a child client that will need to inherit our client params.
|
|
259
280
|
if (parent.instance?.kind === 'templatedHost') {
|
|
260
281
|
goClient.instance = parent.instance;
|
|
@@ -305,7 +326,9 @@ export class clientAdapter {
|
|
|
305
326
|
else if (cred.flows[0].scopes.length > 1) {
|
|
306
327
|
throw new AdapterError('InternalError', `too many scopes defined for credential type ${cred.type}`, cred.model);
|
|
307
328
|
}
|
|
308
|
-
|
|
329
|
+
const ctor = new go.Constructor(`New${goClient.name}`);
|
|
330
|
+
ctor.parameters.push(new go.ClientCredentialParameter('credential', new go.TokenCredential(cred.flows[0].scopes.map(each => each.value))));
|
|
331
|
+
return ctor;
|
|
309
332
|
}
|
|
310
333
|
/**
|
|
311
334
|
* creates a new Go URI parameter from the specified tcgc path parameter
|
|
@@ -324,10 +347,13 @@ export class clientAdapter {
|
|
|
324
347
|
}
|
|
325
348
|
if (go.isURIParameterType(paramType)) {
|
|
326
349
|
const style = forceRequired ? 'required' : this.adaptParameterStyle(sdkParam);
|
|
327
|
-
|
|
328
|
-
|
|
350
|
+
if (this.ta.codeModel.type === 'azure-arm' && !go.isRequiredParameter(style)) {
|
|
351
|
+
throw new AdapterError('UnsupportedTsp', 'optional client parameters for ARM is not supported', sdkParam.__raw?.node ?? NoTarget);
|
|
352
|
+
}
|
|
353
|
+
const uriParam = new go.URIParameter(sdkParam.name, sdkParam.serializedName, paramType, style, isTypePassedByValue(sdkParam.type) || !sdkParam.optional, 'client');
|
|
329
354
|
uriParam.docs.summary = sdkParam.summary;
|
|
330
355
|
uriParam.docs.description = sdkParam.doc;
|
|
356
|
+
uriParam.isApiVersion = sdkParam.isApiVersionParam;
|
|
331
357
|
return uriParam;
|
|
332
358
|
}
|
|
333
359
|
throw new AdapterError('UnsupportedTsp', `unsupported URI parameter type ${paramType.kind}`, sdkParam.__raw?.node ?? NoTarget);
|
|
@@ -369,6 +395,18 @@ export class clientAdapter {
|
|
|
369
395
|
throw new AdapterError('UnsupportedTsp', `paging with re-injected parameters is not supported`, sdkMethod.__raw?.node ?? NoTarget);
|
|
370
396
|
}
|
|
371
397
|
method = new go.PageableMethod(methodName, goClient, sdkMethod.operation.path, sdkMethod.operation.verb, statusCodes, naming);
|
|
398
|
+
if (sdkMethod.pagingMetadata.nextLinkVerb) {
|
|
399
|
+
switch (sdkMethod.pagingMetadata.nextLinkVerb) {
|
|
400
|
+
case 'GET':
|
|
401
|
+
// we default to GET in the ctor for PageableMethod
|
|
402
|
+
break;
|
|
403
|
+
case 'POST':
|
|
404
|
+
method.nextLinkVerb = 'post';
|
|
405
|
+
break;
|
|
406
|
+
default:
|
|
407
|
+
sdkMethod.pagingMetadata.nextLinkVerb;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
372
410
|
if (sdkMethod.pagingMetadata.nextLinkSegments) {
|
|
373
411
|
method.nextLinkName = capitalize(sdkMethod.pagingMetadata.nextLinkSegments.map((segment) => {
|
|
374
412
|
if (segment.kind === 'property') {
|
|
@@ -518,7 +556,9 @@ export class clientAdapter {
|
|
|
518
556
|
opParam.correspondingMethodParams[0].optional = false;
|
|
519
557
|
}
|
|
520
558
|
let adaptedParam;
|
|
521
|
-
|
|
559
|
+
// for the @bodyRoot decorator opParam.type !== param.type which is obviously not a
|
|
560
|
+
// spread parameter. we also need to check the usage flag to disambiguate these cases.
|
|
561
|
+
if (opParam.kind === 'body' && opParam.type.kind === 'model' && opParam.type !== param.type && (opParam.type.usage & tcgc.UsageFlags.Spread)) {
|
|
522
562
|
const paramStyle = this.adaptParameterStyle(param);
|
|
523
563
|
const paramName = getEscapedReservedName(ensureNameCase(param.name, paramStyle === 'required'), 'Param');
|
|
524
564
|
// if the param is required then it's always passed by value
|
|
@@ -609,7 +649,17 @@ export class clientAdapter {
|
|
|
609
649
|
if (!method.receiver.type.parameters.find((v) => {
|
|
610
650
|
return v.name === adaptedParam.name;
|
|
611
651
|
})) {
|
|
652
|
+
if (this.ta.codeModel.type === 'azure-arm' && adaptedParam.style !== 'literal' && adaptedParam.style !== 'required') {
|
|
653
|
+
throw new AdapterError('UnsupportedTsp', 'optional client parameters for ARM is not supported', opParam.__raw?.node ?? NoTarget);
|
|
654
|
+
}
|
|
612
655
|
method.receiver.type.parameters.push(adaptedParam);
|
|
656
|
+
if (method.receiver.type.instance?.kind === 'constructable') {
|
|
657
|
+
// if this is an instantiable client then also add
|
|
658
|
+
// the client parameter to all constructors
|
|
659
|
+
for (const ctor of method.receiver.type.instance.constructors) {
|
|
660
|
+
ctor.parameters.push(adaptedParam);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
613
663
|
}
|
|
614
664
|
}
|
|
615
665
|
}
|
|
@@ -1037,22 +1087,33 @@ export class clientAdapter {
|
|
|
1037
1087
|
adaptHttpOperationExamples(sdkMethod, method, paramMapping) {
|
|
1038
1088
|
if (sdkMethod.operation.examples && sdkMethod.access !== 'internal') {
|
|
1039
1089
|
for (const example of sdkMethod.operation.examples) {
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
const
|
|
1054
|
-
|
|
1055
|
-
|
|
1090
|
+
try {
|
|
1091
|
+
const goExample = new go.MethodExample(example.name, { summary: example.doc }, example.filePath);
|
|
1092
|
+
for (const param of example.parameters) {
|
|
1093
|
+
if (param.parameter.isApiVersionParam && param.parameter.clientDefaultValue) {
|
|
1094
|
+
// skip the api-version param as it's not a formal parameter
|
|
1095
|
+
continue;
|
|
1096
|
+
}
|
|
1097
|
+
const goParams = paramMapping.get(param.parameter);
|
|
1098
|
+
if (!goParams) {
|
|
1099
|
+
throw new AdapterError('InternalError', `can not find go param for example param ${param.parameter.name}`, NoTarget);
|
|
1100
|
+
}
|
|
1101
|
+
if (goParams.length > 1) {
|
|
1102
|
+
// spread case
|
|
1103
|
+
for (const goParam of goParams) {
|
|
1104
|
+
const propertyValue = param.value.value[goParam.serializedName];
|
|
1105
|
+
const paramExample = new go.ParameterExample(goParam, this.adaptExampleType(propertyValue, goParam?.type));
|
|
1106
|
+
if (goParam.group) {
|
|
1107
|
+
goExample.optionalParamsGroup.push(paramExample);
|
|
1108
|
+
}
|
|
1109
|
+
else {
|
|
1110
|
+
goExample.parameters.push(paramExample);
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
else {
|
|
1115
|
+
const paramExample = new go.ParameterExample(goParams[0], this.adaptExampleType(param.value, goParams[0]?.type));
|
|
1116
|
+
if (goParams[0]?.group) {
|
|
1056
1117
|
goExample.optionalParamsGroup.push(paramExample);
|
|
1057
1118
|
}
|
|
1058
1119
|
else {
|
|
@@ -1060,51 +1121,49 @@ export class clientAdapter {
|
|
|
1060
1121
|
}
|
|
1061
1122
|
}
|
|
1062
1123
|
}
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1124
|
+
// only handle 200 response
|
|
1125
|
+
const response = example.responses.find((v) => { return v.statusCode === 200; });
|
|
1126
|
+
if (response) {
|
|
1127
|
+
goExample.responseEnvelope = new go.ResponseEnvelopeExample(method.returns);
|
|
1128
|
+
for (const header of response.headers) {
|
|
1129
|
+
const goHeader = method.returns.headers.find(h => h.headerName === header.header.serializedName);
|
|
1130
|
+
if (!goHeader) {
|
|
1131
|
+
throw new AdapterError('InternalError', `can not find go header for example header ${header.header.serializedName}`, NoTarget);
|
|
1132
|
+
}
|
|
1133
|
+
goExample.responseEnvelope.headers.push(new go.ResponseHeaderExample(goHeader, this.adaptExampleType(header.value, goHeader.type)));
|
|
1067
1134
|
}
|
|
1068
|
-
|
|
1069
|
-
|
|
1135
|
+
// there are some problems with LROs at present which can cause the result
|
|
1136
|
+
// to be undefined even though the operation returns a response.
|
|
1137
|
+
// TODO: https://github.com/Azure/typespec-azure/issues/1688
|
|
1138
|
+
if (response.bodyValue && method.returns.result) {
|
|
1139
|
+
switch (method.returns.result.kind) {
|
|
1140
|
+
case 'anyResult':
|
|
1141
|
+
goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, new go.Any());
|
|
1142
|
+
break;
|
|
1143
|
+
case 'binaryResult':
|
|
1144
|
+
goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, new go.Scalar('byte', false));
|
|
1145
|
+
break;
|
|
1146
|
+
case 'modelResult':
|
|
1147
|
+
goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, method.returns.result.modelType);
|
|
1148
|
+
break;
|
|
1149
|
+
case 'monomorphicResult':
|
|
1150
|
+
goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, method.returns.result.monomorphicType);
|
|
1151
|
+
break;
|
|
1152
|
+
case 'polymorphicResult':
|
|
1153
|
+
goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, method.returns.result.interface);
|
|
1154
|
+
break;
|
|
1155
|
+
}
|
|
1070
1156
|
}
|
|
1071
1157
|
}
|
|
1158
|
+
method.examples.push(goExample);
|
|
1072
1159
|
}
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
for (const header of response.headers) {
|
|
1078
|
-
const goHeader = method.returns.headers.find(h => h.headerName === header.header.serializedName);
|
|
1079
|
-
if (!goHeader) {
|
|
1080
|
-
throw new AdapterError('InternalError', `can not find go header for example header ${header.header.serializedName}`, NoTarget);
|
|
1081
|
-
}
|
|
1082
|
-
goExample.responseEnvelope.headers.push(new go.ResponseHeaderExample(goHeader, this.adaptExampleType(header.value, goHeader.type)));
|
|
1083
|
-
}
|
|
1084
|
-
// there are some problems with LROs at present which can cause the result
|
|
1085
|
-
// to be undefined even though the operation returns a response.
|
|
1086
|
-
// TODO: https://github.com/Azure/typespec-azure/issues/1688
|
|
1087
|
-
if (response.bodyValue && method.returns.result) {
|
|
1088
|
-
switch (method.returns.result.kind) {
|
|
1089
|
-
case 'anyResult':
|
|
1090
|
-
goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, new go.Any());
|
|
1091
|
-
break;
|
|
1092
|
-
case 'binaryResult':
|
|
1093
|
-
goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, new go.Scalar('byte', false));
|
|
1094
|
-
break;
|
|
1095
|
-
case 'modelResult':
|
|
1096
|
-
goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, method.returns.result.modelType);
|
|
1097
|
-
break;
|
|
1098
|
-
case 'monomorphicResult':
|
|
1099
|
-
goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, method.returns.result.monomorphicType);
|
|
1100
|
-
break;
|
|
1101
|
-
case 'polymorphicResult':
|
|
1102
|
-
goExample.responseEnvelope.result = this.adaptExampleType(response.bodyValue, method.returns.result.interface);
|
|
1103
|
-
break;
|
|
1104
|
-
}
|
|
1160
|
+
catch (error) {
|
|
1161
|
+
// Only added try-catch block to output error message and example file path
|
|
1162
|
+
if (error instanceof AdapterError) {
|
|
1163
|
+
throw new AdapterError(error.code, `${error.message} (example file: '${example.filePath}')`, error.target);
|
|
1105
1164
|
}
|
|
1165
|
+
throw error;
|
|
1106
1166
|
}
|
|
1107
|
-
method.examples.push(goExample);
|
|
1108
1167
|
}
|
|
1109
1168
|
}
|
|
1110
1169
|
}
|
|
@@ -1185,17 +1244,23 @@ export class clientAdapter {
|
|
|
1185
1244
|
const ret = new go.StructExample(concreteType);
|
|
1186
1245
|
for (const [k, v] of Object.entries(exampleType.value)) {
|
|
1187
1246
|
const field = concreteType.fields.find(f => f.serializedName === k);
|
|
1247
|
+
if (!field) {
|
|
1248
|
+
throw new AdapterError('InternalError', `field with serializedName '${k}' not found in model '${concreteType.name}'.`, NoTarget);
|
|
1249
|
+
}
|
|
1188
1250
|
ret.value[field.name] = this.adaptExampleType(v, field.type);
|
|
1189
1251
|
}
|
|
1190
1252
|
if (exampleType.additionalPropertiesValue) {
|
|
1191
1253
|
ret.additionalProperties = {};
|
|
1192
1254
|
for (const [k, v] of Object.entries(exampleType.additionalPropertiesValue)) {
|
|
1193
1255
|
const filed = concreteType.fields.find(f => f.annotations.isAdditionalProperties);
|
|
1256
|
+
if (!filed) {
|
|
1257
|
+
throw new AdapterError('InternalError', `additional properties field not found in model '${concreteType.name}'.`, NoTarget);
|
|
1258
|
+
}
|
|
1194
1259
|
if (filed.type.kind === 'map') {
|
|
1195
1260
|
ret.additionalProperties[k] = this.adaptExampleType(v, filed.type.valueType);
|
|
1196
1261
|
}
|
|
1197
1262
|
else {
|
|
1198
|
-
throw new AdapterError('InternalError', `additional properties field type should be map type`, NoTarget);
|
|
1263
|
+
throw new AdapterError('InternalError', `additional properties field type should be map type, but got '${filed.type.kind}' in model '${concreteType.name}'`, NoTarget);
|
|
1199
1264
|
}
|
|
1200
1265
|
}
|
|
1201
1266
|
}
|