This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new e383adc49 refactor(plc4go/spi): converted WriteBufferByteBased options 
to proper options
e383adc49 is described below

commit e383adc494cea6f57d2b7e55c4cbe9da617c970f
Author: Sebastian Rühl <[email protected]>
AuthorDate: Wed Nov 2 10:23:32 2022 +0100

    refactor(plc4go/spi): converted WriteBufferByteBased options to proper 
options
---
 plc4go/internal/ads/MessageCodec.go                |  3 +-
 plc4go/internal/ads/Writer.go                      |  3 +-
 plc4go/internal/eip/MessageCodec.go                |  3 +-
 plc4go/internal/eip/Reader.go                      |  3 +-
 plc4go/internal/eip/Writer.go                      |  3 +-
 plc4go/spi/MessageCodec.go                         |  2 +-
 plc4go/spi/testutils/DriverTestRunner.go           |  4 +--
 plc4go/spi/testutils/ParserSerializerTestRunner.go |  2 +-
 plc4go/spi/utils/WriteBufferByteBased.go           | 35 ++++++++++++----------
 9 files changed, 34 insertions(+), 24 deletions(-)

diff --git a/plc4go/internal/ads/MessageCodec.go 
b/plc4go/internal/ads/MessageCodec.go
index 4eb6909bf..a5574a20d 100644
--- a/plc4go/internal/ads/MessageCodec.go
+++ b/plc4go/internal/ads/MessageCodec.go
@@ -20,6 +20,7 @@
 package ads
 
 import (
+       "encoding/binary"
        "github.com/apache/plc4x/plc4go/protocols/ads/readwrite/model"
        "github.com/apache/plc4x/plc4go/spi"
        "github.com/apache/plc4x/plc4go/spi/default"
@@ -48,7 +49,7 @@ func (m *MessageCodec) Send(message spi.Message) error {
        // Cast the message to the correct type of struct
        tcpPaket := message.(model.AmsTCPPacket)
        // Serialize the request
-       wb := utils.NewLittleEndianWriteBufferByteBased()
+       wb := 
utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
        err := tcpPaket.Serialize(wb)
        if err != nil {
                return errors.Wrap(err, "error serializing request")
diff --git a/plc4go/internal/ads/Writer.go b/plc4go/internal/ads/Writer.go
index ca76a62fd..9af8af02b 100644
--- a/plc4go/internal/ads/Writer.go
+++ b/plc4go/internal/ads/Writer.go
@@ -21,6 +21,7 @@ package ads
 
 import (
        "context"
+       "encoding/binary"
        "github.com/apache/plc4x/plc4go/pkg/api/model"
        readWriteModel 
"github.com/apache/plc4x/plc4go/protocols/ads/readwrite/model"
        "github.com/apache/plc4x/plc4go/spi"
@@ -105,7 +106,7 @@ func (m *Writer) Write(ctx context.Context, writeRequest 
model.PlcWriteRequest)
 
                // Get the value from the request and serialize it to a byte 
array
                value := writeRequest.GetValue(fieldName)
-               io := utils.NewLittleEndianWriteBufferByteBased()
+               io := 
utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
                if err := readWriteModel.DataItemSerialize(io, value, 
adsField.Datatype.PlcValueType(), adsField.StringLength); err != nil {
                        result <- &plc4goModel.DefaultPlcWriteRequestResult{
                                Request:  writeRequest,
diff --git a/plc4go/internal/eip/MessageCodec.go 
b/plc4go/internal/eip/MessageCodec.go
index 421845329..4e701cf1f 100644
--- a/plc4go/internal/eip/MessageCodec.go
+++ b/plc4go/internal/eip/MessageCodec.go
@@ -20,6 +20,7 @@
 package eip
 
 import (
+       "encoding/binary"
        "github.com/apache/plc4x/plc4go/protocols/eip/readwrite/model"
        "github.com/apache/plc4x/plc4go/spi"
        "github.com/apache/plc4x/plc4go/spi/default"
@@ -48,7 +49,7 @@ func (m *MessageCodec) Send(message spi.Message) error {
        // Cast the message to the correct type of struct
        eipPacket := message.(model.EipPacket)
        // Serialize the request
-       wb := utils.NewLittleEndianWriteBufferByteBased()
+       wb := 
utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
        err := eipPacket.Serialize(wb)
        if err != nil {
                return errors.Wrap(err, "error serializing request")
diff --git a/plc4go/internal/eip/Reader.go b/plc4go/internal/eip/Reader.go
index f20058abb..58eee7bc9 100644
--- a/plc4go/internal/eip/Reader.go
+++ b/plc4go/internal/eip/Reader.go
@@ -21,6 +21,7 @@ package eip
 
 import (
        "context"
+       "encoding/binary"
        "github.com/apache/plc4x/plc4go/pkg/api/model"
        "github.com/apache/plc4x/plc4go/pkg/api/values"
        readWriteModel 
"github.com/apache/plc4x/plc4go/protocols/eip/readwrite/model"
@@ -316,7 +317,7 @@ func toAnsi(tag string) ([]byte, error) {
                }
        }
 
-       buffer := utils.NewLittleEndianWriteBufferByteBased()
+       buffer := 
utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
 
        err := buffer.WriteByte("", 0x91)
        if err != nil {
diff --git a/plc4go/internal/eip/Writer.go b/plc4go/internal/eip/Writer.go
index 86101cedf..6dfd587e8 100644
--- a/plc4go/internal/eip/Writer.go
+++ b/plc4go/internal/eip/Writer.go
@@ -21,6 +21,7 @@ package eip
 
 import (
        "context"
+       "encoding/binary"
        "github.com/apache/plc4x/plc4go/pkg/api/model"
        "github.com/apache/plc4x/plc4go/pkg/api/values"
        readWriteModel 
"github.com/apache/plc4x/plc4go/protocols/eip/readwrite/model"
@@ -276,7 +277,7 @@ func (m Writer) Write(ctx context.Context, writeRequest 
model.PlcWriteRequest) <
 }
 
 func encodeValue(value values.PlcValue, _type readWriteModel.CIPDataTypeCode, 
elements uint16) ([]byte, error) {
-       buffer := utils.NewLittleEndianWriteBufferByteBased()
+       buffer := 
utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
        switch _type {
        case readWriteModel.CIPDataTypeCode_SINT:
                err := buffer.WriteByte("", value.GetUint8())
diff --git a/plc4go/spi/MessageCodec.go b/plc4go/spi/MessageCodec.go
index 2e373ed4a..f757c0a60 100644
--- a/plc4go/spi/MessageCodec.go
+++ b/plc4go/spi/MessageCodec.go
@@ -50,7 +50,7 @@ type MessageCodec interface {
        ConnectWithContext(ctx context.Context) error
        // Disconnect disconnects this codec
        Disconnect() error
-       // IsRunning returns tur if the codec (workers are running)
+       // IsRunning returns true if the codec (workers are running)
        IsRunning() bool
 
        // Send is sending a given message
diff --git a/plc4go/spi/testutils/DriverTestRunner.go 
b/plc4go/spi/testutils/DriverTestRunner.go
index 7604c3adb..9d245341c 100644
--- a/plc4go/spi/testutils/DriverTestRunner.go
+++ b/plc4go/spi/testutils/DriverTestRunner.go
@@ -285,7 +285,7 @@ func (m DriverTestsuite) ExecuteStep(connection 
plc4go.PlcConnection, testcase *
                if m.byteOrder == binary.BigEndian {
                        expectedWriteBuffer = utils.NewWriteBufferByteBased()
                } else {
-                       expectedWriteBuffer = 
utils.NewLittleEndianWriteBufferByteBased()
+                       expectedWriteBuffer = 
utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
                }
                err = expectedSerializable.Serialize(expectedWriteBuffer)
                if err != nil {
@@ -371,7 +371,7 @@ func (m DriverTestsuite) ExecuteStep(connection 
plc4go.PlcConnection, testcase *
                if m.byteOrder == binary.BigEndian {
                        wb = utils.NewWriteBufferByteBased()
                } else {
-                       wb = utils.NewLittleEndianWriteBufferByteBased()
+                       wb = 
utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
                }
                err = expectedSerializable.Serialize(wb)
                if err != nil {
diff --git a/plc4go/spi/testutils/ParserSerializerTestRunner.go 
b/plc4go/spi/testutils/ParserSerializerTestRunner.go
index 4c4b20ed4..e8827c690 100644
--- a/plc4go/spi/testutils/ParserSerializerTestRunner.go
+++ b/plc4go/spi/testutils/ParserSerializerTestRunner.go
@@ -214,7 +214,7 @@ func RunParserSerializerTestsuite(t *testing.T, testPath 
string, skippedTestCase
                                }
                                var writeBuffer utils.WriteBufferByteBased
                                if byteOrder == binary.LittleEndian {
-                                       writeBuffer = 
utils.NewLittleEndianWriteBufferByteBased()
+                                       writeBuffer = 
utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.LittleEndian))
                                } else {
                                        writeBuffer = 
utils.NewWriteBufferByteBased()
                                }
diff --git a/plc4go/spi/utils/WriteBufferByteBased.go 
b/plc4go/spi/utils/WriteBufferByteBased.go
index 8effab5dd..4af268216 100644
--- a/plc4go/spi/utils/WriteBufferByteBased.go
+++ b/plc4go/spi/utils/WriteBufferByteBased.go
@@ -35,32 +35,37 @@ type WriteBufferByteBased interface {
        GetTotalBytes() uint64
 }
 
-func NewWriteBufferByteBased() WriteBufferByteBased {
+func NewWriteBufferByteBased(options ...WriteBufferByteBasedOptions) 
WriteBufferByteBased {
        data := new(bytes.Buffer)
        writer := bitio.NewWriter(data)
-       return &byteWriteBuffer{
+       b := &byteWriteBuffer{
                data:      data,
                writer:    writer,
                byteOrder: binary.BigEndian,
        }
+       for _, option := range options {
+               option(b)
+       }
+       return b
 }
 
-func NewLittleEndianWriteBufferByteBased() WriteBufferByteBased {
-       data := new(bytes.Buffer)
-       writer := bitio.NewWriter(data)
-       return &byteWriteBuffer{
-               data:      data,
-               writer:    writer,
-               byteOrder: binary.LittleEndian,
+type WriteBufferByteBasedOptions = func(b *byteWriteBuffer)
+
+func WithInitialSizeForByteBasedBuffer(length int) WriteBufferByteBasedOptions 
{
+       return func(b *byteWriteBuffer) {
+               b.data = bytes.NewBuffer(make([]byte, length))
        }
 }
 
-func NewCustomWriteBufferByteBased(buffer *bytes.Buffer, byteOrder 
binary.ByteOrder) WriteBufferByteBased {
-       writer := bitio.NewWriter(buffer)
-       return &byteWriteBuffer{
-               data:      buffer,
-               writer:    writer,
-               byteOrder: byteOrder,
+func WithByteOrderForByteBasedBuffer(byteOrder binary.ByteOrder) 
WriteBufferByteBasedOptions {
+       return func(b *byteWriteBuffer) {
+               b.byteOrder = byteOrder
+       }
+}
+
+func WithCustomBufferForByteBasedBuffer(buffer *bytes.Buffer) 
WriteBufferByteBasedOptions {
+       return func(b *byteWriteBuffer) {
+               b.data = buffer
        }
 }
 

Reply via email to