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:
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 single answer for this question, it depends on how you want to structure your project. Your options are:
Every .ksy file is a type. That means instead of putting datatypeone
, datatypetwo
, etc into a single file as second-level nesting types (listed in types:
), you can just put datatypeone
into datatypeone.ksy
, datatypetwo
into datatypetwo.ksy
, etc, and import these individually where applicable.
You can go on with your idea, in this case the correct syntax for importing is:
meta:
id: machine_type
imports:
- datatype # refers to datatype.ksy
seq:
- id: foo
type: datatype::datatypeone
Think of why do you really want to separate things in multiple files? For example, putting everything in one .ksy file is also an option, and it solves the code duplication problem.
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).