-
Notifications
You must be signed in to change notification settings - Fork 0
/
filewrapper.go
47 lines (40 loc) · 1.16 KB
/
filewrapper.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
package main
import (
"path"
"strings"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
)
type fileWrapper struct {
*descriptor.FileDescriptorProto
}
func (d fileWrapper) goPackageOption() (impPath GoImportPath, pkg GoPackageName, ok bool) {
opt := d.GetOptions().GetGoPackage()
if opt == "" {
return "", "", false
}
// A semicolon-delimited suffix delimits the import path and package name.
sc := strings.Index(opt, ";")
if sc >= 0 {
return GoImportPath(opt[:sc]), cleanPackageName(opt[sc+1:]), true
}
// The presence of a slash implies there's an import path.
slash := strings.LastIndex(opt, "/")
if slash >= 0 {
return GoImportPath(opt), cleanPackageName(opt[slash+1:]), true
}
return "", cleanPackageName(opt), true
}
func (f fileWrapper) destinationFileName() string {
name := *f.Name
if ext := path.Ext(name); ext == ".proto" || ext == ".protodevel" {
name = name[:len(name)-len(ext)]
}
name += ".gen.go"
if impPath, _, ok := f.goPackageOption(); ok && impPath != "" {
// Replace the existing dirname with the declared import path.
_, name = path.Split(name)
name = path.Join(string(impPath), name)
return name
}
return name
}