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 | |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 49 | // cleanPackageName converts a string to a valid Go package name. |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 50 | func cleanPackageName(name string) GoPackageName { |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 51 | return GoPackageName(cleanGoName(name, false)) |
| 52 | } |
| 53 | |
| 54 | // cleanGoName converts a string to a valid Go identifier. |
| 55 | // If mustExport, then the returned identifier is exported if not already. |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 56 | func cleanGoName(s string, mustExport bool) string { |
| 57 | // Sanitize the input to the set of valid characters, |
| 58 | // which must be '_' or be in the Unicode L or N categories. |
| 59 | s = strings.Map(func(r rune) rune { |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 60 | if unicode.IsLetter(r) || unicode.IsDigit(r) { |
| 61 | return r |
| 62 | } |
| 63 | return '_' |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 64 | }, s) |
| 65 | r, n := utf8.DecodeRuneInString(s) |
| 66 | |
| 67 | // Export the identifier by either uppercasing the first character or by |
| 68 | // prepending 'X' (to ensure name starts in the Unicode Lu category). |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 69 | if mustExport { |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 70 | // If possible, uppercase the first character. However, not all |
| 71 | // characters in the Unicode L category have an Lu equivalent. |
| 72 | if unicode.IsUpper(unicode.ToUpper(r)) { |
| 73 | return string(unicode.ToUpper(r)) + s[n:] |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 74 | } |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 75 | return "X" + s |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 76 | } |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 77 | |
| 78 | // Prepend '_' in the event of a Go keyword conflict or if |
| 79 | // the identifier is invalid (does not start in the Unicode L category). |
| 80 | if token.Lookup(s).IsKeyword() || !unicode.IsLetter(r) { |
| 81 | return "_" + s |
| 82 | } |
| 83 | return s |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Damien Neil | 8721466 | 2018-10-05 11:23:35 -0700 | [diff] [blame] | 86 | var isGoPredeclaredIdentifier = map[string]bool{ |
| 87 | "append": true, |
| 88 | "bool": true, |
| 89 | "byte": true, |
| 90 | "cap": true, |
| 91 | "close": true, |
| 92 | "complex": true, |
| 93 | "complex128": true, |
| 94 | "complex64": true, |
| 95 | "copy": true, |
| 96 | "delete": true, |
| 97 | "error": true, |
| 98 | "false": true, |
| 99 | "float32": true, |
| 100 | "float64": true, |
| 101 | "imag": true, |
| 102 | "int": true, |
| 103 | "int16": true, |
| 104 | "int32": true, |
| 105 | "int64": true, |
| 106 | "int8": true, |
| 107 | "iota": true, |
| 108 | "len": true, |
| 109 | "make": true, |
| 110 | "new": true, |
| 111 | "nil": true, |
| 112 | "panic": true, |
| 113 | "print": true, |
| 114 | "println": true, |
| 115 | "real": true, |
| 116 | "recover": true, |
| 117 | "rune": true, |
| 118 | "string": true, |
| 119 | "true": true, |
| 120 | "uint": true, |
| 121 | "uint16": true, |
| 122 | "uint32": true, |
| 123 | "uint64": true, |
| 124 | "uint8": true, |
| 125 | "uintptr": true, |
| 126 | } |
| 127 | |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 128 | // baseName returns the last path element of the name, with the last dotted suffix removed. |
| 129 | func baseName(name string) string { |
| 130 | // First, find the last element |
| 131 | if i := strings.LastIndex(name, "/"); i >= 0 { |
| 132 | name = name[i+1:] |
| 133 | } |
| 134 | // Now drop the suffix |
| 135 | if i := strings.LastIndex(name, "."); i >= 0 { |
| 136 | name = name[:i] |
| 137 | } |
| 138 | return name |
| 139 | } |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 140 | |
| 141 | // camelCase converts a name to CamelCase. |
| 142 | // |
| 143 | // If there is an interior underscore followed by a lower case letter, |
| 144 | // drop the underscore and convert the letter to upper case. |
| 145 | // There is a remote possibility of this rewrite causing a name collision, |
| 146 | // but it's so remote we're prepared to pretend it's nonexistent - since the |
| 147 | // C++ generator lowercases names, it's extremely unlikely to have two fields |
| 148 | // with different capitalizations. |
Damien Neil | d901677 | 2018-08-23 14:39:30 -0700 | [diff] [blame] | 149 | func camelCase(s string) string { |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 150 | // Invariant: if the next letter is lower case, it must be converted |
| 151 | // to upper case. |
| 152 | // That is, we process a word at a time, where words are marked by _ or |
| 153 | // upper case letter. Digits are treated as words. |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 154 | var b []byte |
| 155 | for i := 0; i < len(s); i++ { |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 156 | c := s[i] |
| 157 | switch { |
Damien Neil | 3863ee5 | 2018-10-09 13:24:04 -0700 | [diff] [blame] | 158 | case c == '.' && i+1 < len(s) && isASCIILower(s[i+1]): |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 159 | // Skip over '.' in ".{{lowercase}}". |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 160 | case c == '.': |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 161 | b = append(b, '_') // convert '.' to '_' |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 162 | case c == '_' && (i == 0 || s[i-1] == '.'): |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 163 | // Convert initial '_' to ensure we start with a capital letter. |
| 164 | // Do the same for '_' after '.' to match historic behavior. |
| 165 | b = append(b, 'X') // convert '_' to 'X' |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 166 | case c == '_' && i+1 < len(s) && isASCIILower(s[i+1]): |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 167 | // Skip over '_' in "_{{lowercase}}". |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 168 | case isASCIIDigit(c): |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 169 | b = append(b, c) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 170 | default: |
| 171 | // Assume we have a letter now - if not, it's a bogus identifier. |
| 172 | // The next word is a sequence of characters that must start upper case. |
| 173 | if isASCIILower(c) { |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 174 | c -= 'a' - 'A' // convert lowercase to uppercase |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 175 | } |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 176 | b = append(b, c) |
| 177 | |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 178 | // Accept lower case sequence that follows. |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 179 | for ; i+1 < len(s) && isASCIILower(s[i+1]); i++ { |
| 180 | b = append(b, s[i+1]) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 184 | return string(b) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Is c an ASCII lower-case letter? |
| 188 | func isASCIILower(c byte) bool { |
| 189 | return 'a' <= c && c <= 'z' |
| 190 | } |
| 191 | |
| 192 | // Is c an ASCII digit? |
| 193 | func isASCIIDigit(c byte) bool { |
| 194 | return '0' <= c && c <= '9' |
| 195 | } |