Skip to content

Commit

Permalink
feat(char): add basic support for the AMQP char type
Browse files Browse the repository at this point in the history
We can't have full UTF32BE support unless we depend on something
like node-iconv, however this adds very basic support assuming
the data is utf8 encoded.
  • Loading branch information
mbroadst committed Nov 3, 2015
1 parent ebcff9d commit eb212f6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
8 changes: 6 additions & 2 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,10 +576,14 @@ Types.prototype._initTypesArray = function() {
{
code: 0x73,
builder: function(val, bufb) {
throw new errors.NotImplementedError('UTF32');
bufb.appendUInt8(0x73);
bufb.appendUInt32BE(val.charCodeAt(0));
},
decoder: function(buf) {
throw new errors.NotImplementedError('UTF32');
// @todo: this will surely break on something in the future, but in order
// to maintain our desire to not depend on any native modules it may
// be the best we can do for the moment.
return String.fromCharCode(buf.readUInt32BE(0));
}
}
]
Expand Down
5 changes: 3 additions & 2 deletions test/unit/test_codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ describe('Codec', function() {
});

it('should fail when not implemented', function() {
var buffer = newBuffer([0x73, 0x01, 0x02, 0x03, 0x04]);
expect(function() { codec.decode(buffer); }).to.throw(Error); // jshint ignore:line
// @todo: use a dec32 until support is added
var buffer = newBuffer([0x74, 0x01, 0x02, 0x03, 0x04]);
expect(function() { codec.decode(buffer); }).to.throw(Error);
});

it('should decode described types', function() {
Expand Down
9 changes: 5 additions & 4 deletions test/unit/test_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Types', function() {
}

describe('errors', function() {
['decimal32', 'decimal64', 'decimal128', 'char'].forEach(function(typeName) {
['decimal32', 'decimal64', 'decimal128'].forEach(function(typeName) {
it('should error on unsupported type: ' + typeName, function() {
var buffer = new BufferBuilder();
var type = new ForcedType(typeName, 'some data');
Expand Down Expand Up @@ -205,7 +205,9 @@ describe('Types', function() {
// ]),
// expectedOutput: 1.1
// },
// { name: 'utf32 (char)', type: 0x73, value: buf([0x24]), expectedOutput: '$' },
{ name: 'utf32 (char)', type: 0x73, value: 'A',
expectedOutput: buf([0x73, 0x00, 0x00, 0x00, 0x41])
},
{
name: 'uuid', type: 0x98,
value: '797ff043-11eb-11e1-80d6-510998755d10',
Expand Down Expand Up @@ -448,7 +450,6 @@ describe('Types', function() {

describe('errors', function() {
[
{ name: 'char', value: buf([0x73, 0x00, 0x00, 0x00, 0x00]) },
{ name: 'decimal32', value: buf([0x74, 0x00, 0x00, 0x00, 0x00]) },
{ name: 'decimal64', value: buf([0x84, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) },
{ name: 'decimal128' , value: buf([0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]) }
Expand Down Expand Up @@ -530,7 +531,7 @@ describe('Types', function() {
// ]),
// expectedOutput: 1.1
// },
// { name: 'utf32 (char)', type: 0x73, value: buf([0x24]), expectedOutput: '$' },
{ name: 'utf32 (char)', value: buf([0x73, 0x00, 0x00, 0x00, 0x41]), expectedOutput: 'A' },
{
name: 'uuid',
value: buf([
Expand Down

0 comments on commit eb212f6

Please sign in to comment.