@@ -5,204 +5,204 @@ import (
55 "fmt"
66 "reflect"
77
8+ "github.com/yomorun/yomo-codec-golang/internal/utils"
9+
810 y3 "github.com/yomorun/yomo-codec-golang"
9- "github.com/yomorun/yomo-codec-golang/pkg/spec/encoding"
11+ )
1012
11- "github.com/yomorun/yomo-codec-golang/internal/utils"
13+ var (
14+ startingToken byte = 0x01
1215)
1316
14- // marshalPrimitive: marshal primitive to []byte
15- func marshalPrimitive (observe byte , input interface {}) (buf []byte , err error ) {
16- switch reflect .ValueOf (input ).Kind () {
17+ func encodeBasic (observe byte , input interface {}) (buf []byte , err error ) {
18+ encoder , err := marshalBasic (observe , input , nil )
19+ if err != nil {
20+ return []byte {}, err
21+ }
22+ return encoder .Encode (), nil
23+ }
24+
25+ func marshalBasic (observe byte , input interface {}, root * y3.NodePacketEncoder ) (encoder * y3.NodePacketEncoder , err error ) {
26+ if observe == 0 {
27+ panic (fmt .Errorf ("observe cannot be 0" ))
28+ }
29+
30+ if root == nil {
31+ root = y3 .NewNodePacketEncoder (int (startingToken ))
32+ }
33+
34+ value := reflect .ValueOf (input )
35+ switch value .Kind () {
1736 case reflect .String :
18- buf , err = marshalString ( input )
37+ marshalBasicString ( observe , input , root )
1938 case reflect .Int32 :
20- buf , err = marshalInt32 ( input )
39+ marshalBasicInt32 ( observe , input , root )
2140 case reflect .Uint32 :
22- buf , err = marshalUint32 ( input )
41+ marshalBasicUint32 ( observe , input , root )
2342 case reflect .Int64 :
24- buf , err = marshalInt64 ( input )
43+ marshalBasicInt64 ( observe , input , root )
2544 case reflect .Uint64 :
26- buf , err = marshalUint64 ( input )
45+ marshalBasicUint64 ( observe , input , root )
2746 case reflect .Float32 :
28- buf , err = marshalFloat32 ( input )
47+ marshalBasicFloat32 ( observe , input , root )
2948 case reflect .Float64 :
30- buf , err = marshalFloat64 ( input )
49+ marshalBasicFloat64 ( observe , input , root )
3150 case reflect .Array , reflect .Slice :
32- if reflect .ValueOf (input ).Len () == 0 {
33- break
34- }
35- switch reflect .ValueOf (input ).Index (0 ).Elem ().Kind () {
36- case reflect .String :
37- buf = marshalStringSlice (observe , input )
38- case reflect .Int32 :
39- buf = marshalInt32Slice (observe , input )
40- case reflect .Uint32 :
41- buf = marshalUint32Slice (observe , input )
42- case reflect .Int64 :
43- buf = marshalInt64Slice (observe , input )
44- case reflect .Uint64 :
45- buf = marshalUint64Slice (observe , input )
46- case reflect .Float32 :
47- buf = marshalFloat32Slice (observe , input )
48- case reflect .Float64 :
49- buf = marshalFloat64Slice (observe , input )
50- default :
51- panic (errors .New ("::Marshal error: no matching type in Slice" ))
52- }
51+ marshalBasicSlice (observe , value , root )
5352 default :
5453 panic (errors .New ("::Marshal error: no matching type" ))
5554 }
5655
57- return buf , err
56+ return root , nil
57+ }
58+
59+ func marshalBasicSlice (observe byte , value reflect.Value , encoder * y3.NodePacketEncoder ) {
60+ if value .Len () == 0 {
61+ return
62+ }
63+
64+ switch value .Index (0 ).Kind () {
65+ case reflect .String :
66+ marshalBasicStringSlice (observe , value , encoder )
67+ case reflect .Int32 :
68+ marshalBasicInt32Slice (observe , value , encoder )
69+ case reflect .Uint32 :
70+ marshalBasicUint32Slice (observe , value , encoder )
71+ case reflect .Int64 :
72+ marshalBasicInt64Slice (observe , value , encoder )
73+ case reflect .Uint64 :
74+ marshalBasicUint64Slice (observe , value , encoder )
75+ case reflect .Float32 :
76+ marshalBasicFloat32Slice (observe , value , encoder )
77+ case reflect .Float64 :
78+ marshalBasicFloat64Slice (observe , value , encoder )
79+ default :
80+ panic (errors .New ("::Marshal error: no matching type in Slice" ))
81+ }
5882}
5983
60- // marshalString: marshal string to []byte
61- func marshalString (input interface {}) (buf []byte , err error ) {
62- return []byte (fmt .Sprintf ("%v" , input )), nil
84+ func marshalBasicString (observe byte , input interface {}, encoder * y3.NodePacketEncoder ) {
85+ var item = y3 .NewPrimitivePacketEncoder (int (observe ))
86+ item .SetStringValue (fmt .Sprintf ("%v" , input ))
87+ encoder .AddPrimitivePacket (item )
6388}
6489
65- // marshalInt32: marshal int32 to []byte
66- func marshalInt32 (input interface {}) (buf []byte , err error ) {
67- size := encoding .SizeOfPVarInt32 (input .(int32 ))
68- codec := encoding.VarCodec {Size : size }
69- buf = make ([]byte , size )
70- err = codec .EncodePVarInt32 (buf , input .(int32 ))
71- return buf , err
90+ func marshalBasicInt32 (observe byte , input interface {}, encoder * y3.NodePacketEncoder ) {
91+ var item = y3 .NewPrimitivePacketEncoder (int (observe ))
92+ item .SetInt32Value (input .(int32 ))
93+ encoder .AddPrimitivePacket (item )
7294}
7395
74- // marshalUint32: marshal uint32 to []byte
75- func marshalUint32 (input interface {}) (buf []byte , err error ) {
76- size := encoding .SizeOfPVarUInt32 (input .(uint32 ))
77- codec := encoding.VarCodec {Size : size }
78- buf = make ([]byte , size )
79- err = codec .EncodePVarUInt32 (buf , input .(uint32 ))
80- return buf , err
96+ func marshalBasicUint32 (observe byte , input interface {}, encoder * y3.NodePacketEncoder ) {
97+ var item = y3 .NewPrimitivePacketEncoder (int (observe ))
98+ item .SetUInt32Value (input .(uint32 ))
99+ encoder .AddPrimitivePacket (item )
81100}
82101
83- // marshalInt64: marshal int64 to []byte
84- func marshalInt64 (input interface {}) (buf []byte , err error ) {
85- size := encoding .SizeOfPVarInt64 (input .(int64 ))
86- codec := encoding.VarCodec {Size : size }
87- buf = make ([]byte , size )
88- err = codec .EncodePVarInt64 (buf , input .(int64 ))
89- return buf , err
102+ func marshalBasicInt64 (observe byte , input interface {}, encoder * y3.NodePacketEncoder ) {
103+ var item = y3 .NewPrimitivePacketEncoder (int (observe ))
104+ item .SetInt64Value (input .(int64 ))
105+ encoder .AddPrimitivePacket (item )
90106}
91107
92- // marshalUint64: marshal uint64 to []byte
93- func marshalUint64 (input interface {}) (buf []byte , err error ) {
94- size := encoding .SizeOfPVarUInt64 (input .(uint64 ))
95- codec := encoding.VarCodec {Size : size }
96- buf = make ([]byte , size )
97- err = codec .EncodePVarUInt64 (buf , input .(uint64 ))
98- return buf , err
108+ func marshalBasicUint64 (observe byte , input interface {}, encoder * y3.NodePacketEncoder ) {
109+ var item = y3 .NewPrimitivePacketEncoder (int (observe ))
110+ item .SetUInt64Value (input .(uint64 ))
111+ encoder .AddPrimitivePacket (item )
99112}
100113
101- // marshalFloat32: marshal float32 to []byte
102- func marshalFloat32 (input interface {}) (buf []byte , err error ) {
103- size := encoding .SizeOfVarFloat32 (input .(float32 ))
104- codec := encoding.VarCodec {Size : size }
105- buf = make ([]byte , size )
106- err = codec .EncodeVarFloat32 (buf , input .(float32 ))
107- return buf , err
114+ func marshalBasicFloat32 (observe byte , input interface {}, encoder * y3.NodePacketEncoder ) {
115+ var item = y3 .NewPrimitivePacketEncoder (int (observe ))
116+ item .SetFloat32Value (input .(float32 ))
117+ encoder .AddPrimitivePacket (item )
108118}
109119
110- // marshalFloat64: marshal float64 to []byte
111- func marshalFloat64 (input interface {}) (buf []byte , err error ) {
112- size := encoding .SizeOfVarFloat64 (input .(float64 ))
113- codec := encoding.VarCodec {Size : size }
114- buf = make ([]byte , size )
115- err = codec .EncodeVarFloat64 (buf , input .(float64 ))
116- return buf , err
120+ func marshalBasicFloat64 (observe byte , input interface {}, encoder * y3.NodePacketEncoder ) {
121+ var item = y3 .NewPrimitivePacketEncoder (int (observe ))
122+ item .SetFloat64Value (input .(float64 ))
123+ encoder .AddPrimitivePacket (item )
117124}
118125
119- // marshalStringSlice: marshal string slice to []byte
120- func marshalStringSlice (observe byte , input interface {}) []byte {
126+ func marshalBasicStringSlice (observe byte , value reflect.Value , encoder * y3.NodePacketEncoder ) {
121127 var node = y3 .NewNodeArrayPacketEncoder (int (observe ))
122- if out , ok := utils .ToStringSliceArray (input ); ok {
128+ if out , ok := utils .ToStringSliceArray (value . Interface () ); ok {
123129 for _ , v := range out {
124130 var item = y3 .NewPrimitivePacketEncoder (utils .KeyOfArrayItem )
125131 item .SetStringValue (fmt .Sprintf ("%v" , v ))
126132 node .AddPrimitivePacket (item )
127133 }
128134 }
129- return node . GetValBuf ( )
135+ encoder . AddNodePacket ( node )
130136}
131137
132- // marshalInt32Slice: marshal int32 slice to []byte
133- func marshalInt32Slice (observe byte , input interface {}) []byte {
138+ func marshalBasicInt32Slice (observe byte , value reflect.Value , encoder * y3.NodePacketEncoder ) {
134139 var node = y3 .NewNodeArrayPacketEncoder (int (observe ))
135- if out , ok := utils .ToInt64SliceArray (input ); ok {
140+ if out , ok := utils .ToInt64SliceArray (value . Interface () ); ok {
136141 for _ , v := range out {
137142 var item = y3 .NewPrimitivePacketEncoder (utils .KeyOfArrayItem )
138143 item .SetInt32Value (int32 (v .(int64 )))
139144 node .AddPrimitivePacket (item )
140145 }
141146 }
142- return node . GetValBuf ( )
147+ encoder . AddNodePacket ( node )
143148}
144149
145- // marshalUint32Slice: marshal uint32 slice to []byte
146- func marshalUint32Slice (observe byte , input interface {}) []byte {
150+ func marshalBasicUint32Slice (observe byte , value reflect.Value , encoder * y3.NodePacketEncoder ) {
147151 var node = y3 .NewNodeArrayPacketEncoder (int (observe ))
148- if out , ok := utils .ToUInt64SliceArray (input ); ok {
152+ if out , ok := utils .ToUInt64SliceArray (value . Interface () ); ok {
149153 for _ , v := range out {
150154 var item = y3 .NewPrimitivePacketEncoder (utils .KeyOfArrayItem )
151155 item .SetUInt32Value (uint32 (v .(uint64 )))
152156 node .AddPrimitivePacket (item )
153157 }
154158 }
155- return node . GetValBuf ( )
159+ encoder . AddNodePacket ( node )
156160}
157161
158- // marshalInt64Slice: marshal int64 slice to []byte
159- func marshalInt64Slice (observe byte , input interface {}) []byte {
162+ func marshalBasicInt64Slice (observe byte , value reflect.Value , encoder * y3.NodePacketEncoder ) {
160163 var node = y3 .NewNodeArrayPacketEncoder (int (observe ))
161- if out , ok := utils .ToInt64SliceArray (input ); ok {
164+ if out , ok := utils .ToInt64SliceArray (value . Interface () ); ok {
162165 for _ , v := range out {
163166 var item = y3 .NewPrimitivePacketEncoder (utils .KeyOfArrayItem )
164167 item .SetInt64Value (v .(int64 ))
165168 node .AddPrimitivePacket (item )
166169 }
167170 }
168- return node . GetValBuf ( )
171+ encoder . AddNodePacket ( node )
169172}
170173
171- // marshalUint64Slice: marshal uint64 slice to []byte
172- func marshalUint64Slice (observe byte , input interface {}) []byte {
174+ func marshalBasicUint64Slice (observe byte , value reflect.Value , encoder * y3.NodePacketEncoder ) {
173175 var node = y3 .NewNodeArrayPacketEncoder (int (observe ))
174- if out , ok := utils .ToUInt64SliceArray (input ); ok {
176+ if out , ok := utils .ToUInt64SliceArray (value . Interface () ); ok {
175177 for _ , v := range out {
176178 var item = y3 .NewPrimitivePacketEncoder (utils .KeyOfArrayItem )
177179 item .SetUInt64Value (v .(uint64 ))
178180 node .AddPrimitivePacket (item )
179181 }
180182 }
181- return node . GetValBuf ( )
183+ encoder . AddNodePacket ( node )
182184}
183185
184- // marshalFloat32Slice: marshal float32 slice to []byte
185- func marshalFloat32Slice (observe byte , input interface {}) []byte {
186+ func marshalBasicFloat32Slice (observe byte , value reflect.Value , encoder * y3.NodePacketEncoder ) {
186187 var node = y3 .NewNodeArrayPacketEncoder (int (observe ))
187- if out , ok := utils .ToUFloat64SliceArray (input ); ok {
188+ if out , ok := utils .ToUFloat64SliceArray (value . Interface () ); ok {
188189 for _ , v := range out {
189190 var item = y3 .NewPrimitivePacketEncoder (utils .KeyOfArrayItem )
190191 item .SetFloat32Value (float32 (v .(float64 )))
191192 node .AddPrimitivePacket (item )
192193 }
193194 }
194- return node . GetValBuf ( )
195+ encoder . AddNodePacket ( node )
195196}
196197
197- // marshalFloat64Slice: marshal float64 slice to []byte
198- func marshalFloat64Slice (observe byte , input interface {}) []byte {
198+ func marshalBasicFloat64Slice (observe byte , value reflect.Value , encoder * y3.NodePacketEncoder ) {
199199 var node = y3 .NewNodeArrayPacketEncoder (int (observe ))
200- if out , ok := utils .ToUFloat64SliceArray (input ); ok {
200+ if out , ok := utils .ToUFloat64SliceArray (value . Interface () ); ok {
201201 for _ , v := range out {
202202 var item = y3 .NewPrimitivePacketEncoder (utils .KeyOfArrayItem )
203203 item .SetFloat64Value (v .(float64 ))
204204 node .AddPrimitivePacket (item )
205205 }
206206 }
207- return node . GetValBuf ( )
207+ encoder . AddNodePacket ( node )
208208}
0 commit comments