From 1ea421d2797671324a91447f6d5cb78ab07f9e4b Mon Sep 17 00:00:00 2001 From: Christian Ege Date: Sun, 24 Dec 2023 12:46:31 +0100 Subject: [PATCH] refactor: move the Chunk into the pcic pakage Signed-off-by: Christian Ege --- pkg/{chunk => pcic}/chunk.go | 59 ++++++++++++++++--------------- pkg/{chunk => pcic}/chunk_test.go | 18 ++++++---- pkg/pcic/frame.go | 9 +++-- pkg/pcic/protocol.go | 4 +-- pkg/pcic/protocol_test.go | 5 ++- 5 files changed, 50 insertions(+), 45 deletions(-) rename pkg/{chunk => pcic}/chunk.go (87%) rename pkg/{chunk => pcic}/chunk_test.go (95%) diff --git a/pkg/chunk/chunk.go b/pkg/pcic/chunk.go similarity index 87% rename from pkg/chunk/chunk.go rename to pkg/pcic/chunk.go index 360132d..b48d929 100644 --- a/pkg/chunk/chunk.go +++ b/pkg/pcic/chunk.go @@ -1,4 +1,4 @@ -package chunk +package pcic import ( "encoding/binary" @@ -7,8 +7,11 @@ import ( "time" ) -type ChunkType uint32 -type DataFormat uint32 +type ( + ChunkType uint32 + DataFormat uint32 + ChunkOption func(c *Chunk) +) // The known Chunk Types const ( @@ -33,19 +36,8 @@ var ( byteSizeLUT [FORMAT_MAX]uint32 = [FORMAT_MAX]uint32{1, 1, 2, 2, 4, 4, 4, 8, 8} ) -type Chunk interface { - UnmarshalBinary(data []byte) error - MarshalBinary() (data []byte, err error) - Type() ChunkType - Size() uint32 - FrameCount() uint32 - Status() uint32 - TimeStamp() time.Time - Bytes() []byte -} - -// A ChunkData object contains all the information based on a PCIC chunk -type ChunkData struct { +// A Chunk object contains all the information based on a PCIC chunk +type Chunk struct { chunkType ChunkType // The type of the Chunk, each chunk type requires a unique ID chunkSize uint32 // The size of the complete chunk, including the header and the data headerSize uint32 // The Size of the chunk header after this amount of bytes the data section starts @@ -82,43 +74,52 @@ const ( MaxSupportedChunkHeaderVersion = 3 ) -func New(cType ChunkType) *ChunkData { - chunk := &ChunkData{ - chunkType: cType, - metadata: "{}", - data: []byte{}, +func NewChunk(options ...ChunkOption) *Chunk { + chunk := &Chunk{ + metadata: "{}", + data: []byte{}, + } + // Apply options + for _, opt := range options { + opt(chunk) } return chunk } -func (c *ChunkData) Type() ChunkType { +func WithChunkType(cType ChunkType) ChunkOption { + return func(c *Chunk) { + c.chunkType = cType + } +} + +func (c *Chunk) Type() ChunkType { return c.chunkType } -func (c *ChunkData) Size() int { +func (c *Chunk) Size() int { return int(c.chunkSize) } -func (c *ChunkData) FrameCount() uint32 { +func (c *Chunk) FrameCount() uint32 { return c.frameCount } -func (c *ChunkData) Status() uint32 { +func (c *Chunk) Status() uint32 { return c.statusCode } -func (c *ChunkData) TimeStamp() time.Time { +func (c *Chunk) TimeStamp() time.Time { return time.Unix(int64(c.timestampSec), int64(c.timestampNSec)) } -func (c *ChunkData) Bytes() []byte { +func (c *Chunk) Bytes() []byte { return c.data } // MarshalBinary creates a binary representation of the Chunk // // The binary representation is encoded in the byte slice -func (c *ChunkData) MarshalBinary() (data []byte, err error) { +func (c *Chunk) MarshalBinary() (data []byte, err error) { return []byte{}, nil } @@ -126,7 +127,7 @@ func (c *ChunkData) MarshalBinary() (data []byte, err error) { // // It copies the data from the input slice to comply with the BinaryUnmarshaler // interface. -func (c *ChunkData) UnmarshalBinary(data []byte) error { +func (c *Chunk) UnmarshalBinary(data []byte) error { dataLen := uint32(len(data)) if dataLen < offsetOfData { return errors.New("unable to parse an empty input") diff --git a/pkg/chunk/chunk_test.go b/pkg/pcic/chunk_test.go similarity index 95% rename from pkg/chunk/chunk_test.go rename to pkg/pcic/chunk_test.go index fab0adf..880c4de 100644 --- a/pkg/chunk/chunk_test.go +++ b/pkg/pcic/chunk_test.go @@ -1,24 +1,24 @@ -package chunk_test +package pcic_test import ( "testing" "time" - "github.com/graugans/go-ovp8xx/pkg/chunk" + "github.com/graugans/go-ovp8xx/pkg/pcic" "github.com/stretchr/testify/assert" ) func TestChunkType(t *testing.T) { - c := chunk.New(chunk.RADIAL_DISTANCE_NOISE) + c := pcic.NewChunk(pcic.WithChunkType(pcic.RADIAL_DISTANCE_NOISE)) assert.Equal(t, - chunk.RADIAL_DISTANCE_NOISE, + pcic.RADIAL_DISTANCE_NOISE, c.Type(), "There is a chunk type mismatch detected", ) } -func TestChunkParse(t *testing.T) { - c := chunk.ChunkData{} +func TestUnmarshalBinary(t *testing.T) { + c := pcic.Chunk{} assert.Error(t, c.UnmarshalBinary([]byte{}), "An error is expected when sending an empty byte slice", @@ -58,7 +58,7 @@ func TestChunkParse(t *testing.T) { "A successful parse expected", ) assert.Equal(t, - chunk.RADIAL_DISTANCE_NOISE, + pcic.RADIAL_DISTANCE_NOISE, c.Type(), "Type mismatch detected", ) @@ -182,3 +182,7 @@ func TestChunkParse(t *testing.T) { "A data size mismatch occurred", ) } + +func TestRoundtrip(t *testing.T) { + +} diff --git a/pkg/pcic/frame.go b/pkg/pcic/frame.go index f0f7c5a..828ea9e 100644 --- a/pkg/pcic/frame.go +++ b/pkg/pcic/frame.go @@ -1,7 +1,10 @@ package pcic -import "github.com/graugans/go-ovp8xx/pkg/chunk" - type Frame struct { - Chunks []chunk.ChunkData + Chunks []Chunk + size int +} + +func (f *Frame) Size() int { + return f.size } diff --git a/pkg/pcic/protocol.go b/pkg/pcic/protocol.go index 88f3481..3e693eb 100644 --- a/pkg/pcic/protocol.go +++ b/pkg/pcic/protocol.go @@ -5,8 +5,6 @@ import ( "errors" "fmt" "io" - - "github.com/graugans/go-ovp8xx/pkg/chunk" ) type PCIC struct { @@ -152,7 +150,7 @@ func asyncResultParser(data []byte) (Frame, error) { remainingBytes := len(content) offset := 0 for remainingBytes > 0 { - c := chunk.ChunkData{} + c := Chunk{} if err := c.UnmarshalBinary(content[offset:]); err != nil { return frame, err } diff --git a/pkg/pcic/protocol_test.go b/pkg/pcic/protocol_test.go index 45055cc..d518352 100644 --- a/pkg/pcic/protocol_test.go +++ b/pkg/pcic/protocol_test.go @@ -10,7 +10,6 @@ import ( "strings" "testing" - "github.com/graugans/go-ovp8xx/pkg/chunk" "github.com/graugans/go-ovp8xx/pkg/pcic" "github.com/stretchr/testify/assert" ) @@ -53,7 +52,7 @@ func TestMinimalReceive(t *testing.T) { } func TestReceiveWithChunk(t *testing.T) { - c := chunk.ChunkData{} + c := pcic.Chunk{} chunkData := []byte{ 0x69, 0x00, 0x00, 0x00, /* CHUNK_TYPE */ 0x34, 0x00, 0x00, 0x00, /* CHUNK_SIZE */ @@ -85,7 +84,7 @@ func TestReceiveWithChunk(t *testing.T) { assert.NoError(t, err, "We expect no error while receiving data") assert.Equal(t, - chunk.RADIAL_DISTANCE_NOISE, + pcic.RADIAL_DISTANCE_NOISE, testHandler.frame.Chunks[0].Type(), )