@azure-tools/typespec-java
Version:
TypeSpec library for emitting Java client from the TypeSpec REST protocol binding
60 lines • 2.44 kB
JavaScript
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
import { Info, Metadata, OperationGroup, Schemas, Security } from "@autorest/codemodel";
import { enableSourceTracking } from "@azure-tools/codegen";
export class CodeModel extends Metadata {
constructor(title, sourceTracking = false, objectInitializer) {
super();
// if we are enabling source tracking, then we have to use a proxied version of this
const $this = sourceTracking ? enableSourceTracking(this) : this;
$this.info = new Info(title);
$this.schemas = new Schemas();
$this.operationGroups = [];
$this.security = new Security(false);
$this.clients = [];
this.applyTo($this, objectInitializer);
}
get globals() {
return this.globalParameters || (this.globalParameters = []);
}
getOperationGroup(group) {
let result = this.operationGroups.find((each) => group.toLowerCase() === each.$key.toLowerCase());
if (!result) {
result = new OperationGroup(group);
this.operationGroups.push(result);
}
return result;
}
findGlobalParameter(predicate) {
return this.globals.find(predicate);
}
addGlobalParameter(predicateOrParameter, create = undefined) {
try {
if (typeof predicateOrParameter !== "function") {
// overload : parameter passed
this.globals.push(predicateOrParameter);
return predicateOrParameter;
}
// overload : predicate, parameter passed
let p = this.findGlobalParameter(predicateOrParameter);
if (!p) {
this.globals.push((p = realize(create)));
}
return p;
}
finally {
this.globalParameters = sortAscendingInvalidLast(this.globals, (each) => { var _a; return (_a = each.extensions) === null || _a === void 0 ? void 0 : _a["x-ms-priority"]; });
}
}
}
function realize(f) {
return f instanceof Function ? f() : f;
}
function sortAscendingInvalidLast(input, accessor) {
return input.sort((a, b) => {
var _a, _b;
const pA = (_a = accessor(a)) !== null && _a !== void 0 ? _a : Number.MAX_VALUE;
const pB = (_b = accessor(b)) !== null && _b !== void 0 ? _b : Number.MAX_VALUE;
return pA - pB;
});
}
//# sourceMappingURL=code-model.js.map