Skip to content

Commit 0a9b5f0

Browse files
authored
feat(internal/config): add RootName to rust module (#3550)
Add `RootName` to rust module config. This param needs to be override when generating the special protojson-conformance library. Add the following config in google-cloud-rust after merging this PR: ``` - name: protojson-conformance copyright_year: "2025" output: tests/protojson-conformance rust: modules: - include_list: conformance.proto module_path: crate::generated::gapic::model output: tests/protojson-conformance/src/generated/convert source: conformance template: convert-prost - generate_setter_samples: "false" include_list: conformance.proto module_path: crate::generated::gapic::model output: tests/protojson-conformance/src/generated/gapic source: conformance template: grpc-client - include_list: conformance.proto module_path: crate::generated::gapic::model output: tests/protojson-conformance/src/generated/protos root_name: conformance-root source: conformance template: prost - generate_setter_samples: "false" include_list: test_messages_proto3.proto module_path: crate::generated::test_protos output: tests/protojson-conformance/src/generated/test_protos source: src/google/protobuf template: mod skip_publish: true veneer: true ``` Fixes #3533
1 parent 56a3348 commit 0a9b5f0

4 files changed

Lines changed: 99 additions & 68 deletions

File tree

internal/config/language.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ type RustModule struct {
9999
// PostProcessProtos contains code to post-process generated protos.
100100
PostProcessProtos string `yaml:"post_process_protos,omitempty"`
101101

102+
// RootName is the key for the root directory in the source map.
103+
// It overrides the default root, googleapis-root, used by the rust+prost generator.
104+
RootName string `yaml:"root_name,omitempty"`
105+
102106
// RoutingRequired indicates whether routing is required.
103107
RoutingRequired bool `yaml:"routing_required,omitempty"`
104108

internal/librarian/rust/codec.go

Lines changed: 17 additions & 12 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, channel *config.Channel, googleapisDir, discoveryDir, protobufRootDir, conformanceDir, showcaseDir string) *sidekickconfig.Config {
24+
func toSidekickConfig(library *config.Library, channel *config.Channel, sources *Sources) *sidekickconfig.Config {
2525
source := map[string]string{}
2626
specFormat := "protobuf"
2727
if library.SpecificationFormat != "" {
@@ -31,21 +31,21 @@ func toSidekickConfig(library *config.Library, channel *config.Channel, googleap
3131
specFormat = "disco"
3232
}
3333

34-
if len(library.Roots) == 0 && googleapisDir != "" {
34+
if len(library.Roots) == 0 && sources.Googleapis != "" {
3535
// Default to googleapis if no roots are specified.
36-
source["googleapis-root"] = googleapisDir
36+
source["googleapis-root"] = sources.Googleapis
3737
source["roots"] = "googleapis"
3838
} else {
3939
source["roots"] = strings.Join(library.Roots, ",")
4040
rootMap := map[string]struct {
4141
path string
4242
key string
4343
}{
44-
"googleapis": {path: googleapisDir, key: "googleapis-root"},
45-
"discovery": {path: discoveryDir, key: "discovery-root"},
46-
"showcase": {path: showcaseDir, key: "showcase-root"},
47-
"protobuf-src": {path: protobufRootDir, key: "protobuf-src-root"},
48-
"conformance": {path: conformanceDir, key: "conformance-root"},
44+
"googleapis": {path: sources.Googleapis, key: "googleapis-root"},
45+
"discovery": {path: sources.Discovery, key: "discovery-root"},
46+
"showcase": {path: sources.Showcase, key: "showcase-root"},
47+
"protobuf-src": {path: sources.ProtobufSrc, key: "protobuf-src-root"},
48+
"conformance": {path: sources.Conformance, key: "conformance-root"},
4949
}
5050
for _, root := range library.Roots {
5151
if r, ok := rootMap[root]; ok && r.path != "" {
@@ -227,10 +227,13 @@ func formatPackageDependency(dep *config.RustPackageDependency) string {
227227
return strings.Join(parts, ",")
228228
}
229229

230-
func moduleToSidekickConfig(library *config.Library, module *config.RustModule, googleapisDir, protobufSrcDir string) *sidekickconfig.Config {
230+
func moduleToSidekickConfig(library *config.Library, module *config.RustModule, sources *Sources) *sidekickconfig.Config {
231231
source := map[string]string{
232-
"googleapis-root": googleapisDir,
233-
"protobuf-src-root": protobufSrcDir,
232+
"conformance-root": sources.Conformance,
233+
"discovery-root": sources.Discovery,
234+
"googleapis-root": sources.Googleapis,
235+
"protobuf-src-root": sources.ProtobufSrc,
236+
"showcase-root": sources.Showcase,
234237
}
235238
for root, dir := range module.ModuleRoots {
236239
source[root] = dir
@@ -317,6 +320,8 @@ func buildModuleCodec(library *config.Library, module *config.RustModule) map[st
317320
if module.DisabledRustdocWarnings != nil {
318321
codec["disabled-rustdoc-warnings"] = strings.Join(module.DisabledRustdocWarnings, ",")
319322
}
320-
323+
if module.RootName != "" {
324+
codec["root-name"] = module.RootName
325+
}
321326
return codec
322327
}

internal/librarian/rust/codec_test.go

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@ import (
2424

2525
func TestToSidekickConfig(t *testing.T) {
2626
for _, test := range []struct {
27-
name string
28-
library *config.Library
29-
channel *config.Channel
30-
googleapisDir string
31-
discoveryDir string
32-
protobufDir string
33-
conformanceDir string
34-
showcaseDir string
35-
want *sidekickconfig.Config
27+
name string
28+
library *config.Library
29+
channel *config.Channel
30+
sources *Sources
31+
want *sidekickconfig.Config
3632
}{
3733
{
3834
name: "minimal config",
@@ -43,7 +39,9 @@ func TestToSidekickConfig(t *testing.T) {
4339
Path: "google/cloud/storage/v1",
4440
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
4541
},
46-
googleapisDir: "/tmp/googleapis",
42+
sources: &Sources{
43+
Googleapis: "/tmp/googleapis",
44+
},
4745
want: &sidekickconfig.Config{
4846
General: sidekickconfig.GeneralConfig{
4947
Language: "rust",
@@ -72,7 +70,9 @@ func TestToSidekickConfig(t *testing.T) {
7270
Path: "google/cloud/storage/v1",
7371
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
7472
},
75-
googleapisDir: "/tmp/googleapis",
73+
sources: &Sources{
74+
Googleapis: "/tmp/googleapis",
75+
},
7676
want: &sidekickconfig.Config{
7777
General: sidekickconfig.GeneralConfig{
7878
Language: "rust",
@@ -102,7 +102,9 @@ func TestToSidekickConfig(t *testing.T) {
102102
Path: "google/cloud/storage/v1",
103103
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
104104
},
105-
googleapisDir: "/tmp/googleapis",
105+
sources: &Sources{
106+
Googleapis: "/tmp/googleapis",
107+
},
106108
want: &sidekickconfig.Config{
107109
General: sidekickconfig.GeneralConfig{
108110
Language: "rust",
@@ -146,7 +148,9 @@ func TestToSidekickConfig(t *testing.T) {
146148
Path: "google/cloud/storage/v1",
147149
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
148150
},
149-
googleapisDir: "/tmp/googleapis",
151+
sources: &Sources{
152+
Googleapis: "/tmp/googleapis",
153+
},
150154
want: &sidekickconfig.Config{
151155
General: sidekickconfig.GeneralConfig{
152156
Language: "rust",
@@ -188,7 +192,9 @@ func TestToSidekickConfig(t *testing.T) {
188192
Path: "google/cloud/storage/v1",
189193
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
190194
},
191-
googleapisDir: "/tmp/googleapis",
195+
sources: &Sources{
196+
Googleapis: "/tmp/googleapis",
197+
},
192198
want: &sidekickconfig.Config{
193199
General: sidekickconfig.GeneralConfig{
194200
Language: "rust",
@@ -229,7 +235,9 @@ func TestToSidekickConfig(t *testing.T) {
229235
Path: "google/cloud/storage/v1",
230236
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
231237
},
232-
googleapisDir: "/tmp/googleapis",
238+
sources: &Sources{
239+
Googleapis: "/tmp/googleapis",
240+
},
233241
want: &sidekickconfig.Config{
234242
General: sidekickconfig.GeneralConfig{
235243
Language: "rust",
@@ -265,11 +273,9 @@ func TestToSidekickConfig(t *testing.T) {
265273
Path: "google/cloud/storage/v1",
266274
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
267275
},
268-
googleapisDir: "/tmp/googleapis",
269-
discoveryDir: "",
270-
protobufDir: "",
271-
conformanceDir: "",
272-
showcaseDir: "",
276+
sources: &Sources{
277+
Googleapis: "/tmp/googleapis",
278+
},
273279
want: &sidekickconfig.Config{
274280
General: sidekickconfig.GeneralConfig{
275281
Language: "rust",
@@ -324,11 +330,9 @@ func TestToSidekickConfig(t *testing.T) {
324330
Path: "google/cloud/storage/v1",
325331
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
326332
},
327-
googleapisDir: "/tmp/googleapis",
328-
discoveryDir: "",
329-
protobufDir: "",
330-
conformanceDir: "",
331-
showcaseDir: "",
333+
sources: &Sources{
334+
Googleapis: "/tmp/googleapis",
335+
},
332336
want: &sidekickconfig.Config{
333337
General: sidekickconfig.GeneralConfig{
334338
Language: "rust",
@@ -374,11 +378,9 @@ func TestToSidekickConfig(t *testing.T) {
374378
Path: "google/cloud/storage/v1",
375379
ServiceConfig: "google/cloud/storage/v1/storage_v1.yaml",
376380
},
377-
googleapisDir: "/tmp/googleapis",
378-
discoveryDir: "",
379-
protobufDir: "",
380-
conformanceDir: "",
381-
showcaseDir: "",
381+
sources: &Sources{
382+
Googleapis: "/tmp/googleapis",
383+
},
382384
want: &sidekickconfig.Config{
383385
General: sidekickconfig.GeneralConfig{
384386
Language: "rust",
@@ -412,8 +414,10 @@ func TestToSidekickConfig(t *testing.T) {
412414
Path: "discoveries/compute.v1.json",
413415
ServiceConfig: "google/cloud/compute/v1/compute_v1.yaml",
414416
},
415-
googleapisDir: "/tmp/googleapis",
416-
discoveryDir: "/tmp/discovery-artifact-manager",
417+
sources: &Sources{
418+
Googleapis: "/tmp/googleapis",
419+
Discovery: "/tmp/discovery-artifact-manager",
420+
},
417421
want: &sidekickconfig.Config{
418422
General: sidekickconfig.GeneralConfig{
419423
Language: "rust",
@@ -442,9 +446,11 @@ func TestToSidekickConfig(t *testing.T) {
442446
Path: "discoveries/compute.v1.json",
443447
ServiceConfig: "google/cloud/compute/v1/compute_v1.yaml",
444448
},
445-
googleapisDir: "/tmp/googleapis",
446-
discoveryDir: "/tmp/discovery-artifact-manager",
447-
showcaseDir: "/tmp/showcase",
449+
sources: &Sources{
450+
Googleapis: "/tmp/googleapis",
451+
Discovery: "/tmp/discovery-artifact-manager",
452+
Showcase: "/tmp/showcase",
453+
},
448454
want: &sidekickconfig.Config{
449455
General: sidekickconfig.GeneralConfig{
450456
Language: "rust",
@@ -474,7 +480,9 @@ func TestToSidekickConfig(t *testing.T) {
474480
channel: &config.Channel{
475481
Path: "google/apps/script/type/gmail",
476482
},
477-
googleapisDir: "/tmp/googleapis",
483+
sources: &Sources{
484+
Googleapis: "/tmp/googleapis",
485+
},
478486
want: &sidekickconfig.Config{
479487
General: sidekickconfig.GeneralConfig{
480488
Language: "rust",
@@ -501,7 +509,9 @@ func TestToSidekickConfig(t *testing.T) {
501509
Path: "google/longrunning",
502510
ServiceConfig: "google/longrunning/longrunning.yaml",
503511
},
504-
googleapisDir: "/tmp/googleapis",
512+
sources: &Sources{
513+
Googleapis: "/tmp/googleapis",
514+
},
505515
want: &sidekickconfig.Config{
506516
General: sidekickconfig.GeneralConfig{
507517
Language: "rust",
@@ -535,7 +545,9 @@ func TestToSidekickConfig(t *testing.T) {
535545
Path: "google/spanner/admin/database/v1",
536546
ServiceConfig: "google/spanner/admin/database/v1/spanner.yaml",
537547
},
538-
googleapisDir: "/tmp/googleapis",
548+
sources: &Sources{
549+
Googleapis: "/tmp/googleapis",
550+
},
539551
want: &sidekickconfig.Config{
540552
General: sidekickconfig.GeneralConfig{
541553
Language: "rust",
@@ -565,7 +577,9 @@ func TestToSidekickConfig(t *testing.T) {
565577
Path: "google/cloud/storageinsights/v1",
566578
ServiceConfig: "google/cloud/storageinsights/v1/storageinsights_v1.yaml",
567579
},
568-
googleapisDir: "/tmp/googleapis",
580+
sources: &Sources{
581+
Googleapis: "/tmp/googleapis",
582+
},
569583
want: &sidekickconfig.Config{
570584
General: sidekickconfig.GeneralConfig{
571585
Language: "rust",
@@ -613,8 +627,10 @@ func TestToSidekickConfig(t *testing.T) {
613627
Path: "discoveries/compute.v1.json",
614628
ServiceConfig: "google/cloud/compute/v1/compute_v1.yaml",
615629
},
616-
googleapisDir: "/tmp/googleapis",
617-
discoveryDir: "/tmp/discovery-artifact-manager",
630+
sources: &Sources{
631+
Googleapis: "/tmp/googleapis",
632+
Discovery: "/tmp/discovery-artifact-manager",
633+
},
618634
want: &sidekickconfig.Config{
619635
General: sidekickconfig.GeneralConfig{
620636
Language: "rust",
@@ -659,9 +675,11 @@ func TestToSidekickConfig(t *testing.T) {
659675
Path: "google/cloud/vision/v1",
660676
ServiceConfig: "google/cloud/vision/v1/vision_v1.yaml",
661677
},
662-
googleapisDir: "/tmp/googleapis",
663-
protobufDir: "/tmp/protobuf/src",
664-
conformanceDir: "/tmp/conformance",
678+
sources: &Sources{
679+
Googleapis: "/tmp/googleapis",
680+
ProtobufSrc: "/tmp/protobuf/src",
681+
Conformance: "/tmp/conformance",
682+
},
665683
want: &sidekickconfig.Config{
666684
General: sidekickconfig.GeneralConfig{
667685
Language: "rust",
@@ -690,7 +708,9 @@ func TestToSidekickConfig(t *testing.T) {
690708
Path: "google/showcase/v1beta1",
691709
ServiceConfig: "google/showcase/v1beta1/showcase_v1beta1.yaml",
692710
},
693-
showcaseDir: "/tmp/gapic-showcase",
711+
sources: &Sources{
712+
Showcase: "/tmp/gapic-showcase",
713+
},
694714
want: &sidekickconfig.Config{
695715
General: sidekickconfig.GeneralConfig{
696716
Language: "rust",
@@ -719,6 +739,7 @@ func TestToSidekickConfig(t *testing.T) {
719739
},
720740
},
721741
},
742+
sources: &Sources{},
722743
want: &sidekickconfig.Config{
723744
General: sidekickconfig.GeneralConfig{
724745
Language: "rust_storage",
@@ -738,6 +759,7 @@ func TestToSidekickConfig(t *testing.T) {
738759
},
739760
},
740761
},
762+
sources: &Sources{},
741763
want: &sidekickconfig.Config{
742764
General: sidekickconfig.GeneralConfig{
743765
Language: "rust",
@@ -757,6 +779,7 @@ func TestToSidekickConfig(t *testing.T) {
757779
},
758780
},
759781
},
782+
sources: &Sources{},
760783
want: &sidekickconfig.Config{
761784
General: sidekickconfig.GeneralConfig{
762785
Language: "rust+prost",
@@ -768,14 +791,14 @@ func TestToSidekickConfig(t *testing.T) {
768791
if test.library.Rust != nil && test.library.Rust.Modules != nil {
769792
var commentOverrides []sidekickconfig.DocumentationOverride
770793
for _, module := range test.library.Rust.Modules {
771-
got := moduleToSidekickConfig(test.library, module, test.googleapisDir, test.protobufDir)
794+
got := moduleToSidekickConfig(test.library, module, test.sources)
772795
commentOverrides = append(commentOverrides, got.CommentOverrides...)
773796
}
774797
if diff := cmp.Diff(test.want.CommentOverrides, commentOverrides); diff != "" {
775798
t.Errorf("mismatch (-want +got):\n%s", diff)
776799
}
777800
} else {
778-
got := toSidekickConfig(test.library, test.channel, test.googleapisDir, test.discoveryDir, test.protobufDir, test.conformanceDir, test.showcaseDir)
801+
got := toSidekickConfig(test.library, test.channel, test.sources)
779802
if diff := cmp.Diff(test.want, got); diff != "" {
780803
t.Errorf("mismatch (-want +got):\n%s", diff)
781804
}

0 commit comments

Comments
 (0)