forked from lyft/protoc-gen-star
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.go
61 lines (48 loc) · 1.21 KB
/
path.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
package pgs
import (
"path"
"strings"
"github.com/golang/protobuf/protoc-gen-go/generator"
)
func goPackageOption(f *generator.FileDescriptor) (impPath, pkg string, ok bool) {
pkg = f.GetOptions().GetGoPackage()
if pkg == "" {
return
}
ok = true
slash := strings.LastIndex(pkg, "/")
if slash < 0 {
return
}
impPath, pkg = pkg, pkg[slash+1:]
sc := strings.IndexByte(impPath, ';')
if sc < 0 {
return
}
impPath, pkg = impPath[:sc], impPath[sc+1:]
return
}
func goFileName(f *generator.FileDescriptor, pathType PathType) string {
name := f.GetName()
if ext := path.Ext(name); ext == ".proto" || ext == ".protodevel" {
name = name[:len(name)-len(ext)]
}
name += ".pb.go"
if pathType == SourceRelative {
return name
}
if impPath, _, ok := goPackageOption(f); ok && impPath != "" {
_, name = path.Split(name)
name = path.Join(impPath, name)
}
return name
}
func goImportPath(g *generator.Generator, f *generator.FileDescriptor) generator.GoImportPath {
fn := goFileName(f, Parameters(g.Param).Paths())
importPath := path.Dir(fn)
if sub, ok := g.ImportMap[f.GetName()]; ok {
importPath = sub
}
importPath = path.Join(g.ImportPrefix, importPath)
return generator.GoImportPath(importPath)
}