Skip to content

Commit

Permalink
added var int16 and uint16
Browse files Browse the repository at this point in the history
  • Loading branch information
billettc committed Nov 12, 2020
1 parent d3b7ff7 commit 11b351a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
44 changes: 41 additions & 3 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ func (d *Decoder) DecodeWithOption(v interface{}, option *DecodeOption) (err err
if err == nil {
rv.Set(reflect.ValueOf((Uint64)(n)))
}

return
case *Uint64:
var n uint64
Expand Down Expand Up @@ -263,6 +262,21 @@ func (d *Decoder) DecodeWithOption(v interface{}, option *DecodeOption) (err err
n, err = d.ReadUint64()
rv.SetUint(n)
return
case *Varint16:
var r int16
r, err = d.ReadVarint16()
rv.SetInt(int64(r))
return
case *Varint32:
var r int64
r, err = d.ReadVarint64()
rv.SetInt(r)
return
case *Varuint16:
var r uint16
r, err = d.ReadUvarint16()
rv.SetUint(uint64(r))
return
case *Varuint32:
var r uint64
r, err = d.ReadUvarint64()
Expand Down Expand Up @@ -303,7 +317,7 @@ func (d *Decoder) DecodeWithOption(v interface{}, option *DecodeOption) (err err
if option.hasSizeOfSlice() {
l = option.getSizeOfSlice()
} else {
length, err := d.ReadUvarint64();
length, err := d.ReadUvarint64()
if err != nil {
return err
}
Expand Down Expand Up @@ -334,7 +348,7 @@ func (d *Decoder) DecodeWithOption(v interface{}, option *DecodeOption) (err err
}

// rv is the instance of the structure
// t is the type of the structurwe
// t is the type of the structure
func (d *Decoder) decodeStruct(v interface{}, t reflect.Type, rv reflect.Value) (err error) {
l := rv.NumField()

Expand Down Expand Up @@ -470,6 +484,30 @@ func (d *Decoder) ReadUvarint32() (out uint32, err error) {
}
return
}
func (d *Decoder) ReadVarint16() (out int16, err error) {
n, err := d.ReadVarint64()
if err != nil {
return out, err
}
out = int16(n)
if traceEnabled {
zlog.Debug("read varint16", zap.Int16("val", out))
}
return
}

func (d *Decoder) ReadUvarint16() (out uint16, err error) {

n, err := d.ReadUvarint64()
if err != nil {
return out, err
}
out = uint16(n)
if traceEnabled {
zlog.Debug("read uvarint16", zap.Uint16("val", out))
}
return
}

func (d *Decoder) ReadByteArray() (out []byte, err error) {

Expand Down
4 changes: 4 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func (e *Encoder) EncodeWithOption(v interface{}, option *EncodeOption) (err err
return e.writeFloat32(cv)
case float64:
return e.writeFloat64(cv)
case Varint16:
return e.writeVarInt(int(cv))
case Varint32:
return e.writeVarInt(int(cv))
case Uint128:
Expand All @@ -107,6 +109,8 @@ func (e *Encoder) EncodeWithOption(v interface{}, option *EncodeOption) (err err
return e.writeUint128(Uint128(cv))
case Float128:
return e.writeUint128(Uint128(cv))
case Varuint16:
return e.writeUVarInt(int(cv))
case Varuint32:
return e.writeUVarInt(int(cv))
case bool:
Expand Down
2 changes: 2 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func (t HexBytes) String() string {
return hex.EncodeToString(t)
}

type Varint16 int16
type Varuint16 uint16
type Varuint32 uint32
type Varint32 int32

Expand Down

0 comments on commit 11b351a

Please sign in to comment.