Skip to content

Commit a411d94

Browse files
committed
decoder
1 parent cdf199d commit a411d94

File tree

5 files changed

+298
-198
lines changed

5 files changed

+298
-198
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 fanweixiao
3+
Copyright (c) 2021 CELLA
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

pkg/spec/decode.go

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ func FromBytes(buf []byte) (p *Packet, err error) {
4545
return p, nil
4646
}
4747

48-
// GetValueAsUInt32 decode value as uint32
49-
func (p *Packet) GetValueAsUInt32() (uint32, error) {
50-
var val uint32
51-
codec := encoding.VarCodec{}
52-
err := codec.DecodePVarUInt32(p.valbuf, &val)
53-
return val, err
54-
}
55-
5648
func readTag(buffer []byte, position int, val *uint64) (cursor int, err error) {
5749
cursor, err = readPVarUInt64(buffer, position, val)
5850
return cursor, err
@@ -83,3 +75,70 @@ func readPVarUInt64(buffer []byte, position int, val *uint64) (cursor int, err e
8375
err = codec.DecodePVarUInt64(bytes, val)
8476
return cursor + position, err
8577
}
78+
79+
// GetValueAsUInt32 decode value as uint32
80+
func (p *Packet) GetValueAsUInt32() (uint32, error) {
81+
var val uint32
82+
codec := encoding.VarCodec{}
83+
err := codec.DecodePVarUInt32(p.valbuf, &val)
84+
return val, err
85+
}
86+
87+
// GetValueAsInt32 decode value as int32
88+
func (p *Packet) GetValueAsInt32() (int32, error) {
89+
var val int32
90+
codec := encoding.VarCodec{}
91+
err := codec.DecodePVarInt32(p.valbuf, &val)
92+
return val, err
93+
}
94+
95+
// GetValueAsUInt64 decode value as uint64
96+
func (p *Packet) GetValueAsUInt64() (uint64, error) {
97+
var val uint64
98+
codec := encoding.VarCodec{}
99+
err := codec.DecodePVarUInt64(p.valbuf, &val)
100+
return val, err
101+
}
102+
103+
// GetValueAsInt64 decode value as int64
104+
func (p *Packet) GetValueAsInt64() (int64, error) {
105+
var val int64
106+
codec := encoding.VarCodec{}
107+
err := codec.DecodePVarInt64(p.valbuf, &val)
108+
return val, err
109+
}
110+
111+
// GetValueAsFloat32 decode value as uint32
112+
func (p *Packet) GetValueAsFloat32() (float32, error) {
113+
var val float32
114+
codec := encoding.VarCodec{Size: p.Length}
115+
err := codec.DecodeVarFloat32(p.valbuf, &val)
116+
return val, err
117+
}
118+
119+
// GetValueAsBool decode value as bool
120+
func (p *Packet) GetValueAsBool() (bool, error) {
121+
res, err := p.GetValueAsUInt64()
122+
if res == 1 {
123+
return true, err
124+
}
125+
return false, err
126+
}
127+
128+
// GetValueAsFloat64 decode value as float64
129+
func (p *Packet) GetValueAsFloat64() (float64, error) {
130+
var val float64
131+
codec := encoding.VarCodec{Size: p.Length}
132+
err := codec.DecodeVarFloat64(p.valbuf, &val)
133+
return val, err
134+
}
135+
136+
// GetValueAsUTF8String decode value as float32
137+
func (p *Packet) GetValueAsUTF8String() string {
138+
return string(p.valbuf)
139+
}
140+
141+
// GetValueAsRawBytes decode value as float32
142+
func (p *Packet) GetValueAsRawBytes() []byte {
143+
return p.valbuf
144+
}

pkg/spec/encode_test.go

Lines changed: 0 additions & 164 deletions
This file was deleted.

pkg/spec/decode_test.go renamed to pkg/spec/tlv_test.go

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,37 @@ import (
44
"testing"
55
)
66

7+
func TestV2Tag(t *testing.T) {
8+
testV2Tags(t, 0x05, []byte{0x05})
9+
testV2Tags(t, 0x3F, []byte{0x3F})
10+
testV2Tags(t, 0x7F, []byte{0x80, 0x7F})
11+
testV2Tags(t, 0xFF, []byte{0x81, 0x7F})
12+
testV2Tags(t, 0xFFFF, []byte{0x83, 0xFF, 0x7F})
13+
testV2Tags(t, 0xFFFFFF, []byte{0x87, 0xFF, 0xFF, 0x7F})
14+
}
15+
16+
func TestV2AddNode(t *testing.T) {
17+
parent, _ := NewPacket(0x01)
18+
19+
child1, _ := NewPacket(0x02)
20+
child1.SetUInt32(3)
21+
22+
parent.AddNode(child1)
23+
compareBytes(t, parent, []byte{0x01, 0x03, 0x02, 0x01, 0x03})
24+
25+
child2, _ := NewPacket(0x03)
26+
child2.SetFloat64(0.01171875)
27+
28+
parent.AddNode(child2)
29+
compareBytes(t, parent, []byte{0x01, 0x07, 0x02, 0x01, 0x03, 0x03, 0x02, 0x3F, 0x88})
30+
31+
child3, _ := NewPacket(0x04)
32+
child3.SetFloat32(68.123)
33+
34+
parent.AddNode(child3)
35+
compareBytes(t, parent, []byte{0x01, 0x0D, 0x02, 0x01, 0x03, 0x03, 0x02, 0x3F, 0x88, 0x04, 0x04, 0x42, 0x88, 0x3E, 0xFA})
36+
}
37+
738
func TestFromBytes(t *testing.T) {
839
testFromBytes(t, []byte{0x03, 0x01, 0x02}, &Packet{Tag: 3, Length: 1, valbuf: []byte{0x02}})
940
testFromBytes(t, []byte{0x81, 0x03, 0x01, 0x06}, &Packet{Tag: 131, Length: 1, valbuf: []byte{0x06}})
@@ -14,25 +45,6 @@ func TestFromBytes(t *testing.T) {
1445
testFromBytes(t, buf, &Packet{Tag: 131, Length: 129, valbuf: foo})
1546
}
1647

17-
func testFromBytes(t *testing.T, buffer []byte, expected *Packet) {
18-
res, err := FromBytes(buffer)
19-
if err != nil {
20-
t.Error(err)
21-
} else {
22-
if res.Tag != expected.Tag {
23-
t.Errorf("Tag expected=[% X], actual=[% X]", expected.Tag, res.Tag)
24-
}
25-
if res.Length != expected.Length {
26-
t.Errorf("Length expected=[% X], actual=[% X]", expected.Length, res.Length)
27-
}
28-
for i := range res.valbuf {
29-
if res.valbuf[i] != expected.valbuf[i] {
30-
t.Errorf("valbuf on [%d] expected=[% X], actual=[% X]", i, expected.valbuf[i], res.valbuf[i])
31-
}
32-
}
33-
}
34-
}
35-
3648
func TestReadTag(t *testing.T) {
3749
testReadTag(t, []byte{0x03, 0x01, 0x02}, 0, 3, 1)
3850
testReadTag(t, []byte{0xFF, 0x04, 0x01, 0x02}, 1, 4, 2)
@@ -74,11 +86,45 @@ func testReadLength(t *testing.T, buffer []byte, position int, expectValue uint6
7486
}
7587
}
7688

77-
func TestGetValueAsUInt32(t *testing.T) {
78-
buf := []byte{0x03, 0x01, 0x02}
79-
p, _ := FromBytes(buf)
80-
actual, _ := p.GetValueAsUInt32()
81-
if actual != 2 {
82-
t.Errorf("expect=%d, actual=%d", 2, actual)
89+
func testFromBytes(t *testing.T, buffer []byte, expected *Packet) {
90+
res, err := FromBytes(buffer)
91+
if err != nil {
92+
t.Error(err)
93+
} else {
94+
if res.Tag != expected.Tag {
95+
t.Errorf("Tag expected=[% X], actual=[% X]", expected.Tag, res.Tag)
96+
}
97+
if res.Length != expected.Length {
98+
t.Errorf("Length expected=[% X], actual=[% X]", expected.Length, res.Length)
99+
}
100+
for i := range res.valbuf {
101+
if res.valbuf[i] != expected.valbuf[i] {
102+
t.Errorf("valbuf on [%d] expected=[% X], actual=[% X]", i, expected.valbuf[i], res.valbuf[i])
103+
}
104+
}
105+
}
106+
}
107+
108+
func testV2Tags(t *testing.T, id uint64, expected []byte) {
109+
p, err := NewPacket(id)
110+
if err != nil {
111+
t.Errorf("TestV2Tag err=%v", err)
112+
}
113+
p.SetInt32(0)
114+
expected = append(expected, []byte{0x01, 0x00}...)
115+
compareBytes(t, p, expected)
116+
}
117+
118+
func compareBytes(t *testing.T, p *Packet, expected []byte) []byte {
119+
result, err := p.Encode()
120+
if err != nil {
121+
t.Errorf("compareBytes error=%v", err)
122+
}
123+
for i, p := range result {
124+
if p != expected[i] {
125+
t.Errorf("\nexpected:[% X]\n actual:[% X]\n", expected, result)
126+
break
127+
}
83128
}
129+
return result
84130
}

0 commit comments

Comments
 (0)