Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement kaitai.Struct common interface #30

Merged
merged 4 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down Expand Up @@ -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=
7 changes: 7 additions & 0 deletions kaitai/struct.go
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm. I think this is fairly unideal, though it's not obvious how one could do better without changing the attribute name mangling.

Other potential bikeshed paint colors: IO_(), KaitaiIO_()

Though maybe special casing kaitai_i_o in the compiler is not a bad idea. It's not like this is likely to come up in non-contrived examples anyways...

Copy link
Member

@generalmimon generalmimon Mar 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IO_()

This is my new favorite. I knew that if you start the identifier with an underscore _, then it doesn't start with a capital letter and won't be exported, but I didn't think of ending the identifier with an underscore _. It's simple and I suppose it potentially works better with autocompletion (assuming that only identifiers starting with what you type are shown) - in case a user would sometimes need to refer to _io, _parent or _root, they can start typing I, Par or Ro and will be offered IO_(), Parent_() and Root_() respectively, and they don't have to remember that there is a special Kaitai_ prefix.

Though maybe special casing kaitai_i_o in the compiler is not a bad idea. It's not like this is likely to come up in non-contrived examples anyways...

This may be true in practice, but I still advocate that there should be no "holes" in the language. There are already some holes in our Go implementation that I'm not happy about (e.g. the Read method - hello_world.go:18, which will clash with a user-specified instance called read), but at least I'd avoid adding more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I also like the idea of IO_(). Just to get an Go expert opinion: how bad it will be from ecosystem perspective — e.g. it will generate code that will have to break varnames linting? How serious it is?

On the side note, to be honest, I won't mind breaking compat and turning Go Read method into Read_ for example (given that vast majority of other languages generates _read() or some similar).

}
46 changes: 46 additions & 0 deletions kaitai/struct_test.go
Original file line number Diff line number Diff line change
@@ -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))
GreyCat marked this conversation as resolved.
Show resolved Hide resolved
assert.Nil(t, err)
GreyCat marked this conversation as resolved.
Show resolved Hide resolved
}

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)
GreyCat marked this conversation as resolved.
Show resolved Hide resolved
}
Loading