Skip to content

Commit 5b6d3e4

Browse files
committed
GetValueAsUInt32()
1 parent d193987 commit 5b6d3e4

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

pkg/spec/decode.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ 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+
4856
func readTag(buffer []byte, position int, val *uint64) (cursor int, err error) {
4957
cursor, err = readPVarUInt64(buffer, position, val)
5058
return cursor, err

pkg/spec/decode_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,12 @@ func testReadLength(t *testing.T, buffer []byte, position int, expectValue uint6
7373
t.Errorf("expect cursor=%d, actual=%d", expectedCursor, cursor)
7474
}
7575
}
76+
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)
83+
}
84+
}

0 commit comments

Comments
 (0)