Comparing version 10.2.0 to 10.3.0
@@ -17,5 +17,6 @@ /** | ||
* @param {[type]} parentMachine [description] | ||
* @param {[type]} rootMachine [description] | ||
* @return {[type]} [description] | ||
*/ | ||
module.exports = function buildLamdaMachine(runtimeLamdaFn, nameOfLamdaInput, parentMachine){ | ||
module.exports = function buildLamdaMachine(runtimeLamdaFn, nameOfLamdaInput, parentMachine, rootMachine){ | ||
@@ -50,9 +51,16 @@ // Get access to Machine constructor in order to call `build` below. | ||
// Parse special lamda defintion syntax to deduce the appropriate example | ||
if (def.like) { | ||
memo[name].example = parentMachine.inputs[def.like].example; | ||
if (_.isString(def.like)) { | ||
if (!rootMachine.inputs[def.like]) { | ||
throw new Error(util.format('Could not find input `%s` in root machine (%s)', def.like, rootMachine.identity)); | ||
} | ||
memo[name].example = rootMachine.inputs[def.like].example; | ||
delete memo[name].like; | ||
} | ||
else if (def.itemOf) { | ||
var referenced = parentMachine.inputs[def.itemOf]; | ||
memo[name].example = (referenced.example.length === 0 ? undefined : referenced.example[0]); | ||
else if (_.isString(def.itemOf)) { | ||
if (rootMachine.inputs[def.itemOf].example.length > 0) { | ||
memo[name].example = rootMachine.inputs[def.itemOf].example[0]; | ||
} | ||
else { | ||
memo[name].example = undefined; | ||
} | ||
delete memo[name].itemOf; | ||
@@ -69,9 +77,13 @@ } | ||
// Parse special lamda defintion syntax to deduce the appropriate example | ||
if (def.like) { | ||
memo[name].example = parentMachine.inputs[def.like].example; | ||
if (_.isString(def.like)) { | ||
memo[name].example = rootMachine.inputs[def.like].example; | ||
delete memo[name].like; | ||
} | ||
else if (def.itemOf) { | ||
var referenced = parentMachine.inputs[def.itemOf]; | ||
memo[name].example = (referenced.example.length === 0 ? undefined : referenced.example[0]); | ||
else if (_.isString(def.itemOf)) { | ||
if (rootMachine.inputs[def.itemOf].example.length > 0) { | ||
memo[name].example = rootMachine.inputs[def.itemOf].example[0]; | ||
} | ||
else { | ||
memo[name].example = undefined; | ||
} | ||
delete memo[name].itemOf; | ||
@@ -84,5 +96,11 @@ } | ||
// Track the root machine on the new machine definition. | ||
// (i.e. a contract inside a contract inside a contract | ||
// still maintains a reference to the top-level machine instance) | ||
lamdaMachineDef._rootMachine = rootMachine; | ||
// console.log('lamdaMachineDef._rootMachine:',lamdaMachineDef._rootMachine); | ||
// Construct and return the machine instance | ||
var instance = Machine.build(lamdaMachineDef); | ||
// console.log('INSTANCE._rootMachine:',instance._rootMachine); | ||
@@ -89,0 +107,0 @@ // Track some stack info so we know that this is an ad-hoc |
@@ -79,5 +79,7 @@ /** | ||
_.isUndefined(input.example) && | ||
_.isUndefined(input.typeclass) && | ||
_.isUndefined(input.getExample) && | ||
_.isUndefined(input.validate) | ||
_.isUndefined(input.like) && | ||
_.isUndefined(input.itemOf) && | ||
_.isUndefined(input.typeclass) && // <= todo: deprecate | ||
_.isUndefined(input.getExample) && // <= todo: deprecate | ||
_.isUndefined(input.validate) // <= todo: deprecate | ||
) { | ||
@@ -124,2 +126,17 @@ _errors.push(inputKey); | ||
// Resolve `like` and `itemOf` directives for each input and exit: | ||
_.each(machineDefinition.inputs, function(inputDef) { | ||
if (_.isUndefined(inputDef.example)) { | ||
inputDef.example = resolveExample(inputDef, machineDefinition); | ||
} | ||
}); | ||
_.each(machineDefinition.exits, function(exitDef) { | ||
if (_.isUndefined(exitDef.example)) { | ||
exitDef.example = resolveExample(exitDef, machineDefinition); | ||
} | ||
}); | ||
// If the catchall exit is defined and not equal to "error", fail with a deprecation error. | ||
@@ -239,2 +256,3 @@ if ( | ||
_callableMachineWrapper.timeout = machineDefinition.timeout; | ||
_callableMachineWrapper._rootMachine = machineDefinition._rootMachine; | ||
@@ -253,1 +271,94 @@ // Last but not least, inject an `.inspect` method to provide usage info | ||
}; | ||
/** | ||
* [resolveExample description] | ||
* @param {[type]} defToResolve [description] | ||
* @param {[type]} machineDefinition [description] | ||
* @return {[type]} [description] | ||
*/ | ||
function resolveExample(defToResolve, machineDefinition){ | ||
// If an explicit example was provided, use that. | ||
if (!_.isUndefined(defToResolve.example)) { | ||
return defToResolve.example; | ||
} | ||
var err; | ||
var referencedInput; | ||
// If `like` was provided, use the example of the specified input. | ||
if (!_.isUndefined(defToResolve.like)) { | ||
referencedInput = machineDefinition.inputs[defToResolve.like]; | ||
// If specified input does not exist, this is an error. | ||
if (!referencedInput) { | ||
err = new Error(util.format( | ||
'Failed to instantiate machine ("%s") from the specified machine definition.\n'+ | ||
'`like` should refer to a known machine input, but there is no input named `%s`.', | ||
machineDefinition.friendlyName, | ||
defToResolve.like)); | ||
err.machine = machineDefinition.identity; | ||
err.code = 'UNRECOGNIZED_INPUT'; | ||
throw new Error(err); | ||
} | ||
return referencedInput.example; | ||
} | ||
// If `itemOf` was provided, return the pattern of the array example from the specified input. | ||
else if (!_.isUndefined(defToResolve.itemOf)) { | ||
referencedInput = machineDefinition.inputs[defToResolve.itemOf]; | ||
// If specified input does not exist, this is an error. | ||
if (!referencedInput) { | ||
err = new Error(util.format( | ||
'Failed to instantiate machine ("%s") from the specified machine definition.\n'+ | ||
'`itemOf` should refer to a known machine input, but there is no input named `%s`.', | ||
machineDefinition.friendlyName, | ||
defToResolve.itemOf)); | ||
err.machine = machineDefinition.identity; | ||
err.code = 'UNRECOGNIZED_INPUT'; | ||
throw new Error(err); | ||
} | ||
// If specified input example is not an array, this is an error. | ||
if (!_.isArray(referencedInput.example)) { | ||
err = new Error(util.format( | ||
'Failed to instantiate machine ("%s") from the specified machine definition.\n'+ | ||
'`itemOf` should refer to a machine input with a patterned array example, but the example of input `%s` is:', | ||
machineDefinition.friendlyName, | ||
defToResolve.itemOf, | ||
referencedInput.example )); | ||
err.machine = machineDefinition.identity; | ||
err.code = 'INPUT_NOT_ARRAY'; | ||
throw new Error(err); | ||
} | ||
// If specified input example does not have a pattern, this is an error. | ||
if (referencedInput.length < 1) { | ||
err = new Error(util.format( | ||
'Failed to instantiate machine ("%s") from the specified machine definition.\n'+ | ||
'`itemOf` should refer to a machine input with a patterned array example, but the example of input `%s` is a generic array.', | ||
machineDefinition.friendlyName, | ||
defToResolve.itemOf )); | ||
err.machine = machineDefinition.identity; | ||
err.code = 'INPUT_NOT_ARRAY'; | ||
throw new Error(err); | ||
} | ||
return referencedInput.example[0]; | ||
} | ||
} |
@@ -90,3 +90,3 @@ /** | ||
// and instantiate the default function into a machine using the contract. | ||
self._configuredInputs[inputName] = buildLamdaMachine(inputDef.defaultsTo, inputName, self); | ||
self._configuredInputs[inputName] = buildLamdaMachine(inputDef.defaultsTo, inputName, self, self._rootMachine||self); | ||
} | ||
@@ -93,0 +93,0 @@ catch (e) { |
@@ -196,3 +196,3 @@ /** | ||
coercedInputValue = buildLamdaMachine(machine._configuredInputs[inputName], inputName, machine); | ||
coercedInputValue = buildLamdaMachine(machine._configuredInputs[inputName], inputName, machine, machine._rootMachine||machine); | ||
@@ -199,0 +199,0 @@ // Return early for now |
{ | ||
"name": "machine", | ||
"version": "10.2.0", | ||
"version": "10.3.0", | ||
"description": "Configure and execute machines", | ||
@@ -40,6 +40,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"async": "^0.9.0", | ||
"licensing": "^0.1.0", | ||
"mocha": "^1.9.0", | ||
"async": "^0.9.0" | ||
"test-machinepack-mocha": "^2.1.1" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
93912
2098
4