Skip to content

Commit 81f8ceb

Browse files
committed
coud encode/decode nil value
1 parent a1e9cf3 commit 81f8ceb

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

pkg/spec/encode.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ func writePVarUInt64Buffer(val uint64) ([]byte, error) {
2727
return buf, err
2828
}
2929

30+
// SetNil set nil value, means length=0 packet
31+
func (p *Packet) SetNil() {
32+
p.valbuf = make([]byte, 0)
33+
}
34+
3035
// SetUInt32 set UInt32 value
3136
func (p *Packet) SetUInt32(v uint32) error {
3237
size := encoding.SizeOfPVarUInt32(v)

pkg/spec/tlv_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ func TestFromBytes(t *testing.T) {
4646
}
4747

4848
func TestReadTag(t *testing.T) {
49+
testReadTag(t, []byte{0xFF, 0x7F, 0x00}, 0, 18446744073709551615, 2)
4950
testReadTag(t, []byte{0x03, 0x01, 0x02}, 0, 3, 1)
5051
testReadTag(t, []byte{0xFF, 0x04, 0x01, 0x02}, 1, 4, 2)
5152
testReadTag(t, []byte{0x81, 0x05, 0x01, 0x02}, 0, 133, 2)
5253
testReadTag(t, []byte{0xFF, 0x81, 0x05, 0x01, 0x02}, 1, 133, 3)
5354
}
5455

5556
func TestReadLength(t *testing.T) {
57+
testReadLength(t, []byte{0xFF, 0x7F, 0x00}, 2, 0, 3)
5658
testReadLength(t, []byte{0x03, 0x01, 0x02}, 1, 1, 2)
5759
testReadLength(t, []byte{0xFF, 0x04, 0x02, 0x00, 0x00}, 2, 2, 3)
5860
testReadLength(t, []byte{0x05, 0x81, 0x05, 0x00}, 1, 133, 3)

pkg/spec/typed_value_test.go

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

7+
func TestV2Nil(t *testing.T) {
8+
p, _ := NewPacket(18446744073709551487)
9+
p.SetNil()
10+
11+
buf := compareBytes(t, p, []byte{0xFE, 0x7F, 0x00})
12+
13+
dp, _ := FromBytes(buf)
14+
if dp.Tag != 18446744073709551487 {
15+
t.Errorf("result.Tag=% X, expect.Tag=% X", dp.Tag, uint64(18446744073709551487))
16+
}
17+
18+
if dp.Length != 0 {
19+
t.Errorf("result.Length=%d, expect.Length=%v", dp.Length, 0)
20+
}
21+
22+
if len(dp.valbuf) != 0 {
23+
t.Errorf("result.len(valbuf)=%d, expect.len(valbuf)=%v", len(dp.valbuf), 0)
24+
}
25+
}
26+
727
func TestV2UInt32(t *testing.T) {
828
testV2UInt32(t, 0x03, 6, []byte{0x03, 0x01, 0x06})
929
testV2UInt32(t, 0x06, 127, []byte{0x06, 0x02, 0x80, 0x7F})

0 commit comments

Comments
 (0)