Skip to content

Commit dc2af10

Browse files
authored
refactor(config): migrate language string literals to a new config.Language type with defined constants (#4287)
Migrate language string literals to a new `config.Language` type with defined constants. Fixes #4010
1 parent 584600d commit dc2af10

43 files changed

Lines changed: 251 additions & 244 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

internal/config/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestRead(t *testing.T) {
2828
t.Fatal(err)
2929
}
3030
want := &Config{
31-
Language: "rust",
31+
Language: LanguageRust,
3232
Sources: &Sources{
3333
Discovery: &Source{
3434
Commit: "b27c80574e918a7e2a36eb21864d1d2e45b8c032",

internal/config/language.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,38 @@
1414

1515
package config
1616

17-
import "github.com/googleapis/librarian/internal/yaml"
17+
import (
18+
"github.com/googleapis/librarian/internal/yaml"
19+
)
20+
21+
const (
22+
// LanguageUnknown represents an unsupported or unspecified language.
23+
LanguageUnknown = "unknown"
24+
// LanguageAll is the identifier for all languages.
25+
LanguageAll = "all"
26+
// LanguageCsharp is the language identifier for C#.
27+
LanguageCsharp = "csharp"
28+
// LanguageDart is the language identifier for Dart.
29+
LanguageDart = "dart"
30+
// LanguageFake is the language identifier for Fakes.
31+
LanguageFake = "fake"
32+
// LanguageGo is the language identifier for Go.
33+
LanguageGo = "go"
34+
// LanguageJava is the language identifier for Java.
35+
LanguageJava = "java"
36+
// LanguageNodejs is the language identifier for Node.js.
37+
LanguageNodejs = "nodejs"
38+
// LanguagePhp is the language identifier for PHP.
39+
LanguagePhp = "php"
40+
// LanguagePython is the language identifier for Python.
41+
LanguagePython = "python"
42+
// LanguageRuby is the language identifier for Ruby.
43+
LanguageRuby = "ruby"
44+
// LanguageRust is the language identifier for Rust.
45+
LanguageRust = "rust"
46+
// LanguageRustStorage is a variation of the Rust generator for storage.
47+
LanguageRustStorage = "rust_storage"
48+
)
1849

1950
// GoModule represents the Go-specific configuration for a library.
2051
type GoModule struct {

internal/docuploader/metadata.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ type DocUploaderMetadata struct {
4444
// https://github.com/googleapis/nodejs-storage/issues
4545
IssueTracker string `json:"issueTracker,omitempty"`
4646

47-
// Language is the (programming) language. While this isn't enforced with an
48-
// enum, you should use one of the following strings: python, nodejs, ruby,
49-
// dotnet, java, go, php, cpp. In some cases you might need to use something
50-
// else, consult the docs team for guidance.
47+
// Language is the (programming) language.
5148
Language string `json:"language,omitempty"`
5249

5350
// Name is the product/API name. This *should* match the DNS name of the API

internal/docuploader/metadata_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"time"
2020

2121
"github.com/google/go-cmp/cmp"
22+
"github.com/googleapis/librarian/internal/config"
2223
"github.com/googleapis/librarian/internal/repometadata"
2324
)
2425

@@ -38,7 +39,7 @@ func TestFromRepoMetadata(t *testing.T) {
3839
DefaultVersion: "default-version-ignored",
3940
DistributionName: "distribution-name",
4041
IssueTracker: "issue-tracker",
41-
Language: "language",
42+
Language: config.LanguageGo,
4243
LibraryType: "library-type-ignored",
4344
Name: "name",
4445
NamePretty: "name-pretty-ignored",
@@ -50,7 +51,7 @@ func TestFromRepoMetadata(t *testing.T) {
5051
DistributionName: "distribution-name",
5152
GithubRepository: "repo",
5253
IssueTracker: "issue-tracker",
53-
Language: "language",
54+
Language: config.LanguageGo,
5455
Name: "name",
5556
ProductPage: "product-documentation",
5657
},

internal/librarian/add.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func runAdd(ctx context.Context, cfg *config.Config, apis ...string) error {
7070

7171
func resolveDependencies(ctx context.Context, cfg *config.Config, name string) (*config.Config, error) {
7272
switch cfg.Language {
73-
case languageRust:
73+
case config.LanguageRust:
7474
lib, err := FindLibrary(cfg, name)
7575
if err != nil {
7676
return nil, err
@@ -87,15 +87,15 @@ func resolveDependencies(ctx context.Context, cfg *config.Config, name string) (
8787

8888
// deriveLibraryName derives a library name from an API path.
8989
// The derivation is language-specific.
90-
func deriveLibraryName(language, api string) string {
90+
func deriveLibraryName(language string, api string) string {
9191
switch language {
92-
case languageDart:
92+
case config.LanguageDart:
9393
return dart.DefaultLibraryName(api)
94-
case languageFake:
94+
case config.LanguageFake:
9595
return fakeDefaultLibraryName(api)
96-
case languagePython:
96+
case config.LanguagePython:
9797
return python.DefaultLibraryName(api)
98-
case languageRust:
98+
case config.LanguageRust:
9999
return rust.DefaultLibraryName(api)
100100
default:
101101
return strings.ReplaceAll(api, "/", "-")

internal/librarian/add_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,15 @@ func TestDeriveLibraryName(t *testing.T) {
287287
apiPath string
288288
want string
289289
}{
290-
{"dart", "google/cloud/secretmanager/v1", "google_cloud_secretmanager_v1"},
291-
{"python", "google/cloud/secretmanager/v1", "google-cloud-secretmanager"},
292-
{"python", "google/cloud/secretmanager/v1beta2", "google-cloud-secretmanager"},
293-
{"python", "google/cloud/storage/v2alpha", "google-cloud-storage"},
294-
{"python", "google/maps/addressvalidation/v1", "google-maps-addressvalidation"},
295-
{"python", "google/api/v1", "google-api"},
296-
{"rust", "google/cloud/secretmanager/v1", "google-cloud-secretmanager-v1"},
297-
{"rust", "google/cloud/secretmanager/v1beta2", "google-cloud-secretmanager-v1beta2"},
298-
{"fake", "google/cloud/secretmanager/v1", "google-cloud-secretmanager-v1"},
290+
{config.LanguageDart, "google/cloud/secretmanager/v1", "google_cloud_secretmanager_v1"},
291+
{config.LanguagePython, "google/cloud/secretmanager/v1", "google-cloud-secretmanager"},
292+
{config.LanguagePython, "google/cloud/secretmanager/v1beta2", "google-cloud-secretmanager"},
293+
{config.LanguagePython, "google/cloud/storage/v2alpha", "google-cloud-storage"},
294+
{config.LanguagePython, "google/maps/addressvalidation/v1", "google-maps-addressvalidation"},
295+
{config.LanguagePython, "google/api/v1", "google-api"},
296+
{config.LanguageRust, "google/cloud/secretmanager/v1", "google-cloud-secretmanager-v1"},
297+
{config.LanguageRust, "google/cloud/secretmanager/v1beta2", "google-cloud-secretmanager-v1beta2"},
298+
{config.LanguageFake, "google/cloud/secretmanager/v1", "google-cloud-secretmanager-v1"},
299299
} {
300300
t.Run(test.language+"/"+test.apiPath, func(t *testing.T) {
301301
got := deriveLibraryName(test.language, test.apiPath)

internal/librarian/bump.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var (
4949
// this should be removed. If a language does not have specific needs, a
5050
// default [semver.DeriveNextOptions] is returned for default semantics.
5151
languageVersioningOptions = map[string]semver.DeriveNextOptions{
52-
"rust": {
52+
config.LanguageRust: {
5353
BumpVersionCore: true,
5454
DowngradePreGAChanges: true,
5555
},
@@ -112,7 +112,7 @@ func runBump(ctx context.Context, cfg *config.Config, all bool, libraryName, ver
112112
if err := git.AssertGitStatusClean(ctx, gitExe); err != nil {
113113
return err
114114
}
115-
if cfg.Language == languageRust {
115+
if cfg.Language == config.LanguageRust {
116116
return legacyRustBump(ctx, cfg, all, libraryName, versionOverride, gitExe)
117117
}
118118

@@ -197,11 +197,11 @@ func bumpLibrary(ctx context.Context, cfg *config.Config, lib *config.Library, g
197197
lib.Version = version
198198

199199
switch cfg.Language {
200-
case languageFake:
200+
case config.LanguageFake:
201201
return fakeBumpLibrary(output, version)
202-
case languageGo:
202+
case config.LanguageGo:
203203
return golang.Bump(lib, output, version)
204-
case languagePython:
204+
case config.LanguagePython:
205205
return python.Bump(output, version)
206206
default:
207207
return fmt.Errorf("%q does not support bump", cfg.Language)
@@ -211,7 +211,7 @@ func bumpLibrary(ctx context.Context, cfg *config.Config, lib *config.Library, g
211211
// postBump performs post version bump cleanup and maintenance tasks after libraries have been processed.
212212
func postBump(ctx context.Context, cfg *config.Config) error {
213213
switch cfg.Language {
214-
case languageRust:
214+
case config.LanguageRust:
215215
cargoExe := "cargo"
216216
if cfg.Release != nil {
217217
cargoExe = command.GetExecutablePath(cfg.Release.Preinstalled, "cargo")
@@ -420,9 +420,9 @@ func legacyRustBumpLibrary(ctx context.Context, cfg *config.Config, lib *config.
420420
}
421421
output := libraryOutput(cfg.Language, lib, cfg.Default)
422422
switch cfg.Language {
423-
case languageRust:
423+
case config.LanguageRust:
424424
return rust.Bump(ctx, lib, output, version, gitExe, lastTag)
425-
case languageFake:
425+
case config.LanguageFake:
426426
lib.Version = version
427427
return fakeBumpLibrary(output, version)
428428
default:

internal/librarian/bump_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ func TestBumpLibrary_Error(t *testing.T) {
379379
name: "unsupported language",
380380
cfg: func() *config.Config {
381381
c := sample.Config()
382-
c.Language = languageRust
382+
c.Language = config.LanguageRust
383383
return c
384384
}(),
385385
versionOverride: "2.0.0",
@@ -603,7 +603,7 @@ func TestPostBump(t *testing.T) {
603603
}
604604
},
605605
cfg: &config.Config{
606-
Language: languageRust,
606+
Language: config.LanguageRust,
607607
Release: &config.Release{
608608
Preinstalled: map[string]string{
609609
"cargo": fakeCargo,
@@ -620,7 +620,7 @@ func TestPostBump(t *testing.T) {
620620
}
621621
},
622622
cfg: &config.Config{
623-
Language: languageRust,
623+
Language: config.LanguageRust,
624624
Release: &config.Release{
625625
Preinstalled: map[string]string{
626626
"cargo": fakeCargo,
@@ -632,7 +632,7 @@ func TestPostBump(t *testing.T) {
632632
{
633633
name: "non-rust language does nothing",
634634
cfg: &config.Config{
635-
Language: languageFake,
635+
Language: config.LanguageFake,
636636
},
637637
},
638638
} {
@@ -662,21 +662,21 @@ func TestDeriveNextVersion(t *testing.T) {
662662
name: "rust library next non-GA version",
663663
cfg: func() *config.Config {
664664
c := sample.Config()
665-
c.Language = languageRust
665+
c.Language = config.LanguageRust
666666
c.Libraries[0].Version = sample.RustNonGAVersion
667667
return c
668668
}(),
669-
versionOpts: languageVersioningOptions[languageRust],
669+
versionOpts: languageVersioningOptions[config.LanguageRust],
670670
wantVersion: sample.RustNextNonGAVersion,
671671
},
672672
{
673673
name: "rust library next GA version",
674674
cfg: func() *config.Config {
675675
c := sample.Config()
676-
c.Language = languageRust
676+
c.Language = config.LanguageRust
677677
return c
678678
}(),
679-
versionOpts: languageVersioningOptions[languageRust],
679+
versionOpts: languageVersioningOptions[config.LanguageRust],
680680
wantVersion: sample.NextVersion,
681681
},
682682
{

internal/librarian/dart/codec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func toModelConfig(library *config.Library, ch *config.API, sources *sidekickcon
4242
if ch.Path == "schema/google/showcase/v1beta1" {
4343
root = sources.Showcase
4444
}
45-
svcConfig, err := serviceconfig.Find(root, ch.Path, serviceconfig.LangDart)
45+
svcConfig, err := serviceconfig.Find(root, ch.Path, config.LanguageDart)
4646
if err != nil {
4747
return nil, err
4848
}

internal/librarian/fake_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestGenerateLibraries(t *testing.T) {
3333
Output: outputDir,
3434
}
3535
cfg := &config.Config{
36-
Language: languageFake,
36+
Language: config.LanguageFake,
3737
}
3838

3939
tmpDir := t.TempDir()

0 commit comments

Comments
 (0)