Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular data & cross references #110 #148

Merged
merged 1 commit into from Oct 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
115 changes: 96 additions & 19 deletions lib/js-yaml/dumper.js
Expand Up @@ -126,6 +126,9 @@ function State(options) {

this.tag = null;
this.result = '';

this.duplicates = [];
this.usedDuplicates = null;
}


Expand Down Expand Up @@ -426,40 +429,114 @@ function writeNode(state, level, object, block, compact) {
compact = false;
}

if ('[object Object]' === type) {
if (block && (0 !== Object.keys(state.dump).length)) {
writeBlockMapping(state, level, state.dump, compact);
} else {
writeFlowMapping(state, level, state.dump);
var objectOrArray = '[object Object]' === type || '[object Array]' === type,
duplicateIndex,
duplicate;

if (objectOrArray) {
duplicateIndex = state.duplicates.indexOf(object);
duplicate = duplicateIndex !== -1;
}

if (duplicate && state.usedDuplicates[duplicateIndex]) {
state.dump = '*ref_' + duplicateIndex;
} else {
if (objectOrArray && duplicate && !state.usedDuplicates[duplicateIndex]) {
state.usedDuplicates[duplicateIndex] = true;
}
} else if ('[object Array]' === type) {
if (block && (0 !== state.dump.length)) {
writeBlockSequence(state, level, state.dump, compact);
if ('[object Object]' === type) {
if (block && (0 !== Object.keys(state.dump).length)) {
writeBlockMapping(state, level, state.dump, compact);
if (duplicate) {
state.dump = '&ref_' + duplicateIndex + (0 === level ? '\n' : '') + state.dump;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... I doubt it, but okay. Just use 0 === level please. ! is for boolean expressions only.

} else {
writeFlowMapping(state, level, state.dump);
if (duplicate) {
state.dump = '&ref_' + duplicateIndex + ' ' + state.dump;
}
}
} else if ('[object Array]' === type) {
if (block && (0 !== state.dump.length)) {
writeBlockSequence(state, level, state.dump, compact);
if (duplicate) {
state.dump = '&ref_' + duplicateIndex + (0 === level ? '\n' : '') + state.dump;
}
} else {
writeFlowSequence(state, level, state.dump);
if (duplicate) {
state.dump = '&ref_' + duplicateIndex + ' ' + state.dump;
}
}
} else if ('[object String]' === type) {
if ('?' !== state.tag) {
writeScalar(state, state.dump);
}
} else if (state.skipInvalid) {
return false;
} else {
writeFlowSequence(state, level, state.dump);
throw new YAMLException('unacceptable kind of an object to dump ' + type);
}
} else if ('[object String]' === type) {
if ('?' !== state.tag) {
writeScalar(state, state.dump);

if (null !== state.tag && '?' !== state.tag) {
state.dump = '!<' + state.tag + '> ' + state.dump;
}
} else if (state.skipInvalid) {
return false;
} else {
throw new YAMLException('unacceptable kind of an object to dump ' + type);
}

if (null !== state.tag && '?' !== state.tag) {
state.dump = '!<' + state.tag + '> ' + state.dump;
}
return true;
}

function getDuplicateReferences(object, state) {
var objects = [],
duplicatesIndexes = [],
index,
length;

inspectNode(object, objects, duplicatesIndexes);

for (index = 0, length = duplicatesIndexes.length; index < length; index += 1) {
state.duplicates.push(objects[duplicatesIndexes[index]]);
}
state.usedDuplicates = new Array(length);
}

function inspectNode(object, objects, duplicatesIndexes) {
var type = _toString.call(object),
objectKeyList,
index,
length;

if (null !== object && 'object' === typeof object) {
index = objects.indexOf(object);
if (-1 !== index) {
if (-1 === duplicatesIndexes.indexOf(index)) {
duplicatesIndexes.push(index);
}
} else {
objects.push(object);

if(Array.isArray(object)) {
for (index = 0, length = object.length; index < length; index += 1) {
inspectNode(object[index], objects, duplicatesIndexes);
}
} else {
objectKeyList = Object.keys(object);

for (index = 0, length = objectKeyList.length; index < length; index += 1) {
inspectNode(object[objectKeyList[index]], objects, duplicatesIndexes);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if you replace this with:

if (Array.isArray(object)) {
  // use object.length to iterate over the array
} else {
  // your current code using Object.keys
}

}
}
}

function dump(input, options) {
options = options || {};

var state = new State(options);

getDuplicateReferences(input, state);

if (writeNode(state, 0, input, true, true)) {
return state.dump + '\n';
} else {
Expand Down
28 changes: 28 additions & 0 deletions test/issues/0110.js
@@ -0,0 +1,28 @@
'use strict';


var assert = require('assert');
var yaml = require('../../');


test('Circular and cross references', function () {
var source = {
a: {a: 1},
b: [1, 2],
c: {},
d: []
};
source.crossObject = source.a;
source.crossArray = source.b;
source.c.circularObject = source;
source.d.push(source.d);
source.d.push(source);

var obtained = yaml.load(yaml.dump(source));

assert.equal(obtained.crossObject, obtained.a);
assert.equal(obtained.crossArray, obtained.b);
assert.equal(obtained.c.circularObject, obtained);
assert.equal(obtained.d[0], obtained.d);
assert.equal(obtained.d[1], obtained);
});