kaitai-struct

Referencing Kaitai Struct file (ksy) in external ksy file in Kaitai Struct


I am trying to achieve files structuring in Kaitai Struct, means that I need to create ksy file can be referenced by another ksy file, so when i compile to my target language (java in my case) the java code will be optimized and reusing the commonly created ksy files and not redundant the generated java code.

I have tried to mimic "Referencing enums in external file in Kaitai Struct" but it did not work.

I started to look at "imports", which means importing a ksy file in an extrenal ksy file, i will explain broadly what i have tried to do and achieve:

  1. I created a ksy for data_types (customized data types ), it does not reference anything, just meta section with id and then types and i started to list all my customized data types (datatypeone, datatypetwo...)
  2. I created a ksy for machine types, each type does have "seq" of fields each field refer to one of the above mentioned data types, the challenge here is in the machineType.ksy , i add dataType.ksy as imports , I am not quite sure what is the syntax to do that, Kaitai struct compiler in Webide keeps telling me customized data types not defined e.g. datatypeone is not defined.

Is there any restrictions on the number of files to be referenced, e.g. i can reference file a from file b and reference file b from file c and reference file c from file d, means file d has "imports of c" and file c have "imports of b " and so forth.


Solution

  • There is no single answer for this question, it depends on how you want to structure your project. Your options are:

    There are many other valid reasons to keep them separate, but please note that Kaitai Struct's idea of having multiple files is not the same as it is for C++ or Java. Given that all entries in a "ksy file" implicitly share the same _root, then file boundary separation should be assessed by this connection rather than some other rule (like 1 class = 1 file, as in Java, or 1 file = 1 compilation unit, as in C++).

    Is there any restrictions on the number of files to be referenced, e.g. i can reference file a from file b and reference file b from file c and reference file c from file d, means file d has "imports of c" and file c have "imports of b " and so forth.

    There is no such restriction (except for natural restrictions, obviously — i.e. size of your memory, memory allocated to the compiler, your storage space, limitations of your file system, etc).