My system has to be able to parse two types of very similar input data.
- If the data comes from a queue it has the following structure:
record
record
...
record
- If the data comes from a file it has the following structure:
header
record
record
...
record
My current code is as follows:
seq:
- id: file_header
type: file_header
- id: record
type: record
repeat: eos
types:
file_header:
seq:
- id: main_version
type: u1
- id: sub_version
type: u1
- id: spare
type: str
size: 30
encoding: UTF-8
record:
seq:
- id: event_id
type: u2
# enum: event_types
- id: event_length
type: u4
- id: enb_id
type: u4
- id: cell_id
type: u1
- id: call_id
type: u4
- id: date_time
type: date_time_record
- id: spare
type: str
size: 2
encoding: UTF-8
- id: crnti
type: u2
- id: body
size: event_length - 21
My idea is to create only one .ksy file that works for both approaches.
How can I get it?
It would basically be making file_header optional, but I don't see a way to do it.
Can somebody please help me on this?
Affiliate disclaimer: I'm a Kaitai Struct maintainer (see my GitHub profile).
You can define a boolean parameter
is_fileon the top-level type and passtruewhen the data comes from a file, otherwisefalse. Like this:Note that the
is_fileparam is mandatory, and you won't be able to instantiate the class without passing a value in it. For that reason, thefromFile(…)helper method will no longer be available, and you'll need to create the parser object normally usingnew(or its closest equivalent in your target language).I don't know what language you're targeting, but in C++, C#, Lua, PHP and Python come the custom
paramsfirst (before_io,_rootand_parent) and in Java, JavaScript and Nim second. For example, in Python you would do:In Java, for instance:
Check the generated code if unsure.