1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #1801 from node-red-hitachi/0.19-multi-values

Make it possible to set multiple values
This commit is contained in:
Nick O'Leary 2018-07-13 14:03:03 +01:00 committed by GitHub
commit afe6afca36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 189 additions and 1 deletions

View File

@ -268,7 +268,36 @@ function createContext(id,seed) {
} }
context = getContextStorage(storage); context = getContextStorage(storage);
} }
context.set(scope, key, value, callback); if (!Array.isArray(key)) {
context.set(scope, key, value, callback);
} else {
// If key is an array, set each key-value pair.
// If the value array is longer than the key array, then the extra values are ignored.
var index = 0;
var values = [].concat(value); // Convert the value to an array
var cb = function(err) {
if (err) {
if (callback) {
callback(err);
}
} else {
index++;
if (index === key.length) {
if (callback) {
callback(null);
}
} else {
if(index < values.length) {
context.set(scope, key[index], values[index], cb);
} else {
// If the value array is shorter than the key array, use null for missing values.
context.set(scope, key[index], null, cb);
}
}
}
};
context.set(scope, key[index], values[index], cb);
}
}; };
obj.keys = function(storage, callback) { obj.keys = function(storage, callback) {
var context; var context;

View File

@ -615,6 +615,165 @@ describe('context', function() {
}); });
}).catch(function(err){ done(err); }); }).catch(function(err){ done(err); });
}); });
it('should store multiple properties if key and value are arrays', function(done) {
Context.init({contextStorage:memoryStorage});
Context.load().then(function(){
var context = Context.get("1","flow");
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3"], "memory", function(err){
if (err) {
done(err);
} else {
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
if (err) {
done(err);
} else {
foo1.should.be.equal("bar1");
foo2.should.be.equal("bar2");
foo3.should.be.equal("bar3");
done();
}
});
}
});
});
});
it('should deletes multiple properties', function(done) {
Context.init({contextStorage:memoryStorage});
Context.load().then(function(){
var context = Context.get("1","flow");
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3"], "memory", function(err){
if (err) {
done(err);
} else {
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
if (err) {
done(err);
} else {
foo1.should.be.equal("bar1");
foo2.should.be.equal("bar2");
foo3.should.be.equal("bar3");
context.set(["foo1","foo2","foo3"], new Array(3), "memory", function(err){
if (err) {
done(err);
} else {
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
if (err) {
done(err);
} else {
should.not.exist(foo1);
should.not.exist(foo2);
should.not.exist(foo3);
done();
}
});
}
});
}
});
}
});
});
});
it('should use null for missing values if the value array is shorter than the key array', function(done) {
Context.init({contextStorage:memoryStorage});
Context.load().then(function(){
var context = Context.get("1","flow");
context.set(["foo1","foo2","foo3"], ["bar1","bar2"], "memory", function(err){
if (err) {
done(err);
} else {
context.keys(function(err, keys){
keys.should.have.length(3);
keys.should.eql(["foo1","foo2","foo3"]);
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
if (err) {
done(err);
} else {
foo1.should.be.equal("bar1");
foo2.should.be.equal("bar2");
should(foo3).be.null();
done();
}
});
});
}
});
});
});
it('should use null for missing values if the value is not array', function(done) {
Context.init({contextStorage:memoryStorage});
Context.load().then(function(){
var context = Context.get("1","flow");
context.set(["foo1","foo2","foo3"], "bar1", "memory", function(err){
if (err) {
done(err);
} else {
context.keys(function(err, keys){
keys.should.have.length(3);
keys.should.eql(["foo1","foo2","foo3"]);
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
if (err) {
done(err);
} else {
foo1.should.be.equal("bar1");
should(foo2).be.null();
should(foo3).be.null();
done();
}
});
});
}
});
});
});
it('should ignore the extra values if the value array is longer than the key array', function(done) {
Context.init({contextStorage:memoryStorage});
Context.load().then(function(){
var context = Context.get("1","flow");
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3","ignored"], "memory", function(err){
if (err) {
done(err);
} else {
context.keys(function(err, keys){
keys.should.have.length(3);
keys.should.eql(["foo1","foo2","foo3"]);
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
if (err) {
done(err);
} else {
foo1.should.be.equal("bar1");
foo2.should.be.equal("bar2");
foo3.should.be.equal("bar3");
done();
}
});
});
}
});
});
});
it('should return an error if an error occurs in storing multiple values', function(done) {
Context.init({contextStorage:contextStorage});
stubSet.onFirstCall().callsArgWith(3, null);
stubSet.onSecondCall().callsArgWith(3, "error2");
stubSet.onThirdCall().callsArgWith(3, null);
Context.load().then(function(){
var context = Context.get("1","flow");
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3"], "memory", function(err){
if (err === "error2") {
done();
} else {
done("An error occurred");
}
});
}).catch(function(err){ done(err); });
});
}); });
describe('delete context',function(){ describe('delete context',function(){