Skip to content

Commit

Permalink
Allow multiple structure includes.
Browse files Browse the repository at this point in the history
  • Loading branch information
rottmann committed Sep 21, 2013
1 parent eb720a0 commit c48ac88
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 75 deletions.
9 changes: 0 additions & 9 deletions lib/plugins/parser_api_define_error_structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,10 @@ function pushTo()
return "global";
} // pushTo

function postParseSender()
{
return {
path: "global",
fields: "defineErrorStructure"
};
} // postParseSender

/**
* Exports.
*/
module.exports = {
parse: parse,
postParseSender: postParseSender,
pushTo: pushTo
};
10 changes: 4 additions & 6 deletions lib/plugins/parser_api_error_structure.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
function parse(content)
{
// Trim
content = content.replace(/^\s+|\s+$/g, "");
name = content.replace(/^\s+|\s+$/g, "");

if(content.length === 0) return null;
return {
errorStructure: content
};
if(name.length === 0) return null;
return name;
} // parse

function pushTo()
{
return "local";
return "local.errorStructure";
} // pushTo

/**
Expand Down
10 changes: 4 additions & 6 deletions lib/plugins/parser_api_structure.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
function parse(content)
{
// Trim
content = content.replace(/^\s+|\s+$/g, "");
name = content.replace(/^\s+|\s+$/g, "");

if(content.length === 0) return null;
return {
structure: content
};
if(name.length === 0) return null;
return name;
} // parse

function pushTo()
{
return "local";
return "local.structure";
} // pushTo

/**
Expand Down
10 changes: 4 additions & 6 deletions lib/plugins/parser_api_success_structure.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
function parse(content)
{
// Trim
content = content.replace(/^\s+|\s+$/g, "");
name = content.replace(/^\s+|\s+$/g, "");

if(content.length === 0) return null;
return {
successStructure: content
};
if(name.length === 0) return null;
return name;
} // parse

function pushTo()
{
return "local";
return "local.successStructure";
} // pushTo

/**
Expand Down
106 changes: 58 additions & 48 deletions lib/plugins/worker_structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ function preProcess(parsedFiles, filenames, structureName, defineStructureName)
structureName = structureName || "structure";
defineStructureName = defineStructureName || "defineStructure";
var result = {};

// console.log(JSON.stringify(parsedFiles, null, 2));

for(var fileIndex = 0; fileIndex < parsedFiles.length; fileIndex += 1)
{
var parsedFile = parsedFiles[fileIndex];
Expand Down Expand Up @@ -53,6 +56,7 @@ function postProcess(parsedFiles, filenames, preProcessResults, structureName, d
{
structureName = structureName || "structure";
defineStructureName = defineStructureName || "defineStructure";

for(var fileIndex = 0; fileIndex < parsedFiles.length; fileIndex += 1)
{
var parsedFile = parsedFiles[fileIndex];
Expand All @@ -61,63 +65,66 @@ function postProcess(parsedFiles, filenames, preProcessResults, structureName, d
var block = parsedFile[blockIndex];
if(block["local"][structureName])
{
var name = block["local"][structureName];
var version = "0.0.0";

if(block.version)
for(var structureIndex = 0; structureIndex < block["local"][structureName].length; structureIndex++)
{
version = block.version;
}
var name = block["local"][structureName][structureIndex];
var version = "0.0.0";

if(preProcessResults[defineStructureName][name])
{
var matchedData = {};
var matchedVersion = version;
if(preProcessResults[defineStructureName][name][version])
if(block.version)
{
// Exact Version
matchedData = preProcessResults[defineStructureName][name][version];
version = block.version;
}
else
{
// Find matching version
var foundIndex = -1;
var lastVersion = "0.0.0";

var versionKeys = Object.keys(preProcessResults[defineStructureName][name]);
for(var versionIndex = 0; versionIndex < versionKeys.length; versionIndex += 1)
if(preProcessResults[defineStructureName][name])
{
var matchedData = {};
var matchedVersion = version;
if(preProcessResults[defineStructureName][name][version])
{
var currentVersion = versionKeys[versionIndex];
if(semver.gte(version, currentVersion) && semver.gte(currentVersion, lastVersion))
// Exact Version
matchedData = preProcessResults[defineStructureName][name][version];
}
else
{
// Find matching version
var foundIndex = -1;
var lastVersion = "0.0.0";

var versionKeys = Object.keys(preProcessResults[defineStructureName][name]);
for(var versionIndex = 0; versionIndex < versionKeys.length; versionIndex += 1)
{
lastVersion = currentVersion;
foundIndex = versionIndex;
}
} // for preIndex
var currentVersion = versionKeys[versionIndex];
if(semver.gte(version, currentVersion) && semver.gte(currentVersion, lastVersion))
{
lastVersion = currentVersion;
foundIndex = versionIndex;
}
} // for preIndex

if(foundIndex === -1)
{
throw new Error("Name \"" + name + "\" in file \"" + filenames[fileIndex] +
"\" block number " + block.index + " has no matching version. " +
"Check if referenced definition block has a higher version number."
);
if(foundIndex === -1)
{
throw new Error("Name \"" + name + "\" in file \"" + filenames[fileIndex] +
"\" block number " + block.index + " has no matching version. " +
"Check if referenced definition block has a higher version number."
);
}
var versionName = versionKeys[foundIndex];
matchedData = preProcessResults[defineStructureName][name][versionName];
}
var versionName = versionKeys[foundIndex];
matchedData = preProcessResults[defineStructureName][name][versionName];
}

// Copy matched elements into parsed block
_recursiveMerge(block.local, matchedData);
// Copy matched elements into parsed block
_recursiveMerge(block.local, matchedData);
}
else
{
throw new Error("Name \"" + name + "\" in file \"" + filenames[fileIndex] +
"\" block number " + block.index + " is not defined."
);
}
} // for structureIndex

// Remove Structure Key
block["local"][structureName] = undefined;
}
else
{
throw new Error("Name \"" + name + "\" in file \"" + filenames[fileIndex] +
"\" block number " + block.index + " is not defined."
);
}
// Remove Structure Key
block["local"][structureName] = undefined;
}
} // for blockIndex
} // for fileIndex
Expand All @@ -133,10 +140,13 @@ function postProcess(parsedFiles, filenames, preProcessResults, structureName, d
function _recursiveMerge(block, matchedData)
{
_.merge(block, matchedData, function(a, b) {
if(a instanceof Array) return a.concat(b);
if(a instanceof Array)
{
return a.concat(b);
}
if(_.isObject(a))
{
return _recursiveMerge(a, b);
_recursiveMerge(a, b);
}
return a;
});
Expand Down

0 comments on commit c48ac88

Please sign in to comment.