Skip to content

Commit

Permalink
Some deprecation notices
Browse files Browse the repository at this point in the history
- Renamed `TypeSize.UInt8` to `TypeSize.Uint8`
- Renamed `TypeSize.UInt16` to `TypeSize.Uint16`
- Renamed `TypeSize.UInt32` to `TypeSize.Uint32`
- Renamed `TypeSize.UInt64` to `TypeSize.Uint64`
- Renamed `TypeSize.UInt128` to `TypeSize.Uint128`
- Renamed `Decoder.ReadUInt8` to `Decoder.ReadUint8`
  • Loading branch information
Matthieu Vachon committed Jan 22, 2020
1 parent 6e70b6c commit cf2f85f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 58 deletions.
144 changes: 93 additions & 51 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,81 @@ type UnmarshalerBinary interface {
}

var TypeSize = struct {
Byte int
Int8 int
UInt8 int
UInt16 int
Int16 int
UInt32 int
UInt64 int
UInt128 int
Float32 int
Float64 int
Checksum160 int
Checksum256 int
Checksum512 int
PublicKey int
Signature int
Bool int
Byte int

Int8 int
Int16 int

Uint8 int
Uint16 int
Uint32 int
Uint64 int
Uint128 int

Float32 int
Float64 int

Checksum160 int
Checksum256 int
Checksum512 int

PublicKey int
Signature int

Tstamp int
BlockTimestamp int
CurrencyName int
Bool int

CurrencyName int

// Deprecated Fields

// Deprecated: use Uint8 instead
UInt8 int
// Deprecated: use Uint16 instead
UInt16 int
// Deprecated: use Uint32 instead
UInt32 int
// Deprecated: use Uint64 instead
UInt64 int
// Deprecated: use Uint128 instead
UInt128 int
}{
Byte: 1,
Int8: 1,
UInt8: 1,
UInt16: 2,
Int16: 2,
UInt32: 4,
UInt64: 8,
UInt128: 16,
Float32: 4,
Float64: 8,
Checksum160: 20,
Checksum256: 32,
Checksum512: 64,
PublicKey: 34,
Signature: 66,
Byte: 1,
Bool: 1,

Int8: 1,
Int16: 2,

Uint8: 1,
Uint16: 2,
Uint32: 4,
Uint64: 8,
Uint128: 16,

Float32: 4,
Float64: 8,

Checksum160: 20,
Checksum256: 32,
Checksum512: 64,

PublicKey: 34,
Signature: 66,

Tstamp: 8,
BlockTimestamp: 4,
CurrencyName: 7,
Bool: 1,

CurrencyName: 7,
}

func init() {
// Deprecated fields initialization
TypeSize.UInt8 = TypeSize.Uint8
TypeSize.UInt16 = TypeSize.Uint16
TypeSize.UInt32 = TypeSize.Uint32
TypeSize.UInt64 = TypeSize.Uint64
TypeSize.UInt128 = TypeSize.Uint128
}

var RegisteredActions = map[AccountName]map[ActionName]reflect.Type{}
Expand Down Expand Up @@ -592,10 +628,16 @@ func (d *Decoder) ReadBool() (out bool, err error) {

}

