Skip to content

Commit e78ab17

Browse files
committed
expose generic parsing methods
DecodeLength, IsRootToken
1 parent 6b734db commit e78ab17

File tree

7 files changed

+43
-23
lines changed

7 files changed

+43
-23
lines changed

basic_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func TestBasicEncoderWithSignals(t *testing.T) {
1414
input := int32(456)
1515

16-
encoder := newBasicEncoder(0x10, basicEncoderOptionRoot(rootToken))
16+
encoder := newBasicEncoder(0x10, basicEncoderOptionRoot(utils.RootToken))
1717
inputBuf, _ := encoder.Encode(input,
1818
createSignal(0x02).SetString("a"),
1919
createSignal(0x03).SetString("b"))
@@ -41,7 +41,7 @@ func TestBasicEncoderWithSignalsNoRoot(t *testing.T) {
4141
func TestBasicSliceEncoderWithSignals(t *testing.T) {
4242
input := []int32{123, 456}
4343

44-
encoder := newBasicEncoder(0x10, basicEncoderOptionRoot(rootToken))
44+
encoder := newBasicEncoder(0x10, basicEncoderOptionRoot(utils.RootToken))
4545
inputBuf, _ := encoder.Encode(input,
4646
createSignal(0x02).SetString("a"),
4747
createSignal(0x03).SetString("b"))

codec.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package y3
22

33
import (
44
"reflect"
5+
6+
"github.com/yomorun/y3-codec-golang/internal/utils"
57
)
68

79
// Codec encode the user's data according to the Y3 encoding rules
@@ -25,9 +27,9 @@ type y3Codec struct {
2527
// Marshal encode interface to []byte
2628
func (c y3Codec) Marshal(input interface{}) ([]byte, error) {
2729
if c.isStruct(input) {
28-
return newStructEncoder(c.observe, structEncoderOptionRoot(rootToken)).Encode(input)
30+
return newStructEncoder(c.observe, structEncoderOptionRoot(utils.RootToken)).Encode(input)
2931
}
30-
return newBasicEncoder(c.observe, basicEncoderOptionRoot(rootToken)).Encode(input)
32+
return newBasicEncoder(c.observe, basicEncoderOptionRoot(utils.RootToken)).Encode(input)
3133
}
3234

3335
// isStruct determine whether an interface is a structure

internal/utils/common.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ const KeyOfSliceItem = 0x00
1717

1818
// KeyStringOfSliceItem 描述数组项的TLV的sid值的字符串表示
1919
const KeyStringOfSliceItem = "0x00"
20+
21+
// RootToken 描述根节点的sid值
22+
const RootToken byte = 0x01

pkg/common/common.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package common
2+
3+
import (
4+
"github.com/yomorun/y3-codec-golang/internal/utils"
5+
"github.com/yomorun/y3-codec-golang/pkg/encoding"
6+
)
7+
8+
// DecodeLength decode to length
9+
func DecodeLength(buf []byte) (length int32, err error) {
10+
varCodec := encoding.VarCodec{}
11+
err = varCodec.DecodePVarInt32(buf, &length)
12+
return
13+
}
14+
15+
// RootToken judge if it is the root node
16+
func IsRootToken(b byte) bool {
17+
return b == (utils.MSB | utils.RootToken)
18+
}

structure_decoder.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,6 @@ func (d *structDecoderImpl) matchingKey(key byte, node *NodePacket) (flag bool,
424424
return false, false, nil
425425
}
426426

427-
var (
428-
// rootToken: mark the root node
429-
rootToken byte = 0x01
430-
)
431-
432427
// field store the contents of a reflect
433428
type field struct {
434429
field reflect.StructField

structure_decoder_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"reflect"
55
"testing"
66

7+
"github.com/yomorun/y3-codec-golang/internal/utils"
8+
79
"github.com/yomorun/y3-codec-golang/internal/tester"
810
)
911

@@ -24,7 +26,7 @@ func TestBasic_Struct(t *testing.T) {
2426
t.Parallel()
2527

2628
input := newBasic()
27-
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(rootToken)).Encode(input)
29+
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
2830

2931
var result tester.BasicTestData
3032
runDecode(t, inputBuf, &result)
@@ -49,7 +51,7 @@ func TestDecode_Embedded(t *testing.T) {
4951
BasicTestData: newBasic(),
5052
Vaction: "drink",
5153
}
52-
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(rootToken)).Encode(input)
54+
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
5355

5456
var result tester.EmbeddedTestData
5557
_, err := newStructDecoder(&result).Decode(inputBuf)
@@ -69,7 +71,7 @@ func TestDecode_EmbeddedMore(t *testing.T) {
6971
t.Parallel()
7072

7173
input := tester.EmbeddedMoreTestData{EmbeddedTestData: tester.EmbeddedTestData{BasicTestData: newBasic(), Vaction: "drink"}, Vanimal: "bird"}
72-
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(rootToken)).Encode(input)
74+
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
7375

7476
var result tester.EmbeddedMoreTestData
7577
_, err := newStructDecoder(&result).Decode(inputBuf)
@@ -93,7 +95,7 @@ func TestDecoder_Named(t *testing.T) {
9395
t.Parallel()
9496

9597
input := tester.NamedTestData{Base: newBasic(), Vaction: "drink"}
96-
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(rootToken)).Encode(input)
98+
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
9799

98100
var result tester.NamedTestData
99101
_, err := newStructDecoder(&result).Decode(inputBuf)
@@ -113,7 +115,7 @@ func TestDecoder_NamedMore(t *testing.T) {
113115
t.Parallel()
114116

115117
input := tester.NamedMoreTestData{MyNest: tester.NamedTestData{Base: newBasic(), Vaction: "drink"}, Vanimal: "bird"}
116-
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(rootToken)).Encode(input)
118+
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
117119

118120
var result tester.NamedMoreTestData
119121
_, err := newStructDecoder(&result).Decode(inputBuf)
@@ -146,7 +148,7 @@ func TestArray(t *testing.T) {
146148
[2]float32{1, 2},
147149
[2]float64{1, 2},
148150
}
149-
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(rootToken)).Encode(input)
151+
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
150152

151153
var result tester.ArrayTestData
152154
_, err := newStructDecoder(&result).Decode(inputBuf)
@@ -178,7 +180,7 @@ func TestSlice(t *testing.T) {
178180
[]float64{1, 2},
179181
}
180182

181-
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(rootToken)).Encode(input)
183+
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
182184

183185
var result tester.SliceTestData
184186
_, err := newStructDecoder(&result).Decode(inputBuf)
@@ -210,7 +212,7 @@ func TestSliceStruct(t *testing.T) {
210212
{EmbeddedTestData: tester.EmbeddedTestData{BasicTestData: newBasic(), Vaction: "drink"}, Vanimal: "bird"}},
211213
}
212214

