/
rpc.go
106 lines (97 loc) · 2.32 KB
/
rpc.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package udwRpc2Builder
import (
"github.com/tachyon-protocol/udw/udwGoSource/udwGoParser"
"path"
"strconv"
)
type RpcService struct {
List []RpcApi
}
type RpcApi struct {
Pos int
Name string
InputParameterList []RpcParameter
OutputParameterList []RpcParameter
}
type RpcParameter struct {
Pos int
Name string
Type RpcType
IsClientIp bool
IsHijackCtx bool
}
type RpcType struct {
Kind string
Elem *RpcType
FieldList []RpcField
StructName string
GoPkg string
}
type RpcField struct {
Pos int
Name string
Type RpcType
}
const (
RpcTypeKindNamedStruct = "namedStruct"
RpcTypeKindString = "string"
RpcTypeKindInt = "int"
RpcTypeKindBool = "bool"
RpcTypeKindSlice = "slice"
)
func initRpcDefine(RpcDefine *RpcService) {
for i := range RpcDefine.List {
api := &RpcDefine.List[i]
api.Pos = i + 1
for i := range api.InputParameterList {
parameter := &api.InputParameterList[i]
parameter.Pos = i + 1
if parameter.Name == "" {
parameter.Name = "fi" + strconv.Itoa(i+1)
}
thisTyp := ¶meter.Type
initRpcType(thisTyp)
if thisTyp.Kind == RpcTypeKindNamedStruct && thisTyp.StructName == "PeerIp" && thisTyp.GoPkg == "github.com/tachyon-protocol/udw/udwRpc2" {
parameter.IsClientIp = true
}
}
for i := range api.OutputParameterList {
parameter := &api.OutputParameterList[i]
parameter.Pos = i + 1
if parameter.Name == "" {
parameter.Name = "fo" + strconv.Itoa(i+1)
}
initRpcType(¶meter.Type)
}
}
}
func initRpcType(Type *RpcType) {
switch Type.Kind {
case RpcTypeKindSlice:
initRpcType(Type.Elem)
case RpcTypeKindNamedStruct:
for i := range Type.FieldList {
field := &Type.FieldList[i]
field.Pos = i + 1
initRpcType(&field.Type)
}
}
}
func mustRpcTypeToGoTypeName(writer *udwGoParser.GoFileWriter, Type *RpcType) string {
switch Type.Kind {
case RpcTypeKindString:
return "string"
case RpcTypeKindInt:
return "int"
case RpcTypeKindNamedStruct:
if Type.GoPkg == "" || Type.GoPkg == writer.GetPkgImportPath() {
return Type.StructName
}
writer.AddImportPath(Type.GoPkg)
return path.Base(Type.GoPkg) + "." + Type.StructName
case RpcTypeKindSlice:
return "[]" + mustRpcTypeToGoTypeName(writer, Type.Elem)
default:
panic("not support Type [" + Type.Kind + "]")
}
}