Skip to content

Commit 92d8fce

Browse files
authored
fix(internal/serviceconfig): set transport at API level (#3958)
Set transport of `google/cloud/aiplatform/v1` to grpc. The value is non-default and is different than another api (`google/cloud/aiplatform/v1beta1`) in the library. Therefore, set it in API level, rather than library level. For #3618
1 parent d5037e6 commit 92d8fce

5 files changed

Lines changed: 114 additions & 7 deletions

File tree

doc/api-allowlist-schema.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This document describes the schema for the API Allowlist.
44

55
## API Configuration
66

7-
[Link to code](../internal/serviceconfig/api.go#L48)
7+
[Link to code](../internal/serviceconfig/api.go#L49)
88
| Field | Type | Description |
99
| :--- | :--- | :--- |
1010
| `Path` | string | Path is the proto directory path in github.com/googleapis/googleapis. If ServiceConfig is empty, the service config is assumed to live at this path. |

internal/librarian/golang/generate.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,7 @@ func buildGAPICOpts(apiPath string, library *config.Library, goAPI *config.GoAPI
181181
// TODO(https://github.com/googleapis/librarian/issues/3775): assuming
182182
// transport is library-wide for now, until we have figured out the config
183183
// for transports.
184-
transport := library.Transport
185-
if transport == "" {
186-
transport = "grpc+rest"
187-
}
188-
opts = append(opts, "transport="+transport)
184+
opts = append(opts, "transport="+getTransport(sc))
189185
if library.ReleaseLevel != "" {
190186
opts = append(opts, "release-level="+library.ReleaseLevel)
191187
}
@@ -373,3 +369,13 @@ func updateSnippetMetadata(library *config.Library, output string) error {
373369
return nil
374370
})
375371
}
372+
373+
// getTransport get transport from serviceconfig.API for language Go.
374+
//
375+
// The default value is serviceconfig.GRPCRest.
376+
func getTransport(sc *serviceconfig.API) string {
377+
if sc != nil {
378+
return sc.Transport("go")
379+
}
380+
return string(serviceconfig.GRPCRest)
381+
}

internal/librarian/golang/generate_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,47 @@ func TestBuildGAPICImportPath(t *testing.T) {
351351
})
352352
}
353353
}
354+
355+
func TestGetTransport(t *testing.T) {
356+
for _, test := range []struct {
357+
name string
358+
sc *serviceconfig.API
359+
want string
360+
}{
361+
{
362+
name: "nil serviceconfig",
363+
sc: nil,
364+
want: "grpc+rest",
365+
},
366+
{
367+
name: "empty serviceconfig",
368+
sc: &serviceconfig.API{},
369+
want: "grpc+rest",
370+
},
371+
{
372+
name: "go specific transport",
373+
sc: &serviceconfig.API{
374+
Transports: map[string]serviceconfig.Transport{
375+
"go": serviceconfig.GRPC,
376+
},
377+
},
378+
want: "grpc",
379+
},
380+
{
381+
name: "other language transport",
382+
sc: &serviceconfig.API{
383+
Transports: map[string]serviceconfig.Transport{
384+
"python": serviceconfig.GRPC,
385+
},
386+
},
387+
want: "grpc+rest",
388+
},
389+
} {
390+
t.Run(test.name, func(t *testing.T) {
391+
got := getTransport(test.sc)
392+
if diff := cmp.Diff(test.want, got); diff != "" {
393+
t.Errorf("mismatch (-want +got):\n%s", diff)
394+
}
395+
})
396+
}
397+
}

internal/serviceconfig/api.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package serviceconfig
1818

1919
const (
2020
langDart = "dart"
21+
langGo = "go"
2122
langPython = "python"
2223
langRust = "rust"
2324

@@ -81,6 +82,17 @@ type API struct {
8182
Transports map[string]Transport
8283
}
8384

85+
// Transport gets transport for a given language.
86+
//
87+
// The default value is GRPCRest, if undefined for a language.
88+
func (api *API) Transport(language string) string {
89+
if trans, ok := api.Transports[language]; ok {
90+
return string(trans)
91+
}
92+
93+
return string(GRPCRest)
94+
}
95+
8496
// APIs defines all API paths and their language availability.
8597
var APIs = []API{
8698
{Path: "google/ads/admanager/v1", Languages: []string{langPython}},
@@ -121,7 +133,7 @@ var APIs = []API{
121133
{Path: "google/chat/v1", Languages: []string{langPython}},
122134
{Path: "google/cloud/accessapproval/v1"},
123135
{Path: "google/cloud/advisorynotifications/v1"},
124-
{Path: "google/cloud/aiplatform/v1"},
136+
{Path: "google/cloud/aiplatform/v1", Transports: map[string]Transport{langGo: GRPC}},
125137
{Path: "google/cloud/aiplatform/v1/schema/predict/instance", ServiceConfig: serviceConfigAIPlatformSchema},
126138
{Path: "google/cloud/aiplatform/v1/schema/predict/params", ServiceConfig: serviceConfigAIPlatformSchema},
127139
{Path: "google/cloud/aiplatform/v1/schema/predict/prediction", ServiceConfig: serviceConfigAIPlatformSchema},

internal/serviceconfig/api_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package serviceconfig
1616

1717
import (
1818
"testing"
19+
20+
"github.com/google/go-cmp/cmp"
1921
)
2022

2123
func TestAPIsNoDuplicates(t *testing.T) {
@@ -37,3 +39,46 @@ func TestAPIsAlphabeticalOrder(t *testing.T) {
3739
}
3840
}
3941
}
42+
43+
func TestGetTransport(t *testing.T) {
44+
for _, test := range []struct {
45+
name string
46+
sc *API
47+
lang string
48+
want string
49+
}{
50+
{
51+
name: "empty serviceconfig",
52+
sc: &API{},
53+
lang: "go",
54+
want: "grpc+rest",
55+
},
56+
{
57+
name: "go specific transport",
58+
sc: &API{
59+
Transports: map[string]Transport{
60+
"go": GRPC,
61+
},
62+
},
63+
lang: "go",
64+
want: "grpc",
65+
},
66+
{
67+
name: "other language transport",
68+
sc: &API{
69+
Transports: map[string]Transport{
70+
"go": GRPC,
71+
},
72+
},
73+
lang: "python",
74+
want: "grpc+rest",
75+
},
76+
} {
77+
t.Run(test.name, func(t *testing.T) {
78+
got := test.sc.Transport(test.lang)
79+
if diff := cmp.Diff(test.want, got); diff != "" {
80+
t.Errorf("mismatch (-want +got):\n%s", diff)
81+
}
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)