Skip to content

Commit d81b502

Browse files
authored
refactor(librarian): migrate map-based source options to SourceConfig (#4354)
Reapply "refactor(librarian): migrate map-based source options to SourceConfig" (#4339). This reverts commit dadc578. This originally failed because google-cloud-rust/librarian.yaml still contained a root_name: conformance-root; however, the -root suffix is no longer needed after we moved to the SourceConfig struct. We'll cleanup conformance-root. Fixes #3863 Fixes #4300
1 parent 1d54810 commit d81b502

File tree

25 files changed

+260
-491
lines changed

25 files changed

+260
-491
lines changed

doc/config-schema.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ This document describes the schema for the librarian.yaml.
270270
| `name_overrides` | string | Contains codec-level overrides for type and service names. |
271271
| `output` | string | Is the directory where generated code is written (e.g., "src/storage/src/generated/gapic"). |
272272
| `post_process_protos` | string | Contains code to post-process generated protos. |
273-
| `root_name` | string | Is the key for the root directory in the source map. It overrides the default root, googleapis-root, used by the rust+prost generator. |
273+
| `root_name` | string | Is the key for the root directory in the source map. It overrides the default root, googleapis, used by the rust+prost generator. |
274274
| `routing_required` | bool | Indicates whether routing is required. |
275275
| `service_config` | string | Is the path to the service config file. |
276276
| `skipped_ids` | list of string | Is a list of proto IDs to skip in generation. |

internal/config/language.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ type RustModule struct {
163163
PostProcessProtos string `yaml:"post_process_protos,omitempty"`
164164

165165
// RootName is the key for the root directory in the source map.
166-
// It overrides the default root, googleapis-root, used by the rust+prost generator.
166+
// It overrides the default root, googleapis, used by the rust+prost generator.
167167
RootName string `yaml:"root_name,omitempty"`
168168

169169
// RoutingRequired indicates whether routing is required.

internal/librarian/dart/codec.go

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package dart
1717
import (
1818
"errors"
1919
"fmt"
20-
"strings"
2120

2221
"github.com/googleapis/librarian/internal/config"
2322
"github.com/googleapis/librarian/internal/serviceconfig"
@@ -33,10 +32,10 @@ func toModelConfig(library *config.Library, ch *config.API, sources *sidekickcon
3332
return nil, fmt.Errorf("%w, got %q", errInvalidSpecificationFormat, library.SpecificationFormat)
3433
}
3534

36-
src := addLibraryRoots(library, sources)
35+
src := sidekickconfig.NewSourceConfig(*sources, library.Roots)
3736

3837
if library.Dart != nil && library.Dart.IncludeList != nil {
39-
src["include-list"] = strings.Join(library.Dart.IncludeList, ",")
38+
src.IncludeList = library.Dart.IncludeList
4039
}
4140
root := sources.Googleapis
4241
if ch.Path == "schema/google/showcase/v1beta1" {
@@ -128,36 +127,3 @@ func buildCodec(library *config.Library) map[string]string {
128127
}
129128
return codec
130129
}
131-
132-
// TODO(https://github.com/googleapis/librarian/issues/3863): remove this function once we removed sidekick config.
133-
func addLibraryRoots(library *config.Library, sources *sidekickconfig.Sources) map[string]string {
134-
src := make(map[string]string)
135-
if library.Rust == nil {
136-
library.Rust = &config.RustCrate{}
137-
}
138-
139-
if len(library.Roots) == 0 && sources.Googleapis != "" {
140-
// Default to googleapis if no roots are specified.
141-
src["googleapis-root"] = sources.Googleapis
142-
src["roots"] = "googleapis"
143-
} else {
144-
src["roots"] = strings.Join(library.Roots, ",")
145-
rootMap := map[string]struct {
146-
path string
147-
key string
148-
}{
149-
"googleapis": {path: sources.Googleapis, key: "googleapis-root"},
150-
"discovery": {path: sources.Discovery, key: "discovery-root"},
151-
"showcase": {path: sources.Showcase, key: "showcase-root"},
152-
"protobuf-src": {path: sources.ProtobufSrc, key: "protobuf-src-root"},
153-
"conformance": {path: sources.Conformance, key: "conformance-root"},
154-
}
155-
for _, root := range library.Roots {
156-
if r, ok := rootMap[root]; ok && r.path != "" {
157-
src[r.key] = r.path
158-
}
159-
}
160-
}
161-
162-
return src
163-
}

internal/librarian/dart/codec_test.go

Lines changed: 20 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,11 @@ func TestToModelConfig(t *testing.T) {
296296
want: &parser.ModelConfig{
297297
SpecificationFormat: config.SpecProtobuf,
298298
SpecificationSource: "google/cloud/functions/v2",
299-
Source: map[string]string{
300-
"googleapis-root": googleapisDir,
301-
"roots": "googleapis",
299+
Source: sidekickconfig.SourceConfig{
300+
Sources: sidekickconfig.Sources{
301+
Googleapis: googleapisDir,
302+
},
303+
ActiveRoots: []string{"googleapis"},
302304
},
303305
Codec: map[string]string{},
304306
Override: api.ModelOverride{},
@@ -316,9 +318,11 @@ func TestToModelConfig(t *testing.T) {
316318
want: &parser.ModelConfig{
317319
SpecificationFormat: config.SpecProtobuf,
318320
SpecificationSource: "google/cloud/functions/v2",
319-
Source: map[string]string{
320-
"googleapis-root": googleapisDir,
321-
"roots": "googleapis",
321+
Source: sidekickconfig.SourceConfig{
322+
Sources: sidekickconfig.Sources{
323+
Googleapis: googleapisDir,
324+
},
325+
ActiveRoots: []string{"googleapis"},
322326
},
323327
Codec: map[string]string{},
324328
Override: api.ModelOverride{
@@ -340,9 +344,11 @@ func TestToModelConfig(t *testing.T) {
340344
want: &parser.ModelConfig{
341345
SpecificationFormat: config.SpecProtobuf,
342346
SpecificationSource: "google/cloud/functions/v2",
343-
Source: map[string]string{
344-
"googleapis-root": googleapisDir,
345-
"roots": "googleapis",
347+
Source: sidekickconfig.SourceConfig{
348+
Sources: sidekickconfig.Sources{
349+
Googleapis: googleapisDir,
350+
},
351+
ActiveRoots: []string{"googleapis"},
346352
},
347353
Codec: map[string]string{},
348354
Override: api.ModelOverride{
@@ -384,9 +390,11 @@ func TestToModelConfig(t *testing.T) {
384390
want: &parser.ModelConfig{
385391
SpecificationFormat: config.SpecProtobuf,
386392
SpecificationSource: "google/cloud/functions/v2",
387-
Source: map[string]string{
388-
"googleapis-root": googleapisDir,
389-
"roots": "googleapis",
393+
Source: sidekickconfig.SourceConfig{
394+
Sources: sidekickconfig.Sources{
395+
Googleapis: googleapisDir,
396+
},
397+
ActiveRoots: []string{"googleapis"},
390398
},
391399
Override: api.ModelOverride{
392400
Title: "library-title-override",
@@ -442,72 +450,3 @@ func TestToModelConfig(t *testing.T) {
442450
})
443451
}
444452
}
445-
446-
func TestAddLibraryRoots(t *testing.T) {
447-
for _, test := range []struct {
448-
name string
449-
library *config.Library
450-
source *sidekickconfig.Sources
451-
want map[string]string
452-
}{
453-
{
454-
name: "empty roots",
455-
library: &config.Library{},
456-
source: &sidekickconfig.Sources{
457-
Googleapis: "example/path",
458-
},
459-
want: map[string]string{
460-
"googleapis-root": "example/path",
461-
"roots": "googleapis",
462-
},
463-
},
464-
{
465-
name: "non existed sources",
466-
library: &config.Library{
467-
Roots: []string{"non-existed", "googleapis"},
468-
},
469-
source: &sidekickconfig.Sources{
470-
Googleapis: "example/path",
471-
ProtobufSrc: "protobuf/path",
472-
},
473-
want: map[string]string{
474-
"googleapis-root": "example/path",
475-
"roots": "non-existed,googleapis",
476-
},
477-
},
478-
{
479-
name: "all sources",
480-
library: &config.Library{
481-
Roots: []string{
482-
"conformance",
483-
"discovery",
484-
"googleapis",
485-
"protobuf-src",
486-
"showcase",
487-
},
488-
},
489-
source: &sidekickconfig.Sources{
490-
Conformance: "conformance/path",
491-
Discovery: "discovery/path",
492-
Googleapis: "googleapis/path",
493-
ProtobufSrc: "protobuf/path",
494-
Showcase: "showcase/path",
495-
},
496-
want: map[string]string{
497-
"conformance-root": "conformance/path",
498-
"discovery-root": "discovery/path",
499-
"googleapis-root": "googleapis/path",
500-
"protobuf-src-root": "protobuf/path",
501-
"showcase-root": "showcase/path",
502-
"roots": "conformance,discovery,googleapis,protobuf-src,showcase",
503-
},
504-
},
505-
} {
506-
t.Run(test.name, func(t *testing.T) {
507-
got := addLibraryRoots(test.library, test.source)
508-
if diff := cmp.Diff(test.want, got); diff != "" {
509-
t.Errorf("mismatch (-want +got):\n%s", diff)
510-
}
511-
})
512-
}
513-
}

internal/librarian/rust/codec.go

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func libraryToModelConfig(library *config.Library, ch *config.API, sources *side
3131
specFormat = library.SpecificationFormat
3232
}
3333

34-
src := addLibraryRoots(library, sources)
34+
src := sidekickconfig.NewSourceConfig(*sources, library.Roots)
3535
root := sources.Googleapis
3636
if ch.Path == "schema/google/showcase/v1beta1" {
3737
root = sources.Showcase
@@ -222,10 +222,14 @@ func formatPackageDependency(dep *config.RustPackageDependency) string {
222222
}
223223

224224
func moduleToModelConfig(library *config.Library, module *config.RustModule, sources *sidekickconfig.Sources) (*parser.ModelConfig, error) {
225-
src := addLibraryRoots(library, sources)
225+
src := sidekickconfig.NewSourceConfig(*sources, library.Roots)
226226
var title string
227-
if module.APIPath != "" && src["roots"] == "googleapis" {
228-
api, err := serviceconfig.Find(sources.Googleapis, module.APIPath, config.LanguageRust)
227+
if module.APIPath != "" && len(src.ActiveRoots) == 1 && src.ActiveRoots[0] == "googleapis" {
228+
root := sources.Googleapis
229+
if module.APIPath == "schema/google/showcase/v1beta1" {
230+
root = sources.Showcase
231+
}
232+
api, err := serviceconfig.Find(root, module.APIPath, config.LanguageRust)
229233
if err != nil {
230234
return nil, fmt.Errorf("failed to find service config for %q: %w", module.APIPath, err)
231235
}
@@ -243,7 +247,7 @@ func moduleToModelConfig(library *config.Library, module *config.RustModule, sou
243247
specificationFormat = module.SpecificationFormat
244248
}
245249
if module.IncludeList != "" {
246-
src["include-list"] = module.IncludeList
250+
src.IncludeList = strings.Split(module.IncludeList, ",")
247251
}
248252
modelCfg := &parser.ModelConfig{
249253
Language: language,
@@ -323,36 +327,3 @@ func buildModuleCodec(library *config.Library, module *config.RustModule) map[st
323327
}
324328
return codec
325329
}
326-
327-
// TODO(https://github.com/googleapis/librarian/issues/3863): remove this function once we removed sidekick config.
328-
func addLibraryRoots(library *config.Library, sources *sidekickconfig.Sources) map[string]string {
329-
src := make(map[string]string)
330-
if library.Rust == nil {
331-
library.Rust = &config.RustCrate{}
332-
}
333-
334-
if len(library.Roots) == 0 && sources.Googleapis != "" {
335-
// Default to googleapis if no roots are specified.
336-
src["googleapis-root"] = sources.Googleapis
337-
src["roots"] = "googleapis"
338-
} else {
339-
src["roots"] = strings.Join(library.Roots, ",")
340-
rootMap := map[string]struct {
341-
path string
342-
key string
343-
}{
344-
"googleapis": {path: sources.Googleapis, key: "googleapis-root"},
345-
"discovery": {path: sources.Discovery, key: "discovery-root"},
346-
"showcase": {path: sources.Showcase, key: "showcase-root"},
347-
"protobuf-src": {path: sources.ProtobufSrc, key: "protobuf-src-root"},
348-
"conformance": {path: sources.Conformance, key: "conformance-root"},
349-
}
350-
for _, root := range library.Roots {
351-
if r, ok := rootMap[root]; ok && r.path != "" {
352-
src[r.key] = r.path
353-
}
354-
}
355-
}
356-
357-
return src
358-
}

0 commit comments

Comments
 (0)