let's say I have the following two types:
types:
arrayf4:
params:
- id: size
type: u4
seq:
- id: member
type: f4
repeat: expr
repeat-expr: size
arrays2:
params:
- id: size
type: u4
seq:
- id: member
type: s2
repeat: expr
repeat-expr: size
is it possible to implement a generic type in kaitai as follows? (this example below does not compile in kaitai Web IDE). This must be able to work not only with simple types like s2 or f4 but also with user-defined types.
types:
generic_array:
params:
- id: size
type: u4
- id: dtype
type: strz
seq:
- id: member
type: dtype
repeat: expr
repeat-expr: size
As understood from this thread on GitHub, there is no generics in kaitai as of time when this answer had been posted.
I was able to find a workaround for myself - unfortunately, the workaround still requires a lot unnecessary effort to maintain. However, in my case, it still makes my kaitai code cleaner, so I am posting this solution here, in case it might be helpful for someone in a similar situation.
My generic type is defined as follows:
In code this type is used as follows:
Pros.
ptrtype becomes much cleaner. I have a lot of pointers of this type in the binary format I am parsing, so this has made my code immediately a ton more readable.python, in my case) is truly generic. You always get the samePtrclass for your object, and thedatainside may be of any type defined, in my casematerial,node, oranimation, so at the end it has exactly the effect I was aiming for.Cons.
switch-onclause, otherwise by default it will be set to anunknowntype which is simply defined as an empty dummy which does not read any bytes. If you have several generic types, this scales respectively. I initially thought about writing a pre-processing routine but in my case it is easier to maintain this manually.ptr('other_generic(some_type)'), of course, won't work.