@autorest/codemodel
Version:
AutoRest code model library
66 lines • 2.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeModel = void 0;
const codegen_1 = require("@azure-tools/codegen");
const info_1 = require("./info");
const metadata_1 = require("./metadata");
const operation_1 = require("./operation");
const schemas_1 = require("./schemas");
const security_1 = require("./security");
class CodeModel extends metadata_1.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 ? (0, codegen_1.enableSourceTracking)(this) : this;
$this.info = new info_1.Info(title);
$this.schemas = new schemas_1.Schemas();
$this.operationGroups = [];
$this.security = new security_1.Security(false);
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 operation_1.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"]; });
}
}
}
exports.CodeModel = CodeModel;
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