Skip to content

Commit

Permalink
Encode more integer types (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Dec 8, 2023
1 parent 534806c commit 8ccde10
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
10 changes: 8 additions & 2 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,22 @@ func (enc *Encoder) writeValue(ename string, v reflect.Value) (int, error) {
case reflect.String:
count += enc.writeElem(TypeString, ename)
count += enc.writeString(v.String())
case reflect.Int32:
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
count += enc.writeElem(TypeInt32, ename)
count += enc.writeInt32(int32(v.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
count += enc.writeElem(TypeInt32, ename)
count += enc.writeInt32(int32(v.Uint()))
case reflect.Uint64:
count += enc.writeElem(TypeInt64, ename)
count += enc.writeInt64(int64(v.Uint()))
case reflect.Int64:
count += enc.writeElem(TypeInt64, ename)
count += enc.writeInt64(int64(v.Int()))
case reflect.Bool:
count += enc.writeElem(TypeBool, ename)
count += enc.writeBool(v.Bool())
case reflect.Float64:
case reflect.Float32, reflect.Float64:
count += enc.writeElem(TypeDouble, ename)
count += enc.writeInt64(int64(math.Float64bits(v.Float())))

Expand Down
28 changes: 28 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,34 @@ import (
"testing"
)

func TestEncode(t *testing.T) {
var buf bytes.Buffer
enc := NewEncoder(&buf)

var err error
var arr A

arr = A{
bool(true),
int(123),
int8(123),
int16(123),
int32(123),
int64(123),
uint(123),
uint8(123),
uint16(123),
uint32(123),
uint64(123),
float32(123),
float64(123),
}
err = enc.Encode(arr)
mustOk(t, err)
wantBytes(t, buf.Bytes(), "70000000083000011031007b0000001032007b0000001033007b0000001034007b0000001235007b000000000000001036007b0000001037007b0000001038007b0000001039007b000000123130007b00000000000000013131000000000000c05e40013132000000000000c05e4000")
buf.Reset()
}

func TestEncodeA(t *testing.T) {
var buf bytes.Buffer
enc := NewEncoder(&buf)
Expand Down

0 comments on commit 8ccde10

Please sign in to comment.