-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement kaitai.Struct common interface (#30)
* Implement kaitai.Struct: common interface mandating `IO_()` method on all ksc-generated types. * Work around lint problems
- Loading branch information
Showing
5 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package kaitai | ||
|
||
// Struct is the common interface guaranteed to be implemented by all types generated by | ||
// Kaitai Struct compiler. | ||
type Struct interface { | ||
// IO_ returns the stream object associated with the struct. | ||
// | ||
// This is deliberately named with a `_` suffix to avoid conflicts with other methods | ||
// that may result from the attributes in Kaitai Struct type, e.g. if a user will define | ||
// attribute `i_o` this will conflict with the method `IO()`. | ||
IO_() *Stream | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package kaitai | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// Declare two struct simulating types generated by Kaitai Struct compiler | ||
type oneStruct struct { | ||
one int | ||
_io *Stream | ||
} | ||
|
||
func (s *oneStruct) IO_() *Stream { | ||
return s._io | ||
} | ||
|
||
type twoStruct struct { | ||
two int | ||
_io *Stream | ||
} | ||
|
||
func (s *twoStruct) IO_() *Stream { | ||
return s._io | ||
} | ||
|
||
func WorkWithStruct(t *testing.T, s Struct, expectedSize int) { | ||
t.Helper() | ||
actualSize, err := s.IO_().Size() | ||
assert.Nil(t, err) | ||
assert.Equal(t, actualSize, int64(expectedSize)) | ||
} | ||
|
||
func TestKaitaiStruct(t *testing.T) { | ||
// Instantiate streams for the structs | ||
oneStream := NewStream(bytes.NewReader([]byte("a quick"))) | ||
twoStream := NewStream(bytes.NewReader([]byte("brown fox"))) | ||
|
||
// Instantiate the structs | ||
one := oneStruct{111, oneStream} | ||
two := twoStruct{222, twoStream} | ||
|
||
// Check if the structs implement the Struct interface | ||
WorkWithStruct(t, &one, 7) | ||
WorkWithStruct(t, &two, 9) | ||
} |