Skip to content

Commit 9a3cef9

Browse files
authored
chore(internal/serviceconfig): add default api short name (#4064)
Add default API short name from service name. For #3617
1 parent f383810 commit 9a3cef9

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

internal/serviceconfig/serviceconfig.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,17 @@ func findServiceConfig(googleapisDir, path string) (string, error) {
171171
}
172172

173173
func populateFromServiceConfig(api *API, cfg *Service) *API {
174-
if api.Title == "" {
175-
api.Title = cfg.GetTitle()
174+
if api.Description == "" && cfg.GetDocumentation() != nil {
175+
api.Description = strings.TrimSpace(cfg.GetDocumentation().GetSummary())
176176
}
177177
if api.ServiceName == "" {
178178
api.ServiceName = cfg.GetName()
179179
}
180-
if api.Description == "" && cfg.GetDocumentation() != nil {
181-
api.Description = strings.TrimSpace(cfg.GetDocumentation().GetSummary())
180+
if api.ShortName == "" {
181+
api.ShortName = defaultShortName(api.ServiceName)
182+
}
183+
if api.Title == "" {
184+
api.Title = cfg.GetTitle()
182185
}
183186
publishing := cfg.GetPublishing()
184187
if publishing != nil {
@@ -238,6 +241,11 @@ func isServiceConfigFile(path string) (bool, error) {
238241
return false, scanner.Err()
239242
}
240243

244+
// defaultShortName returns the default short name from serviceName.
245+
func defaultShortName(serviceName string) string {
246+
return strings.Split(serviceName, ".")[0]
247+
}
248+
241249
// FindGRPCServiceConfig searches for gRPC service config files in the given
242250
// API directory. It returns the path relative to googleapisDir for use with
243251
// protoc's retry-config option. Returns empty string if no config is found.

internal/serviceconfig/serviceconfig_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func TestFind(t *testing.T) {
126126
Path: "google/cloud/aiplatform/v1/schema/predict/instance",
127127
ServiceConfig: "google/cloud/aiplatform/v1/schema/aiplatform_v1.yaml",
128128
ServiceName: "aiplatform.googleapis.com",
129+
ShortName: "aiplatform",
129130
Title: "Vertex AI API",
130131
Transports: map[string]Transport{"python": "grpc"},
131132
},
@@ -149,10 +150,11 @@ func TestFind(t *testing.T) {
149150
name: "discovery",
150151
api: "discoveries/compute.v1.json",
151152
want: &API{
152-
Path: "google/cloud/compute/v1",
153153
Discovery: "discoveries/compute.v1.json",
154+
Path: "google/cloud/compute/v1",
154155
ServiceConfig: "google/cloud/compute/v1/compute_v1.yaml",
155156
ServiceName: "compute.googleapis.com",
157+
ShortName: "compute",
156158
Title: "Google Compute Engine API",
157159
Transports: map[string]Transport{"csharp": "rest", "go": "rest", "java": "rest", "php": "rest"},
158160
},
@@ -287,6 +289,17 @@ func TestPopulateFromServiceConfig(t *testing.T) {
287289
ShortName: "override short name",
288290
},
289291
},
292+
{
293+
name: "default short name",
294+
api: &API{},
295+
cfg: &Service{
296+
Name: "accessapproval.googleapis.com",
297+
},
298+
want: &API{
299+
ServiceName: "accessapproval.googleapis.com",
300+
ShortName: "accessapproval",
301+
},
302+
},
290303
} {
291304
t.Run(test.name, func(t *testing.T) {
292305
got := populateFromServiceConfig(test.api, test.cfg)

0 commit comments

Comments
 (0)