Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Jun 26, 2023
1 parent 12ea719 commit e6994bd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 42 deletions.
42 changes: 17 additions & 25 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ var TypeSize = struct {

PublicKey int
Signature int

Tstamp int
BlockTimestamp int

CurrencyName int
}{
Byte: 1,
Bool: 1,
Expand All @@ -69,9 +64,11 @@ var TypeSize = struct {

Float32: 4,
Float64: 8,

PublicKey: 32,
Signature: 64,
}

// Decoder implements the EOS unpacking, similar to FC_BUFFER
type Decoder struct {
data []byte
pos int
Expand Down Expand Up @@ -281,11 +278,6 @@ func (dec *Decoder) ReadLength() (length int, err error) {
return
}

type peekAbleByteReader interface {
io.ByteReader
Peek(n int) ([]byte, error)
}

func readNBytes(n int, reader *Decoder) ([]byte, error) {
if n == 0 {
return make([]byte, 0), nil
Expand Down Expand Up @@ -481,15 +473,6 @@ func (dec *Decoder) ReadInt16(order binary.ByteOrder) (out int16, err error) {
return
}

func (dec *Decoder) ReadInt64(order binary.ByteOrder) (out int64, err error) {
n, err := dec.ReadUint64(order)
out = int64(n)
if traceEnabled {
zlog.Debug("decode: read int64", zap.Int64("val", out))
}
return
}

func (dec *Decoder) ReadUint32(order binary.ByteOrder) (out uint32, err error) {
if dec.Remaining() < TypeSize.Uint32 {
err = fmt.Errorf("uint32 required [%d] bytes, remaining [%d]", TypeSize.Uint32, dec.Remaining())
Expand Down Expand Up @@ -530,12 +513,13 @@ func (dec *Decoder) ReadUint64(order binary.ByteOrder) (out uint64, err error) {
return
}

func (dec *Decoder) ReadInt128(order binary.ByteOrder) (out Int128, err error) {
v, err := dec.ReadUint128(order)
if err != nil {
return
func (dec *Decoder) ReadInt64(order binary.ByteOrder) (out int64, err error) {
n, err := dec.ReadUint64(order)
out = int64(n)
if traceEnabled {
zlog.Debug("decode: read int64", zap.Int64("val", out))
}
return Int128(v), nil
return
}

func (dec *Decoder) ReadUint128(order binary.ByteOrder) (out Uint128, err error) {
Expand All @@ -562,6 +546,14 @@ func (dec *Decoder) ReadUint128(order binary.ByteOrder) (out Uint128, err error)
return
}

func (dec *Decoder) ReadInt128(order binary.ByteOrder) (out Int128, err error) {
v, err := dec.ReadUint128(order)
if err != nil {
return
}
return Int128(v), nil
}

func (dec *Decoder) ReadFloat32(order binary.ByteOrder) (out float32, err error) {
if dec.Remaining() < TypeSize.Float32 {
err = fmt.Errorf("float32 required [%d] bytes, remaining [%d]", TypeSize.Float32, dec.Remaining())
Expand Down
34 changes: 19 additions & 15 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import (
)

type Encoder struct {
output io.Writer
count int
count int

currentFieldOpt *option
encoding Encoding

encoding Encoding
output io.Writer
}

func (enc *Encoder) IsBorsh() bool {
Expand Down Expand Up @@ -227,13 +227,6 @@ func (e *Encoder) WriteInt16(i int16, order binary.ByteOrder) (err error) {
return e.WriteUint16(uint16(i), order)
}

func (e *Encoder) WriteInt32(i int32, order binary.ByteOrder) (err error) {
if traceEnabled {
zlog.Debug("encode: write int32", zap.Int32("val", i))
}
return e.WriteUint32(uint32(i), order)
}

func (e *Encoder) WriteUint32(i uint32, order binary.ByteOrder) (err error) {
if traceEnabled {
zlog.Debug("encode: write uint32", zap.Uint32("val", i))
Expand All @@ -243,11 +236,11 @@ func (e *Encoder) WriteUint32(i uint32, order binary.ByteOrder) (err error) {
return e.toWriter(buf)
}

func (e *Encoder) WriteInt64(i int64, order binary.ByteOrder) (err error) {
func (e *Encoder) WriteInt32(i int32, order binary.ByteOrder) (err error) {
if traceEnabled {
zlog.Debug("encode: write int64", zap.Int64("val", i))
zlog.Debug("encode: write int32", zap.Int32("val", i))
}
return e.WriteUint64(uint64(i), order)
return e.WriteUint32(uint32(i), order)
}

func (e *Encoder) WriteUint64(i uint64, order binary.ByteOrder) (err error) {
Expand All @@ -259,6 +252,13 @@ func (e *Encoder) WriteUint64(i uint64, order binary.ByteOrder) (err error) {
return e.toWriter(buf)
}

func (e *Encoder) WriteInt64(i int64, order binary.ByteOrder) (err error) {
if traceEnabled {
zlog.Debug("encode: write int64", zap.Int64("val", i))
}
return e.WriteUint64(uint64(i), order)
}

func (e *Encoder) WriteUint128(i Uint128, order binary.ByteOrder) (err error) {
if traceEnabled {
zlog.Debug("encode: write uint128", zap.Stringer("hex", i), zap.Uint64("lo", i.Lo), zap.Uint64("hi", i.Hi))
Expand Down Expand Up @@ -348,15 +348,19 @@ func (e *Encoder) WriteRustString(s string) (err error) {
return e.WriteBytes([]byte(s), false)
}

func (e *Encoder) WriteCompactU16Length(ln int) (err error) {
func (e *Encoder) WriteCompactU16(ln int) (err error) {
if traceEnabled {
zlog.Debug("encode: write compact-u16 length", zap.Int("val", ln))
zlog.Debug("encode: write compact-u16", zap.Int("val", ln))
}
buf := make([]byte, 0)
EncodeCompactU16Length(&buf, ln)
return e.toWriter(buf)
}

func (e *Encoder) WriteCompactU16Length(ln int) (err error) {
return e.WriteCompactU16(ln)
}

func reflect_writeArrayOfBytes(e *Encoder, l int, rv reflect.Value) error {
arr := make([]byte, l)
for i := 0; i < l; i++ {
Expand Down
4 changes: 2 additions & 2 deletions encoder_borsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ type BorshEnum uint8
// EmptyVariant is an empty borsh enum variant.
type EmptyVariant struct{}

func (_ *EmptyVariant) MarshalWithEncoder(_ *Encoder) error {
func (*EmptyVariant) MarshalWithEncoder(_ *Encoder) error {
return nil
}

func (_ *EmptyVariant) UnmarshalWithDecoder(_ *Decoder) error {
func (*EmptyVariant) UnmarshalWithDecoder(_ *Decoder) error {
return nil
}

Expand Down

0 comments on commit e6994bd

Please sign in to comment.