binary-parser icon indicating copy to clipboard operation
binary-parser copied to clipboard

User defined types?

Open itsthatguy opened this issue 11 years ago • 4 comments

User defined types does not seem very straight forward. After mucking around with it a bit, it appears that I need to fork the repo and rewrite a large portion of the code, include the "SPECIAL_TYPES" to add a VLQ type.

Is this true, or am I missing something?

itsthatguy avatar Feb 18 '14 05:02 itsthatguy

Hi @itsthatguy , unfortunately you're right, there's currently no way to define user defined types. Sorry if my documentation confused you, but I meant combinations of pre-defined primitive and compound types with "user defined types".

I could enhance the code to support any custom parsing logic. Any ideas for the API? Maybe it goes like this:

var parser = new Parser().custom(function() {
    this.vars.a = foo(offset);
    this.offset += bar;
});

keichi avatar Feb 19 '14 15:02 keichi

I feel like in a real environment you're going to want to define a parser independently from the header.

Being able to assign new datatypes to the parser class would be ideal.

ie;

var Parser = require('binary-parser').Parser;

Parser.createDataType("myDataType", function(params) {
  // parsing logic here
});


var parser = new Parser().myDataType({"param1": 0, "param2": 1});

itsthatguy avatar Feb 20 '14 19:02 itsthatguy

To some extent, my desire for user-defined types is handled by nest and by the extension that I submitted in pull request #6 to add a way to set the constructor method to be used to create the object.

Thoughts?

waywardmonkeys avatar Oct 25 '14 12:10 waywardmonkeys

@waywardmonkeys I think what we're discussing here is about adding support for binary data types that can't be easily described with the built-in primitive/compound data types. You're talking about support for custom data types of the parsed result -- which is also important and your PR is appreciated. However, custom binary data types and custom parsing logic is also needed .

keichi avatar Oct 27 '14 07:10 keichi