@azure-tools/typespec-java 0.7.2 → 0.7.4
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/dist/src/code-model-builder.d.ts +4 -3
- package/dist/src/code-model-builder.d.ts.map +1 -1
- package/dist/src/code-model-builder.js +107 -120
- package/dist/src/code-model-builder.js.map +1 -1
- package/dist/src/common/operation.d.ts +1 -10
- package/dist/src/common/operation.d.ts.map +1 -1
- package/dist/src/common/operation.js +0 -5
- package/dist/src/common/operation.js.map +1 -1
- package/dist/src/emitter.d.ts +1 -1
- package/dist/src/emitter.d.ts.map +1 -1
- package/dist/src/models.d.ts +5 -2
- package/dist/src/models.d.ts.map +1 -1
- package/dist/src/models.js +19 -1
- package/dist/src/models.js.map +1 -1
- package/dist/src/operation-utils.d.ts +17 -0
- package/dist/src/operation-utils.d.ts.map +1 -0
- package/dist/src/operation-utils.js +160 -0
- package/dist/src/operation-utils.js.map +1 -0
- package/dist/src/type-utils.d.ts +31 -0
- package/dist/src/type-utils.d.ts.map +1 -0
- package/dist/src/type-utils.js +112 -0
- package/dist/src/type-utils.js.map +1 -0
- package/dist/src/utils.d.ts +3 -44
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +11 -246
- package/dist/src/utils.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/target/azure-typespec-extension-jar-with-dependencies.jar +0 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { SyntaxKind, ignoreDiagnostics, resolvePath } from "@typespec/compiler";
|
|
2
|
+
import { getHeaderFieldName, getQueryParamName, getPathParamName, isStatusCode, getAllHttpServices, } from "@typespec/http";
|
|
3
|
+
import { resolveOperationId } from "@typespec/openapi";
|
|
4
|
+
import { ServiceVersion } from "./common/client.js";
|
|
5
|
+
import { getVersion } from "@typespec/versioning";
|
|
6
|
+
import { getNamespace, logWarning, pascalCase } from "./utils.js";
|
|
7
|
+
export const specialHeaderNames = new Set([
|
|
8
|
+
"repeatability-request-id",
|
|
9
|
+
"repeatability-first-sent",
|
|
10
|
+
"x-ms-client-request-id",
|
|
11
|
+
]);
|
|
12
|
+
export const originApiVersion = "modelerfour:synthesized/api-version";
|
|
13
|
+
export async function loadExamples(program, options) {
|
|
14
|
+
const operationExamplesMap = new Map();
|
|
15
|
+
if (options["examples-directory"]) {
|
|
16
|
+
const operationIdExamplesMap = new Map();
|
|
17
|
+
const service = ignoreDiagnostics(getAllHttpServices(program))[0];
|
|
18
|
+
let version = undefined;
|
|
19
|
+
const versioning = getVersion(program, service.namespace);
|
|
20
|
+
if (versioning && versioning.getVersions()) {
|
|
21
|
+
const versions = versioning.getVersions();
|
|
22
|
+
version = versions[versions.length - 1].value;
|
|
23
|
+
}
|
|
24
|
+
const exampleDir = version
|
|
25
|
+
? resolvePath(options["examples-directory"], version)
|
|
26
|
+
: resolvePath(options["examples-directory"]);
|
|
27
|
+
try {
|
|
28
|
+
if (!(await program.host.stat(exampleDir)).isDirectory())
|
|
29
|
+
return operationExamplesMap;
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
logWarning(program, `Examples directory '${exampleDir}' does not exist.`);
|
|
33
|
+
return operationExamplesMap;
|
|
34
|
+
}
|
|
35
|
+
const exampleFiles = await program.host.readDir(exampleDir);
|
|
36
|
+
for (const fileName of exampleFiles) {
|
|
37
|
+
try {
|
|
38
|
+
const exampleFile = await program.host.readFile(resolvePath(exampleDir, fileName));
|
|
39
|
+
const example = JSON.parse(exampleFile.text);
|
|
40
|
+
if (!example.operationId) {
|
|
41
|
+
logWarning(program, `Example file '${fileName}' is missing operationId.`);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (!operationIdExamplesMap.has(example.operationId)) {
|
|
45
|
+
operationIdExamplesMap.set(example.operationId, example);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
logWarning(program, `Failed to load example file '${fileName}'.`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (operationIdExamplesMap.size > 0) {
|
|
53
|
+
const routes = service.operations;
|
|
54
|
+
routes.forEach((it) => {
|
|
55
|
+
const operationId = pascalCaseForOperationId(resolveOperationId(program, it.operation));
|
|
56
|
+
if (operationIdExamplesMap.has(operationId)) {
|
|
57
|
+
operationExamplesMap.set(it.operation, operationIdExamplesMap.get(operationId));
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return operationExamplesMap;
|
|
63
|
+
}
|
|
64
|
+
function pascalCaseForOperationId(name) {
|
|
65
|
+
return name
|
|
66
|
+
.split("_")
|
|
67
|
+
.map((s) => pascalCase(s))
|
|
68
|
+
.join("_");
|
|
69
|
+
}
|
|
70
|
+
export function operationContainsJsonMergePatch(op) {
|
|
71
|
+
for (const param of op.parameters.parameters) {
|
|
72
|
+
if (param.type === "header" && param.name.toLowerCase() === "content-type") {
|
|
73
|
+
if (param.param.type.kind === "String" && param.param.type.value === "application/merge-patch+json") {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
export function operationIsMultipleContentTypes(httpOperation) {
|
|
81
|
+
if (httpOperation.parameters.parameters &&
|
|
82
|
+
httpOperation.parameters.parameters.some((parameter) => {
|
|
83
|
+
var _a, _b, _c;
|
|
84
|
+
return (parameter === null || parameter === void 0 ? void 0 : parameter.type) === "header" &&
|
|
85
|
+
((_a = parameter === null || parameter === void 0 ? void 0 : parameter.name) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === "content-type" &&
|
|
86
|
+
((_c = (_b = parameter === null || parameter === void 0 ? void 0 : parameter.param) === null || _b === void 0 ? void 0 : _b.type) === null || _c === void 0 ? void 0 : _c.kind) === "Union";
|
|
87
|
+
})) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
export function isPayloadProperty(program, property) {
|
|
93
|
+
const headerInfo = getHeaderFieldName(program, property);
|
|
94
|
+
const queryInfo = getQueryParamName(program, property);
|
|
95
|
+
const pathInfo = getPathParamName(program, property);
|
|
96
|
+
const statusCodeInfo = isStatusCode(program, property);
|
|
97
|
+
return !(headerInfo || queryInfo || pathInfo || statusCodeInfo);
|
|
98
|
+
}
|
|
99
|
+
export function getClientApiVersions(client) {
|
|
100
|
+
var _a;
|
|
101
|
+
if ((_a = client.globalParameters) === null || _a === void 0 ? void 0 : _a.find((it) => it.origin === originApiVersion)) {
|
|
102
|
+
return client.apiVersions;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export function getServiceVersion(client) {
|
|
109
|
+
let name = client.language.default.name;
|
|
110
|
+
let description = name;
|
|
111
|
+
if (name.endsWith("Client")) {
|
|
112
|
+
name = name.substring(0, name.length - "Client".length);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
description = description + "Client";
|
|
116
|
+
}
|
|
117
|
+
if (name.endsWith("Service")) {
|
|
118
|
+
name = name + "Version";
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
name = name + "ServiceVersion";
|
|
122
|
+
}
|
|
123
|
+
return new ServiceVersion(name, description);
|
|
124
|
+
}
|
|
125
|
+
export function isLroNewPollingStrategy(operation, lroMetadata) {
|
|
126
|
+
// at present, checks if operation uses template from Azure.Core
|
|
127
|
+
const azureCoreLroSvs = [
|
|
128
|
+
"LongRunningResourceCreateOrReplace",
|
|
129
|
+
"LongRunningResourceCreateOrUpdate",
|
|
130
|
+
"LongRunningResourceDelete",
|
|
131
|
+
"LongRunningResourceAction",
|
|
132
|
+
"LongRunningRpcOperation",
|
|
133
|
+
];
|
|
134
|
+
let ret = false;
|
|
135
|
+
if (lroMetadata.pollingInfo &&
|
|
136
|
+
lroMetadata.statusMonitorStep &&
|
|
137
|
+
lroMetadata.pollingInfo.responseModel.name === "OperationStatus" &&
|
|
138
|
+
getNamespace(lroMetadata.pollingInfo.responseModel) === "Azure.Core.Foundations") {
|
|
139
|
+
if (operation.node.signature.kind === SyntaxKind.OperationSignatureReference) {
|
|
140
|
+
if (operation.node.signature.baseOperation.target.kind === SyntaxKind.MemberExpression) {
|
|
141
|
+
const sv = operation.node.signature.baseOperation.target.id.sv;
|
|
142
|
+
ret = azureCoreLroSvs.includes(sv);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return ret;
|
|
147
|
+
// if (verb === "put" && !lroMetadata.finalStep) {
|
|
148
|
+
// // PUT without last GET on resource
|
|
149
|
+
// useNewPollStrategy = true;
|
|
150
|
+
// } else if (
|
|
151
|
+
// verb === "post" &&
|
|
152
|
+
// lroMetadata.finalStep &&
|
|
153
|
+
// lroMetadata.finalStep.kind === "pollingSuccessProperty" &&
|
|
154
|
+
// lroMetadata.finalStep.target.name === "result"
|
|
155
|
+
// ) {
|
|
156
|
+
// // POST with final result in "result" property
|
|
157
|
+
// useNewPollStrategy = true;
|
|
158
|
+
// }
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=operation-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operation-utils.js","sourceRoot":"","sources":["../../src/operation-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqC,UAAU,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACnH,OAAO,EAEL,kBAAkB,EAClB,iBAAiB,EACjB,gBAAgB,EAChB,YAAY,EACZ,kBAAkB,GACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAGvD,OAAO,EAA6B,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAG/E,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,UAA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CAAC,CAAC;AAC/C,CAAC;AAED,MAAM,UAAU,uBAAuB,CAAC,SAAoB,EAAE,WAAwB;IACpF,gEAAgE;IAChE,MAAM,eAAe,GAAG;QACtB,oCAAoC;QACpC,mCAAmC;QACnC,2BAA2B;QAC3B,2BAA2B;QAC3B,yBAAyB;KAC1B,CAAC;IAEF,IAAI,GAAG,GAAG,KAAK,CAAC;IAChB,IACE,WAAW,CAAC,WAAW;QACvB,WAAW,CAAC,iBAAiB;QAC7B,WAAW,CAAC,WAAW,CAAC,aAAa,CAAC,IAAI,KAAK,iBAAiB;QAChE,YAAY,CAAC,WAAW,CAAC,WAAW,CAAC,aAAa,CAAC,KAAK,wBAAwB,EAChF;QACA,IAAI,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,KAAK,UAAU,CAAC,2BAA2B,EAAE;YAC5E,IAAI,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,CAAC,gBAAgB,EAAE;gBACtF,MAAM,EAAE,GAAG,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;gBAC/D,GAAG,GAAG,eAAe,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;aACpC;SACF;KACF;IACD,OAAO,GAAG,CAAC;IAEX,kDAAkD;IAClD,wCAAwC;IACxC,+BAA+B;IAC/B,cAAc;IACd,uBAAuB;IACvB,6BAA6B;IAC7B,+DAA+D;IAC/D,mDAAmD;IACnD,MAAM;IACN,mDAAmD;IACnD,+BAA+B;IAC/B,IAAI;AACN,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { EncodeData, IntrinsicScalarName, Model, Scalar, TemplatedTypeBase, Type, UnionVariant } from "@typespec/compiler";
|
|
2
|
+
import { DurationSchema } from "./common/schemas/time";
|
|
3
|
+
/** Acts as a cache for processing inputs.
|
|
4
|
+
*
|
|
5
|
+
* If the input is undefined, the output is always undefined.
|
|
6
|
+
* for a given input, the process is only ever called once.
|
|
7
|
+
*
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
export declare class ProcessingCache<In, Out> {
|
|
11
|
+
private transform;
|
|
12
|
+
private results;
|
|
13
|
+
constructor(transform: (orig: In, ...args: Array<any>) => Out);
|
|
14
|
+
has(original: In | undefined): boolean;
|
|
15
|
+
set(original: In, result: Out): Out;
|
|
16
|
+
process(original: In | undefined, ...args: Array<any>): Out | undefined;
|
|
17
|
+
}
|
|
18
|
+
/** adds only if the item is not in the collection already
|
|
19
|
+
*
|
|
20
|
+
* @note While this isn't very efficient, it doesn't disturb the original
|
|
21
|
+
* collection, so you won't get inadvertent side effects from using Set, etc.
|
|
22
|
+
*/
|
|
23
|
+
export declare function pushDistinct<T>(targetArray: Array<T>, ...items: Array<T>): Array<T>;
|
|
24
|
+
export declare function modelContainsDerivedModel(model: Model): boolean;
|
|
25
|
+
export declare function isModelReferredInTemplate(template: TemplatedTypeBase, target: Model): boolean;
|
|
26
|
+
export declare function getNameForTemplate(target: Type): string;
|
|
27
|
+
export declare function isNullableType(type: Type): boolean;
|
|
28
|
+
export declare function isSameLiteralTypes(variants: UnionVariant[]): boolean;
|
|
29
|
+
export declare function getDurationFormat(encode: EncodeData): DurationSchema["format"];
|
|
30
|
+
export declare function hasScalarAsBase(type: Scalar, scalarName: IntrinsicScalarName): boolean;
|
|
31
|
+
//# sourceMappingURL=type-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type-utils.d.ts","sourceRoot":"","sources":["../../src/type-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,mBAAmB,EACnB,KAAK,EACL,MAAM,EACN,iBAAiB,EACjB,IAAI,EACJ,YAAY,EAIb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD;;;;;;GAMG;AACH,qBAAa,eA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|
+
/** Acts as a cache for processing inputs.
|
|
3
|
+
*
|
|
4
|
+
* If the input is undefined, the output is always undefined.
|
|
5
|
+
* for a given input, the process is only ever called once.
|
|
6
|
+
*
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
export class ProcessingCache {
|
|
10
|
+
constructor(transform) {
|
|
11
|
+
this.transform = transform;
|
|
12
|
+
this.results = new Map();
|
|
13
|
+
}
|
|
14
|
+
has(original) {
|
|
15
|
+
return !!original && !!this.results.get(original);
|
|
16
|
+
}
|
|
17
|
+
set(original, result) {
|
|
18
|
+
this.results.set(original, result);
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
process(original, ...args) {
|
|
22
|
+
if (original) {
|
|
23
|
+
const result = this.results.get(original) || this.transform(original, ...args);
|
|
24
|
+
this.results.set(original, result);
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** adds only if the item is not in the collection already
|
|
31
|
+
*
|
|
32
|
+
* @note While this isn't very efficient, it doesn't disturb the original
|
|
33
|
+
* collection, so you won't get inadvertent side effects from using Set, etc.
|
|
34
|
+
*/
|
|
35
|
+
export function pushDistinct(targetArray, ...items) {
|
|
36
|
+
for (const i of items) {
|
|
37
|
+
if (!targetArray.includes(i)) {
|
|
38
|
+
targetArray.push(i);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return targetArray;
|
|
42
|
+
}
|
|
43
|
+
export function modelContainsDerivedModel(model) {
|
|
44
|
+
return !isTemplateDeclaration(model) && !(isTemplateInstance(model) && model.derivedModels.length === 0);
|
|
45
|
+
}
|
|
46
|
+
export function isModelReferredInTemplate(template, target) {
|
|
47
|
+
var _a, _b, _c;
|
|
48
|
+
return (template === target ||
|
|
49
|
+
((_c = (_b = (_a = template === null || template === void 0 ? void 0 : template.templateMapper) === null || _a === void 0 ? void 0 : _a.args) === null || _b === void 0 ? void 0 : _b.some((it) => it.kind === "Model" || it.kind === "Union" ? isModelReferredInTemplate(it, target) : false)) !== null && _c !== void 0 ? _c : false));
|
|
50
|
+
}
|
|
51
|
+
export function getNameForTemplate(target) {
|
|
52
|
+
switch (target.kind) {
|
|
53
|
+
case "Model": {
|
|
54
|
+
let name = target.name;
|
|
55
|
+
if (target.templateMapper && target.templateMapper.args) {
|
|
56
|
+
name = name + target.templateMapper.args.map((it) => getNameForTemplate(it)).join("");
|
|
57
|
+
}
|
|
58
|
+
return name;
|
|
59
|
+
}
|
|
60
|
+
case "String":
|
|
61
|
+
return target.value;
|
|
62
|
+
default:
|
|
63
|
+
return "";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export function isNullableType(type) {
|
|
67
|
+
if (type.kind === "Union") {
|
|
68
|
+
const nullVariants = Array.from(type.variants.values()).filter((it) => isNullType(it.type));
|
|
69
|
+
return nullVariants.length >= 1;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export function isSameLiteralTypes(variants) {
|
|
76
|
+
const kindSet = new Set(variants.map((it) => it.type.kind));
|
|
77
|
+
if (kindSet.size === 1) {
|
|
78
|
+
const kind = kindSet.values().next().value;
|
|
79
|
+
return kind === "String" || kind === "Number" || kind === "Boolean";
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export function getDurationFormat(encode) {
|
|
86
|
+
let format = "duration-rfc3339";
|
|
87
|
+
// duration encoded as seconds
|
|
88
|
+
if (encode.encoding === "seconds") {
|
|
89
|
+
const scalarName = encode.type.name;
|
|
90
|
+
if (scalarName.startsWith("int") || scalarName.startsWith("uint") || scalarName === "safeint") {
|
|
91
|
+
format = "seconds-integer";
|
|
92
|
+
}
|
|
93
|
+
else if (scalarName.startsWith("float")) {
|
|
94
|
+
format = "seconds-number";
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
throw new Error(`Unrecognized scalar type used by duration encoded as seconds: '${scalarName}'.`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return format;
|
|
101
|
+
}
|
|
102
|
+
export function hasScalarAsBase(type, scalarName) {
|
|
103
|
+
let scalarType = type;
|
|
104
|
+
while (scalarType) {
|
|
105
|
+
if (scalarType.name === scalarName) {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
scalarType = scalarType.baseScalar;
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=type-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type-utils.js","sourceRoot":"","sources":["../../src/type-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,UAAU,EACV,qBAAqB,EACrB,kBAAkB,GACnB,MAAM,oBAAoB,CAAC;AAG5B;;;;;;GAMG;AACH,MAAM,OAAO,eAAe;IAE1B,YAAoB,SAAiD;QAAjD,cAAS,GAAT,SAAS,CAAwC;QAD7D,YAAO,GAAG,IAAI,GAAG,EAAW,CAAC;IACmC,CAAC;IACzE,GAAG,CAAC,QAAwB;QAC1B,OAAO,CAAC,CAAC,QAAQ,IAAI,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACpD,CAAC;IACD,GAAG,CAAC,QAAY,EAAE,MAAW;QAC3B,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QACnC,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,OAAO,CAAC,QAAwB,EAAE,GAAG,IAAgB;QACnD,IAAI,QAAQ,EAAE;YACZ,MAAM,MAAM,GAAQ,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,GAAG,IAAI,CAAC,CAAC;YACpF,IAAI,CAAC,OAAO,CAAC,GAAG,C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,MAAM,CAAC,QAAQ,KAAK,SAAS,EAAE;QACjC,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;QACpC,IAAI,UAAU,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,UAAU,KAAK,SAAS,EAAE;YAC7F,MAAM,GAAG,iBAAiB,CAAC;SAC5B;aAAM,IAAI,UAAU,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE;YACzC,MAAM,GAAG,gBAAgB,CAAC;SAC3B;aAAM;YACL,MAAM,IAAI,KAAK,CAAC,kEAAkE,UAAU,IAAI,CAAC,CAAC;SACnG;KACF;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,IAAY,EAAE,UAA+B;IAC3E,IAAI,UAAU,GAAuB,IAAI,CAAC;IAC1C,OAAO,UAAU,EAAE;QACjB,IAAI,UAAU,CAAC,IAAI,KAAK,UAAU,EAAE;YAClC,OAAO,IAAI,CAAC;SACb;QACD,UAAU,GAAG,UAAU,CAAC,UAAU,CAAC;KACpC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/dist/src/utils.d.ts
CHANGED
|
@@ -1,49 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { HttpOperation } from "@typespec/http";
|
|
3
|
-
import { ApiVersions } from "@autorest/codemodel";
|
|
4
|
-
import { LroMetadata } from "@azure-tools/typespec-azure-core";
|
|
5
|
-
import { Client as CodeModelClient, ServiceVersion } from "./common/client.js";
|
|
6
|
-
import { CodeModel } from "./common/code-model.js";
|
|
7
|
-
import { EmitterOptions } from "./emitter.js";
|
|
8
|
-
import { DurationSchema } from "./common/schemas/time.js";
|
|
9
|
-
export declare const specialHeaderNames: Set<string>;
|
|
10
|
-
export declare const originApiVersion = "modelerfour:synthesized/api-version";
|
|
11
|
-
/** Acts as a cache for processing inputs.
|
|
12
|
-
*
|
|
13
|
-
* If the input is undefined, the output is always undefined.
|
|
14
|
-
* for a given input, the process is only ever called once.
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*/
|
|
18
|
-
export declare class ProcessingCache<In, Out> {
|
|
19
|
-
private transform;
|
|
20
|
-
private results;
|
|
21
|
-
constructor(transform: (orig: In, ...args: Array<any>) => Out);
|
|
22
|
-
has(original: In | undefined): boolean;
|
|
23
|
-
set(original: In, result: Out): Out;
|
|
24
|
-
process(original: In | undefined, ...args: Array<any>): Out | undefined;
|
|
25
|
-
}
|
|
26
|
-
/** adds only if the item is not in the collection already
|
|
27
|
-
*
|
|
28
|
-
* @note While this isn't very efficient, it doesn't disturb the original
|
|
29
|
-
* collection, so you won't get inadvertent side effects from using Set, etc.
|
|
30
|
-
*/
|
|
31
|
-
export declare function pushDistinct<T>(targetArray: Array<T>, ...items: Array<T>): Array<T>;
|
|
1
|
+
import { Enum, Model, Operation, Program, Union } from "@typespec/compiler";
|
|
32
2
|
export declare function logWarning(program: Program, msg: string): void;
|
|
33
|
-
export declare function
|
|
3
|
+
export declare function trace(program: Program, msg: string): void;
|
|
4
|
+
export declare function pascalCase(name: string): string;
|
|
34
5
|
export declare function getNamespace(type: Model | Enum | Union | Operation): string | undefined;
|
|
35
6
|
export declare function getJavaNamespace(namespace: string | undefined): string | undefined;
|
|
36
|
-
export declare function modelContainsDerivedModel(model: Model): boolean;
|
|
37
|
-
export declare function isModelReferredInTemplate(template: TemplatedTypeBase, target: Model): boolean;
|
|
38
|
-
export declare function getNameForTemplate(target: Type): string;
|
|
39
7
|
export declare function stringArrayContainsIgnoreCase(stringList: string[], str: string): boolean;
|
|
40
|
-
export declare function operationContainsJsonMergePatch(op: HttpOperation): boolean;
|
|
41
|
-
export declare function pascalCase(name: string): string;
|
|
42
|
-
export declare function isPayloadProperty(program: Program, property: ModelProperty): boolean;
|
|
43
|
-
export declare function getClientApiVersions(client: CodeModelClient): ApiVersions | undefined;
|
|
44
|
-
export declare function getServiceVersion(client: CodeModelClient | CodeModel): ServiceVersion;
|
|
45
|
-
export declare function isLroMetadataSupported(operation: Operation, lroMetadata: LroMetadata): boolean;
|
|
46
|
-
export declare function isLroNewPollingStrategy(operation: Operation, lroMetadata: LroMetadata): boolean;
|
|
47
|
-
export declare function getDurationFormat(encode: EncodeData): DurationSchema["format"];
|
|
48
|
-
export declare function hasScalarAsBase(type: Scalar, scalarName: IntrinsicScalarName): boolean;
|
|
49
8
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/src/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,KAAK,EAAY,SAAS,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAEtF,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,QAQvD;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,QAElD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAM/C;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,KAAK,GAAG,IAAI,GAAG,KAAK,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAQvF;AAED,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAElF;AAED,wBAAgB,6BAA6B,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAExF"}
|
package/dist/src/utils.js
CHANGED
|
@@ -1,57 +1,6 @@
|
|
|
1
|
-
import { NoTarget
|
|
2
|
-
import { getHeaderFieldName, getQueryParamName, getPathParamName, isStatusCode, getAllHttpServices, } from "@typespec/http";
|
|
3
|
-
import { resolveOperationId } from "@typespec/openapi";
|
|
4
|
-
import { ServiceVersion } from "./common/client.js";
|
|
5
|
-
import { getVersion } from "@typespec/versioning";
|
|
6
|
-
export const specialHeaderNames = new Set([
|
|
7
|
-
"repeatability-request-id",
|
|
8
|
-
"repeatability-first-sent",
|
|
9
|
-
"x-ms-client-request-id",
|
|
10
|
-
]);
|
|
11
|
-
export const originApiVersion = "modelerfour:synthesized/api-version";
|
|
12
|
-
/** Acts as a cache for processing inputs.
|
|
13
|
-
*
|
|
14
|
-
* If the input is undefined, the output is always undefined.
|
|
15
|
-
* for a given input, the process is only ever called once.
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*/
|
|
19
|
-
export class ProcessingCache {
|
|
20
|
-
constructor(transform) {
|
|
21
|
-
this.transform = transform;
|
|
22
|
-
this.results = new Map();
|
|
23
|
-
}
|
|
24
|
-
has(original) {
|
|
25
|
-
return !!original && !!this.results.get(original);
|
|
26
|
-
}
|
|
27
|
-
set(original, result) {
|
|
28
|
-
this.results.set(original, result);
|
|
29
|
-
return result;
|
|
30
|
-
}
|
|
31
|
-
process(original, ...args) {
|
|
32
|
-
if (original) {
|
|
33
|
-
const result = this.results.get(original) || this.transform(original, ...args);
|
|
34
|
-
this.results.set(original, result);
|
|
35
|
-
return result;
|
|
36
|
-
}
|
|
37
|
-
return undefined;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
/** adds only if the item is not in the collection already
|
|
41
|
-
*
|
|
42
|
-
* @note While this isn't very efficient, it doesn't disturb the original
|
|
43
|
-
* collection, so you won't get inadvertent side effects from using Set, etc.
|
|
44
|
-
*/
|
|
45
|
-
export function pushDistinct(targetArray, ...items) {
|
|
46
|
-
for (const i of items) {
|
|
47
|
-
if (!targetArray.includes(i)) {
|
|
48
|
-
targetArray.push(i);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return targetArray;
|
|
52
|
-
}
|
|
1
|
+
import { NoTarget } from "@typespec/compiler";
|
|
53
2
|
export function logWarning(program, msg) {
|
|
54
|
-
|
|
3
|
+
trace(program, msg);
|
|
55
4
|
program.reportDiagnostic({
|
|
56
5
|
code: "typespec-java",
|
|
57
6
|
severity: "warning",
|
|
@@ -59,62 +8,16 @@ export function logWarning(program, msg) {
|
|
|
59
8
|
target: NoTarget,
|
|
60
9
|
});
|
|
61
10
|
}
|
|
62
|
-
export
|
|
63
|
-
|
|
64
|
-
if (options["examples-directory"]) {
|
|
65
|
-
const operationIdExamplesMap = new Map();
|
|
66
|
-
const service = ignoreDiagnostics(getAllHttpServices(program))[0];
|
|
67
|
-
let version = undefined;
|
|
68
|
-
const versioning = getVersion(program, service.namespace);
|
|
69
|
-
if (versioning && versioning.getVersions()) {
|
|
70
|
-
const versions = versioning.getVersions();
|
|
71
|
-
version = versions[versions.length - 1].value;
|
|
72
|
-
}
|
|
73
|
-
const exampleDir = version
|
|
74
|
-
? resolvePath(options["examples-directory"], version)
|
|
75
|
-
: resolvePath(options["examples-directory"]);
|
|
76
|
-
try {
|
|
77
|
-
if (!(await program.host.stat(exampleDir)).isDirectory())
|
|
78
|
-
return operationExamplesMap;
|
|
79
|
-
}
|
|
80
|
-
catch (err) {
|
|
81
|
-
logWarning(program, `Examples directory '${exampleDir}' does not exist.`);
|
|
82
|
-
return operationExamplesMap;
|
|
83
|
-
}
|
|
84
|
-
const exampleFiles = await program.host.readDir(exampleDir);
|
|
85
|
-
for (const fileName of exampleFiles) {
|
|
86
|
-
try {
|
|
87
|
-
const exampleFile = await program.host.readFile(resolvePath(exampleDir, fileName));
|
|
88
|
-
const example = JSON.parse(exampleFile.text);
|
|
89
|
-
if (!example.operationId) {
|
|
90
|
-
logWarning(program, `Example file '${fileName}' is missing operationId.`);
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
if (!operationIdExamplesMap.has(example.operationId)) {
|
|
94
|
-
operationIdExamplesMap.set(example.operationId, example);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
catch (err) {
|
|
98
|
-
logWarning(program, `Failed to load example file '${fileName}'.`);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
if (operationIdExamplesMap.size > 0) {
|
|
102
|
-
const routes = service.operations;
|
|
103
|
-
routes.forEach((it) => {
|
|
104
|
-
const operationId = pascalCaseForOperationId(resolveOperationId(program, it.operation));
|
|
105
|
-
if (operationIdExamplesMap.has(operationId)) {
|
|
106
|
-
operationExamplesMap.set(it.operation, operationIdExamplesMap.get(operationId));
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return operationExamplesMap;
|
|
11
|
+
export function trace(program, msg) {
|
|
12
|
+
program.trace("typespec-java", msg);
|
|
112
13
|
}
|
|
113
|
-
function
|
|
114
|
-
|
|
115
|
-
.
|
|
116
|
-
|
|
117
|
-
|
|
14
|
+
export function pascalCase(name) {
|
|
15
|
+
if (name.length > 0) {
|
|
16
|
+
return name[0].toUpperCase() + name.slice(1);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return name;
|
|
20
|
+
}
|
|
118
21
|
}
|
|
119
22
|
export function getNamespace(type) {
|
|
120
23
|
let namespaceRef = type.namespace;
|
|
@@ -128,145 +31,7 @@ export function getNamespace(type) {
|
|
|
128
31
|
export function getJavaNamespace(namespace) {
|
|
129
32
|
return namespace ? "com." + namespace.toLowerCase() : undefined;
|
|
130
33
|
}
|
|
131
|
-
export function modelContainsDerivedModel(model) {
|
|
132
|
-
return !isTemplateDeclaration(model) && !(isTemplateInstance(model) && model.derivedModels.length === 0);
|
|
133
|
-
}
|
|
134
|
-
export function isModelReferredInTemplate(template, target) {
|
|
135
|
-
var _a, _b, _c;
|
|
136
|
-
return (template === target ||
|
|
137
|
-
((_c = (_b = (_a = template === null || template === void 0 ? void 0 : template.templateMapper) === null || _a === void 0 ? void 0 : _a.args) === null || _b === void 0 ? void 0 : _b.some((it) => it.kind === "Model" || it.kind === "Union" ? isModelReferredInTemplate(it, target) : false)) !== null && _c !== void 0 ? _c : false));
|
|
138
|
-
}
|
|
139
|
-
export function getNameForTemplate(target) {
|
|
140
|
-
switch (target.kind) {
|
|
141
|
-
case "Model": {
|
|
142
|
-
let name = target.name;
|
|
143
|
-
if (target.templateMapper && target.templateMapper.args) {
|
|
144
|
-
name = name + target.templateMapper.args.map((it) => getNameForTemplate(it)).join("");
|
|
145
|
-
}
|
|
146
|
-
return name;
|
|
147
|
-
}
|
|
148
|
-
case "String":
|
|
149
|
-
return target.value;
|
|
150
|
-
default:
|
|
151
|
-
return "";
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
34
|
export function stringArrayContainsIgnoreCase(stringList, str) {
|
|
155
35
|
return stringList && str ? stringList.findIndex((s) => s.toLowerCase() === str.toLowerCase()) != -1 : false;
|
|
156
36
|
}
|
|
157
|
-
export function operationContainsJsonMergePatch(op) {
|
|
158
|
-
for (const param of op.parameters.parameters) {
|
|
159
|
-
if (param.type === "header" && param.name.toLowerCase() === "content-type") {
|
|
160
|
-
if (param.param.type.kind === "String" && param.param.type.value === "application/merge-patch+json") {
|
|
161
|
-
return true;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return false;
|
|
166
|
-
}
|
|
167
|
-
export function pascalCase(name) {
|
|
168
|
-
if (name.length > 0) {
|
|
169
|
-
return name[0].toUpperCase() + name.slice(1);
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
return name;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
export function isPayloadProperty(program, property) {
|
|
176
|
-
const headerInfo = getHeaderFieldName(program, property);
|
|
177
|
-
const queryInfo = getQueryParamName(program, property);
|
|
178
|
-
const pathInfo = getPathParamName(program, property);
|
|
179
|
-
const statusCodeInfo = isStatusCode(program, property);
|
|
180
|
-
return !(headerInfo || queryInfo || pathInfo || statusCodeInfo);
|
|
181
|
-
}
|
|
182
|
-
export function getClientApiVersions(client) {
|
|
183
|
-
var _a;
|
|
184
|
-
if ((_a = client.globalParameters) === null || _a === void 0 ? void 0 : _a.find((it) => it.origin === originApiVersion)) {
|
|
185
|
-
return client.apiVersions;
|
|
186
|
-
}
|
|
187
|
-
else {
|
|
188
|
-
return undefined;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
export function getServiceVersion(client) {
|
|
192
|
-
let name = client.language.default.name;
|
|
193
|
-
let description = name;
|
|
194
|
-
if (name.endsWith("Client")) {
|
|
195
|
-
name = name.substring(0, name.length - "Client".length);
|
|
196
|
-
}
|
|
197
|
-
else {
|
|
198
|
-
description = description + "Client";
|
|
199
|
-
}
|
|
200
|
-
if (name.endsWith("Service")) {
|
|
201
|
-
name = name + "Version";
|
|
202
|
-
}
|
|
203
|
-
else {
|
|
204
|
-
name = name + "ServiceVersion";
|
|
205
|
-
}
|
|
206
|
-
return new ServiceVersion(name, description);
|
|
207
|
-
}
|
|
208
|
-
export function isLroMetadataSupported(operation, lroMetadata) {
|
|
209
|
-
const azureCoreLroSvs = [
|
|
210
|
-
"LongRunningResourceCreateOrReplace",
|
|
211
|
-
"LongRunningResourceCreateOrUpdate",
|
|
212
|
-
"LongRunningResourceDelete",
|
|
213
|
-
"LongRunningResourceAction",
|
|
214
|
-
];
|
|
215
|
-
let ret = false;
|
|
216
|
-
if (lroMetadata.statusMonitorStep &&
|
|
217
|
-
lroMetadata.statusMonitorStep.responseModel.name === "OperationStatus" &&
|
|
218
|
-
getNamespace(lroMetadata.statusMonitorStep.responseModel) === "Azure.Core.Foundations") {
|
|
219
|
-
if (operation.node.signature.kind === SyntaxKind.OperationSignatureReference) {
|
|
220
|
-
if (operation.node.signature.baseOperation.target.kind === SyntaxKind.MemberExpression) {
|
|
221
|
-
const sv = operation.node.signature.baseOperation.target.id.sv;
|
|
222
|
-
ret = azureCoreLroSvs.includes(sv);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
return ret;
|
|
227
|
-
}
|
|
228
|
-
export function isLroNewPollingStrategy(operation, lroMetadata) {
|
|
229
|
-
// at present, it is same as isLroMetadataSupported, which checks if operation uses template from Azure.Core
|
|
230
|
-
// will change later when isLroMetadataSupported extends to other types of operations
|
|
231
|
-
return true;
|
|
232
|
-
// if (verb === "put" && !lroMetadata.finalStep) {
|
|
233
|
-
// // PUT without last GET on resource
|
|
234
|
-
// useNewPollStrategy = true;
|
|
235
|
-
// } else if (
|
|
236
|
-
// verb === "post" &&
|
|
237
|
-
// lroMetadata.finalStep &&
|
|
238
|
-
// lroMetadata.finalStep.kind === "pollingSuccessProperty" &&
|
|
239
|
-
// lroMetadata.finalStep.target.name === "result"
|
|
240
|
-
// ) {
|
|
241
|
-
// // POST with final result in "result" property
|
|
242
|
-
// useNewPollStrategy = true;
|
|
243
|
-
// }
|
|
244
|
-
}
|
|
245
|
-
export function getDurationFormat(encode) {
|
|
246
|
-
let format = "duration-rfc3339";
|
|
247
|
-
// duration encoded as seconds
|
|
248
|
-
if (encode.encoding === "seconds") {
|
|
249
|
-
const scalarName = encode.type.name;
|
|
250
|
-
if (scalarName.startsWith("int") || scalarName.startsWith("uint") || scalarName === "safeint") {
|
|
251
|
-
format = "seconds-integer";
|
|
252
|
-
}
|
|
253
|
-
else if (scalarName.startsWith("float")) {
|
|
254
|
-
format = "seconds-number";
|
|
255
|
-
}
|
|
256
|
-
else {
|
|
257
|
-
throw new Error(`Unrecognized scalar type used by duration encoded as seconds: '${scalarName}'.`);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
return format;
|
|
261
|
-
}
|
|
262
|
-
export function hasScalarAsBase(type, scalarName) {
|
|
263
|
-
let scalarType = type;
|
|
264
|
-
while (scalarType) {
|
|
265
|
-
if (scalarType.name === scalarName) {
|
|
266
|
-
return true;
|
|
267
|
-
}
|
|
268
|
-
scalarType = scalarType.baseScalar;
|
|
269
|
-
}
|
|
270
|
-
return false;
|
|
271
|
-
}
|
|
272
37
|
//# sourceMappingURL=utils.js.map
|