@@ -43,11 +43,21 @@ func TestMain(m *testing.M) {
4343}
4444
4545const (
46- exampleIDLPath = "testdata/idl/example2.proto"
47- exampleJSON = "testdata/data/example2req.json"
48- exampleProtoPath = "testdata/data/example2_pb.bin"
46+ exampleIDLPath = "testdata/idl/example2.proto"
47+ exampleJSON = "testdata/data/example2req.json"
48+ exampleProtoPath = "testdata/data/example2_pb.bin" // not used
49+ basicExampleJSON = "testdata/data/basic_example.json"
50+ basicExampleIDLPath = "testdata/idl/basic_example.proto"
4951)
5052
53+ func TestBuildExampleJSONData (t * testing.T ) {
54+ buildExampleJSONData ()
55+ }
56+
57+ func TestBuildBasicExampleJSONData (t * testing.T ) {
58+ buildBasicExampleJSONData ()
59+ }
60+
5161func TestConvJSON2Protobf (t * testing.T ) {
5262 // buildExampleJSONData()
5363 desc := getExampleDesc ()
@@ -85,6 +95,71 @@ func TestConvJSON2Protobf(t *testing.T) {
8595 require .Equal (t , exp , act )
8696}
8797
98+ func TestConvJSON2Protobf_BasicExample (t * testing.T ) {
99+ // buildExampleJSONData()
100+ desc := getBasicExampleDesc ()
101+ data := getBasicExampleData ()
102+ // pdata, _ := ioutil.ReadFile(util_test.MustGitPath(exampleProtoPath))
103+ // fmt.Println(pdata)
104+ cv := NewBinaryConv (conv.Options {})
105+ ctx := context .Background ()
106+ // get protobuf-encode bytes
107+ out , err := cv .Do (ctx , desc , data )
108+ require .Nil (t , err )
109+ exp := & base.BasicExample {}
110+ // unmarshal target struct
111+ err = json .Unmarshal (data , exp )
112+ require .Nil (t , err )
113+ act := & base.BasicExample {}
114+ l := 0
115+ // fmt.Print(out)
116+ dataLen := len (out )
117+ // fastRead to get target struct
118+ for l < dataLen {
119+ id , wtyp , tagLen := goprotowire .ConsumeTag (out )
120+ fmt .Println ("id" , id )
121+ fmt .Println ("w" , wtyp )
122+ if tagLen < 0 {
123+ t .Fatal ("test failed" )
124+ }
125+ l += tagLen
126+ out = out [tagLen :]
127+ offset , err := act .FastRead (out , int8 (wtyp ), int32 (id ))
128+ require .Nil (t , err )
129+ out = out [offset :]
130+ l += offset
131+ }
132+ require .Nil (t , err )
133+ // compare exp and act struct
134+ fmt .Println (exp )
135+ fmt .Println ("----------------" )
136+ fmt .Println (act )
137+ require .Equal (t , exp , act )
138+ }
139+
140+ func getBasicExampleDesc () * proto.TypeDescriptor {
141+ opts := proto.Options {}
142+ includeDirs := util_test .MustGitPath ("testdata/idl/" ) // includeDirs is used to find the include files.
143+ svc , err := opts .NewDescriptorFromPath (context .Background (), util_test .MustGitPath (basicExampleIDLPath ), includeDirs )
144+ if err != nil {
145+ panic (err )
146+ }
147+ res := (* svc ).LookupMethodByName ("ExampleMethod" ).Input ()
148+
149+ if res == nil {
150+ panic ("can't find Target MessageDescriptor" )
151+ }
152+ return res
153+ }
154+
155+ func getBasicExampleData () []byte {
156+ out , err := ioutil .ReadFile (util_test .MustGitPath (basicExampleJSON ))
157+ if err != nil {
158+ panic (err )
159+ }
160+ return out
161+ }
162+
88163func getExampleDesc () * proto.TypeDescriptor {
89164 opts := proto.Options {}
90165 includeDirs := util_test .MustGitPath ("testdata/idl/" ) // includeDirs is used to find the include files.
@@ -116,11 +191,13 @@ func getExample2Req() *example2.ExampleReq {
116191 req .InnerBase2 .Bool = true
117192 req .InnerBase2 .Uint32 = uint32 (123 )
118193 req .InnerBase2 .Uint64 = uint64 (123 )
194+ req .InnerBase2 .Int32 = int32 (123 )
195+ req .InnerBase2 .SInt64 = int64 (123 )
119196 req .InnerBase2 .Double = float64 (22.3 )
120197 req .InnerBase2 .String_ = "hello_inner"
121198 req .InnerBase2 .ListInt32 = []int32 {12 , 13 , 14 , 15 , 16 , 17 }
122199 req .InnerBase2 .MapStringString = map [string ]string {"m1" : "aaa" , "m2" : "bbb" }
123- req .InnerBase2 .SetInt32 = []int32 {200 , 201 , 202 , 203 , 204 , 205 }
200+ req .InnerBase2 .ListSInt64 = []int64 {200 , 201 , 202 , 203 , 204 , 205 }
124201 req .InnerBase2 .Foo = example2 .FOO_FOO_A
125202 req .InnerBase2 .MapInt32String = map [int32 ]string {1 : "aaa" , 2 : "bbb" , 3 : "ccc" , 4 : "ddd" }
126203 req .InnerBase2 .Binary = []byte {0x1 , 0x2 , 0x3 , 0x4 }
@@ -203,6 +280,140 @@ func getExample2Req() *example2.ExampleReq {
203280 return req
204281}
205282
283+ func getBasicExampleReq () * base.BasicExample {
284+ req := new (base.BasicExample )
285+ req = & base.BasicExample {
286+ Int32 : 123 ,
287+ Int64 : 123 ,
288+ Uint32 : uint32 (math .MaxInt32 ),
289+ Uint64 : uint64 (math .MaxInt64 ),
290+ Sint32 : 123 ,
291+ Sint64 : 123 ,
292+ Sfixed32 : 123 ,
293+ Sfixed64 : 123 ,
294+ Fixed32 : 123 ,
295+ Fixed64 : 123 ,
296+ Float : 123.123 ,
297+ Double : 123.123 ,
298+ Bool : true ,
299+ Str : "hello world!" ,
300+ Bytes : []byte {0x1 , 0x2 , 0x3 , 0x4 },
301+ ListInt32 : []int32 {100 , 200 , 300 , 400 , 500 },
302+ ListInt64 : []int64 {100 , 200 , 300 , 400 , 500 },
303+ ListUint32 : []uint32 {100 , 200 , 300 , 400 , 500 },
304+ ListUint64 : []uint64 {100 , 200 , 300 , 400 , 500 },
305+ ListSint32 : []int32 {100 , 200 , 300 , 400 , 500 },
306+ ListSint64 : []int64 {100 , 200 , 300 , 400 , 500 },
307+ ListSfixed32 : []int32 {100 , 200 , 300 , 400 , 500 },
308+ ListSfixed64 : []int64 {100 , 200 , 300 , 400 , 500 },
309+ ListFixed32 : []uint32 {100 , 200 , 300 , 400 , 500 },
310+ ListFixed64 : []uint64 {100 , 200 , 300 , 400 , 500 },
311+ ListFloat : []float32 {1.1 , 2.2 , 3.3 , 4.4 , 5.5 },
312+ ListDouble : []float64 {1.1 , 2.2 , 3.3 , 4.4 , 5.5 },
313+ ListBool : []bool {true , false , true , false , true },
314+ ListString : []string {"a1" , "b2" , "c3" , "d4" , "e5" },
315+ ListBytes : [][]byte {{0x1 , 0x2 , 0x3 , 0x4 }, {0x5 , 0x6 , 0x7 , 0x8 }},
316+ MapInt64SINT32 : map [int64 ]int32 {
317+ 0 : 0 ,
318+ math .MaxInt64 : math .MaxInt32 ,
319+ math .MinInt64 : math .MinInt32 ,
320+ },
321+ MapInt64Sfixed32 : map [int64 ]int32 {
322+ 0 : 0 ,
323+ math .MaxInt64 : math .MaxInt32 ,
324+ math .MinInt64 : math .MinInt32 ,
325+ },
326+ MapInt64Fixed32 : map [int64 ]uint32 {
327+ math .MaxInt64 : uint32 (math .MaxInt32 ),
328+ math .MinInt64 : 0 ,
329+ },
330+ MapInt64Uint32 : map [int64 ]uint32 {
331+ 0 : 0 ,
332+ math .MaxInt64 : uint32 (math .MaxInt32 ),
333+ math .MinInt64 : 0 ,
334+ },
335+ MapInt64Double : map [int64 ]float64 {
336+ 0 : 0 ,
337+ math .MaxInt64 : math .MaxFloat64 ,
338+ math .MinInt64 : math .SmallestNonzeroFloat64 ,
339+ },
340+ MapInt64Bool : map [int64 ]bool {
341+ 0 : false ,
342+ math .MaxInt64 : true ,
343+ math .MinInt64 : false ,
344+ },
345+ MapInt64String : map [int64 ]string {
346+ 0 : "0" ,
347+ math .MaxInt64 : "max" ,
348+ math .MinInt64 : "min" ,
349+ },
350+ MapInt64Bytes : map [int64 ][]byte {
351+ 0 : {0x0 },
352+ math .MaxInt64 : {0x1 , 0x2 , 0x3 , 0x4 },
353+ math .MinInt64 : {0x5 , 0x6 , 0x7 , 0x8 },
354+ },
355+ MapInt64Float : map [int64 ]float32 {
356+ 0 : 0 ,
357+ math .MaxInt64 : math .MaxFloat32 ,
358+ math .MinInt64 : math .SmallestNonzeroFloat32 ,
359+ },
360+ MapInt64Int32 : map [int64 ]int32 {
361+ 0 : 0 ,
362+ math .MaxInt64 : math .MaxInt32 ,
363+ math .MinInt64 : math .MinInt32 ,
364+ },
365+ MapstringSINT64 : map [string ]int64 {
366+ "0" : 0 ,
367+ "max" : math .MaxInt64 ,
368+ "min" : math .MinInt64 ,
369+ },
370+ MapstringSfixed64 : map [string ]int64 {
371+ "0" : 0 ,
372+ "max" : math .MaxInt64 ,
373+ "min" : math .MinInt64 ,
374+ },
375+ MapstringFixed64 : map [string ]uint64 {
376+ "max" : uint64 (math .MaxInt64 ),
377+ "min" : 0 ,
378+ },
379+ MapstringUint64 : map [string ]uint64 {
380+ "max" : uint64 (math .MaxInt64 ),
381+ "min" : 0 ,
382+ },
383+ MapstringDouble : map [string ]float64 {
384+ "0" : 0 ,
385+ "max" : math .MaxFloat64 ,
386+ "min" : math .SmallestNonzeroFloat64 ,
387+ },
388+ MapstringBool : map [string ]bool {
389+ "0" : false ,
390+ "max" : true ,
391+ },
392+ MapstringString : map [string ]string {
393+ "0" : "0" ,
394+ "max" : "max" ,
395+ "min" : "min" ,
396+ },
397+ MapstringBytes : map [string ][]byte {
398+ "0" : {0x0 },
399+ "max" : {0x1 , 0x2 , 0x3 , 0x4 },
400+ "min" : {0x5 , 0x6 , 0x7 , 0x8 },
401+ },
402+ MapstringFloat : map [string ]float32 {
403+ "0" : 0 ,
404+ "max" : math .MaxFloat32 ,
405+ "min" : math .SmallestNonzeroFloat32 ,
406+ },
407+ MapstringInt64 : map [string ]int64 {
408+ "0" : 0 ,
409+ "max" : math .MaxInt64 ,
410+ "min" : math .MinInt64 ,
411+ },
412+ }
413+
414+ return req
415+ }
416+
206417func buildExampleJSONData () error {
207418 req := getExample2Req ()
208419 data , err := json .Marshal (req )
@@ -237,6 +448,40 @@ func buildExampleJSONData() error {
237448 return nil
238449}
239450
451+ func buildBasicExampleJSONData () error {
452+ req := getBasicExampleReq ()
453+ data , err := json .Marshal (req )
454+ if err != nil {
455+ panic (fmt .Sprintf ("buildExampleJSONData failed, err: %v" , err .Error ()))
456+ }
457+ checkExist := func (path string ) bool {
458+ _ , err := os .Stat (path )
459+ if err != nil {
460+ if os .IsExist (err ) {
461+ return true
462+ }
463+ return false
464+ }
465+ return true
466+ }
467+ var file * os.File
468+ absoluteExampleJSONPath := util_test .MustGitPath (basicExampleJSON )
469+ if checkExist (absoluteExampleJSONPath ) == true {
470+ if err = os .Remove (absoluteExampleJSONPath ); err != nil {
471+ panic ("delete protoJSONFile failed" )
472+ }
473+ }
474+ file , err = os .Create (absoluteExampleJSONPath )
475+ if err != nil {
476+ panic ("create protoJSONFile failed" )
477+ }
478+ defer file .Close ()
479+ if _ , err := file .WriteString (string (data )); err != nil {
480+ panic ("write protoJSONData failed" )
481+ }
482+ return nil
483+ }
484+
240485func getExampleInt2Float () * proto.TypeDescriptor {
241486 includeDirs := util_test .MustGitPath ("testdata/idl/" ) // includeDirs is used to find the include files.
242487 svc , err := proto .NewDescritorFromPath (context .Background (), util_test .MustGitPath (exampleIDLPath ), includeDirs )
0 commit comments