Skip to content

Hooks around variable scope #1227

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

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
master:
new features:
- Added support for "password" data type in Variable
- Added ability to have hooks around getter and setter for variable scope

4.1.0:
date: 2021-08-16
new features:
Expand Down
38 changes: 35 additions & 3 deletions lib/collection/variable-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ _.inherit((
* @param {VariableScope.definition} definition The constructor accepts an initial set of values for initialising
* the scope
* @param {Array<VariableList>=} layers Additional parent scopes to search for and resolve variables
* @param {Object<Enum('in','out'):Function>=} hooks definition of casting function to be executed. Casting
* hooks are functions that act as middlewares to modify values before setting or getting.
*
* @example <caption>Load a environment from file, modify and save back</caption>
* var fs = require('fs'), // assuming NodeJS
Expand All @@ -70,7 +72,7 @@ _.inherit((
* env.set('sum', sum, 'number');
* fs.writeFileSync('./sum-of-vars.postman_environment', JSON.stringify(env.toJSON()));
*/
VariableScope = function PostmanVariableScope (definition, layers) {
VariableScope = function PostmanVariableScope (definition, layers, hooks) {
// in case the definition is an array (legacy format) or existing as list, we convert to actual format
if (_.isArray(definition) || VariableList.isVariableList(definition)) {
definition = { values: definition };
Expand All @@ -80,6 +82,10 @@ _.inherit((
// so we can easily loop though them and add them to the instance.
layers && !_.isArray(layers) && (layers = [layers]);

if (castingHooks) {
this.hooks = hooks;
}

// this constructor is intended to inherit and as such the super constructor is required to be executed
VariableScope.super_.call(this, definition);

Expand Down Expand Up @@ -200,6 +206,8 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
*/
get: function (key) {
var variable = this.values.oneNormalizedVariable(key),
type,
value,
i,
ii;

Expand All @@ -211,7 +219,18 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
}
}

return (variable && variable.disabled !== true) ? variable.valueOf() : undefined;
if ((variable && variable.disabled !== true)) {
value = variable.valueOf();
type = variable.valueType();
}

if (_.has(this.hooks, type)) {
let hook = this.hooks[type];

value = _.isFunction(hook) ? hook(value, key, 'get') : hook.get(value, key);
}

return value;
},

/**
Expand All @@ -227,7 +246,20 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
// create an object that will be used as setter
update = { key, value };

_.isString(type) && (update.type = type);
// add type change to the setter if provided as part of param
if (_.isString(type)) {
update.type = type;
}
else {
type = 'any'; // @todo refer this directly from variable property to avoid typedef coupling across files
}


if (_.has(this.hooks, type)) {
let hook = this.hooks[type];

update.value = _.isFunction(hook) ? hook(value, key, 'set') : hook.set(value, key);
}

// If a variable by the name key exists, update it's value and return.
// @note adds new variable if existing is disabled. Disabled variables are not updated.
Expand Down
44 changes: 25 additions & 19 deletions lib/collection/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,28 @@ _.assign(Variable, /** @lends Variable */ {
*/
number: Number,

/**
* Free-form type of a value. This is the default for any variable, unless specified otherwise. It ensures that
* the variable can store data in any type and no conversion is done while using {@link Variable#get}.
*/
any: {
/**
* @param {*} val -
* @returns {*}
*/
in (val) {
return val; // pass through
},

/**
* @param {*} val -
* @returns {*}
*/
out (val) {
return val; // pass through
}
},

/**
* A "array" type value stores Array data format
*/
Expand Down Expand Up @@ -342,26 +364,10 @@ _.assign(Variable, /** @lends Variable */ {
},

/**
* Free-form type of a value. This is the default for any variable, unless specified otherwise. It ensures that
* the variable can store data in any type and no conversion is done while using {@link Variable#get}.
* A "password" type is essentially a string. The conflation of data-type and input nature is a deliberate
* attempt to make dealing with sensitive data within environment templates.
*/
any: {
/**
* @param {*} val -
* @returns {*}
*/
in (val) {
return val; // pass through
},

/**
* @param {*} val -
* @returns {*}
*/
out (val) {
return val; // pass through
}
}
password: String
},

/**
Expand Down
Loading