From c5ccbe6b63d53611ff8e4c31f45801c233ed98ee Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 13 Nov 2018 22:46:01 +0100 Subject: [PATCH] cleanup serial package --- common/serial/serial.go | 4 ++++ common/serial/string.go | 7 +------ common/serial/typed_message.go | 4 ++++ common/serial/typed_message_test.go | 7 +++++++ 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/common/serial/serial.go b/common/serial/serial.go index e58b3a36..3c196af1 100644 --- a/common/serial/serial.go +++ b/common/serial/serial.go @@ -5,6 +5,7 @@ import ( "io" ) +// ReadUint16 reads first two bytes from the reader, and then coverts them to an uint16 value. func ReadUint16(reader io.Reader) (uint16, error) { var b [2]byte if _, err := io.ReadFull(reader, b[:]); err != nil { @@ -13,18 +14,21 @@ func ReadUint16(reader io.Reader) (uint16, error) { return binary.BigEndian.Uint16(b[:]), nil } +// WriteUint16 writes an uint16 value into writer. func WriteUint16(writer io.Writer, value uint16) (int, error) { var b [2]byte binary.BigEndian.PutUint16(b[:], value) return writer.Write(b[:]) } +// WriteUint32 writes an uint32 value into writer. func WriteUint32(writer io.Writer, value uint32) (int, error) { var b [4]byte binary.BigEndian.PutUint32(b[:], value) return writer.Write(b[:]) } +// WriteUint64 writes an uint64 value into writer. func WriteUint64(writer io.Writer, value uint64) (int, error) { var b [8]byte binary.BigEndian.PutUint64(b[:], value) diff --git a/common/serial/string.go b/common/serial/string.go index fa9fe098..70de87a7 100644 --- a/common/serial/string.go +++ b/common/serial/string.go @@ -25,6 +25,7 @@ func ToString(v interface{}) string { } } +// Concat concatenates all input into a single string. func Concat(v ...interface{}) string { builder := strings.Builder{} for _, value := range v { @@ -32,9 +33,3 @@ func Concat(v ...interface{}) string { } return builder.String() } - -func WriteString(s string) func([]byte) (int, error) { - return func(b []byte) (int, error) { - return copy(b, []byte(s)), nil - } -} diff --git a/common/serial/typed_message.go b/common/serial/typed_message.go index 197e4193..6f78506b 100644 --- a/common/serial/typed_message.go +++ b/common/serial/typed_message.go @@ -7,6 +7,7 @@ import ( "github.com/golang/protobuf/proto" ) +// ToTypeMessage converts a proto Message into TypedMessage. func ToTypedMessage(message proto.Message) *TypedMessage { if message == nil { return nil @@ -18,10 +19,12 @@ func ToTypedMessage(message proto.Message) *TypedMessage { } } +// GetMessageType returns the name of this proto Message. func GetMessageType(message proto.Message) string { return proto.MessageName(message) } +// GetInstance creates a new instance of the message with messageType. func GetInstance(messageType string) (interface{}, error) { mType := proto.MessageType(messageType) if mType == nil || mType.Elem() == nil { @@ -30,6 +33,7 @@ func GetInstance(messageType string) (interface{}, error) { return reflect.New(mType.Elem()).Interface(), nil } +// GetInstance converts current TypedMessage into a proto Message. func (v *TypedMessage) GetInstance() (proto.Message, error) { instance, err := GetInstance(v.Type) if err != nil { diff --git a/common/serial/typed_message_test.go b/common/serial/typed_message_test.go index 4bf7c540..084beafc 100644 --- a/common/serial/typed_message_test.go +++ b/common/serial/typed_message_test.go @@ -14,3 +14,10 @@ func TestGetInstance(t *testing.T) { assert(p, IsNil) assert(err, IsNotNil) } + +func TestConvertingNilMessage(t *testing.T) { + x := ToTypedMessage(nil) + if x != nil { + t.Error("expect nil, but actually not") + } +}