Skip to content

Commit

Permalink
Made constructor return something
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Oct 30, 2011
1 parent 2480b67 commit c40dd37
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
60 changes: 48 additions & 12 deletions lib/js-yaml/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ JS.require('JS.Class');
JS.require('JS.Hash');


var __ = require('./core').import('types', 'error', 'nodes'),
var $$ = require('./core'),
__ = $$.import('types', 'error', 'nodes'),
debug = require('./debug');


Expand Down Expand Up @@ -156,9 +157,18 @@ var BaseConstructor = exports.BaseConstructor = new JS.Class('BaseConstructor',
node.startMark);
}

mapping = new JS.Hash();
node.forEachPair(function (key_node, value_node) {
var key, value;
mapping = {};

if (debug.ENABLED) {
debug('constructor#constructMapping -- constructing', {value: node.value});
}

$$.each(node.value, function (pair) {
var key_node = pair[0], value_node = pair[1], key, value;

if (debug.ENABLED) {
debug('constructor#constructMapping -- got pair', {pair: pair});
}

key = this.constructObject(key_node, deep);
// TODO: Replace with interface test???
Expand All @@ -167,8 +177,14 @@ var BaseConstructor = exports.BaseConstructor = new JS.Class('BaseConstructor',
"found unhashable key", node.startMark);
}
value = this.constructObject(value_node, deep);
mapping.store(key, value);

if (debug.ENABLED) {
debug('constructor#constructMapping -- constructed', {key: key, value: value});
}

mapping[key] = value;
}, this);

return mapping;
},

Expand Down Expand Up @@ -204,12 +220,18 @@ var SafeConstructor = exports.SafeConstructor = new JS.Class('SafeConstructor',
constructScalar: function (node) {
var result;

if (!node.isA(__.MappingNode)) {
node.value.forEachPair(function (key_node, value_node) {
if (debug.ENABLED) {
debug('constructor#constructScalar', {node: node});
}

if (node.isA(__.MappingNode)) {
$$.each(node.value, function (pair) {
var key_node = pair[0], value_node = pair[1], value;

if ('tag:yaml.org,2002:value' === key_node.tag) {
result = this.constructScalar(value_node);
}
});
}, this);

if (undefined !== result) {
return result;
Expand All @@ -222,9 +244,14 @@ var SafeConstructor = exports.SafeConstructor = new JS.Class('SafeConstructor',
flattenMapping: function (node) {
var merge = [], index = 0;

if (debug.ENABLED) {
debug('constructor#flattenMapping', {value: node.value});
}

while (index < node.value.length) {
// TODO Implement this crazy logic
console.log('skip'); // for lint
// TODO Implement this crazy logic
debug('constructor#flattenMapping', {index: index});
index++;
}

if (!!merge.length) {
Expand Down Expand Up @@ -336,7 +363,7 @@ var SafeConstructor = exports.SafeConstructor = new JS.Class('SafeConstructor',


constructYamlStr: function (node) {
return "STR: Not Implemented Yet";
return this.constructScalar(node);
},


Expand All @@ -346,11 +373,20 @@ var SafeConstructor = exports.SafeConstructor = new JS.Class('SafeConstructor',


constructYamlMap: function (node) {
return "MAP: Not Implemented Yet";
var data = {}, value;
/* yield data */
value = this.constructMapping(node, true);
$$.extend(data, value);
return data; // should be rewritten in a normal manner
},


constructUndefined: function (node) {
if (debug.ENABLED) {
debug('constructor#constructUndefined', {node: node});
throw new Error('WTF');
}

throw new ConstructorError(null, null,
"could not determine constructor for the tag " + node.tag,
node.startMark);
Expand Down
15 changes: 12 additions & 3 deletions lib/js-yaml/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@ $$.import = function import() {
// iterates through all object keys-value pairs calling iterator on each one
// example: $$.each(hash, function (val, key) { /* ... */ });
$$.each = function each(obj, iterator, context) {
var keys = Object.getOwnPropertyNames(obj), i, l;
var keys, i, l;

if (null === obj || undefined === obj) {
return;
}

context = context || iterator;

for (i = 0, l = keys.length; i < l; i++) {
iterator.call(context, obj[keys[i]], keys[i], obj);
if (obj.forEach === Array.prototype.forEach) {
obj.forEach(iterator, context);
} else {
keys = Object.getOwnPropertyNames(obj);
for (i = 0, l = keys.length; i < l; i++) {
iterator.call(context, obj[keys[i]], keys[i], obj);
}
}
};

Expand Down

0 comments on commit c40dd37

Please sign in to comment.