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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,9 @@ function State(options) { | |
|
||
this.tag = null; | ||
this.result = ''; | ||
|
||
this.duplicates = []; | ||
this.usedDuplicates = null; | ||
} | ||
|
||
|
||
|
@@ -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; | ||
} | ||
} 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); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.