Skip to content

Commit a282284

Browse files
authored
refactor(internal/config): create Channel struct (#3081)
The Channel, ServiceConfig, SpecificationFormat fields have been moved from config.Library to a new config.Channel struct. This refactors the configuration to better support cases when there are multiple channels within a single library.
1 parent 9370b35 commit a282284

File tree

6 files changed

+77
-42
lines changed

6 files changed

+77
-42
lines changed

internal/config/config.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ type Library struct {
119119

120120
// Channels specifies multiple API versions to bundle into one library (for multi-version libraries).
121121
// Alternative to API field for libraries that bundle multiple versions.
122-
Channels []string `yaml:"channels,omitempty"`
122+
Channels []*Channel `yaml:"channels,omitempty"`
123123

124124
// CopyrightYear is the copyright year for the library.
125125
CopyrightYear string `yaml:"copyright_year,omitempty"`
@@ -147,20 +147,27 @@ type Library struct {
147147
// Rust contains Rust-specific library configuration.
148148
Rust *RustCrate `yaml:"rust,omitempty"`
149149

150-
// SpecificationFormat specifies the API specification format.
151-
// Valid values are "protobuf" (default) or "discovery".
150+
// SpecificationFormat specifies the API specification format. Valid values
151+
// are "protobuf" (default) or "discovery".
152152
SpecificationFormat string `yaml:"specification_format,omitempty"`
153153

154-
// ServiceConfig is the path to the service config file.
155-
ServiceConfig string `yaml:"-"`
156-
157154
// Transport overrides the default transport.
158155
Transport string `yaml:"transport,omitempty"`
159156

160157
// Version is the library version.
161158
Version string `yaml:"version,omitempty"`
162159
}
163160

161+
// Channel contains information about an API channel.
162+
type Channel struct {
163+
// Path specifies which googleapis Path to generate from (for generated
164+
// libraries).
165+
Path string `yaml:"channel,omitempty"`
166+
167+
// ServiceConfig is the path to the service config file.
168+
ServiceConfig string `yaml:"service_config,omitempty"`
169+
}
170+
164171
// LibraryGenerate contains per-library generate configuration.
165172
type LibraryGenerate struct {
166173
// Disabled prevents library generation.

internal/config/testdata/rust/librarian.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ default:
3838
force_used: true
3939
libraries:
4040
- name: google-cloud-secretmanager-v1
41-
channel: google/cloud/secretmanager/v1
41+
channels:
42+
- path: google/cloud/secretmanager/v1
4243
- name: google-cloud-storage-v2
43-
channel: google/cloud/storage/v2
44+
channels:
45+
- path: google/cloud/storage/v2
4446
rust:
4547
disabled_rustdoc_warnings:
4648
- rustdoc::bare_urls

internal/language/internal/rust/codec.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
sidekickconfig "github.com/googleapis/librarian/internal/sidekick/config"
2222
)
2323

24-
func toSidekickConfig(library *config.Library, serviceConfig, googleapisDir, discoveryDir string) *sidekickconfig.Config {
24+
func toSidekickConfig(library *config.Library, channel *config.Channel, googleapisDir, discoveryDir string) *sidekickconfig.Config {
2525
source := map[string]string{
2626
"googleapis-root": googleapisDir,
2727
}
@@ -35,8 +35,8 @@ func toSidekickConfig(library *config.Library, serviceConfig, googleapisDir, dis
3535
General: sidekickconfig.GeneralConfig{
3636
Language: "rust",
3737
SpecificationFormat: specFormat,
38-
ServiceConfig: serviceConfig,
39-
SpecificationSource: library.Channel,
38+
ServiceConfig: channel.ServiceConfig,
39+
SpecificationSource: channel.Path,
4040
},
4141
Source: source,
4242
Codec: buildCodec(library),

internal/language/internal/rust/codec_test.go

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@ func TestToSidekickConfig(t *testing.T) {
2626
for _, test := range []struct {
2727
name string
2828
library *config.Library
29-
serviceConfig string
29+
channel *config.Channel
3030
googleapisDir string
3131
discoveryDir string
3232
want *sidekickconfig.Config
3333
}{
3434
{
3535
name: "minimal config",
3636
library: &config.Library{
37-
Channel: "google/cloud/storage/v1",
38-
Name: "google-cloud-storage",
37+
Name: "google-cloud-storage",
38+
},
39+
channel: &config.Channel{
40+
Path: "google/cloud/storage/v1",
41+
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
3942
},
4043
googleapisDir: "/tmp/googleapis",
41-
serviceConfig: "google/cloud/storage/v1/storage_v1.yaml",
4244
want: &sidekickconfig.Config{
4345
General: sidekickconfig.GeneralConfig{
4446
Language: "rust",
@@ -57,13 +59,15 @@ func TestToSidekickConfig(t *testing.T) {
5759
{
5860
name: "with version and release level",
5961
library: &config.Library{
60-
Channel: "google/cloud/storage/v1",
6162
Name: "google-cloud-storage",
6263
Version: "0.1.0",
6364
ReleaseLevel: "preview",
6465
},
66+
channel: &config.Channel{
67+
Path: "google/cloud/storage/v1",
68+
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
69+
},
6570
googleapisDir: "/tmp/googleapis",
66-
serviceConfig: "google/cloud/storage/v1/storage_v1.yaml",
6771
want: &sidekickconfig.Config{
6872
General: sidekickconfig.GeneralConfig{
6973
Language: "rust",
@@ -84,12 +88,14 @@ func TestToSidekickConfig(t *testing.T) {
8488
{
8589
name: "with copyright year",
8690
library: &config.Library{
87-
Channel: "google/cloud/storage/v1",
8891
Name: "google-cloud-storage",
8992
CopyrightYear: "2024",
9093
},
94+
channel: &config.Channel{
95+
Path: "google/cloud/storage/v1",
96+
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
97+
},
9198
googleapisDir: "/tmp/googleapis",
92-
serviceConfig: "google/cloud/storage/v1/storage_v1.yaml",
9399
want: &sidekickconfig.Config{
94100
General: sidekickconfig.GeneralConfig{
95101
Language: "rust",
@@ -109,8 +115,7 @@ func TestToSidekickConfig(t *testing.T) {
109115
{
110116
name: "with rust config",
111117
library: &config.Library{
112-
Channel: "google/cloud/storage/v1",
113-
Name: "google-cloud-storage",
118+
Name: "google-cloud-storage",
114119
Rust: &config.RustCrate{
115120
ModulePath: "gcs",
116121
PerServiceFeatures: true,
@@ -126,8 +131,11 @@ func TestToSidekickConfig(t *testing.T) {
126131
TemplateOverride: "custom-template",
127132
},
128133
},
134+
channel: &config.Channel{
135+
Path: "google/cloud/storage/v1",
136+
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
137+
},
129138
googleapisDir: "/tmp/googleapis",
130-
serviceConfig: "google/cloud/storage/v1/storage_v1.yaml",
131139
want: &sidekickconfig.Config{
132140
General: sidekickconfig.GeneralConfig{
133141
Language: "rust",
@@ -158,15 +166,17 @@ func TestToSidekickConfig(t *testing.T) {
158166
{
159167
name: "with publish disabled",
160168
library: &config.Library{
161-
Channel: "google/cloud/storage/v1",
162-
Name: "google-cloud-storage",
169+
Name: "google-cloud-storage",
163170
Publish: &config.LibraryPublish{
164171
Disabled: true,
165172
},
166173
Rust: &config.RustCrate{},
167174
},
175+
channel: &config.Channel{
176+
Path: "google/cloud/storage/v1",
177+
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
178+
},
168179
googleapisDir: "/tmp/googleapis",
169-
serviceConfig: "google/cloud/storage/v1/storage_v1.yaml",
170180
want: &sidekickconfig.Config{
171181
General: sidekickconfig.GeneralConfig{
172182
Language: "rust",
@@ -186,8 +196,7 @@ func TestToSidekickConfig(t *testing.T) {
186196
{
187197
name: "with package dependencies",
188198
library: &config.Library{
189-
Channel: "google/cloud/storage/v1",
190-
Name: "google-cloud-storage",
199+
Name: "google-cloud-storage",
191200
Rust: &config.RustCrate{
192201
PackageDependencies: []config.RustPackageDependency{
193202
{
@@ -201,8 +210,11 @@ func TestToSidekickConfig(t *testing.T) {
201210
},
202211
},
203212
},
213+
channel: &config.Channel{
214+
Path: "google/cloud/storage/v1",
215+
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
216+
},
204217
googleapisDir: "/tmp/googleapis",
205-
serviceConfig: "google/cloud/storage/v1/storage_v1.yaml",
206218
want: &sidekickconfig.Config{
207219
General: sidekickconfig.GeneralConfig{
208220
Language: "rust",
@@ -222,8 +234,7 @@ func TestToSidekickConfig(t *testing.T) {
222234
{
223235
name: "with documentation overrides",
224236
library: &config.Library{
225-
Channel: "google/cloud/storage/v1",
226-
Name: "google-cloud-storage",
237+
Name: "google-cloud-storage",
227238
Rust: &config.RustCrate{
228239
DocumentationOverrides: []config.RustDocumentationOverride{
229240
{
@@ -234,8 +245,11 @@ func TestToSidekickConfig(t *testing.T) {
234245
},
235246
},
236247
},
248+
channel: &config.Channel{
249+
Path: "google/cloud/storage/v1",
250+
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
251+
},
237252
googleapisDir: "/tmp/googleapis",
238-
serviceConfig: "google/cloud/storage/v1/storage_v1.yaml",
239253
want: &sidekickconfig.Config{
240254
General: sidekickconfig.GeneralConfig{
241255
Language: "rust",
@@ -261,8 +275,7 @@ func TestToSidekickConfig(t *testing.T) {
261275
{
262276
name: "with pagination overrides",
263277
library: &config.Library{
264-
Channel: "google/cloud/storage/v1",
265-
Name: "google-cloud-storage",
278+
Name: "google-cloud-storage",
266279
Rust: &config.RustCrate{
267280
PaginationOverrides: []config.RustPaginationOverride{
268281
{
@@ -272,8 +285,11 @@ func TestToSidekickConfig(t *testing.T) {
272285
},
273286
},
274287
},
288+
channel: &config.Channel{
289+
Path: "google/cloud/storage/v1",
290+
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
291+
},
275292
googleapisDir: "/tmp/googleapis",
276-
serviceConfig: "google/cloud/storage/v1/storage_v1.yaml",
277293
want: &sidekickconfig.Config{
278294
General: sidekickconfig.GeneralConfig{
279295
Language: "rust",
@@ -298,13 +314,15 @@ func TestToSidekickConfig(t *testing.T) {
298314
{
299315
name: "with discovery format",
300316
library: &config.Library{
301-
Channel: "discoveries/compute.v1.json",
302317
Name: "google-cloud-compute-v1",
303318
SpecificationFormat: "discovery",
304319
},
320+
channel: &config.Channel{
321+
Path: "discoveries/compute.v1.json",
322+
ServiceConfig: "google/cloud/compute/v1/compute_v1.yaml",
323+
},
305324
googleapisDir: "/tmp/googleapis",
306325
discoveryDir: "/tmp/discovery-artifact-manager",
307-
serviceConfig: "google/cloud/compute/v1/compute_v1.yaml",
308326
want: &sidekickconfig.Config{
309327
General: sidekickconfig.GeneralConfig{
310328
Language: "rust",
@@ -324,7 +342,7 @@ func TestToSidekickConfig(t *testing.T) {
324342
},
325343
} {
326344
t.Run(test.name, func(t *testing.T) {
327-
got := toSidekickConfig(test.library, test.serviceConfig, test.googleapisDir, test.discoveryDir)
345+
got := toSidekickConfig(test.library, test.channel, test.googleapisDir, test.discoveryDir)
328346
if diff := cmp.Diff(test.want, got); diff != "" {
329347
t.Errorf("mismatch (-want +got):\n%s", diff)
330348
}

internal/language/internal/rust/generate.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package rust
1616

1717
import (
1818
"context"
19+
"fmt"
1920

2021
"github.com/googleapis/librarian/internal/config"
2122
"github.com/googleapis/librarian/internal/fetch"
@@ -38,7 +39,10 @@ func Generate(ctx context.Context, library *config.Library, sources *config.Sour
3839
if err != nil {
3940
return err
4041
}
41-
sidekickConfig := toSidekickConfig(library, library.ServiceConfig, googleapisDir, discoveryDir)
42+
if len(library.Channels) != 1 {
43+
return fmt.Errorf("the Rust generator only supports a single channel per library")
44+
}
45+
sidekickConfig := toSidekickConfig(library, library.Channels[0], googleapisDir, discoveryDir)
4246
model, err := parser.CreateModel(sidekickConfig)
4347
if err != nil {
4448
return err

internal/language/internal/rust/generate_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ func TestGenerate(t *testing.T) {
3434
outDir := t.TempDir()
3535
googleapisDir := filepath.Join(testdataDir, "googleapis")
3636
library := &config.Library{
37-
Name: "secretmanager",
38-
Output: outDir,
39-
ServiceConfig: "google/cloud/secretmanager/v1/secretmanager_v1.yaml",
40-
Channel: "google/cloud/secretmanager/v1",
37+
Name: "secretmanager",
38+
Output: outDir,
39+
Channels: []*config.Channel{
40+
{
41+
Path: "google/cloud/secretmanager/v1",
42+
ServiceConfig: "google/cloud/secretmanager/v1/secretmanager_v1.yaml",
43+
},
44+
},
4145
Version: "0.1.0",
4246
ReleaseLevel: "preview",
4347
CopyrightYear: "2025",

0 commit comments

Comments
 (0)