/
pkg.go
86 lines (79 loc) · 2.31 KB
/
pkg.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
// Copyright 2010 The Go Authors. All rights reserved.
//
// Helper funcs copied from protoc-gen-go codebase at https://github.com/golang/protobuf
package parser
import (
"strings"
"unicode"
"unicode/utf8"
"google.golang.org/protobuf/types/descriptorpb"
)
// goPackageOption interprets the file's go_package option.
// If there is no go_package, it returns ("", "", false).
// If there's a simple name, it returns ("", pkg, true).
// If the option implies an import path, it returns (impPath, pkg, true).
func goPackageOption(d *descriptorpb.FileDescriptorProto) (impPath, pkg string, 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 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 opt, cleanPackageName(opt[slash+1:]), true
}
return "", cleanPackageName(opt), true
}
var isGoKeyword = map[string]bool{
"break": true,
"case": true,
"chan": true,
"const": true,
"continue": true,
"default": true,
"else": true,
"defer": true,
"fallthrough": true,
"for": true,
"func": true,
"go": true,
"goto": true,
"if": true,
"import": true,
"interface": true,
"map": true,
"package": true,
"range": true,
"return": true,
"select": true,
"struct": true,
"switch": true,
"type": true,
"var": true,
}
func cleanPackageName(name string) string {
name = strings.Map(badToUnderscore, name)
// Identifier must not be keyword or predeclared identifier: insert _.
if isGoKeyword[name] {
name = "_" + name
}
// Identifier must not begin with digit: insert _.
if r, _ := utf8.DecodeRuneInString(name); unicode.IsDigit(r) {
name = "_" + name
}
return name
}
// badToUnderscore is the mapping function used to generate Go names from package names,
// which can be dotted in the input .proto file. It replaces non-identifier characters such as
// dot or dash with underscore.
func badToUnderscore(r rune) rune {
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' {
return r
}
return '_'
}