Skip to content

Commit

Permalink
fix(j2p): support sint/fixed/sfixed types convertion and generic call (
Browse files Browse the repository at this point in the history
…#57)

* test: add test data for pb

* fix: fix convert error with sint,fix32,fix64

* test: test proto generic example

* feat(proto): support streaming mode for proto (#56)

feat: support streaming mode for proto

* fix: fix unsafe convert

* style: delete unused code

* test: delete service code in proto

* test: update test data

* fix: encodeMapKey

* test: test sint/fix types map

---------

Co-authored-by: Marina Sakai <[email protected]>
  • Loading branch information
khan-yin and Marina-Sakai committed Jun 17, 2024
1 parent 5f5563f commit 1ce2e1f
Show file tree
Hide file tree
Showing 35 changed files with 9,962 additions and 4,592 deletions.
253 changes: 249 additions & 4 deletions conv/j2p/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ func TestMain(m *testing.M) {
}

const (
exampleIDLPath = "testdata/idl/example2.proto"
exampleJSON = "testdata/data/example2req.json"
exampleProtoPath = "testdata/data/example2_pb.bin"
exampleIDLPath = "testdata/idl/example2.proto"
exampleJSON = "testdata/data/example2req.json"
exampleProtoPath = "testdata/data/example2_pb.bin" // not used
basicExampleJSON = "testdata/data/basic_example.json"
basicExampleIDLPath = "testdata/idl/basic_example.proto"
)

func TestBuildExampleJSONData(t *testing.T) {
buildExampleJSONData()
}

func TestBuildBasicExampleJSONData(t *testing.T) {
buildBasicExampleJSONData()
}

func TestConvJSON2Protobf(t *testing.T) {
// buildExampleJSONData()
desc := getExampleDesc()
Expand Down Expand Up @@ -85,6 +95,71 @@ func TestConvJSON2Protobf(t *testing.T) {
require.Equal(t, exp, act)
}

func TestConvJSON2Protobf_BasicExample(t *testing.T) {
// buildExampleJSONData()
desc := getBasicExampleDesc()
data := getBasicExampleData()
// pdata, _ := ioutil.ReadFile(util_test.MustGitPath(exampleProtoPath))
// fmt.Println(pdata)
cv := NewBinaryConv(conv.Options{})
ctx := context.Background()
// get protobuf-encode bytes
out, err := cv.Do(ctx, desc, data)
require.Nil(t, err)
exp := &base.BasicExample{}
// unmarshal target struct
err = json.Unmarshal(data, exp)
require.Nil(t, err)
act := &base.BasicExample{}
l := 0
// fmt.Print(out)
dataLen := len(out)
// fastRead to get target struct
for l < dataLen {
id, wtyp, tagLen := goprotowire.ConsumeTag(out)
fmt.Println("id", id)
fmt.Println("w", wtyp)
if tagLen < 0 {
t.Fatal("test failed")
}
l += tagLen
out = out[tagLen:]
offset, err := act.FastRead(out, int8(wtyp), int32(id))
require.Nil(t, err)
out = out[offset:]
l += offset
}
require.Nil(t, err)
// compare exp and act struct
fmt.Println(exp)
fmt.Println("----------------")
fmt.Println(act)
require.Equal(t, exp, act)
}

func getBasicExampleDesc() *proto.TypeDescriptor {
opts := proto.Options{}
includeDirs := util_test.MustGitPath("testdata/idl/") // includeDirs is used to find the include files.
svc, err := opts.NewDescriptorFromPath(context.Background(), util_test.MustGitPath(basicExampleIDLPath), includeDirs)
if err != nil {
panic(err)
}
res := (*svc).LookupMethodByName("ExampleMethod").Input()

if res == nil {
panic("can't find Target MessageDescriptor")
}
return res
}

func getBasicExampleData() []byte {
out, err := ioutil.ReadFile(util_test.MustGitPath(basicExampleJSON))
if err != nil {
panic(err)
}
return out
}

func getExampleDesc() *proto.TypeDescriptor {
opts := proto.Options{}
includeDirs := util_test.MustGitPath("testdata/idl/") // includeDirs is used to find the include files.
Expand Down Expand Up @@ -116,11 +191,13 @@ func getExample2Req() *example2.ExampleReq {
req.InnerBase2.Bool = true
req.InnerBase2.Uint32 = uint32(123)
req.InnerBase2.Uint64 = uint64(123)
req.InnerBase2.Int32 = int32(123)
req.InnerBase2.SInt64 = int64(123)
req.InnerBase2.Double = float64(22.3)
req.InnerBase2.String_ = "hello_inner"
req.InnerBase2.ListInt32 = []int32{12, 13, 14, 15, 16, 17}
req.InnerBase2.MapStringString = map[string]string{"m1": "aaa", "m2": "bbb"}
req.InnerBase2.SetInt32 = []int32{200, 201, 202, 203, 204, 205}
req.InnerBase2.ListSInt64 = []int64{200, 201, 202, 203, 204, 205}
req.InnerBase2.Foo = example2.FOO_FOO_A
req.InnerBase2.MapInt32String = map[int32]string{1: "aaa", 2: "bbb", 3: "ccc", 4: "ddd"}
req.InnerBase2.Binary = []byte{0x1, 0x2, 0x3, 0x4}
Expand Down Expand Up @@ -203,6 +280,140 @@ func getExample2Req() *example2.ExampleReq {
return req
}

func getBasicExampleReq() *base.BasicExample {
req := new(base.BasicExample)
req = &base.BasicExample{
Int32: 123,
Int64: 123,
Uint32: uint32(math.MaxInt32),
Uint64: uint64(math.MaxInt64),
Sint32: 123,
Sint64: 123,
Sfixed32: 123,
Sfixed64: 123,
Fixed32: 123,
Fixed64: 123,
Float: 123.123,
Double: 123.123,
Bool: true,
Str: "hello world!",
Bytes: []byte{0x1, 0x2, 0x3, 0x4},
ListInt32: []int32{100, 200, 300, 400, 500},
ListInt64: []int64{100, 200, 300, 400, 500},
ListUint32: []uint32{100, 200, 300, 400, 500},
ListUint64: []uint64{100, 200, 300, 400, 500},
ListSint32: []int32{100, 200, 300, 400, 500},
ListSint64: []int64{100, 200, 300, 400, 500},
ListSfixed32: []int32{100, 200, 300, 400, 500},
ListSfixed64: []int64{100, 200, 300, 400, 500},
ListFixed32: []uint32{100, 200, 300, 400, 500},
ListFixed64: []uint64{100, 200, 300, 400, 500},
ListFloat: []float32{1.1, 2.2, 3.3, 4.4, 5.5},
ListDouble: []float64{1.1, 2.2, 3.3, 4.4, 5.5},
ListBool: []bool{true, false, true, false, true},
ListString: []string{"a1", "b2", "c3", "d4", "e5"},
ListBytes: [][]byte{{0x1, 0x2, 0x3, 0x4}, {0x5, 0x6, 0x7, 0x8}},
MapInt64SINT32: map[int64]int32{
0: 0,
math.MaxInt64: math.MaxInt32,
math.MinInt64: math.MinInt32,
},
MapInt64Sfixed32: map[int64]int32{
0: 0,
math.MaxInt64: math.MaxInt32,
math.MinInt64: math.MinInt32,
},
MapInt64Fixed32: map[int64]uint32{
math.MaxInt64: uint32(math.MaxInt32),
math.MinInt64: 0,
},
MapInt64Uint32: map[int64]uint32{
0: 0,
math.MaxInt64: uint32(math.MaxInt32),
math.MinInt64: 0,
},
MapInt64Double: map[int64]float64{
0: 0,
math.MaxInt64: math.MaxFloat64,
math.MinInt64: math.SmallestNonzeroFloat64,
},
MapInt64Bool: map[int64]bool{
0: false,
math.MaxInt64: true,
math.MinInt64: false,
},
MapInt64String: map[int64]string{
0: "0",
math.MaxInt64: "max",
math.MinInt64: "min",
},
MapInt64Bytes: map[int64][]byte{
0: {0x0},
math.MaxInt64: {0x1, 0x2, 0x3, 0x4},
math.MinInt64: {0x5, 0x6, 0x7, 0x8},
},
MapInt64Float: map[int64]float32{
0: 0,
math.MaxInt64: math.MaxFloat32,
math.MinInt64: math.SmallestNonzeroFloat32,
},
MapInt64Int32: map[int64]int32{
0: 0,
math.MaxInt64: math.MaxInt32,
math.MinInt64: math.MinInt32,
},
MapstringSINT64: map[string]int64{
"0": 0,
"max": math.MaxInt64,
"min": math.MinInt64,
},
MapstringSfixed64: map[string]int64{
"0": 0,
"max": math.MaxInt64,
"min": math.MinInt64,
},
MapstringFixed64: map[string]uint64{
"max": uint64(math.MaxInt64),
"min": 0,
},
MapstringUint64: map[string]uint64{
"max": uint64(math.MaxInt64),
"min": 0,
},
MapstringDouble: map[string]float64{
"0": 0,
"max": math.MaxFloat64,
"min": math.SmallestNonzeroFloat64,
},
MapstringBool: map[string]bool{
"0": false,
"max": true,
},
MapstringString: map[string]string{
"0": "0",
"max": "max",
"min": "min",
},
MapstringBytes: map[string][]byte{
"0": {0x0},
"max": {0x1, 0x2, 0x3, 0x4},
"min": {0x5, 0x6, 0x7, 0x8},
},
MapstringFloat: map[string]float32{
"0": 0,
"max": math.MaxFloat32,
"min": math.SmallestNonzeroFloat32,
},
MapstringInt64: map[string]int64{
"0": 0,
"max": math.MaxInt64,
"min": math.MinInt64,
},
}

return req
}

func buildExampleJSONData() error {
req := getExample2Req()
data, err := json.Marshal(req)
Expand Down Expand Up @@ -237,6 +448,40 @@ func buildExampleJSONData() error {
return nil
}

func buildBasicExampleJSONData() error {
req := getBasicExampleReq()
data, err := json.Marshal(req)
if err != nil {
panic(fmt.Sprintf("buildExampleJSONData failed, err: %v", err.Error()))
}
checkExist := func(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
var file *os.File
absoluteExampleJSONPath := util_test.MustGitPath(basicExampleJSON)
if checkExist(absoluteExampleJSONPath) == true {
if err = os.Remove(absoluteExampleJSONPath); err != nil {
panic("delete protoJSONFile failed")
}
}
file, err = os.Create(absoluteExampleJSONPath)
if err != nil {
panic("create protoJSONFile failed")
}
defer file.Close()
if _, err := file.WriteString(string(data)); err != nil {
panic("write protoJSONData failed")
}
return nil
}

func getExampleInt2Float() *proto.TypeDescriptor {
includeDirs := util_test.MustGitPath("testdata/idl/") // includeDirs is used to find the include files.
svc, err := proto.NewDescritorFromPath(context.Background(), util_test.MustGitPath(exampleIDLPath), includeDirs)
Expand Down
Loading

0 comments on commit 1ce2e1f

Please sign in to comment.