Skip to content

Commit 9369971

Browse files
authored
refactor(*): Refactoring ProtoCodec (#48)
- Merging Unmarshal - Removing interface methods that are not used externally - Adding control over Inform - NewCodec switch to streamingCodec implementation close #47
1 parent 3cc897e commit 9369971

File tree

6 files changed

+123
-82
lines changed

6 files changed

+123
-82
lines changed

pkg/codes/proto.go

+47-19
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,15 @@ type ProtoCodec interface {
1818

1919
// UnmarshalStruct: Unmarshal struct to interface
2020
UnmarshalStruct(data []byte, mold interface{}) error
21-
// UnmarshalStructNative: Unmarshal struct to interface by native data, No Outside Nodes
22-
UnmarshalStructNative(data []byte, mold interface{}) error
2321

2422
// UnmarshalBasic: Unmarshal basic type to interface
2523
UnmarshalBasic(data []byte, mold *interface{}) error
26-
// UnmarshalBasicNative: Unmarshal basic type to interface by native data, No Outside Nodes
27-
UnmarshalBasicNative(data []byte, mold *interface{}) error
2824

29-
// IsStruct: mold is Struct?
30-
IsStruct(mold interface{}) bool
25+
// UnmarshalStruct: Unmarshal []byte to interface
26+
Unmarshal(data []byte, moldInfo *MoldInfo) error
3127

32-
// @deprecated
33-
UnmarshalStructByNodePacket(node *y3.NodePacket, mold interface{}) error
34-
// @deprecated
35-
UnmarshalBasicByNodePacket(node *y3.NodePacket, mold *interface{}) error
28+
// UnmarshalByNodePacket: Unmarshal NodePacket to interface
29+
UnmarshalByNodePacket(node *y3.NodePacket, moldInfo *MoldInfo) error
3630
}
3731

3832
// protoCodec: Implementation of the ProtoCodec Interface
@@ -50,16 +44,21 @@ func NewProtoCodec(observe byte) ProtoCodec {
5044
}
5145
}
5246

