@@ -77,6 +77,9 @@ type goConfig struct {
77
77
// goGenerateProto indicates whether to generate go_proto_library
78
78
goGenerateProto bool
79
79
80
+ // goNamingConvention controls the name of generated targets
81
+ goNamingConvention namingConvention
82
+
80
83
// goProtoCompilers is the protocol buffers compiler(s) to use for go code.
81
84
goProtoCompilers []string
82
85
@@ -171,6 +174,15 @@ func (gc *goConfig) setBuildTags(tags string) error {
171
174
return nil
172
175
}
173
176
177
+ func (gc * goConfig ) setNamingConvention (s string ) error {
178
+ if nc , ok := namingConventionFromString (s ); ok {
179
+ gc .goNamingConvention = nc
180
+ return nil
181
+ } else {
182
+ return fmt .Errorf ("unknown go_naming_convention %q" , s )
183
+ }
184
+ }
185
+
174
186
func getProtoMode (c * config.Config ) proto.Mode {
175
187
if gc := getGoConfig (c ); ! gc .goGenerateProto {
176
188
return proto .DisableMode
@@ -236,6 +248,59 @@ func (f tagsFlag) String() string {
236
248
return ""
237
249
}
238
250
251
+ type goNamingConventionFlag func (string ) error
252
+
253
+ func (f goNamingConventionFlag ) Set (value string ) error {
254
+ return f (value )
255
+ }
256
+
257
+ func (f goNamingConventionFlag ) String () string {
258
+ return ""
259
+ }
260
+
261
+ // namingConvention determines how go targets are named.
262
+ type namingConvention int
263
+
264
+ const (
265
+ // 'go_default_library', 'go_default_test', and TODO(tomlu)
266
+ goDefaultLibraryNC = iota
267
+
268
+ // For an import path that ends with foo, the go_library rules target is
269
+ // named 'foo', the go_test is named 'foo_test'.
270
+ // For a main package, the go_binary takes the 'foo' name, the library
271
+ // is named 'foo_lib', and the go_test is named 'foo_test'.
272
+ importNC
273
+
274
+ // Same as importNC, but generate alias rules for libraries that have
275
+ // the legacy 'go_default_library' name.
276
+ importAliasNC
277
+ )
278
+
279
+ func (nc namingConvention ) String () string {
280
+ switch nc {
281
+ case goDefaultLibraryNC :
282
+ return "go_default_library"
283
+ case importNC :
284
+ return "import"
285
+ case importAliasNC :
286
+ return "import_alias"
287
+ }
288
+ return ""
289
+ }
290
+
291
+ func namingConventionFromString (s string ) (namingConvention , bool ) {
292
+ switch s {
293
+ case "go_default_library" :
294
+ return goDefaultLibraryNC , true
295
+ case "import" :
296
+ return importNC , true
297
+ case "import_alias" :
298
+ return importAliasNC , true
299
+ default :
300
+ return goDefaultLibraryNC , false
301
+ }
302
+ }
303
+
239
304
type moduleRepo struct {
240
305
repoName , modulePath string
241
306
}
@@ -249,6 +314,7 @@ func (*goLang) KnownDirectives() []string {
249
314
"build_tags" ,
250
315
"go_generate_proto" ,
251
316
"go_grpc_compilers" ,
317
+ "go_naming_convention" ,
252
318
"go_proto_compilers" ,
253
319
"go_visibility" ,
254
320
"importmap_prefix" ,
@@ -290,6 +356,10 @@ func (*goLang) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) {
290
356
"go_repository_module_mode" ,
291
357
false ,
292
358
"set when gazelle is invoked by go_repository in module mode" )
359
+ fs .Var (
360
+ goNamingConventionFlag (gc .setNamingConvention ),
361
+ "go_naming_convention" ,
362
+ "controls generated library names. One of (go_default_library, import, import_alias)" )
293
363
294
364
case "update-repos" :
295
365
fs .Var (& gzflag.AllowedStringFlag {Value : & gc .buildExternalAttr , Allowed : validBuildExternalAttr },
@@ -408,12 +478,15 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.`
408
478
gc .preprocessTags ()
409
479
gc .setBuildTags (d .Value )
410
480
case "go_generate_proto" :
411
-
412
481
if goGenerateProto , err := strconv .ParseBool (d .Value ); err == nil {
413
482
gc .goGenerateProto = goGenerateProto
414
483
} else {
415
484
log .Printf ("parsing go_generate_proto: %v" , err )
416
485
}
486
+ case "go_naming_convention" :
487
+ if err := gc .setNamingConvention (d .Value ); err != nil {
488
+ log .Print (err )
489
+ }
417
490
case "go_grpc_compilers" :
418
491
// Special syntax (empty value) to reset directive.
419
492
if d .Value == "" {
0 commit comments