-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_test.go
More file actions
84 lines (68 loc) · 1.71 KB
/
Copy pathparse_test.go
File metadata and controls
84 lines (68 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
_ "github.com/golang/protobuf/protoc-gen-go/descriptor"
_ "github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/golang/protobuf/proto"
_ "github.com/golang/protobuf/descriptor"
"github.com/springstar/robot/pb"
_ "github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/dynamic"
)
func TestParse(t *testing.T) {
addr := &pb.Address{
State: "china",
Province: "fujian",
City:"xiamen",
Code: 92,
User: &pb.User{
Name: "ruida",
},
}
d, _ := proto.Marshal(addr)
var parser protoparse.Parser
fds, err := parser.ParseFiles("msg/protocol/test.proto")
if err != nil {
fmt.Println(err)
return
}
for _, fd := range fds {
fmt.Print(fd.GetName())
msg := fd.FindMessage("message.Address")
if (msg == nil) {
fmt.Println("\tmsg not found")
return
}
dmsg := dynamic.NewMessage(msg)
if (dmsg == nil) {
return
}
dmsg.Unmarshal(d)
var addrMsg pb.Address
dmsg.ConvertTo(&addrMsg)
assert.Equal(t, "china", addrMsg.GetState())
assert.Equal(t, "fujian", addrMsg.GetProvince())
assert.Equal(t, "xiamen", addrMsg.GetCity())
assert.Equal(t, int32(92), addrMsg.GetCode())
assert.Equal(t, "ruida", addrMsg.GetUser().GetName())
}
}
func TestReflect(t *testing.T) {
var parser protoparse.Parser
fds, err := parser.ParseFiles("msg/protocol/test.proto")
if err != nil {
fmt.Println(err)
return
}
for _, fd := range fds {
for _, msgDesc := range fd.GetMessageTypes() {
fmt.Println("\t", msgDesc.GetName())
for _, fieldDesc := range msgDesc.GetFields() {
fmt.Println("\t", fieldDesc.GetType().String(), fieldDesc.GetName())
}
}
}
}