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