foliojs/restructure

Nested objects in struct?

krlicmuhamed opened this issue · 1 comments

First of all, great module!

I tried to do this:

var Record = new r.Struct({....});
...
...
var Packet = new r.Struct({
      number_of_records: r.uint8,
      records: new r.Array(Record, 'number_of_records')
});

var stream = new r.DecodeStream(buf);
var object = Packet.decode(stream);

var object returns this error:

TypeError: type.decode is not a function
      at Struct._parseFields (node_modules/restructure/src/Struct.js:53:22)
      at Struct.decode (node_modules/restructure/src/Struct.js:18:12)
      at StructTest.parse (main.js:79:23)
      at Context.<anonymous> (test/protocol.js:14:26)

Does this mean I can't do array of structs? :(

To elaborate what was my problem, maybe someone will benefit from this...
You get this TypeError: type.decode is not a function If you nest an object into r.Struct({}); like this:

var test = new r.Struct({
   header: {
   ...
   }
});

To get arount this issue I just nested r.Struct like this:

var test = new r.Struct({
   header: r.Struct({
   ...
   })
});