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 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 15 | "google.golang.org/protobuf/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 | 4069211 | 2019-02-27 20:25:51 -0800 | [diff] [blame] | 51 | return GoPackageName(cleanGoName(name)) |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // cleanGoName converts a string to a valid Go identifier. |
Joe Tsai | 4069211 | 2019-02-27 20:25:51 -0800 | [diff] [blame] | 55 | func cleanGoName(s string) string { |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 56 | // Sanitize the input to the set of valid characters, |
| 57 | // which must be '_' or be in the Unicode L or N categories. |
| 58 | s = strings.Map(func(r rune) rune { |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 59 | if unicode.IsLetter(r) || unicode.IsDigit(r) { |
| 60 | return r |
| 61 | } |
| 62 | return '_' |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 63 | }, s) |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 64 | |
| 65 | // Prepend '_' in the event of a Go keyword conflict or if |
| 66 | // the identifier is invalid (does not start in the Unicode L category). |
Joe Tsai | 4069211 | 2019-02-27 20:25:51 -0800 | [diff] [blame] | 67 | r, _ := utf8.DecodeRuneInString(s) |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 68 | if token.Lookup(s).IsKeyword() || !unicode.IsLetter(r) { |
| 69 | return "_" + s |
| 70 | } |
| 71 | return s |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 74 | // baseName returns the last path element of the name, with the last dotted suffix removed. |
| 75 | func baseName(name string) string { |
| 76 | // First, find the last element |
| 77 | if i := strings.LastIndex(name, "/"); i >= 0 { |
| 78 | name = name[i+1:] |
| 79 | } |
| 80 | // Now drop the suffix |
| 81 | if i := strings.LastIndex(name, "."); i >= 0 { |
| 82 | name = name[:i] |
| 83 | } |
| 84 | return name |
| 85 | } |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 86 | |
| 87 | // camelCase converts a name to CamelCase. |
| 88 | // |
| 89 | // If there is an interior underscore followed by a lower case letter, |
| 90 | // drop the underscore and convert the letter to upper case. |
| 91 | // There is a remote possibility of this rewrite causing a name collision, |
| 92 | // but it's so remote we're prepared to pretend it's nonexistent - since the |
| 93 | // C++ generator lowercases names, it's extremely unlikely to have two fields |
| 94 | // with different capitalizations. |
Damien Neil | d901677 | 2018-08-23 14:39:30 -0700 | [diff] [blame] | 95 | func camelCase(s string) string { |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 96 | // Invariant: if the next letter is lower case, it must be converted |
| 97 | // to upper case. |
| 98 | // That is, we process a word at a time, where words are marked by _ or |
| 99 | // upper case letter. Digits are treated as words. |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 100 | var b []byte |
| 101 | for i := 0; i < len(s); i++ { |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 102 | c := s[i] |
| 103 | switch { |
Damien Neil | 3863ee5 | 2018-10-09 13:24:04 -0700 | [diff] [blame] | 104 | case c == '.' && i+1 < len(s) && isASCIILower(s[i+1]): |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 105 | // Skip over '.' in ".{{lowercase}}". |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 106 | case c == '.': |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 107 | b = append(b, '_') // convert '.' to '_' |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 108 | case c == '_' && (i == 0 || s[i-1] == '.'): |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 109 | // Convert initial '_' to ensure we start with a capital letter. |
| 110 | // Do the same for '_' after '.' to match historic behavior. |
| 111 | b = append(b, 'X') // convert '_' to 'X' |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 112 | case c == '_' && i+1 < len(s) && isASCIILower(s[i+1]): |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 113 | // Skip over '_' in "_{{lowercase}}". |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 114 | case isASCIIDigit(c): |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 115 | b = append(b, c) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 116 | default: |
| 117 | // Assume we have a letter now - if not, it's a bogus identifier. |
| 118 | // The next word is a sequence of characters that must start upper case. |
| 119 | if isASCIILower(c) { |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 120 | c -= 'a' - 'A' // convert lowercase to uppercase |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 121 | } |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 122 | b = append(b, c) |
| 123 | |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 124 | // Accept lower case sequence that follows. |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 125 | for ; i+1 < len(s) && isASCIILower(s[i+1]); i++ { |
| 126 | b = append(b, s[i+1]) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame] | 130 | return string(b) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Is c an ASCII lower-case letter? |
| 134 | func isASCIILower(c byte) bool { |
| 135 | return 'a' <= c && c <= 'z' |
| 136 | } |
| 137 | |
| 138 | // Is c an ASCII digit? |
| 139 | func isASCIIDigit(c byte) bool { |
| 140 | return '0' <= c && c <= '9' |
| 141 | } |