From 1ae6f5532f8f22a90106cf1d42e5a842b2d83c5c Mon Sep 17 00:00:00 2001 From: Joel Ling Date: Sat, 18 Dec 2021 16:07:06 +0900 Subject: [PATCH] restore standard library features --- binary.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++ metadata-format.go | 3 +++ 2 files changed, 64 insertions(+) diff --git a/binary.go b/binary.go index 16a7447..0a08e4c 100644 --- a/binary.go +++ b/binary.go @@ -1,7 +1,9 @@ package binary import ( + stdlib "encoding/binary" "fmt" + "io" "github.com/encodingx/binary/internal/validation" ) @@ -85,3 +87,62 @@ func Unmarshal(bytes []byte, iface interface{}) (e error) { return } + +// Standard library features + +const ( + MaxVarintLen16 = stdlib.MaxVarintLen16 + MaxVarintLen32 = stdlib.MaxVarintLen32 + MaxVarintLen64 = stdlib.MaxVarintLen64 +) + +var ( + BigEndian = stdlib.BigEndian + LittleEndian = stdlib.LittleEndian +) + +type ByteOrder interface { + Uint16([]byte) uint16 + Uint32([]byte) uint32 + Uint64([]byte) uint64 + PutUint16([]byte, uint16) + PutUint32([]byte, uint32) + PutUint64([]byte, uint64) + String() string +} + +func PutUvarint(buf []byte, x uint64) int { + return stdlib.PutUvarint(buf, x) +} + +func PutVarint(buf []byte, x int64) int { + return stdlib.PutVarint(buf, x) +} + +func Read(r io.Reader, order ByteOrder, data interface{}) error { + return stdlib.Read(r, order, data) +} + +func ReadUvarint(r io.ByteReader) (uint64, error) { + return stdlib.ReadUvarint(r) +} + +func ReadVarint(r io.ByteReader) (int64, error) { + return stdlib.ReadVarint(r) +} + +func Size(v interface{}) int { + return stdlib.Size(v) +} + +func Uvarint(buf []byte) (uint64, int) { + return stdlib.Uvarint(buf) +} + +func Varint(buf []byte) (int64, int) { + return stdlib.Varint(buf) +} + +func Write(w io.Writer, order ByteOrder, data interface{}) error { + return stdlib.Write(w, order, data) +} diff --git a/metadata-format.go b/metadata-format.go index 30ac863..0bd48e6 100644 --- a/metadata-format.go +++ b/metadata-format.go @@ -53,6 +53,9 @@ func newFormatMetadataFromTypeReflection(reflection reflect.Type) ( } func (m formatMetadata) marshal(reflection reflect.Value) (bytes []byte) { + // Merge byte slices marshalled from words, + // in the order they appear in the format. + var ( copyIndex int i int