@microsoft.azure/autorest-core
Advanced tools
Comparing version 2.0.4194 to 2.0.4196
@@ -94,2 +94,10 @@ "use strict"; | ||
} | ||
// important for anchors! | ||
const memoize = (factory) => cache => { | ||
if (cache.has(yamlNode)) | ||
return cache.get(yamlNode); | ||
const result = factory(cache, o => cache.set(yamlNode, o)); | ||
cache.set(yamlNode, result); | ||
return result; | ||
}; | ||
switch (yamlNode.kind) { | ||
@@ -99,4 +107,4 @@ case exports.Kind.SCALAR: { | ||
return yamlNode.valueFunc = yamlNodeScalar.valueObject !== undefined | ||
? () => yamlNodeScalar.valueObject | ||
: () => yamlNodeScalar.value; | ||
? memoize(() => yamlNodeScalar.valueObject) | ||
: memoize(() => yamlNodeScalar.value); | ||
} | ||
@@ -108,4 +116,5 @@ case exports.Kind.MAPPING: | ||
const yamlNodeMapping = yamlNode; | ||
return yamlNode.valueFunc = () => { | ||
return yamlNode.valueFunc = memoize((cache, set) => { | ||
const result = stable_object_1.NewEmptyObject(); | ||
set(result); | ||
for (const mapping of yamlNodeMapping.mappings) { | ||
@@ -119,11 +128,18 @@ if (mapping.key.kind !== exports.Kind.SCALAR) { | ||
else { | ||
result[mapping.key.value] = ParseNodeInternal(yamlRootNode, mapping.value, onError)(); | ||
result[mapping.key.value] = ParseNodeInternal(yamlRootNode, mapping.value, onError)(cache); | ||
} | ||
} | ||
return result; | ||
}; | ||
}); | ||
} | ||
case exports.Kind.SEQ: { | ||
const yamlNodeSequence = yamlNode; | ||
return yamlNode.valueFunc = () => yamlNodeSequence.items.map(item => ParseNodeInternal(yamlRootNode, item, onError)()); | ||
return yamlNode.valueFunc = memoize((cache, set) => { | ||
const result = []; | ||
set(result); | ||
for (const item of yamlNodeSequence.items) { | ||
result.push(ParseNodeInternal(yamlRootNode, item, onError)(cache)); | ||
} | ||
return result; | ||
}); | ||
} | ||
@@ -143,3 +159,3 @@ case exports.Kind.ANCHOR_REF: { | ||
ParseNodeInternal(yamlNode, yamlNode, onError); | ||
return yamlNode.valueFunc(); | ||
return yamlNode.valueFunc(new WeakMap()); | ||
} | ||
@@ -167,2 +183,3 @@ exports.ParseNode = ParseNode; | ||
function Normalize(object) { | ||
const seen = new WeakSet(); | ||
const clone = Clone(object); | ||
@@ -174,2 +191,6 @@ const norm = (o) => { | ||
else if (o && typeof o == "object") { | ||
if (seen.has(o)) { | ||
return; | ||
} | ||
seen.add(o); | ||
const keys = Object.keys(o).sort(); | ||
@@ -204,10 +225,27 @@ const oo = Object.assign({}, o); | ||
function FastStringify(obj) { | ||
try { | ||
return JSON.stringify(obj, null, 1); | ||
// has duplicate objects? | ||
const seen = new WeakSet(); | ||
const losslessJsonSerializable = (o) => { | ||
if (o && typeof o == "object") { | ||
if (seen.has(o)) | ||
return false; | ||
seen.add(o); | ||
} | ||
if (Array.isArray(o)) { | ||
return o.every(losslessJsonSerializable); | ||
} | ||
else if (o && typeof o == "object") { | ||
return Object.values(o).every(losslessJsonSerializable); | ||
} | ||
return true; | ||
}; | ||
if (losslessJsonSerializable(obj)) { | ||
try { | ||
return JSON.stringify(obj, null, 1); | ||
} | ||
catch (_a) { } | ||
} | ||
catch (e) { | ||
return Stringify(obj); | ||
} | ||
return Stringify(obj); | ||
} | ||
exports.FastStringify = FastStringify; | ||
//# sourceMappingURL=yaml.js.map |
{ | ||
"name": "@microsoft.azure/autorest-core", | ||
"version": "2.0.4194", | ||
"version": "2.0.4196", | ||
"description": "AutoRest core module", | ||
@@ -5,0 +5,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
50745516
7489