Skip to content

Commit

Permalink
Added support for prefix in VariableScope (#1364)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pranav2612000 authored Jul 29, 2024
1 parent 01caecb commit ef65526
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ unreleased:
- GH-1366 Fixed a bug where $randomAvatarImage was returning an invalid URL
chores:
- GH-1362 Updated `Request~size` to handle `downloadedBytes` property
- GH-1364 Added support for `prefix` in VariableScope

4.4.0:
date: 2024-02-28
Expand Down
25 changes: 17 additions & 8 deletions lib/collection/variable-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var _ = require('../util').lodash,
* @typedef VariableScope.definition
* @property {String} [id] ID of the scope
* @property {String} [name] A name of the scope
* @property {String} [prefix] A prefix to be used for variable names in this scope
* @property {Array.<Variable.definition>} [values] A list of variables defined in an array in form of `{name:String,
* value:String}`
*
Expand Down Expand Up @@ -111,6 +112,10 @@ _.inherit((
if (mutations) {
this.mutations = new MutationTracker(mutations);
}

if (definition && definition.prefix) {
this.prefix = definition.prefix;
}
}), Property);

/**
Expand Down Expand Up @@ -176,15 +181,16 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
* false otherwise
*/
has: function (key) {
var variable = this.values.oneNormalizedVariable(key),
const _key = (this.prefix || '') + key;
var variable = this.values.oneNormalizedVariable(_key),
i,
ii;

// if a variable is disabled or does not exist in local scope,
// we search all the layers and return the first occurrence.
if ((!variable || variable.disabled === true) && this._layers) {
for (i = 0, ii = this._layers.length; i < ii; i++) {
variable = this._layers[i].oneNormalizedVariable(key);
variable = this._layers[i].oneNormalizedVariable(_key);
if (variable && variable.disabled !== true) { break; }
}
}
Expand All @@ -199,14 +205,15 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
* @returns {*} The value of the specified variable across scopes.
*/
get: function (key) {
var variable = this.values.oneNormalizedVariable(key),
const _key = (this.prefix || '') + key;
var variable = this.values.oneNormalizedVariable(_key),
i,
ii;

// if a variable does not exist in local scope, we search all the layers and return the first occurrence.
if ((!variable || variable.disabled === true) && this._layers) {
for (i = 0, ii = this._layers.length; i < ii; i++) {
variable = this._layers[i].oneNormalizedVariable(key);
variable = this._layers[i].oneNormalizedVariable(_key);
if (variable && variable.disabled !== true) { break; }
}
}
Expand All @@ -222,10 +229,11 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
* @param {Variable.types} [type] - Optionally, the value of the variable can be set to a type
*/
set: function (key, value, type) {
var variable = this.values.oneNormalizedVariable(key),
const _key = (this.prefix || '') + key;
var variable = this.values.oneNormalizedVariable(_key),

// create an object that will be used as setter
update = { key, value };
update = { key: _key, value: value };

_.isString(type) && (update.type = type);

Expand All @@ -248,11 +256,12 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
* @param {String} key -
*/
unset: function (key) {
const _key = (this.prefix || '') + key;
var lastDisabledVariable;

this.values.remove(function (variable) {
// bail out if variable name didn't match
if (variable.key !== key) {
if (variable.key !== _key) {
return false;
}

Expand All @@ -269,7 +278,7 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {

// restore the reference with the last disabled variable
if (lastDisabledVariable) {
this.values.reference[key] = lastDisabledVariable;
this.values.reference[_key] = lastDisabledVariable;
}

// track the change if mutation tracking is enabled
Expand Down
85 changes: 85 additions & 0 deletions test/unit/variable-scope.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@ describe('VariableScope', function () {
expect(scope.get('random')).to.be.undefined;
});

it('should get variables with prefix', function () {
var scope = new VariableScope({
prefix: 'vault:',
values: [
{ key: 'vault:var-1', value: 'var-1-value' },
{ key: 'vault:var-2', value: 'var-2-value' }
] });

expect(scope.get('var-2')).to.equal('var-2-value');
});

describe('multi layer search', function () {
it('should get from parent scope', function () {
var scope = new VariableScope([
Expand Down Expand Up @@ -506,6 +517,19 @@ describe('VariableScope', function () {
scope.set('var-4', 3.142, 'boolean');
expect(scope.get('var-4')).to.be.true;
});

it('should correctly update an existing value with prefix', function () {
var scope = new VariableScope({
prefix: 'vault:',
values: [{
key: 'vault:var-1',
value: 'var-1-value'
}]
});

scope.set('var-1', 'new-var-1-value');
expect(scope.get('var-1')).to.equal('new-var-1-value');
});
});

describe('unset', function () {
Expand Down Expand Up @@ -607,6 +631,24 @@ describe('VariableScope', function () {
// check reference list
expect(scope.values.reference).to.have.property('var-2');
});

it('should correctly remove an existing variable with prefix', function () {
var scope = new VariableScope({
prefix: 'vault:',
values: [{
key: 'vault:var-1',
value: 'var-1-value'
}, {
key: 'vault:var-2',
value: 'var-2-value'
}]
});

scope.unset('var-1');

expect(scope.values.count()).to.equal(1);
expect(scope.get('var-1')).to.be.undefined;
});
});

describe('clear', function () {
Expand Down Expand Up @@ -1233,6 +1275,20 @@ describe('VariableScope', function () {

expect(scope.has('alpha')).to.be.true;
});

it('should find variable from current scope with prefix', function () {
var scope = new VariableScope({
prefix: 'vault:',
values: [
{ key: 'vault:alpha', value: 'foo' },
{ key: 'vault:gamma', value: 'baz', disabled: true }
]
});

expect(scope.has('alpha')).to.be.true;
expect(scope.has('gamma')).to.be.false;
expect(scope.has('random')).to.be.false;
});
});

describe('disabled variable', function () {
Expand Down Expand Up @@ -1379,6 +1435,35 @@ describe('VariableScope', function () {
expect(scope1.values).to.eql(scope2.values);
});

it('should be capable of being replayed with prefix', function () {
var initialState = {
prefix: 'vault:',
values: [{
key: 'foo',
value: 'foo'
}, {
key: 'bar',
value: 'bar'
}]
},
scope1 = new VariableScope(initialState),
scope2 = new VariableScope(initialState);

scope1.enableTracking();

// add a new key
scope1.set('baz', 'baz');
// update a key
scope1.set('foo', 'foo updated');
// remove a key
scope1.unset('bar');

// replay mutations on a different object
scope1.mutations.applyOn(scope2);

expect(scope1.values).to.eql(scope2.values);
});

it('should be serialized', function () {
var scope = new VariableScope(),
serialized,
Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2333,12 +2333,14 @@ declare module "postman-collection" {
* }
* @property [id] - ID of the scope
* @property [name] - A name of the scope
* @property [prefix] - A prefix to be used for variable names in this scope
* @property [values] - A list of variables defined in an array in form of `{name:String,
* value:String}`
*/
type definition = {
id?: string;
name?: string;
prefix?: string;
values?: Variable.definition[];
};
}
Expand Down

0 comments on commit ef65526

Please sign in to comment.