Skip to content

Commit a29a87b

Browse files
authored
feat(internal/librarian/golang): set default library name for Go (#5191)
The default library name for Go is now derived by stripping common API prefixes such as `google/cloud/`, `google/api/`, and `google/devtools/`. This ensures that the resulting library names are consistent with the Go client library conventions. A mapping is introduced to handle special cases like `maps` and `shopping`, where all related APIs are grouped into a single library. For other APIs, the name is derived from the first segment of the path after the known prefixes. Fixes #4996
1 parent 7cd74ea commit a29a87b

2 files changed

Lines changed: 47 additions & 12 deletions

File tree

internal/librarian/golang/module.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ import (
2828

2929
const (
3030
// rootModule is the name of the root module in the configuration.
31-
rootModule = "root-module"
31+
rootModule = "root-module"
32+
apiPrefix = "google/api/"
33+
cloudAPIPrefix = "google/cloud/"
34+
devtoolsAPIPrefix = "google/devtools/"
3235
)
3336

3437
var (
@@ -78,16 +81,23 @@ func Fill(library *config.Library) (*config.Library, error) {
7881
return library, nil
7982
}
8083

81-
// DefaultLibraryName derives a default library name from an API path.
82-
// For a versioned API path, it returns the service name. For a non-versioned
83-
// API path, it returns the last path segment.
84+
// DefaultLibraryName derives a default library name from an API path by stripping
85+
// known prefixes (e.g., "google/cloud/", "google/api/") and returning the first
86+
// segment of the remaining path.
8487
func DefaultLibraryName(api string) string {
85-
path := api
86-
if serviceconfig.ExtractVersion(api) != "" {
87-
// Strip version suffix (v1, v1beta2, v2alpha, etc.).
88-
path = filepath.Dir(api)
88+
api = strings.TrimPrefix(api, cloudAPIPrefix)
89+
// Some non-cloud APIs, e.g., google/api, google/devtools/, etc., create one library
90+
// per API. The resulting library configurations need to set additional configurations,
91+
// e.g., import_path, for the generation to work.
92+
// We don't infer the configuration here and let the user set the configurations manually.
93+
api = strings.TrimPrefix(api, apiPrefix)
94+
api = strings.TrimPrefix(api, devtoolsAPIPrefix)
95+
api = strings.TrimPrefix(api, "google/")
96+
idx := strings.Index(api, "/")
97+
if idx == -1 {
98+
return api
8999
}
90-
return filepath.Base(path)
100+
return api[:idx]
91101
}
92102

93103
// DefaultOutput returns the default output directory for a Go library.

internal/librarian/golang/module_test.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,14 +612,39 @@ func TestDefaultLibraryName(t *testing.T) {
612612
want string
613613
}{
614614
{
615-
name: "versioned api",
615+
name: "cloud API",
616616
api: "google/cloud/secretmanager/v1",
617617
want: "secretmanager",
618618
},
619619
{
620-
name: "non versioned api",
620+
name: "devtools API",
621+
api: "google/devtools/artifactregistry/v1",
622+
want: "artifactregistry",
623+
},
624+
{
625+
name: "google/api API",
626+
api: "google/api/cloudquotas/v1",
627+
want: "cloudquotas",
628+
},
629+
{
630+
name: "maps API",
631+
api: "google/maps/geocode/v4",
632+
want: "maps",
633+
},
634+
{
635+
name: "other API",
636+
api: "google/other/v4",
637+
want: "other",
638+
},
639+
{
640+
name: "shopping API",
621641
api: "google/shopping/type",
622-
want: "type",
642+
want: "shopping",
643+
},
644+
{
645+
name: "non existent API",
646+
api: "google/random",
647+
want: "random",
623648
},
624649
} {
625650
t.Run(test.name, func(t *testing.T) {

0 commit comments

Comments
 (0)