func (d *Decoder) ReadUInt8() (out uint8, err error) {
func (d *Decoder) ReadUint8() (out uint8, err error) {
out, err = d.ReadByte()
return
}

// Deprecated: Use `ReadUint8` (with a lower case `i`) instead
func (d *Decoder) ReadUInt8() (out uint8, err error) {
return d.ReadUint8()
}

func (d *Decoder) ReadInt8() (out int8, err error) {
b, err := d.ReadByte()
out = int8(b)
Expand All @@ -606,13 +648,13 @@ func (d *Decoder) ReadInt8() (out int8, err error) {
}

func (d *Decoder) ReadUint16() (out uint16, err error) {
if d.remaining() < TypeSize.UInt16 {
err = fmt.Errorf("uint16 required [%d] bytes, remaining [%d]", TypeSize.UInt16, d.remaining())
if d.remaining() < TypeSize.Uint16 {
err = fmt.Errorf("uint16 required [%d] bytes, remaining [%d]", TypeSize.Uint16, d.remaining())
return
}

out = binary.LittleEndian.Uint16(d.data[d.pos:])
d.pos += TypeSize.UInt16
d.pos += TypeSize.Uint16
if loggingEnabled {
decoderLog.Debug("read uint16", zap.Uint16("val", out))
}
Expand All @@ -637,13 +679,13 @@ func (d *Decoder) ReadInt64() (out int64, err error) {
}

func (d *Decoder) ReadUint32() (out uint32, err error) {
if d.remaining() < TypeSize.UInt32 {
err = fmt.Errorf("uint32 required [%d] bytes, remaining [%d]", TypeSize.UInt32, d.remaining())
if d.remaining() < TypeSize.Uint32 {
err = fmt.Errorf("uint32 required [%d] bytes, remaining [%d]", TypeSize.Uint32, d.remaining())
return
}

out = binary.LittleEndian.Uint32(d.data[d.pos:])
d.pos += TypeSize.UInt32
d.pos += TypeSize.Uint32
if loggingEnabled {
decoderLog.Debug("read uint32", zap.Uint32("val", out))
}
Expand All @@ -659,31 +701,31 @@ func (d *Decoder) ReadInt32() (out int32, err error) {
}

func (d *Decoder) ReadUint64() (out uint64, err error) {
if d.remaining() < TypeSize.UInt64 {
err = fmt.Errorf("uint64 required [%d] bytes, remaining [%d]", TypeSize.UInt64, d.remaining())
if d.remaining() < TypeSize.Uint64 {
err = fmt.Errorf("uint64 required [%d] bytes, remaining [%d]", TypeSize.Uint64, d.remaining())
return
}

data := d.data[d.pos : d.pos+TypeSize.UInt64]
data := d.data[d.pos : d.pos+TypeSize.Uint64]
out = binary.LittleEndian.Uint64(data)
d.pos += TypeSize.UInt64
d.pos += TypeSize.Uint64
if loggingEnabled {
decoderLog.Debug("read uint64", zap.Uint64("val", out), zap.Stringer("hex", HexBytes(data)))
}
return
}

func (d *Decoder) ReadUint128(typeName string) (out Uint128, err error) {
if d.remaining() < TypeSize.UInt128 {
err = fmt.Errorf("%s required [%d] bytes, remaining [%d]", typeName, TypeSize.UInt128, d.remaining())
if d.remaining() < TypeSize.Uint128 {
err = fmt.Errorf("%s required [%d] bytes, remaining [%d]", typeName, TypeSize.Uint128, d.remaining())
return
}

data := d.data[d.pos : d.pos+TypeSize.UInt128]
data := d.data[d.pos : d.pos+TypeSize.Uint128]
out.Lo = binary.LittleEndian.Uint64(data)
out.Hi = binary.LittleEndian.Uint64(data[8:])

d.pos += TypeSize.UInt128
d.pos += TypeSize.Uint128
if loggingEnabled {
decoderLog.Debug("read uint128", zap.Stringer("hex", out), zap.Uint64("lo", out.Lo), zap.Uint64("lo", out.Lo))
}
Expand Down Expand Up @@ -775,7 +817,7 @@ func (d *Decoder) ReadChecksum512() (out Checksum512, err error) {
}

func (d *Decoder) ReadPublicKey() (out ecc.PublicKey, err error) {
typeID, err := d.ReadUInt8()
typeID, err := d.ReadUint8()
if err != nil {
return out, fmt.Errorf("unable to read public key type: %s", err)
}
Expand Down Expand Up @@ -850,7 +892,7 @@ func (d *Decoder) readWAPublicKeyMaterial() (out []byte, err error) {
}

func (d *Decoder) ReadSignature() (out ecc.Signature, err error) {
typeID, err := d.ReadUInt8()
typeID, err := d.ReadUint8()
if err != nil {
return out, fmt.Errorf("unable to read signature type: %s", err)
}
Expand Down
14 changes: 7 additions & 7 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (e *Encoder) writeUint16(i uint16) (err error) {
if loggingEnabled {
encoderLog.Debug("write uint16", zap.Uint16("val", i))
}
buf := make([]byte, TypeSize.UInt16)
buf := make([]byte, TypeSize.Uint16)
binary.LittleEndian.PutUint16(buf, i)
return e.toWriter(buf)
}
Expand All @@ -318,7 +318,7 @@ func (e *Encoder) writeUint32(i uint32) (err error) {
if loggingEnabled {
encoderLog.Debug("write uint32", zap.Uint32("val", i))
}
buf := make([]byte, TypeSize.UInt32)
buf := make([]byte, TypeSize.Uint32)
binary.LittleEndian.PutUint32(buf, i)
return e.toWriter(buf)
}
Expand All @@ -334,7 +334,7 @@ func (e *Encoder) writeUint64(i uint64) (err error) {
if loggingEnabled {
encoderLog.Debug("write uint64", zap.Uint64("val", i))
}
buf := make([]byte, TypeSize.UInt64)
buf := make([]byte, TypeSize.Uint64)
binary.LittleEndian.PutUint64(buf, i)
return e.toWriter(buf)
}
Expand All @@ -343,9 +343,9 @@ func (e *Encoder) writeUint128(i Uint128) (err error) {
if loggingEnabled {
encoderLog.Debug("write uint128", zap.Stringer("hex", i), zap.Uint64("lo", i.Lo), zap.Uint64("hi", i.Hi))
}
buf := make([]byte, TypeSize.UInt128)
buf := make([]byte, TypeSize.Uint128)
binary.LittleEndian.PutUint64(buf, i.Lo)
binary.LittleEndian.PutUint64(buf[TypeSize.UInt64:], i.Hi)
binary.LittleEndian.PutUint64(buf[TypeSize.Uint64:], i.Hi)
return e.toWriter(buf)
}

Expand All @@ -354,7 +354,7 @@ func (e *Encoder) writeFloat32(f float32) (err error) {
encoderLog.Debug("write float32", zap.Float32("val", f))
}
i := math.Float32bits(f)
buf := make([]byte, TypeSize.UInt32)
buf := make([]byte, TypeSize.Uint32)
binary.LittleEndian.PutUint32(buf, i)

return e.toWriter(buf)
Expand All @@ -364,7 +364,7 @@ func (e *Encoder) writeFloat64(f float64) (err error) {
encoderLog.Debug("write float64", zap.Float64("val", f))
}
i := math.Float64bits(f)
buf := make([]byte, TypeSize.UInt64)
buf := make([]byte, TypeSize.Uint64)
binary.LittleEndian.PutUint64(buf, i)

return e.toWriter(buf)
Expand Down

0 comments on commit cf2f85f

Please sign in to comment.