From 47c8778d3cc077ed53d2422cfe77c51c73d14ab7 Mon Sep 17 00:00:00 2001 From: Mikhail Yakshin Date: Sun, 31 Mar 2024 23:14:02 +0100 Subject: [PATCH] Implement kaitai.Struct common interface (#30) * Implement kaitai.Struct: common interface mandating `IO_()` method on all ksc-generated types. * Work around lint problems --- .golangci.yml | 1 + go.mod | 5 ++++- go.sum | 18 ++++++++++++++++ kaitai/struct.go | 12 +++++++++++ kaitai/struct_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 kaitai/struct.go create mode 100644 kaitai/struct_test.go diff --git a/.golangci.yml b/.golangci.yml index 6f91f94..fae3138 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -87,3 +87,4 @@ issues: # excluded by default patterns execute `golangci-lint run --help` exclude: - Using the variable on range scope `tt` in function literal + - "var-naming: don't use underscores in Go names; method IO_ should be IO" diff --git a/go.mod b/go.mod index 747eef3..6a0e40b 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/kaitai-io/kaitai_struct_go_runtime go 1.13 -require golang.org/x/text v0.14.0 +require ( + github.com/stretchr/testify v1.9.0 // indirect + golang.org/x/text v0.14.0 +) diff --git a/go.sum b/go.sum index 4303ed2..f6312f8 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,17 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -30,3 +44,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/kaitai/struct.go b/kaitai/struct.go new file mode 100644 index 0000000..9a49878 --- /dev/null +++ b/kaitai/struct.go @@ -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 +} diff --git a/kaitai/struct_test.go b/kaitai/struct_test.go new file mode 100644 index 0000000..7cdf9c0 --- /dev/null +++ b/kaitai/struct_test.go @@ -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) +}