Skip to content

Commit 42d78d2

Browse files
authored
refactor(internal/sources): move Sources types out of sidekick/config (#4725)
The Sources and SourceConfig types in internal/sidekick/config represent resolved directory paths for source repositories. However, sources is not a sidekick-specific concept. While sidekick language generators (e.g. rust, dart) currently use this more heavily than others, the concept itself should be generic and usable across the codebase. Move both types into a new internal/sources package as a first step toward decoupling and preventing circular dependencies. Additionally, change Sources and SourceConfig to be passed by reference consistently throughout the codebase. For #4721
1 parent 8452b73 commit 42d78d2

32 files changed

Lines changed: 419 additions & 329 deletions

internal/librarian/dart/codec.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@ import (
2121
"github.com/googleapis/librarian/internal/config"
2222
"github.com/googleapis/librarian/internal/serviceconfig"
2323
"github.com/googleapis/librarian/internal/sidekick/api"
24-
sidekickconfig "github.com/googleapis/librarian/internal/sidekick/config"
2524
"github.com/googleapis/librarian/internal/sidekick/parser"
25+
"github.com/googleapis/librarian/internal/sources"
2626
)
2727

2828
var errInvalidSpecificationFormat = errors.New("dart generation requires protobuf specification format")
2929

30-
func toModelConfig(library *config.Library, ch *config.API, sources *sidekickconfig.Sources) (*parser.ModelConfig, error) {
30+
func toModelConfig(library *config.Library, ch *config.API, srcs *sources.Sources) (*parser.ModelConfig, error) {
3131
if library.SpecificationFormat != "" && library.SpecificationFormat != config.SpecProtobuf {
3232
return nil, fmt.Errorf("%w, got %q", errInvalidSpecificationFormat, library.SpecificationFormat)
3333
}
3434

35-
src := sidekickconfig.NewSourceConfig(*sources, library.Roots)
35+
src := sources.NewSourceConfig(srcs, library.Roots)
3636

3737
if library.Dart != nil && library.Dart.IncludeList != nil {
3838
src.IncludeList = library.Dart.IncludeList
3939
}
40-
root := sources.Googleapis
40+
root := srcs.Googleapis
4141
if ch.Path == "schema/google/showcase/v1beta1" {
42-
root = sources.Showcase
42+
root = srcs.Showcase
4343
src.ActiveRoots = append(src.ActiveRoots, "showcase")
4444
}
4545
svcConfig, err := serviceconfig.Find(root, ch.Path, config.LanguageDart)

internal/librarian/dart/codec_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"github.com/google/go-cmp/cmp"
2424
"github.com/googleapis/librarian/internal/config"
2525
"github.com/googleapis/librarian/internal/sidekick/api"
26-
sidekickconfig "github.com/googleapis/librarian/internal/sidekick/config"
2726
"github.com/googleapis/librarian/internal/sidekick/parser"
27+
"github.com/googleapis/librarian/internal/sources"
2828
)
2929

3030
func TestBuildCodec(t *testing.T) {
@@ -312,8 +312,8 @@ func TestToModelConfig(t *testing.T) {
312312
want: &parser.ModelConfig{
313313
SpecificationFormat: config.SpecProtobuf,
314314
SpecificationSource: "google/cloud/functions/v2",
315-
Source: sidekickconfig.SourceConfig{
316-
Sources: sidekickconfig.Sources{
315+
Source: &sources.SourceConfig{
316+
Sources: &sources.Sources{
317317
Googleapis: googleapisDir,
318318
},
319319
ActiveRoots: []string{"googleapis"},
@@ -334,8 +334,8 @@ func TestToModelConfig(t *testing.T) {
334334
want: &parser.ModelConfig{
335335
SpecificationFormat: config.SpecProtobuf,
336336
SpecificationSource: "google/cloud/functions/v2",
337-
Source: sidekickconfig.SourceConfig{
338-
Sources: sidekickconfig.Sources{
337+
Source: &sources.SourceConfig{
338+
Sources: &sources.Sources{
339339
Googleapis: googleapisDir,
340340
},
341341
ActiveRoots: []string{"googleapis"},
@@ -360,8 +360,8 @@ func TestToModelConfig(t *testing.T) {
360360
want: &parser.ModelConfig{
361361
SpecificationFormat: config.SpecProtobuf,
362362
SpecificationSource: "google/cloud/functions/v2",
363-
Source: sidekickconfig.SourceConfig{
364-
Sources: sidekickconfig.Sources{
363+
Source: &sources.SourceConfig{
364+
Sources: &sources.Sources{
365365
Googleapis: googleapisDir,
366366
},
367367
ActiveRoots: []string{"googleapis"},
@@ -406,8 +406,8 @@ func TestToModelConfig(t *testing.T) {
406406
want: &parser.ModelConfig{
407407
SpecificationFormat: config.SpecProtobuf,
408408
SpecificationSource: "google/cloud/functions/v2",
409-
Source: sidekickconfig.SourceConfig{
410-
Sources: sidekickconfig.Sources{
409+
Source: &sources.SourceConfig{
410+
Sources: &sources.Sources{
411411
Googleapis: googleapisDir,
412412
},
413413
ActiveRoots: []string{"googleapis"},
@@ -445,8 +445,8 @@ func TestToModelConfig(t *testing.T) {
445445
want: &parser.ModelConfig{
446446
SpecificationFormat: config.SpecProtobuf,
447447
SpecificationSource: "google/cloud/functions/v2",
448-
Source: sidekickconfig.SourceConfig{
449-
Sources: sidekickconfig.Sources{
448+
Source: &sources.SourceConfig{
449+
Sources: &sources.Sources{
450450
Googleapis: googleapisDir,
451451
},
452452
ActiveRoots: []string{"googleapis"},
@@ -469,8 +469,8 @@ func TestToModelConfig(t *testing.T) {
469469
want: &parser.ModelConfig{
470470
SpecificationFormat: config.SpecProtobuf,
471471
SpecificationSource: "google/cloud/functions/v2",
472-
Source: sidekickconfig.SourceConfig{
473-
Sources: sidekickconfig.Sources{
472+
Source: &sources.SourceConfig{
473+
Sources: &sources.Sources{
474474
Googleapis: googleapisDir,
475475
},
476476
ActiveRoots: []string{"googleapis"},
@@ -492,8 +492,8 @@ func TestToModelConfig(t *testing.T) {
492492
SpecificationFormat: config.SpecProtobuf,
493493
SpecificationSource: "schema/google/showcase/v1beta1",
494494
ServiceConfig: "schema/google/showcase/v1beta1/showcase_v1beta1.yaml",
495-
Source: sidekickconfig.SourceConfig{
496-
Sources: sidekickconfig.Sources{
495+
Source: &sources.SourceConfig{
496+
Sources: &sources.Sources{
497497
Googleapis: googleapisDir,
498498
Showcase: showcaseDir,
499499
},
@@ -517,8 +517,8 @@ func TestToModelConfig(t *testing.T) {
517517
want: &parser.ModelConfig{
518518
SpecificationFormat: config.SpecProtobuf,
519519
SpecificationSource: "google/cloud/functions/v2",
520-
Source: sidekickconfig.SourceConfig{
521-
Sources: sidekickconfig.Sources{
520+
Source: &sources.SourceConfig{
521+
Sources: &sources.Sources{
522522
Googleapis: googleapisDir,
523523
},
524524
ActiveRoots: []string{"googleapis", "extra-root"},
@@ -540,7 +540,7 @@ func TestToModelConfig(t *testing.T) {
540540
},
541541
} {
542542
t.Run(test.name, func(t *testing.T) {
543-
sources := &sidekickconfig.Sources{
543+
sources := &sources.Sources{
544544
Googleapis: test.googleapisDir,
545545
}
546546
if test.showcaseDir != "" {

internal/librarian/dart/generate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ import (
2323

2424
"github.com/googleapis/librarian/internal/command"
2525
"github.com/googleapis/librarian/internal/config"
26-
sidekickconfig "github.com/googleapis/librarian/internal/sidekick/config"
2726
sidekickdart "github.com/googleapis/librarian/internal/sidekick/dart"
2827
"github.com/googleapis/librarian/internal/sidekick/parser"
28+
"github.com/googleapis/librarian/internal/sources"
2929
)
3030

3131
// Generate generates a Dart client library.
32-
func Generate(ctx context.Context, library *config.Library, sources *sidekickconfig.Sources) error {
32+
func Generate(ctx context.Context, library *config.Library, sources *sources.Sources) error {
3333
modelConfig, err := toModelConfig(library, library.APIs[0], sources)
3434
if err != nil {
3535
return err

internal/librarian/dart/generate_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
"github.com/google/go-cmp/cmp"
2525
"github.com/googleapis/librarian/internal/config"
26-
sidekickconfig "github.com/googleapis/librarian/internal/sidekick/config"
26+
"github.com/googleapis/librarian/internal/sources"
2727
"github.com/googleapis/librarian/internal/testhelper"
2828
)
2929

@@ -67,7 +67,7 @@ func TestGenerate(t *testing.T) {
6767
for _, library := range libraries {
6868
library.Output = filepath.Join(outDir, "generated", library.Name)
6969
}
70-
sources := &sidekickconfig.Sources{
70+
sources := &sources.Sources{
7171
Googleapis: googleapisDir,
7272
}
7373
for _, library := range libraries {
@@ -102,7 +102,7 @@ func TestGenerate_Error(t *testing.T) {
102102
APIs: []*config.API{{Path: "broken"}},
103103
},
104104
}
105-
sources := &sidekickconfig.Sources{
105+
sources := &sources.Sources{
106106
Googleapis: googleapisDir,
107107
}
108108
gotErr := Generate(t.Context(), libraries[0], sources)
@@ -149,7 +149,7 @@ func TestGenerateLibrary(t *testing.T) {
149149
},
150150
},
151151
}
152-
sources := &sidekickconfig.Sources{
152+
sources := &sources.Sources{
153153
Googleapis: googleapisDir,
154154
}
155155
if err := Generate(t.Context(), library, sources); err != nil {

internal/librarian/generate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"github.com/googleapis/librarian/internal/librarian/nodejs"
2828
"github.com/googleapis/librarian/internal/librarian/python"
2929
"github.com/googleapis/librarian/internal/librarian/rust"
30-
sidekickconfig "github.com/googleapis/librarian/internal/sidekick/config"
30+
"github.com/googleapis/librarian/internal/sources"
3131
"github.com/googleapis/librarian/internal/yaml"
3232
"github.com/urfave/cli/v3"
3333
"golang.org/x/sync/errgroup"
@@ -145,7 +145,7 @@ func cleanLibraries(language string, libraries []*config.Library) error {
145145
// generateLibraries generates and formats all the given libraries,
146146
// delegating to language-specific code. Each language chooses its own
147147
// concurrency strategy for these two steps.
148-
func generateLibraries(ctx context.Context, cfg *config.Config, libraries []*config.Library, src *sidekickconfig.Sources) error {
148+
func generateLibraries(ctx context.Context, cfg *config.Config, libraries []*config.Library, src *sources.Sources) error {
149149
switch cfg.Language {
150150
case config.LanguageDart:
151151
g, gctx := errgroup.WithContext(ctx)

internal/librarian/rust/codec.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ import (
2121
"github.com/googleapis/librarian/internal/config"
2222
"github.com/googleapis/librarian/internal/serviceconfig"
2323
"github.com/googleapis/librarian/internal/sidekick/api"
24-
sidekickconfig "github.com/googleapis/librarian/internal/sidekick/config"
2524
"github.com/googleapis/librarian/internal/sidekick/parser"
25+
"github.com/googleapis/librarian/internal/sources"
2626
)
2727

28-
func libraryToModelConfig(library *config.Library, ch *config.API, sources *sidekickconfig.Sources) (*parser.ModelConfig, error) {
28+
func libraryToModelConfig(library *config.Library, ch *config.API, srcs *sources.Sources) (*parser.ModelConfig, error) {
2929
specFormat := config.SpecProtobuf
3030
if library.SpecificationFormat != "" {
3131
specFormat = library.SpecificationFormat
3232
}
3333

34-
src := sidekickconfig.NewSourceConfig(*sources, library.Roots)
35-
root := sources.Googleapis
34+
src := sources.NewSourceConfig(srcs, library.Roots)
35+
root := srcs.Googleapis
3636
if ch.Path == "schema/google/showcase/v1beta1" {
37-
root = sources.Showcase
37+
root = srcs.Showcase
3838
}
3939
svcConfig, err := serviceconfig.Find(root, ch.Path, config.LanguageRust)
4040
if err != nil {
@@ -222,13 +222,13 @@ func formatPackageDependency(dep *config.RustPackageDependency) string {
222222
return strings.Join(parts, ",")
223223
}
224224

225-
func moduleToModelConfig(library *config.Library, module *config.RustModule, sources *sidekickconfig.Sources) (*parser.ModelConfig, error) {
226-
src := sidekickconfig.NewSourceConfig(*sources, library.Roots)
225+
func moduleToModelConfig(library *config.Library, module *config.RustModule, srcs *sources.Sources) (*parser.ModelConfig, error) {
226+
src := sources.NewSourceConfig(srcs, library.Roots)
227227
var title string
228228
if module.APIPath != "" && len(src.ActiveRoots) == 1 && src.ActiveRoots[0] == "googleapis" {
229-
root := sources.Googleapis
229+
root := srcs.Googleapis
230230
if module.APIPath == "schema/google/showcase/v1beta1" {
231-
root = sources.Showcase
231+
root = srcs.Showcase
232232
}
233233
api, err := serviceconfig.Find(root, module.APIPath, config.LanguageRust)
234234
if err != nil {

0 commit comments

Comments
 (0)