Skip to content

Commit

Permalink
Implement kaitai.Struct: common interface mandating Kaitai_IO() metho…
Browse files Browse the repository at this point in the history
…d on all ksc-generated types
  • Loading branch information
GreyCat committed Mar 29, 2024
1 parent 296c155 commit cac6536
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
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
}
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))
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)
}

0 comments on commit cac6536

Please sign in to comment.