I need to unpack a few values of known types and offsets from a binary file. The difference with earlier discussed cases is, firstly, that the values are not adjacent in the file, so it is necessary to specify offsefs; secondly, there are some differences in the way the values are packed. So I may do something like this:
struct Foo
val1:: Int32
val2::Float16
val3::UInt64
end
foo_layout=[12 :pack_style_1; 34 :pack_style_2; 56 pack_style_3]
Then the unpacking function itrates over fields of Foo, reads them into v::Vector{Any}
, taking into account the offsets and styles, and then returns Foo(v...)
. The drawback of this solution is the necessity to keep in sync the separate definitions of Foo and its layout. It would be more convenient to supply some “pragmas” within the definion of Foo, and then having a macro to generate both the struct and the layout description:
@gen_layout struct Foo
val1:: Int32 # 12 :pack_style_1
val2::Float16 # 34 :pack_style_2
val3::UInt64 # 56 :pack_style_3
end
What is unclear to me is, if the metaprogramming facilities work on the parsed AST, do they have access to the comments?
Or maybe, there’s some altogether better way to do this?