-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto_parser_package.go
138 lines (129 loc) · 4.72 KB
/
proto_parser_package.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package protokit
import (
"path"
"sort"
"github.com/jhump/protoreflect/desc"
"github.com/sandwich-go/boost/xpanic"
"github.com/sandwich-go/boost/xslice"
)
type IsMapEntry interface {
IsMapEntry() bool
}
func namespace(nsList []*Namespace, name string) *Namespace {
for _, v := range nsList {
if name == v.Name {
return v
}
}
return nil
}
func (p *Parser) parsePackage(nsList []*Namespace) {
var upPackage = func(protoPackage *Package, dotFullyQualifiedTypeName string, protoFile *ProtoFile) {
if protoPackage == nil {
return
}
structName, item := p.addImportByDotFullyQualifiedTypeName(dotFullyQualifiedTypeName, protoPackage.ImportSet)
if item != nil {
p.pythonModule(dotFullyQualifiedTypeName, structName, item, protoPackage.ImportSet, protoPackage.IsGlobal, protoFile)
}
}
// 处理所有注册进来的消息
var ss = make([]string, 0, len(p.dotFullyQualifiedTypeNameToProtoFile))
for dotFullyQualifiedTypeName := range p.dotFullyQualifiedTypeNameToProtoFile {
ss = append(ss, dotFullyQualifiedTypeName)
}
sort.Strings(ss)
for _, dotFullyQualifiedTypeName := range ss {
protoFile := p.dotFullyQualifiedTypeNameToProtoFile[dotFullyQualifiedTypeName]
tt := p.dotFullyQualifiedTypeNameToDescriptor[dotFullyQualifiedTypeName]
if mapEntry, ok := tt.(IsMapEntry); ok && mapEntry.IsMapEntry() {
// map entry不做处理
continue
}
if _, ok := tt.(*desc.EnumDescriptor); ok {
// enum不做处理
continue
}
// 根据protoFile获取namespace
ns := namespace(nsList, protoFile.Namespace)
xpanic.WhenTrue(ns == nil, "can not got namspace with name: %s", protoFile.Namespace)
golangPackagePath := protoFile.GolangPackagePath
pi, ok := ns.Packages[golangPackagePath]
if !ok {
pi = NewPackageWithPackageName(protoFile.GolangPackageName, protoFile.GolangPackagePath)
pi.FilePath, _ = path.Split(protoFile.FilePath)
pi.Package = protoFile.Package
pi.GolangRelative = p.cc.GolangRelative
// 设定import忽略路径
pi.ImportSet.ExcludeImportName = p.cc.ImportSetExclude
ns.Packages[golangPackagePath] = pi
}
// 本保内的消息
upPackage(pi, dotFullyQualifiedTypeName, protoFile)
// 注册全局消息
globalPackage := ns.Packages[NamespaceMessageRegistryPackageName]
if globalPackage != nil {
// 重新赋值一次,默认的message registry package在创建的时候没有这个参数
globalPackage.GolangRelative = p.cc.GolangRelative
// 设定import忽略路径
globalPackage.ImportSet.ExcludeImportName = p.cc.ImportSetExclude
upPackage(globalPackage, dotFullyQualifiedTypeName, protoFile)
}
}
for _, protoFile := range p.protoFilePathToProtoFile {
sg := protoFile.ServiceGroups[ServiceTagALL]
if sg == nil || len(sg.Services) == 0 {
continue
}
ns := namespace(nsList, protoFile.Namespace)
xpanic.WhenTrue(ns == nil, "can not got namspace with name: %s", protoFile.Namespace)
pp := ns.Packages[NamespaceMessageRegistryPackageName]
if pp == nil {
continue
}
messageDotFullQualifiedNameToGolangType := p.getMessageDotFullQualifiedNameToGolangType(nsList, ns)
for _, service := range sg.Services {
for _, method := range service.Methods {
// 找出类型在当前ImportSet下的类型名
golangInputType := messageDotFullQualifiedNameToGolangType[method.TypeInputDotFullQualifiedName]
golangOutputType := messageDotFullQualifiedNameToGolangType[method.TypeOutputDotFullQualifiedName]
if golangInputType != "" && method.TypeInputAlias != "" {
pp.AliasToGolangType[method.TypeInputAlias] = golangInputType
}
if method.IsActor {
golangInputType = pp.ImportSet.MessageDotFullQualifiedNameToGolangType[method.TypeInputDotFullQualifiedName]
golangOutputType = pp.ImportSet.MessageDotFullQualifiedNameToGolangType[method.TypeOutputDotFullQualifiedName]
if golangInputType != "" {
pp.ActorMessageGolangType = xslice.StringsSetAdd(pp.ActorMessageGolangType, golangInputType)
}
if golangOutputType != "" {
pp.ActorMessageGolangType = xslice.StringsSetAdd(pp.ActorMessageGolangType, golangOutputType)
}
}
}
}
}
}
func (p *Parser) getMessageDotFullQualifiedNameToGolangType(nsList []*Namespace, ns *Namespace) map[string]string {
if ns == nil {
return nil
}
pp := ns.Packages[NamespaceMessageRegistryPackageName]
if pp == nil {
return nil
}
switch ns.Name {
case NamespaceGoogle, NamespaceNetutils:
return pp.ImportSet.MessageDotFullQualifiedNameToGolangType
}
out := make(map[string]string)
for k, v := range pp.ImportSet.MessageDotFullQualifiedNameToGolangType {
out[k] = v
}
for _, name := range []string{NamespaceGoogle, NamespaceNetutils} {
for k, v := range p.getMessageDotFullQualifiedNameToGolangType(nsList, namespace(nsList, name)) {
out[k] = v
}
}
return out
}