manyfest
Version:
JSON Object Manifest for Data Description and Parsing
117 lines (105 loc) • 3.61 kB
JavaScript
/**
* @author <steven@velozo.com>
*/
let libSimpleLog = require('./Manyfest-LogToConsole.js');
/**
* Hash Translation
*
* This is a very simple translation table for hashes, which allows the same schema to resolve
* differently based on a loaded translation table.
*
* This is to prevent the requirement for mutating schemas over and over again when we want to
* reuse the structure but look up data elements by different addresses.
*
* One side-effect of this is that a translation table can "override" the built-in hashes, since
* this is always used to resolve hashes before any of the functionCallByHash(pHash, ...) perform
* their lookups by hash.
*
* @class ManyfestHashTranslation
*/
class ManyfestHashTranslation
{
constructor(pInfoLog, pErrorLog)
{
// Wire in logging
this.logInfo = (typeof(pInfoLog) === 'function') ? pInfoLog : libSimpleLog;
this.logError = (typeof(pErrorLog) === 'function') ? pErrorLog : libSimpleLog;
this.translationTable = {};
}
translationCount()
{
return Object.keys(this.translationTable).length;
}
addTranslation(pTranslation)
{
// This adds a translation in the form of:
// { "SourceHash": "DestinationHash", "SecondSourceHash":"SecondDestinationHash" }
if (typeof(pTranslation) != 'object')
{
this.logError(`Hash translation addTranslation expected a translation be type object but was passed in ${typeof(pTranslation)}`);
return false;
}
let tmpTranslationSources = Object.keys(pTranslation)
tmpTranslationSources.forEach(
(pTranslationSource) =>
{
if (typeof(pTranslation[pTranslationSource]) != 'string')
{
this.logError(`Hash translation addTranslation expected a translation destination hash for [${pTranslationSource}] to be a string but the referrant was a ${typeof(pTranslation[pTranslationSource])}`);
}
else
{
this.translationTable[pTranslationSource] = pTranslation[pTranslationSource];
}
});
}
removeTranslationHash(pTranslationHash)
{
if (pTranslationHash in this.translationTable)
{
delete this.translationTable[pTranslationHash];
}
}
// This removes translations.
// If passed a string, just removes the single one.
// If passed an object, it does all the source keys.
removeTranslation(pTranslation)
{
if (typeof(pTranslation) == 'string')
{
this.removeTranslationHash(pTranslation);
return true;
}
else if (typeof(pTranslation) == 'object')
{
let tmpTranslationSources = Object.keys(pTranslation)
tmpTranslationSources.forEach(
(pTranslationSource) =>
{
this.removeTranslation(pTranslationSource);
});
return true;
}
else
{
this.logError(`Hash translation removeTranslation expected either a string or an object but the passed-in translation was type ${typeof(pTranslation)}`);
return false;
}
}
clearTranslations()
{
this.translationTable = {};
}
translate(pTranslation)
{
if (pTranslation in this.translationTable)
{
return this.translationTable[pTranslation];
}
else
{
return pTranslation;
}
}
}
module.exports = ManyfestHashTranslation;