Joe Tsai | 411f339 | 2018-11-16 15:31:26 -0800 | [diff] [blame^] | 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 5 | package protogen |
| 6 | |
| 7 | import ( |
Damien Neil | d901677 | 2018-08-23 14:39:30 -0700 | [diff] [blame] | 8 | "fmt" |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 9 | "go/token" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "unicode" |
| 13 | "unicode/utf8" |
Damien Neil | abc6fc1 | 2018-08-23 14:39:30 -0700 | [diff] [blame] | 14 | |
Joe Tsai | 01ab296 | 2018-09-21 17:44:00 -0700 | [diff] [blame] | 15 | "github.com/golang/protobuf/v2/reflect/protoreflect" |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 16 | ) |
| 17 | |
Damien Neil | d901677 | 2018-08-23 14:39:30 -0700 | [diff] [blame] | 18 | // A GoIdent is a Go identifier, consisting of a name and import path. |
Joe Tsai | 411f339 | 2018-11-16 15:31:26 -0800 | [diff] [blame^] | 19 | // The name is a single identifier and may not be a dot-qualified selector. |
Damien Neil | d901677 | 2018-08-23 14:39:30 -0700 | [diff] [blame] | 20 | type GoIdent struct { |
| 21 | GoName string |
| 22 | GoImportPath GoImportPath |
| 23 | } |
| 24 | |
| 25 | func (id GoIdent) String() string { return fmt.Sprintf("%q.%v", id.GoImportPath, id.GoName) } |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 26 | |
Damien Neil | abc6fc1 | 2018-08-23 14:39:30 -0700 | [diff] [blame] | 27 | // newGoIdent returns the Go identifier for a descriptor. |
| 28 | func newGoIdent(f *File, d protoreflect.Descriptor) GoIdent { |
| 29 | name := strings.TrimPrefix(string(d.FullName()), string(f.Desc.Package())+".") |
| 30 | return GoIdent{ |
| 31 | GoName: camelCase(name), |
| 32 | GoImportPath: f.GoImportPath, |
| 33 | } |
| 34 | } |
| 35 | |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 36 | // A GoImportPath is the import path of a Go package. e.g., "google.golang.org/genproto/protobuf". |
| 37 | type GoImportPath string |
| 38 | |
| 39 | func (p GoImportPath) String() string { return strconv.Quote(string(p)) } |
| 40 | |
Joe Tsai | c1c17aa | 2018-11-16 11:14:14 -0800 | [diff] [blame] | 41 | // Ident returns a GoIdent with s as the GoName and p as the GoImportPath. |
| 42 | func (p GoImportPath) Ident(s string) GoIdent { |
| 43 | return GoIdent{GoName: s, GoImportPath: p} |
| 44 | } |
| 45 | |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 46 | // A GoPackageName is the name of a Go package. e.g., "protobuf". |
| 47 | type GoPackageName string |
| 48 | |
| 49 | // cleanPacakgeName converts a string to a valid Go package name. |
| 50 | func cleanPackageName(name string) GoPackageName { |
| 51 | name = strings.Map(badToUnderscore, name) |
| 52 | // Identifier must not be keyword: insert _. |
| 53 | if token.Lookup(name).IsKeyword() { |
| 54 | name = "_" + name |
| 55 | } |
| 56 | // Identifier must not begin with digit: insert _. |
| 57 | if r, _ := utf8.DecodeRuneInString(name); unicode.IsDigit(r) { |
| 58 | name = "_" + name |
| 59 | } |
| 60 | return GoPackageName(name) |
| 61 | } |
| 62 | |
Damien Neil | 8721466 | 2018-10-05 11:23:35 -0700 | [diff] [blame] | 63 | var isGoPredeclaredIdentifier = map[string]bool{ |
| 64 | "append": true, |
| 65 | "bool": true, |
| 66 | "byte": true, |
| 67 | "cap": true, |
| 68 | "close": true, |
| 69 | "complex": true, |
| 70 | "complex128": true, |
| 71 | "complex64": true, |
| 72 | "copy": true, |
| 73 | "delete": true, |
| 74 | "error": true, |
| 75 | "false": true, |
| 76 | "float32": true, |
| 77 | "float64": true, |
| 78 | "imag": true, |
| 79 | "int": true, |
| 80 | "int16": true, |
| 81 | "int32": true, |
| 82 | "int64": true, |
| 83 | "int8": true, |
| 84 | "iota": true, |
| 85 | "len": true, |
| 86 | "make": true, |
| 87 | "new": true, |
| 88 | "nil": true, |
| 89 | "panic": true, |
| 90 | "print": true, |
| 91 | "println": true, |
| 92 | "real": true, |
| 93 | "recover": true, |
| 94 | "rune": true, |
| 95 | "string": true, |
| 96 | "true": true, |
| 97 | "uint": true, |
| 98 | "uint16": true, |
| 99 | "uint32": true, |
| 100 | "uint64": true, |
| 101 | "uint8": true, |
| 102 | "uintptr": true, |
| 103 | } |
| 104 | |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 105 | // badToUnderscore is the mapping function used to generate Go names from package names, |
| 106 | // which can be dotted in the input .proto file. It replaces non-identifier characters such as |
| 107 | // dot or dash with underscore. |
| 108 | func badToUnderscore(r rune) rune { |
| 109 | if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' { |
| 110 | return r |
| 111 | } |
| 112 | return '_' |
| 113 | } |
| 114 | |
| 115 | // baseName returns the last path element of the name, with the last dotted suffix removed. |
| 116 | func baseName(name string) string { |
| 117 | // First, find the last element |
| 118 | if i := strings.LastIndex(name, "/"); i >= 0 { |
| 119 | name = name[i+1:] |
| 120 | } |
| 121 | // Now drop the suffix |
| 122 | if i := strings.LastIndex(name, "."); i >= 0 { |
| 123 | name = name[:i] |
| 124 | } |
| 125 | return name |
| 126 | } |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 127 | |
| 128 | // camelCase converts a name to CamelCase. |
| 129 | // |
| 130 | // If there is an interior underscore followed by a lower case letter, |
| 131 | // drop the underscore and convert the letter to upper case. |
| 132 | // There is a remote possibility of this rewrite causing a name collision, |
| 133 | // but it's so remote we're prepared to pretend it's nonexistent - since the |
| 134 | // C++ generator lowercases names, it's extremely unlikely to have two fields |
| 135 | // with different capitalizations. |
Damien Neil | d901677 | 2018-08-23 14:39:30 -0700 | [diff] [blame] | 136 | func camelCase(s string) string { |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 137 | if s == "" { |
| 138 | return "" |
| 139 | } |
| 140 | var t []byte |
| 141 | i := 0 |
| 142 | // Invariant: if the next letter is lower case, it must be converted |
| 143 | // to upper case. |
| 144 | // That is, we process a word at a time, where words are marked by _ or |
| 145 | // upper case letter. Digits are treated as words. |
| 146 | for ; i < len(s); i++ { |
| 147 | c := s[i] |
| 148 | switch { |
Damien Neil | 3863ee5 | 2018-10-09 13:24:04 -0700 | [diff] [blame] | 149 | case c == '.' && i+1 < len(s) && isASCIILower(s[i+1]): |
| 150 | // Skip over .<lowercase>, to match historic behavior. |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 151 | case c == '.': |
| 152 | t = append(t, '_') // Convert . to _. |
| 153 | case c == '_' && (i == 0 || s[i-1] == '.'): |
| 154 | // Convert initial _ to X so we start with a capital letter. |
| 155 | // Do the same for _ after .; not strictly necessary, but matches |
| 156 | // historic behavior. |
| 157 | t = append(t, 'X') |
| 158 | case c == '_' && i+1 < len(s) && isASCIILower(s[i+1]): |
| 159 | // Skip the underscore in s. |
| 160 | case isASCIIDigit(c): |
| 161 | t = append(t, c) |
| 162 | default: |
| 163 | // Assume we have a letter now - if not, it's a bogus identifier. |
| 164 | // The next word is a sequence of characters that must start upper case. |
| 165 | if isASCIILower(c) { |
| 166 | c ^= ' ' // Make it a capital letter. |
| 167 | } |
| 168 | t = append(t, c) // Guaranteed not lower case. |
| 169 | // Accept lower case sequence that follows. |
| 170 | for i+1 < len(s) && isASCIILower(s[i+1]) { |
| 171 | i++ |
| 172 | t = append(t, s[i]) |
| 173 | } |
| 174 | } |
| 175 | } |
Damien Neil | d901677 | 2018-08-23 14:39:30 -0700 | [diff] [blame] | 176 | return string(t) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Is c an ASCII lower-case letter? |
| 180 | func isASCIILower(c byte) bool { |
| 181 | return 'a' <= c && c <= 'z' |
| 182 | } |
| 183 | |
| 184 | // Is c an ASCII digit? |
| 185 | func isASCIIDigit(c byte) bool { |
| 186 | return '0' <= c && c <= '9' |
| 187 | } |