Skip to content

Commit f58189c

Browse files
committed
make string type identifiers work (type coersion)
1 parent e3bab35 commit f58189c

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/struct.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ function set (buffer, offset, value) {
169169

170170
function defineProperty (name, type) {
171171
debug('defining new struct type field', name)
172+
173+
// allow string types for convenience
174+
type = ref.coerceType(type)
175+
172176
assert(!this._instanceCreated, 'an instance of this Struct type has already '
173177
+ 'been created, cannot add new "fields" anymore')
174178
assert.equal('string', typeof name, 'expected a "string" field name')

test/struct.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ describe('Struct', function () {
9494
assert.equal(400, ps.childB.b)
9595
})
9696

97+
describe('string type identifiers', function () {
98+
99+
it('should work with string type identifiers', function () {
100+
var S = Struct({
101+
'int': 'int'
102+
, 'long': 'long'
103+
, 'string': 'string'
104+
})
105+
106+
assert.strictEqual(ref.types.int, S.fields.int.type)
107+
assert.strictEqual(ref.types.long, S.fields.long.type)
108+
assert.strictEqual(ref.types.Utf8String, S.fields.string.type)
109+
})
110+
111+
})
112+
97113
describe('offsets and sizeofs', function () {
98114

99115
function test (structType, testNumber) {

0 commit comments

Comments
 (0)