47+
// hold the model data
48+
type MoldInfo struct {
49+
Mold interface{}
50+
}
51+
5352
func (c *protoCodec) Marshal(input interface{}) ([]byte, error) {
54-
if c.IsStruct(input) {
53+
if c.isStruct(input) {
5554
return packetstructure.EncodeToBytesWith(c.Observe, input)
5655
}
5756
//return marshalBasicNative(c.Observe, input)
5857
return encodeBasic(c.Observe, input)
5958
}
6059

6160
func (c *protoCodec) MarshalNative(input interface{}) ([]byte, error) {
62-
if c.IsStruct(input) {
61+
if c.isStruct(input) {
6362
np, err := packetstructure.Encode(input)
6463
if err != nil {
6564
return nil, err
@@ -73,19 +72,19 @@ func (c *protoCodec) UnmarshalStruct(data []byte, mold interface{}) error {
7372
return c.structDecoder.Unmarshal(data, mold)
7473
}
7574

76-
func (c *protoCodec) UnmarshalStructNative(data []byte, mold interface{}) error {
75+
func (c *protoCodec) unmarshalStructNative(data []byte, mold interface{}) error {
7776
return c.structDecoder.UnmarshalNative(data, mold)
7877
}
7978

8079
func (c *protoCodec) UnmarshalBasic(data []byte, mold *interface{}) error {
8180
return c.basicDecoder.Unmarshal(data, mold)
8281
}
8382

84-
func (c *protoCodec) UnmarshalBasicNative(data []byte, mold *interface{}) error {
83+
func (c *protoCodec) unmarshalBasicNative(data []byte, mold *interface{}) error {
8584
return c.basicDecoder.UnmarshalNative(data, mold)
8685
}
8786

88-
func (c *protoCodec) IsStruct(mold interface{}) bool {
87+
func (c *protoCodec) isStruct(mold interface{}) bool {
8988
isStruct := false
9089

9190
moldValue := reflect.Indirect(reflect.ValueOf(mold))
@@ -102,12 +101,41 @@ func (c *protoCodec) IsStruct(mold interface{}) bool {
102101
return isStruct
103102
}
104103

105-
// @deprecated
106-
func (c *protoCodec) UnmarshalStructByNodePacket(node *y3.NodePacket, mold interface{}) error {
104+
func (c *protoCodec) Unmarshal(data []byte, moldInfo *MoldInfo) error {
105+
if c.isStruct(moldInfo.Mold) {
106+
err := c.unmarshalStructNative(data, moldInfo.Mold)
107+
if err != nil {
108+
return err
109+
}
110+
111+
} else {
112+
err := c.unmarshalBasicNative(data, &moldInfo.Mold)
113+
if err != nil {
114+
return err
115+
}
116+
}
117+
return nil
118+
}
119+
120+
func (c *protoCodec) unmarshalStructByNodePacket(node *y3.NodePacket, mold interface{}) error {
107121
return c.structDecoder.UnmarshalByNodePacket(node, mold)
108122
}
109123

110-
// @deprecated
111-
func (c *protoCodec) UnmarshalBasicByNodePacket(node *y3.NodePacket, mold *interface{}) error {
124+
func (c *protoCodec) unmarshalBasicByNodePacket(node *y3.NodePacket, mold *interface{}) error {
112125
return c.basicDecoder.UnmarshalByNodePacket(node, mold)
113126
}
127+
128+
func (c *protoCodec) UnmarshalByNodePacket(node *y3.NodePacket, moldInfo *MoldInfo) error {
129+
if c.isStruct(moldInfo.Mold) {
130+
err := c.unmarshalStructByNodePacket(node, moldInfo.Mold)
131+
if err != nil {
132+
return err
133+
}
134+
} else {
135+
err := c.unmarshalBasicByNodePacket(node, &moldInfo.Mold)
136+
if err != nil {
137+
return err
138+
}
139+
}
140+
return nil
141+
}

pkg/codes/yomo.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ type YomoCodec interface {
2121
Refresh(w io.Writer) (int, error)
2222
}
2323

24+
// NewCodec: create new `YomoCodec`
2425
func NewCodec(observe string) YomoCodec {
2526
//return NewCollectingCodec(observe)
26-
return NewMergingCodec(packetutils.KeyOf(observe))
27+
//return NewMergingCodec(packetutils.KeyOf(observe))
28+
return NewStreamingCodecNoInform(packetutils.KeyOf(observe))
2729
}
2830

31+
// NewCodec: create new `YomoCodec` with channel inform
2932
func NewCodecWithInform(observe string) (YomoCodec, <-chan bool) {
3033
return NewStreamingCodec(packetutils.KeyOf(observe))
3134
}

pkg/codes/yomo_collecting.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,12 @@ func (codec *collectingCodec) Read(mold interface{}) (interface{}, error) {
128128
result := codec.Result[0]
129129
codec.Result = codec.Result[1:]
130130

131-
proto := codec.proto
132-
if proto.IsStruct(mold) {
133-
err := proto.UnmarshalStructByNodePacket(result, mold)
134-
if err != nil {
135-
return nil, err
136-
}
137-
} else {
138-
err := proto.UnmarshalBasicByNodePacket(result, &mold)
139-
if err != nil {
140-
return nil, err
141-
}
131+
info := &MoldInfo{Mold: mold}
132+
err := codec.proto.UnmarshalByNodePacket(result, info)
133+
if err != nil {
134+
return nil, err
142135
}
136+
mold = info.Mold
143137

144138
// for Encoder::merge
145139
codec.Value = result

pkg/codes/yomo_merging.go

+5-34
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,6 @@ func NewMergingCodec(observe byte) YomoCodec {
5858
return codec
5959
}
6060

61-
type decoderStatus uint8
62-
63-
const (
64-
decoderInit decoderStatus = 0
65-
decoderTag decoderStatus = 1
66-
decoderLength decoderStatus = 2
67-
decoderValue decoderStatus = 3
68-
decoderMatching decoderStatus = 4
69-
decoderFinished decoderStatus = 5
70-
)
71-
72-
type collectedStatus uint8
73-
74-
const (
75-
collectedInit collectedStatus = 0
76-
collectedTag collectedStatus = 1
77-
collectedLength collectedStatus = 2
78-
collectedBody collectedStatus = 3
79-
collectedCaching collectedStatus = 4
80-
collectedFinished collectedStatus = 5
81-
)
82-
8361
// Decoder: Collects bytes from buf and decodes them
8462
func (d *mergingCodec) Decoder(buf []byte) {
8563

@@ -256,19 +234,12 @@ func (d *mergingCodec) Read(mold interface{}) (interface{}, error) {
256234
result := d.Result[0]
257235
d.Result = d.Result[1:]
258236

259-
proto := NewProtoCodec(d.Observe)
260-
if proto.IsStruct(mold) {
261-
err := proto.UnmarshalStructNative(matching, mold)
262-
if err != nil {
263-
return nil, err
264-
}
265-
266-
} else {
267-
err := proto.UnmarshalBasicNative(matching, &mold)
268-
if err != nil {
269-
return nil, err
270-
}
237+
info := &MoldInfo{Mold: mold}
238+
err := d.proto.Unmarshal(matching, info)
239+
if err != nil {
240+
return nil, err
271241
}
242+
mold = info.Mold
272243

273244
// for Encoder::merge
274245
d.Value = result

pkg/codes/yomo_streaming.go

+58-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010
)
1111

1212
type streamingCodec struct {
13-
inform chan bool
13+
inform chan bool
14+
enableInform bool
1415

1516
Length int32
1617
Size int32
@@ -37,7 +38,8 @@ type streamingCodec struct {
3738

3839
func NewStreamingCodec(observe byte) (YomoCodec, <-chan bool) {
3940
codec := &streamingCodec{
40-
inform: make(chan bool, 10),
41+
inform: make(chan bool, 10),
42+
enableInform: true,
4143

4244
Observe: observe,
4345
SourceBuf: make([]byte, 0),
@@ -58,6 +60,52 @@ func NewStreamingCodec(observe byte) (YomoCodec, <-chan bool) {
5860
return codec, codec.inform
5961
}
6062

63+
func NewStreamingCodecNoInform(observe byte) YomoCodec {
64+
codec := &streamingCodec{
65+
inform: make(chan bool, 10),
66+
enableInform: false,
67+
68+
Observe: observe,
69+
SourceBuf: make([]byte, 0),
70+
Status: decoderInit,
71+
Matching: make([][]byte, 0),
72+
OriginResult: make([][]byte, 0),
73+
74+
CollectedStatus: collectedInit,
75+
CollectedTag: nil,
76+
CollectedSize: 0,
77+
CollectedLength: 0,
78+
CollectedLengthBuf: make([]byte, 0),
79+
CollectedBuffer: make([]byte, 0),
80+
CollectedResult: make([][]byte, 0),
81+
82+
proto: NewProtoCodec(observe),
83+
}
84+
return codec
85+
}
86+
87+
type decoderStatus uint8
88+
89+
const (
90+
decoderInit decoderStatus = 0
91+
decoderTag decoderStatus = 1
92+
decoderLength decoderStatus = 2
93+
decoderValue decoderStatus = 3
94+
decoderMatching decoderStatus = 4
95+
decoderFinished decoderStatus = 5
96+
)
97+
98+
type collectedStatus uint8
99+
100+
const (
101+
collectedInit collectedStatus = 0
102+
collectedTag collectedStatus = 1
103+
collectedLength collectedStatus = 2
104+
collectedBody collectedStatus = 3
105+
collectedCaching collectedStatus = 4
106+
collectedFinished collectedStatus = 5
107+
)
108+
61109
// Decoder: Collects bytes from buf and decodes them
62110
func (d *streamingCodec) Decoder(buf []byte) {
63111
for _, c := range buf {
@@ -188,7 +236,9 @@ func (d *streamingCodec) decode(buf []byte) {
188236
d.matchingMutex.Lock()
189237
d.Matching = append(d.Matching, curBuf)
190238
d.matchingMutex.Unlock()
191-
d.inform <- true
239+
if d.enableInform {
240+
d.inform <- true
241+
}
192242
d.Status = decoderMatching
193243
}
194244

@@ -234,19 +284,12 @@ func (d *streamingCodec) Read(mold interface{}) (interface{}, error) {
234284
d.Matching = d.Matching[1:]
235285
d.matchingMutex.Unlock()
236286

237-
proto := NewProtoCodec(d.Observe)
238-
if proto.IsStruct(mold) {
239-
err := proto.UnmarshalStructNative(matching, mold)
240-
if err != nil {
241-
return nil, err
242-
}
243-
244-
} else {
245-
err := proto.UnmarshalBasicNative(matching, &mold)
246-
if err != nil {
247-
return nil, err
248-
}
287+
info := &MoldInfo{Mold: mold}
288+
err := d.proto.Unmarshal(matching, info)
289+
if err != nil {
290+
return nil, err
249291
}
292+
mold = info.Mold
250293

251294
return mold, nil
252295
}

pkg/codes/yomo_streaming_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestInformByString(t *testing.T) {
8181
var mold interface{} = ""
8282
NewProtoCodec(0x10).UnmarshalBasic(buf, &mold)
8383
if mold != "y-new!" {
84-
t.Errorf("TestInformByString, The value should be %v", target)
84+
t.Errorf("TestInformByString, The value should be %v, but mold=%v", target, mold)
8585
}
8686
}
8787

@@ -114,7 +114,7 @@ func TestInformByStringSlice(t *testing.T) {
114114
arr, _ := utils.ToStringSliceArray(mold)
115115
for i, v := range arr {
116116
if v != target[i] {
117-
t.Errorf("TestInformByStringSlice, The value should be %v=%v", i, target[i])
117+
t.Errorf("TestInformByStringSlice, The value should be %v=%v, but v=%v", i, target[i], v)
118118
}
119119
}
120120

@@ -137,7 +137,9 @@ func testInformBy(t *testing.T, mold interface{}, input []byte, handle func(v in
137137
case flag := <-inform:
138138
if flag {
139139
v, _ := codec.Read(mold)
140+
//fmt.Printf("#4 v=%v\n", v)
140141
vv := handle(v)
142+
//fmt.Printf("#4 vv=%v\n", vv)
141143
codec.Write(&verifier{Verify: verify}, vv, mold)
142144
}
143145
codec.Refresh(&verifier{Verify: verify})

0 commit comments

Comments
 (0)