@azure-tools/codegen
Version:
Autorest Code generator common and base classes
108 lines • 3.01 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.serialize = exports.deserialize = void 0;
const js_yaml_1 = require("js-yaml");
const yaml_dump_1 = require("./yaml-dump");
const propertyPriority = [
"info",
"$key",
"booleans",
"numbers",
"chars",
"strings",
"choices",
"sealedChoices",
"constants",
"dictionaries",
"compounds",
"name",
"schemas",
"type",
"format",
"schema",
"operationId",
"path",
"method",
"description",
"default",
];
const propertyNegativePriority = [
"objects",
"arrays",
"request",
"responses",
"exceptions",
"callbacks",
"http",
"commands",
"operations",
"extensions",
"details",
"language",
"protocol",
];
function sortWithPriorty(a, b) {
if (a == b) {
return 0;
}
const ia = propertyPriority.indexOf(a);
const ib = propertyPriority.indexOf(b);
const na = propertyNegativePriority.indexOf(a);
const nb = propertyNegativePriority.indexOf(b);
const dota = `${a}`.startsWith(".");
const dotb = `${b}`.startsWith(".");
if (dota) {
if (!dotb) {
return 1;
}
}
else {
if (dotb) {
return -1;
}
}
if (na > -1) {
if (nb > -1) {
return na - nb;
}
return 1;
}
if (nb > -1) {
return -1;
}
if (ia != -1) {
return ib != -1 ? ia - ib : -1;
}
return ib != -1 || a > b ? 1 : a < b ? -1 : 0;
}
function deserialize(text, filename, schema = js_yaml_1.DEFAULT_SCHEMA) {
return (0, js_yaml_1.load)(text, {
schema,
filename,
});
}
exports.deserialize = deserialize;
function serialize(model, schemaOrOptions = js_yaml_1.DEFAULT_SCHEMA) {
const options = schemaOrOptions instanceof js_yaml_1.Schema
? { schema: schemaOrOptions, sortKeys: true }
: { schema: js_yaml_1.DEFAULT_SCHEMA, sortKeys: true, ...schemaOrOptions };
return (0, yaml_dump_1.dump)(model, {
schema: options.schema,
sortKeys: options.sortKeys && sortWithPriorty,
skipInvalid: true,
lineWidth: 240,
});
// .replace(/\s*\w*: {}/g, '')
// .replace(/\s*\w*: \[\]/g, '')
// .replace(/(\s*- \$key:)/g, '\n$1')
// .replace(/-\n\s+version/g, '- version');
// .replace(/(\s*)(language:)/g, '\n$1## ----------------------------------------------------------------------$1$2')
// replace(/([^:]\n)(\s*-)/g, '$1\n$2')
//.replace(/(\s*language:)/g, '\n$1');
}
exports.serialize = serialize;
//# sourceMappingURL=yaml.js.map