Comparing version 0.7.4 to 0.7.5
@@ -69,2 +69,3 @@ 'use strict' | ||
function configureExchanges(config) { | ||
config.exchanges = ensureKeyedCollection(config.exchanges) | ||
_.each(config.exchanges, function(exchangeConfig, name) { | ||
@@ -77,2 +78,3 @@ debug(format('Configuring exchange: %s', name)) | ||
function configureQueues(config) { | ||
config.queues = ensureKeyedCollection(config.queues) | ||
_.each(config.queues, function(queueConfig, name) { | ||
@@ -87,4 +89,5 @@ debug(format('Configuring queue: %s', name)) | ||
function configureBindings(config) { | ||
expandBindings(config.bindings) | ||
config.bindings = expandBindings(ensureKeyedCollection(config.bindings)) | ||
_.each(config.bindings, function(bindingConfig, name) { | ||
@@ -99,3 +102,4 @@ debug(format('Configuring binding: %s', name)) | ||
if (bindingConfig.destinationType === 'queue') { | ||
config.bindings[name].bindingKey = fqn.prefix(config.queues[bindingConfig.destination].replyTo, bindingConfig.bindingKey, '.') | ||
var queue = config.queues[bindingConfig.destination] | ||
config.bindings[name].bindingKey = fqn.prefix(queue && queue.replyTo, bindingConfig.bindingKey, '.') | ||
} | ||
@@ -116,11 +120,12 @@ }) | ||
function expandBindings(definitions) { | ||
var result = {} | ||
_.each(definitions, function(bindingConfig, name) { | ||
delete definitions[name] | ||
var parsedConfig = parseBindingName(name) | ||
var bindingKeys = _.chain([]).concat(bindingConfig.bindingKeys, bindingConfig.bindingKey, parsedConfig.bindingKeys).compact().unique().value() | ||
if (bindingKeys.length <= 1) return definitions[name] = _({ bindingKey: bindingKeys[0] }).defaults(bindingConfig, parsedConfig).omit('bindingKeys').value() | ||
if (bindingKeys.length <= 1) return result[name] = _({ bindingKey: bindingKeys[0] }).defaults(bindingConfig, parsedConfig).omit('bindingKeys').value() | ||
_.each(bindingKeys, function(bindingKey) { | ||
definitions[format('%s:%s', name, bindingKey)] = _({ bindingKey: bindingKey }).defaults(bindingConfig, parsedConfig).omit('bindingKeys').value() | ||
result[format('%s:%s', name, bindingKey)] = _({ bindingKey: bindingKey }).defaults(bindingConfig, parsedConfig).omit('bindingKeys').value() | ||
}) | ||
}) | ||
return result | ||
} | ||
@@ -134,1 +139,8 @@ | ||
} | ||
function ensureKeyedCollection(collection) { | ||
if (!_.isArray(collection)) return collection | ||
return _.chain(collection).map(function(item) { | ||
return _.isString(item) ? { name: item } : _.defaults(item, { name: 'unnamed-' + uuid() }) | ||
}).indexBy('name').value() | ||
} |
{ | ||
"name": "rascal", | ||
"version": "0.7.4", | ||
"version": "0.7.5", | ||
"description": "A friendly wrapper around amqplib with (mostly) safe defaults", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
107
README.md
@@ -404,45 +404,2 @@ # Rascal | ||
} | ||
``` | ||
#### Bindings Shortcut Notation | ||
Binding configuration can get rather verbose, so you short cut the notcation as follows... | ||
```json | ||
{ | ||
"bindings": { | ||
"e1 -> q1": { | ||
} | ||
} | ||
} | ||
``` | ||
is equivalent to... | ||
```json | ||
{ | ||
"bindings": { | ||
"b1": { | ||
"source": "e1", | ||
"destination": "q1" | ||
} | ||
} | ||
} | ||
``` | ||
and | ||
```json | ||
{ | ||
"bindings": { | ||
"e1[foo, bar.baz] -> q1": { | ||
} | ||
} | ||
} | ||
``` | ||
is equivalent to... | ||
```json | ||
{ | ||
"bindings": { | ||
"b1": { | ||
"source": "e1", | ||
"destination": "q1", | ||
"bindingKeys": ["foo", "bar.baz"] | ||
} | ||
} | ||
} | ||
``` | ||
@@ -753,3 +710,65 @@ ### Publications | ||
### Running the tests | ||
``` | ||
#### Shorthand Notation | ||
Rascal configuration can get rather verbose, so you can use the shorthand notation | ||
```json | ||
{ | ||
"exchanges": { | ||
"e1": {}, | ||
"e2": {} | ||
}, | ||
"queues": { | ||
"q1": {}, | ||
"q2": {} | ||
} | ||
"bindings": { | ||
"b1": { | ||
"source": "e1", | ||
"destination": "q1" | ||
}, | ||
"b2": { | ||
"source": "e1", | ||
"destination": "q2", | ||
"bindingKeys": ["bk1", "bk2"] | ||
} | ||
} | ||
} | ||
``` | ||
is equivalent to... | ||
```json | ||
{ | ||
"exchanges": ["e1", "e2"], | ||
"queues": ["q1", "e2"], | ||
"bindings": [ | ||
"e1 -> q1", | ||
"e2[bk1, bk2] -> q2" | ||
] | ||
} | ||
``` | ||
If you need to specify exchange, queue or binding parameters you can mix and match string and object configuration... | ||
```json | ||
{ | ||
"exchanges": { | ||
"e1": {}, | ||
"e2" : { | ||
"type": "fanout" | ||
} | ||
] | ||
} | ||
``` | ||
is equivalent to... | ||
```json | ||
{ | ||
"exchanges": [ | ||
"e1", | ||
{ | ||
"name": "e2", | ||
"type": "fanout" | ||
} | ||
] | ||
} | ||
``` | ||
## Running the tests | ||
```bash | ||
@@ -756,0 +775,0 @@ npm test |
@@ -190,2 +190,3 @@ var assert = require('assert') | ||
assert.ifError(err) | ||
console.log(config) | ||
assert.equal(config.vhosts.v1.exchanges.e1.assert, false) | ||
@@ -247,2 +248,38 @@ assert.equal(config.vhosts.v1.exchanges.e1.type, 'direct') | ||
}) | ||
it('should support array of names configuration', function() { | ||
configure({ | ||
vhosts: { | ||
v1: { | ||
exchanges: [ 'e1', 'e2' ] | ||
} | ||
} | ||
}, function(err, config) { | ||
assert.ifError(err) | ||
assert.ok(!_.isArray(config.vhosts.v1.exchanges)) | ||
assert.equal(config.vhosts.v1.exchanges.e1.name, 'e1') | ||
assert.equal(config.vhosts.v1.exchanges.e1.fullyQualifiedName, 'e1') | ||
assert.equal(config.vhosts.v1.exchanges.e2.name, 'e2') | ||
assert.equal(config.vhosts.v1.exchanges.e2.fullyQualifiedName, 'e2') | ||
}) | ||
}) | ||
it('should support a mixed array of names / objects configuration', function() { | ||
configure({ | ||
vhosts: { | ||
v1: { | ||
exchanges: [ 'e1', { name: 'e2' } ] | ||
} | ||
} | ||
}, function(err, config) { | ||
assert.ifError(err) | ||
assert.ok(!_.isArray(config.vhosts.v1.exchanges)) | ||
assert.equal(config.vhosts.v1.exchanges.e1.name, 'e1') | ||
assert.equal(config.vhosts.v1.exchanges.e1.fullyQualifiedName, 'e1') | ||
assert.equal(config.vhosts.v1.exchanges.e2.name, 'e2') | ||
assert.equal(config.vhosts.v1.exchanges.e2.fullyQualifiedName, 'e2') | ||
}) | ||
}) | ||
}) | ||
@@ -366,2 +403,20 @@ | ||
}) | ||
it('should support a mixed array of names / objects configuration', function() { | ||
configure({ | ||
vhosts: { | ||
v1: { | ||
queues: ['q1', 'q2'] | ||
} | ||
} | ||
}, function(err, config) { | ||
assert.ifError(err) | ||
assert.ok(!_.isArray(config.vhosts.v1.queues)) | ||
assert.equal(config.vhosts.v1.queues.q1.name, 'q1') | ||
assert.equal(config.vhosts.v1.queues.q1.fullyQualifiedName, 'q1') | ||
assert.equal(config.vhosts.v1.queues.q2.name, 'q2') | ||
assert.equal(config.vhosts.v1.queues.q2.fullyQualifiedName, 'q2') | ||
}) | ||
}) | ||
}) | ||
@@ -595,2 +650,27 @@ | ||
}) | ||
}), | ||
it('should support a mixed array of names / objects configuration', function() { | ||
configure({ | ||
vhosts: { | ||
v1: { | ||
queues: [ | ||
'q1', | ||
'q2' | ||
], | ||
bindings: [ | ||
'e1 -> q1', | ||
'e1 -> q2' | ||
] | ||
} | ||
} | ||
}, function(err, config) { | ||
assert.ifError(err) | ||
assert.ok(!_.isArray(config.vhosts.v1.bindings)) | ||
assert.equal(config.vhosts.v1.bindings['e1 -> q1'].source, 'e1') | ||
assert.equal(config.vhosts.v1.bindings['e1 -> q1'].destination, 'q1') | ||
assert.equal(config.vhosts.v1.bindings['e1 -> q2'].source, 'e1') | ||
assert.equal(config.vhosts.v1.bindings['e1 -> q2'].destination, 'q2') | ||
}) | ||
}) | ||
@@ -597,0 +677,0 @@ }) |
222549
4812
780