Skip to content

Instantly share code, notes, and snippets.

@domgetter
Last active August 29, 2015 14:04
Show Gist options
  • Save domgetter/83f8c236aa991760aab6 to your computer and use it in GitHub Desktop.
Save domgetter/83f8c236aa991760aab6 to your computer and use it in GitHub Desktop.
Interdefined Bindata Records problem
I want to create two records which refer to each other, but
Bindata seems to not want to let me do that.
The two solutions I came up with are to create a copy of one
record to another class with most the same functionality, or
to create an anonymous struct in one to copy most the functionality.
Both of these solutions require me to repeat myself, so I'd
like to create a more compact version like the first example.
Am I defining things wrong or does Bindata simply not allow
me to refer to Records that don't exist yet? Can I force
Bindata to load the class definition ahead of time somehow?
class Chunk < BinData::Record
endian :little
string :id, length: 4
uint32 :chunk_size
choice :data, selection: :id do
riff_data "RIFF"
array :default,
type: :uint8,
initial_length: -> {chunk_size.odd? ? chunk_size+1 : chunk_size}
end
end
class RiffData < BinData::Record
string :form_type, length: 4
array :chunks, type: :chunk, read_until: :eof
end
class Chunk < BinData::Record
endian :little
string :id, length: 4
uint32 :chunk_size
choice :data, selection: :id do
riff_data "RIFF"
end
end
class SubChunk < BinData::Record
endian :little
string :id, length: 4
uint32 :chunk_size
choice :data, selection: :id do
array :default,
type: :uint8,
initial_length: -> {chunk_size.odd? ? chunk_size+1 : chunk_size}
end
end
class RiffData < BinData::Record
string :form_type, length: 4
array :chunks, type: :sub_chunk, read_until: :eof
end
class Chunk < BinData::Record
endian :little
string :id, length: 4
uint32 :chunk_size
choice :data, selection: :id do
riff_data "RIFF"
array :default,
type: :uint8,
initial_length: -> {chunk_size.odd? ? chunk_size+1 : chunk_size}
end
end
class RiffData < BinData::Record
string :form_type, length: 4
array :chunks, read_until: :eof do
endian :little
string :id, length: 4
uint32 :chunk_size
choice :data, selection: :id do
array :default,
type: :uint8,
initial_length: -> {chunk_size.odd? ? chunk_size+1 : chunk_size}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment