|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "bytes" |
5 | 6 | "fmt" |
6 | 7 | "go/ast" |
@@ -114,28 +115,168 @@ func refactorProtoSources(files []*protogen.File) (descs []*FileDescriptor, err |
114 | 115 | return |
115 | 116 | } |
116 | 117 |
|
| 118 | +const ( |
| 119 | + stripNone = iota |
| 120 | + stripNested |
| 121 | + stripAll |
| 122 | +) |
| 123 | + |
117 | 124 | func refactorEnumConstants(enums []*protogen.Enum, prefixes ...string) error { |
118 | 125 | if !camelcaseEnumConstants { |
119 | 126 | return nil |
120 | 127 | } |
121 | 128 |
|
122 | | - var prefix string |
| 129 | + var ( |
| 130 | + prefix string |
| 131 | + enumType string |
| 132 | + ) |
| 133 | + |
123 | 134 | if len(prefixes) > 0 { |
124 | 135 | prefix = prefixes[0] |
125 | 136 | } |
126 | 137 |
|
127 | 138 | for _, enum := range enums { |
128 | | - enum.GoIdent.GoName = strcase.UpperCamelCase(enum.GoIdent.GoName) |
| 139 | + enumType = strings.TrimPrefix(enum.GoIdent.GoName, prefix) |
| 140 | + strip := renameEnumType(enum) |
129 | 141 | for _, value := range enum.Values { |
130 | | - value.GoIdent.GoName = strings.TrimPrefix(value.GoIdent.GoName, prefix) |
131 | | - value.GoIdent.GoName = strings.TrimPrefix(value.GoIdent.GoName, enum.GoIdent.GoName) |
132 | | - value.GoIdent.GoName = strcase.UpperCamelCase(enum.GoIdent.GoName + value.GoIdent.GoName) |
| 142 | + renameEnumValue(value, prefix, enumType, strip) |
133 | 143 | } |
134 | 144 | } |
135 | 145 |
|
136 | 146 | return nil |
137 | 147 | } |
138 | 148 |
|
| 149 | +func renameEnumType(enum *protogen.Enum) (strip int) { |
| 150 | + const ( |
| 151 | + stripComment = "@go.enum=strip" |
| 152 | + goNameComment = "@go.name=" |
| 153 | + ) |
| 154 | + |
| 155 | + var name string |
| 156 | + |
| 157 | + leading := enum.Comments.Leading.String() |
| 158 | + var buf bytes.Buffer |
| 159 | + scanner := bufio.NewScanner(strings.NewReader(leading)) |
| 160 | + for scanner.Scan() { |
| 161 | + line := scanner.Text() |
| 162 | + if line == "" { |
| 163 | + continue |
| 164 | + } |
| 165 | + |
| 166 | + if index := strings.Index(line, stripComment); index >= 0 { |
| 167 | + line = line[index+len(stripComment):] |
| 168 | + if strings.HasPrefix(line, "=nested") { |
| 169 | + strip = stripNested |
| 170 | + } else { |
| 171 | + strip = stripAll |
| 172 | + } |
| 173 | + |
| 174 | + continue |
| 175 | + } |
| 176 | + |
| 177 | + if index := strings.Index(line, goNameComment); index >= 0 { |
| 178 | + name = strings.TrimSpace(line[index+len(goNameComment):]) |
| 179 | + continue |
| 180 | + } |
| 181 | + |
| 182 | + buf.WriteString(line) |
| 183 | + buf.WriteByte('\n') |
| 184 | + } |
| 185 | + |
| 186 | + enum.Comments.Leading = protogen.Comments(buf.String()) |
| 187 | + |
| 188 | + trailing := enum.Comments.Trailing.String() |
| 189 | + if index := strings.Index(trailing, stripComment); index >= 0 { |
| 190 | + trailing = trailing[index+len(stripComment):] |
| 191 | + if strings.HasPrefix(trailing, "=nested") { |
| 192 | + strip = stripNested |
| 193 | + } else { |
| 194 | + strip = stripAll |
| 195 | + } |
| 196 | + enum.Comments.Trailing = "" |
| 197 | + } else if index := strings.Index(trailing, goNameComment); index >= 0 { |
| 198 | + enum.Comments.Trailing = "" |
| 199 | + name = strings.TrimSpace(trailing[index+len(goNameComment):]) |
| 200 | + } |
| 201 | + |
| 202 | + if name != "" { |
| 203 | + enum.GoIdent.GoName = name |
| 204 | + } else { |
| 205 | + enum.GoIdent.GoName = strcase.UpperCamelCase(enum.GoIdent.GoName) |
| 206 | + } |
| 207 | + |
| 208 | + return |
| 209 | +} |
| 210 | + |
| 211 | +func renameEnumValue(value *protogen.EnumValue, prefix, enum string, strip int) { |
| 212 | + var ( |
| 213 | + name string |
| 214 | + buf bytes.Buffer |
| 215 | + ) |
| 216 | + |
| 217 | + const goNameComment = "@go.name=" |
| 218 | + |
| 219 | + scanner := bufio.NewScanner(strings.NewReader(value.Comments.Leading.String())) |
| 220 | + for scanner.Scan() { |
| 221 | + line := scanner.Text() |
| 222 | + if line == "" { |
| 223 | + continue |
| 224 | + } |
| 225 | + |
| 226 | + if index := strings.Index(line, goNameComment); index >= 0 { |
| 227 | + name = strings.TrimSpace(line[index+len(goNameComment):]) |
| 228 | + continue |
| 229 | + } |
| 230 | + |
| 231 | + buf.WriteString(line) |
| 232 | + buf.WriteByte('\n') |
| 233 | + } |
| 234 | + |
| 235 | + value.Comments.Leading = protogen.Comments(buf.String()) |
| 236 | + if name == "" { |
| 237 | + trailing := value.Comments.Trailing.String() |
| 238 | + if index := strings.Index(trailing, goNameComment); index >= 0 { |
| 239 | + name = strings.TrimSpace(trailing[index+len(goNameComment):]) |
| 240 | + value.Comments.Trailing = "" |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + switch strip { |
| 245 | + case stripAll: |
| 246 | + if name != "" { |
| 247 | + value.GoIdent.GoName = name |
| 248 | + return |
| 249 | + } |
| 250 | + |
| 251 | + if prefix != "" { |
| 252 | + value.GoIdent.GoName = strings.TrimPrefix(value.GoIdent.GoName, prefix+"_") |
| 253 | + } else { |
| 254 | + value.GoIdent.GoName = strings.TrimPrefix(value.GoIdent.GoName, enum+"_") |
| 255 | + |
| 256 | + } |
| 257 | + case stripNested: |
| 258 | + if name != "" { |
| 259 | + value.GoIdent.GoName = strcase.UpperCamelCase(enum) + name |
| 260 | + return |
| 261 | + } else { |
| 262 | + value.GoIdent.GoName = strings.TrimPrefix(value.GoIdent.GoName, prefix+"_") |
| 263 | + value.GoIdent.GoName = enum + "_" + value.GoIdent.GoName |
| 264 | + } |
| 265 | + default: |
| 266 | + if name != "" { |
| 267 | + if prefix != "" { |
| 268 | + value.GoIdent.GoName = strcase.UpperCamelCase(prefix) + name |
| 269 | + } else { |
| 270 | + value.GoIdent.GoName = strcase.UpperCamelCase(enum) + name |
| 271 | + } |
| 272 | + |
| 273 | + return |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + value.GoIdent.GoName = strcase.UpperCamelCase(value.GoIdent.GoName) |
| 278 | +} |
| 279 | + |
139 | 280 | func regenerateGoSources(descs []*FileDescriptor, sources []*pluginpb.CodeGeneratorResponse_File) (err error) { |
140 | 281 | if len(descs) == 0 || len(sources) == 0 { |
141 | 282 | return |
|
0 commit comments