213-
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(rootToken)).Encode(input)
215+
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
214216

215217
var result tester.SliceStructTestData
216218
_, err := newStructDecoder(&result).Decode(inputBuf)
@@ -258,7 +260,7 @@ func TestArrayStruct(t *testing.T) {
258260
{EmbeddedTestData: tester.EmbeddedTestData{BasicTestData: newBasic(), Vaction: "drink"}, Vanimal: "bird"}},
259261
}
260262

261-
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(rootToken)).Encode(input)
263+
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
262264

263265
var result tester.ArrayStructTestData
264266
_, err := newStructDecoder(&result).Decode(inputBuf)
@@ -297,7 +299,7 @@ func TestRootSliceWithBasicStruct(t *testing.T) {
297299

298300
input := []tester.BasicTestData{newBasic(), newBasic()}
299301

300-
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(rootToken)).Encode(input)
302+
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
301303

302304
var result []tester.BasicTestData
303305
_, err := newStructDecoder(&result).Decode(inputBuf)
@@ -329,7 +331,7 @@ func TestRootSliceWithSliceStruct(t *testing.T) {
329331

330332
input := []tester.SliceStructTestData{input1, input1}
331333

332-
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(rootToken)).Encode(input)
334+
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
333335

334336
var result []tester.SliceStructTestData
335337
_, err := newStructDecoder(&result).Decode(inputBuf)
@@ -375,7 +377,7 @@ func TestNested(t *testing.T) {
375377
BasicList: []tester.BasicTestData{newBasic(), newBasic()},
376378
}}}}
377379

378-
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(rootToken)).Encode(input)
380+
inputBuf, _ := newStructEncoder(0x3f, structEncoderOptionRoot(utils.RootToken)).Encode(input)
379381

380382
var result tester.NestedTestData
381383
_, err := newStructDecoder(&result).Decode(inputBuf)

structure_encoder_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestStructEncoderWithSignals(t *testing.T) {
1616
Therm: thermometer{Temperature: float32(30), Humidity: float32(40)},
1717
}
1818

19-
encoder := newStructEncoder(0x30, structEncoderOptionRoot(rootToken),
19+
encoder := newStructEncoder(0x30, structEncoderOptionRoot(utils.RootToken),
2020
structEncoderOptionConfig(&structEncoderConfig{
2121
ZeroFields: true,
2222
TagName: "y3",
@@ -65,7 +65,7 @@ func TestStructSliceEncoderWithSignals(t *testing.T) {
6565
},
6666
}
6767

68-
encoder := newStructEncoder(0x30, structEncoderOptionRoot(rootToken))
68+
encoder := newStructEncoder(0x30, structEncoderOptionRoot(utils.RootToken))
6969
inputBuf, _ := encoder.Encode(input,
7070
createSignal(0x02).SetString("a"),
7171
createSignal(0x03).SetString("b"))

0 commit comments

Comments
 (0)