From cac65364fa6074b0856d3521c79dc345546ad377 Mon Sep 17 00:00:00 2001 From: Mikhail Yakshin Date: Fri, 29 Mar 2024 19:56:43 +0000 Subject: [PATCH 1/4] Implement kaitai.Struct: common interface mandating Kaitai_IO() method on all ksc-generated types --- go.mod | 5 ++++- go.sum | 18 +++++++++++++++++ kaitai/struct.go | 7 +++++++ kaitai/struct_test.go | 46 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 kaitai/struct.go create mode 100644 kaitai/struct_test.go 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..811c536 --- /dev/null +++ b/kaitai/struct.go @@ -0,0 +1,7 @@ +package kaitai + +// Struct is the common interface guaranteed to be implemented by all types generated +// by Kaitai Struct compiler. +type Struct interface { + Kaitai_IO() *Stream +} diff --git a/kaitai/struct_test.go b/kaitai/struct_test.go new file mode 100644 index 0000000..59deca3 --- /dev/null +++ b/kaitai/struct_test.go @@ -0,0 +1,46 @@ +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) Kaitai_IO() *Stream { + return s._io +} + +type twoStruct struct { + two int + _io *Stream +} + +func (s *twoStruct) Kaitai_IO() *Stream { + return s._io +} + +func WorkWithStruct(s Struct, t *testing.T, expectedSize int) { + actualSize, err := s.Kaitai_IO().Size() + assert.Equal(t, actualSize, int64(expectedSize)) + assert.Nil(t, err) +} + +func TestKaitaiStruct(t *testing.T) { + // Instantiate streams for the structs + oneStream := NewStream(bytes.NewReader([]byte("a"))) + twoStream := NewStream(bytes.NewReader([]byte("ab"))) + + // Instantiate the structs + one := oneStruct{1, oneStream} + two := twoStruct{2, twoStream} + + // Check if the structs implement the Struct interface + WorkWithStruct(&one, t, 1) + WorkWithStruct(&two, t, 2) +} From cfc42b9ca72a0ca5df173e832d279719533fb566 Mon Sep 17 00:00:00 2001 From: Mikhail Yakshin Date: Fri, 29 Mar 2024 21:19:58 +0000 Subject: [PATCH 2/4] Make test less obscure as per PR discussion --- kaitai/struct_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/kaitai/struct_test.go b/kaitai/struct_test.go index 59deca3..9c98dba 100644 --- a/kaitai/struct_test.go +++ b/kaitai/struct_test.go @@ -27,20 +27,20 @@ func (s *twoStruct) Kaitai_IO() *Stream { func WorkWithStruct(s Struct, t *testing.T, expectedSize int) { actualSize, err := s.Kaitai_IO().Size() - assert.Equal(t, actualSize, int64(expectedSize)) 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"))) - twoStream := NewStream(bytes.NewReader([]byte("ab"))) + oneStream := NewStream(bytes.NewReader([]byte("a quick"))) + twoStream := NewStream(bytes.NewReader([]byte("brown fox"))) // Instantiate the structs - one := oneStruct{1, oneStream} - two := twoStruct{2, twoStream} + one := oneStruct{111, oneStream} + two := twoStruct{222, twoStream} // Check if the structs implement the Struct interface - WorkWithStruct(&one, t, 1) - WorkWithStruct(&two, t, 2) + WorkWithStruct(&one, t, 7) + WorkWithStruct(&two, t, 9) } From 9f594fdea4cd921695d2a8089d3fe793e0f2ab67 Mon Sep 17 00:00:00 2001 From: Mikhail Yakshin Date: Fri, 29 Mar 2024 23:18:05 +0000 Subject: [PATCH 3/4] Fix lint problems, add some explanations in the docstrings --- .golangci.yml | 2 ++ kaitai/struct.go | 9 +++++++-- kaitai/struct_test.go | 8 +++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 6f91f94..9d5b4e2 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -87,3 +87,5 @@ 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 Kaitai_IO should be KaitaiIO" + - "ST1003: should not use underscores in Go names; method Kaitai_IO should be KaitaiIO" diff --git a/kaitai/struct.go b/kaitai/struct.go index 811c536..0facd02 100644 --- a/kaitai/struct.go +++ b/kaitai/struct.go @@ -1,7 +1,12 @@ package kaitai -// Struct is the common interface guaranteed to be implemented by all types generated -// by Kaitai Struct compiler. +// Struct is the common interface guaranteed to be implemented by all types generated by +// Kaitai Struct compiler. type Struct interface { + // Kaitai_IO returns the stream object associated with the struct. + // + // This is deliberately named with a `Kaitai_` prefix and underscore to avoid conflicts + // with other methods that may result from the attributes in Kaitai Struct type, e.g. + // is a user will define attribute `i_o` this will conflict with the method `IO()`. Kaitai_IO() *Stream } diff --git a/kaitai/struct_test.go b/kaitai/struct_test.go index 9c98dba..250fc02 100644 --- a/kaitai/struct_test.go +++ b/kaitai/struct_test.go @@ -3,6 +3,7 @@ package kaitai import ( "bytes" "testing" + "github.com/stretchr/testify/assert" ) @@ -25,7 +26,8 @@ func (s *twoStruct) Kaitai_IO() *Stream { return s._io } -func WorkWithStruct(s Struct, t *testing.T, expectedSize int) { +func WorkWithStruct(t *testing.T, s Struct, expectedSize int) { + t.Helper() actualSize, err := s.Kaitai_IO().Size() assert.Nil(t, err) assert.Equal(t, actualSize, int64(expectedSize)) @@ -41,6 +43,6 @@ func TestKaitaiStruct(t *testing.T) { two := twoStruct{222, twoStream} // Check if the structs implement the Struct interface - WorkWithStruct(&one, t, 7) - WorkWithStruct(&two, t, 9) + WorkWithStruct(t, &one, 7) + WorkWithStruct(t, &two, 9) } From af7c638b22676288a197da5b4f4c54c9c6757213 Mon Sep 17 00:00:00 2001 From: Mikhail Yakshin Date: Sat, 30 Mar 2024 21:16:39 +0000 Subject: [PATCH 4/4] Renamed `Kaitai_IO` -> `IO_` as per PR discussion --- .golangci.yml | 3 +-- kaitai/struct.go | 10 +++++----- kaitai/struct_test.go | 6 +++--- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 9d5b4e2..fae3138 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -87,5 +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 Kaitai_IO should be KaitaiIO" - - "ST1003: should not use underscores in Go names; method Kaitai_IO should be KaitaiIO" + - "var-naming: don't use underscores in Go names; method IO_ should be IO" diff --git a/kaitai/struct.go b/kaitai/struct.go index 0facd02..9a49878 100644 --- a/kaitai/struct.go +++ b/kaitai/struct.go @@ -3,10 +3,10 @@ package kaitai // Struct is the common interface guaranteed to be implemented by all types generated by // Kaitai Struct compiler. type Struct interface { - // Kaitai_IO returns the stream object associated with the struct. + // IO_ returns the stream object associated with the struct. // - // This is deliberately named with a `Kaitai_` prefix and underscore to avoid conflicts - // with other methods that may result from the attributes in Kaitai Struct type, e.g. - // is a user will define attribute `i_o` this will conflict with the method `IO()`. - Kaitai_IO() *Stream + // 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 index 250fc02..7cdf9c0 100644 --- a/kaitai/struct_test.go +++ b/kaitai/struct_test.go @@ -13,7 +13,7 @@ type oneStruct struct { _io *Stream } -func (s *oneStruct) Kaitai_IO() *Stream { +func (s *oneStruct) IO_() *Stream { return s._io } @@ -22,13 +22,13 @@ type twoStruct struct { _io *Stream } -func (s *twoStruct) Kaitai_IO() *Stream { +func (s *twoStruct) IO_() *Stream { return s._io } func WorkWithStruct(t *testing.T, s Struct, expectedSize int) { t.Helper() - actualSize, err := s.Kaitai_IO().Size() + actualSize, err := s.IO_().Size() assert.Nil(t, err) assert.Equal(t, actualSize, int64(expectedSize)) }