Skip to content

Commit

Permalink
feat(encoder): introduce exported Encoder for easy type annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Mar 4, 2017
1 parent 95446a9 commit 0d7e2c3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
4 changes: 3 additions & 1 deletion lib/index.js
@@ -1,12 +1,14 @@
'use strict';
var Policy = require('./policies/policy'),
Client = require('./amqp_client'),
pu = require('./policies/policy_utilities');
pu = require('./policies/policy_utilities'),
types = require('./types');

module.exports = {
Client: Client,
Constants: require('./constants'),
Errors: require('./errors'),
Encoder: new types.Encoder(),

/**
* Policies encode many of the optional behaviors and settings of AMQP into a
Expand Down
6 changes: 6 additions & 0 deletions lib/types.js
Expand Up @@ -5,14 +5,20 @@ var Builder = require('buffer-builder'),
AMQPArray = require('./types/amqp_composites').Array,
AMQPError = require('./types/amqp_error'),
ForcedType = require('./types/forced_type'),
DescribedType = require('./types/described_type'),
u = require('./utilities');

// constants
var MAX_UINT = Math.pow(1<<16, 2);
var MAX_SAFE_HIGH_BITS = Math.pow(2, 53 - 32);

var types = module.exports = {};
types.Encoder = function Encoder() {};
types.Encoder.prototype.described = function(descriptor, value) { return new DescribedType(descriptor, value); };

function registerType(name, options) {
types.Encoder.prototype[name] = function(value) { return new ForcedType(name, value); };

if (typeof options === 'string') { // this is an alias
types[name] = types[options];
return;
Expand Down
9 changes: 5 additions & 4 deletions test/integration/servicebus/eventhubs/client.test.js
Expand Up @@ -2,7 +2,7 @@
var amqp = require('../../../../lib'),
AMQPClient = amqp.Client,
Policy = amqp.Policy,
translator = amqp.translator,
t = amqp.Encoder,
Promise = require('bluebird'),
config = require('./config'),
expect = require('chai').expect,
Expand All @@ -24,9 +24,10 @@ function boundedFilter(offset) {

return {
attach: { source: { filter: {
'apache.org:selector-filter:string': translator(
['described', ['symbol', 'apache.org:selector-filter:string'],
['string', 'amqp.annotation.x-opt-enqueuedtimeutc > ' + enqueuedTimeUTC]])
'apache.org:selector-filter:string': t.described(
t.symbol('apache.org:selector-filter:string'),
t.string('amqp.annotation.x-opt-enqueuedtimeutc > ' + enqueuedTimeUTC)
)
} } }
};
}
Expand Down
14 changes: 9 additions & 5 deletions test/integration/servicebus/eventhubs/streams.test.js
Expand Up @@ -2,7 +2,7 @@
var Promise = require('bluebird'),
amqp = require('../../../..'),
AMQPClient = amqp.Client,
translator = amqp.translator,
t = amqp.Encoder,
Policy = amqp.Policy,
config = require('./config'),
expect = require('chai').expect,
Expand Down Expand Up @@ -44,8 +44,10 @@ describe('ReceiverStream', function() {
// be doing filter options in this test, since that's explicitly tested below.
var filterOptions = {
attach: { source: { filter: {
'apache.org:selector-filter:string': translator(
['described', ['symbol', 'apache.org:selector-filter:string'], ['string', 'amqp.annotation.x-opt-enqueuedtimeutc > ' + now]])
'apache.org:selector-filter:string': t.described(
t.symbol('apache.org:selector-filter:string'),
t.string('amqp.annotation.x-opt-enqueuedtimeutc > ' + now)
)
} } }
};

Expand Down Expand Up @@ -91,8 +93,10 @@ describe('SenderStream', function() {
// be doing filter options in this test, since that's explicitly tested below.
var filterOptions = {
attach: { source: { filter: {
'apache.org:selector-filter:string': translator(
['described', ['symbol', 'apache.org:selector-filter:string'], ['string', 'amqp.annotation.x-opt-enqueuedtimeutc > ' + now]])
'apache.org:selector-filter:string': t.described(
t.symbol('apache.org:selector-filter:string'),
t.string('amqp.annotation.x-opt-enqueuedtimeutc > ' + now)
)
} } }
};

Expand Down
8 changes: 3 additions & 5 deletions test/unit/frames.test.js
Expand Up @@ -10,9 +10,9 @@ var frames = require('../../lib/frames'),
DeliveryState = require('../../lib/types/delivery_state'),
AMQPError = require('../../lib/types/amqp_error'),
ForcedType = require('../../lib/types/forced_type'),
t = require('../../lib').Encoder,

terminus = require('../../lib/types/terminus'),
translator = require('../../lib/adapters/translate_encoder');
terminus = require('../../lib/types/terminus');

describe('Frames', function() {
describe('errors', function() {
Expand Down Expand Up @@ -303,9 +303,7 @@ describe('AttachFrame', function() {
}
});
attach.channel = 1;
attach.source.filter = translator([
'described', ['symbol', 'apache.org:legacy-amqp-direct-binding:string'], ['string', 'news']
]);
attach.source.filter = t.described(t.symbol('apache.org:legacy-amqp-direct-binding:string'), t.string('news'));

var actual = tu.convertFrameToBuffer(attach);
var expected = tu.buildBuffer([
Expand Down

0 comments on commit 0d7e2c3

Please sign in to comment.