-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto_parser_service_import.go
60 lines (56 loc) · 2.23 KB
/
proto_parser_service_import.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
package protokit
import (
"fmt"
"path"
"sort"
"strings"
"github.com/sandwich-go/boost/xslice"
)
func (p *Parser) addImportByDotFullyQualifiedTypeName(dotFullyQualifiedTypeName string, set *ImportSet) (string, *Import) {
if dotFullyQualifiedTypeName == ".google.protobuf.Empty" {
return "protobufEmpty.Empty", nil
}
protoFile, ok := p.dotFullyQualifiedTypeNameToProtoFile[dotFullyQualifiedTypeName]
if !ok {
return dotFullyQualifiedTypeName, nil
}
structName, item := set.AddWithDotFullQualifiedName(dotFullyQualifiedTypeName, protoFile)
return structName, item
}
func (p *Parser) parseImport() {
// fixme 校验req rsp映射关系,TCP需要严格校验,HTTP缺可以不严格校验
var ss = make([]string, 0, len(p.protoFilePathToProtoFile))
for protoFilePath := range p.protoFilePathToProtoFile {
ss = append(ss, protoFilePath)
}
sort.Strings(ss)
for _, protoFilePath := range ss {
protoFile := p.protoFilePathToProtoFile[protoFilePath]
for _, sg := range protoFile.ServiceGroups {
// 设定import忽略路径
sg.ImportSet.ExcludeImportName = p.cc.ImportSetExclude
for _, service := range sg.Services {
for _, method := range service.Methods {
// 名称需要做一次修复,根据import的package名称
method.TypeInput, _ = p.addImportByDotFullyQualifiedTypeName(method.TypeInputDotFullQualifiedName, sg.ImportSet)
method.TypeOutput, _ = p.addImportByDotFullyQualifiedTypeName(method.TypeOutputDotFullQualifiedName, sg.ImportSet)
service.InputOutputTypes = xslice.StringsSetAdd(service.InputOutputTypes, method.TypeInput, method.TypeOutput)
// 请求使用使用的uri名称, 需要用这个名字来作为http请求的路径,携带自由的package名称
uriUsing := method.TypeInputWithSelfPackage
if method.TypeInputAlias != "" {
uriUsing = method.TypeInputAlias
}
// http请求path逻辑校验,需要依赖纠正过后的TypeInput
if method.HTTPPath == "" {
method.HTTPPathComment = "auto generate by ProtoKitGo"
pathUsing := strings.TrimLeft(uriUsing, ".")
if !strings.HasPrefix(pathUsing, "/") {
pathUsing = "/" + pathUsing
}
method.HTTPPath = path.Clean(fmt.Sprintf(p.cc.NamePatternHTTPPath, pathUsing))
}
}
}
}
}
}