-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
93 lines (86 loc) · 3.11 KB
/
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
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
package protokit
import (
"fmt"
"strings"
"github.com/sandwich-go/boost/xslice"
)
// AddWithDotFullQualifiedName 返回pd在本次import中应该使用的struct名称
func (e *ImportSet) AddWithDotFullQualifiedName(dotFullyQualifiedName string, pf *ProtoFile) (string, *Import) {
item := &Import{
MessageDotFullQualifiedName: []string{dotFullyQualifiedName},
ProtoFilePath: pf.FilePath,
GolangPackageName: pf.GolangPackageName,
GolangPackagePath: pf.GolangPackagePath,
CSNamespace: pf.OptionCSNamespace,
CSNamespaceName: pf.GolangPackageName, // cs 使用golang的packagename作为别名
PythonModuleName: pf.GolangPackageName, // python使用golang的package name作为别名
PythonModulePath: pythonModulePath(pf.FilePath),
}
e.Add(item)
// 根据item的名字调节使用的struct名称
structName := GoStructNameWithGolangPackage(dotFullyQualifiedName, pf.Package, item.GolangPackageName)
e.MessageDotFullQualifiedNameToGolangType[dotFullyQualifiedName] = structName
return structName, item
}
func (e *ImportSet) onGolangPackageNameChange(add *Import) {
add.CSNamespaceName = add.GolangPackageName
add.PythonModuleName = add.GolangPackageName
}
func (e *ImportSet) Add(add *Import) {
if xslice.StringsContain(e.ExcludeImportName, add.GolangPackageName) {
return
}
duplicated := false
// 本包内
if add.GolangPackagePath == e.GolangPackagePath {
// 上层需要根据Name重新定位package
add.GolangPackageName = "."
return
}
if add.GolangPackageName == e.GolangPackageName {
// path不同但是package name相同,起别名
n := e.importAliasMappingCount[add.GolangPackageName]
n++
add.GolangPackageName = fmt.Sprintf("%s%d", add.GolangPackageName, n)
e.onGolangPackageNameChange(add)
e.importAliasMappingCount[add.GolangPackageName] = n
}
originalName := add.GolangPackageName
for _, i := range e.Set {
if i.GolangPackagePath == add.GolangPackagePath {
duplicated = true
add.GolangPackageName = i.GolangPackageName
e.onGolangPackageNameChange(add)
i.MessageDotFullQualifiedName = xslice.StringsSetAdd(i.MessageDotFullQualifiedName, add.MessageDotFullQualifiedName...)
break
}
// path不同但是package name相同,起别名
if i.GolangPackageName == add.GolangPackageName {
add.GolangPackageName = fmt.Sprintf("%s%d", add.GolangPackageName, e.importAliasMappingCount[originalName])
e.onGolangPackageNameChange(add)
}
}
if !duplicated {
e.Set = append(e.Set, add)
e.importAliasMappingCount[originalName] += 1
}
}
func fileNameWithoutExtension(fileName string) string {
if pos := strings.LastIndexByte(fileName, '.'); pos != -1 {
return fileName[:pos]
}
return fileName
}
func pythonPB2FilePath(fileName string) string {
fileName = fileNameWithoutExtension(fileName)
fp := strings.Replace(fmt.Sprintf(".%s_pb2", fileName), "/", ".", -1)
fp = strings.TrimPrefix(fp, ".")
return fp
}
func pythonModulePath(fileName string) string {
fileName = pythonPB2FilePath(fileName)
if pos := strings.LastIndexByte(fileName, '.'); pos != -1 {
return fileName[:pos]
}
return fileName
}