Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/config/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ type GoAPI struct {
// NoMetadata indicates whether to skip generating gapic_metadata.json.
// This is typically false.
NoMetadata bool `yaml:"no_metadata,omitempty"`
// NoSnippets indicates whether to skip generating snippets.
// This is typically false.
NoSnippets bool `yaml:"no_snippets,omitempty"`
// Path is the source path.
Path string `yaml:"path,omitempty"`
// ProtoOnly determines whether to generate a Proto-only client.
Expand Down
6 changes: 6 additions & 0 deletions internal/librarian/golang/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ func buildGAPICOpts(apiPath string, goAPI *config.GoAPI, googleapisDir string) (
if !goAPI.NoMetadata {
opts = append(opts, "metadata")
}
if goAPI.NoSnippets {
opts = append(opts, "omit-snippets")
}
if sc != nil && sc.HasRESTNumericEnums(config.LanguageGo) {
opts = append(opts, "rest-numeric-enums")
}
Expand Down Expand Up @@ -199,6 +202,9 @@ func moveAPIDirectory(library *config.Library, goAPI *config.GoAPI, outDir strin
// moveAndUpdateSnippets moves the generated snippets from the temporary location to their final
// destination and updates their library versions.
func moveAndUpdateSnippets(library *config.Library, goAPI *config.GoAPI, outDir string) error {
if goAPI.NoSnippets {
return nil
}
snippetDirPrefix := filepath.Join(outDir, "cloud.google.com", "go", "internal", "generated", "snippets")
snippetDest := findSnippetDirectory(library, goAPI, outDir)
if snippetDest == "" {
Expand Down
61 changes: 59 additions & 2 deletions internal/librarian/golang/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,26 @@ func TestBuildGAPICOpts(t *testing.T) {
"release-level=ga",
},
},
{
name: "no snippets",
apiPath: "google/cloud/gkehub/v1",
goAPI: &config.GoAPI{
ClientPackage: "gkehub",
ImportPath: "gkehub/apiv1",
NoSnippets: true,
Path: "google/cloud/gkehub/v1",
},
googleapisDir: googleapisDir,
want: []string{
"go-gapic-package=cloud.google.com/go/gkehub/apiv1;gkehub",
"metadata",
"omit-snippets",
"rest-numeric-enums",
"api-service-config=" + filepath.Join(googleapisDir, "google/cloud/gkehub/v1/gkehub_v1.yaml"),
"transport=grpc+rest",
"release-level=ga",
},
},
{
name: "generator features",
apiPath: "google/cloud/bigquery/v2",
Expand Down Expand Up @@ -802,6 +822,37 @@ func TestMoveGeneratedFiles(t *testing.T) {
return outDir, filepath.Join(outDir, "apiv1"), filepath.Join(repoRoot, "internal", "generated", "snippets", "lib", "apiv1"), lib
},
},
{
name: "no snippets",
setup: func(t *testing.T, tmpDir string) (string, string, string, *config.Library) {
repoRoot := filepath.Join(tmpDir, "repo")
outDir := filepath.Join(repoRoot, "lib")
srcDir := filepath.Join(outDir, "cloud.google.com", "go", "lib", "apiv1")
if err := os.MkdirAll(srcDir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(srcDir, "main.go"), []byte("package foo"), 0644); err != nil {
t.Fatal(err)
}
// Even if snippet source exists in cloud.google.com/go, it should not be moved.
snippetDirSuffix := filepath.Join("internal", "generated", "snippets", "lib", "apiv1")
snippetSrcDir := filepath.Join(outDir, "cloud.google.com", "go", snippetDirSuffix)
if err := os.MkdirAll(snippetSrcDir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(snippetSrcDir, "snippet.go"), []byte("package internal"), 0644); err != nil {
t.Fatal(err)
}
lib := &config.Library{
Name: "lib",
APIs: []*config.API{{Path: "lib/v1"}},
Go: &config.GoModule{
GoAPIs: []*config.GoAPI{{Path: "lib/v1", ImportPath: "lib/apiv1", NoSnippets: true}},
},
}
return outDir, filepath.Join(outDir, "apiv1"), filepath.Join(repoRoot, snippetDirSuffix), lib
},
},
} {
t.Run(test.name, func(t *testing.T) {
tmpDir := t.TempDir()
Expand All @@ -813,8 +864,14 @@ func TestMoveGeneratedFiles(t *testing.T) {
if _, err := os.Stat(filepath.Join(apiDir, "main.go")); err != nil {
t.Errorf("expected main.go to exist, got err: %v", err)
}
if _, err := os.Stat(filepath.Join(snippetDir, "snippet.go")); err != nil {
t.Errorf("expected snippet.go to exist, got err: %v", err)
if lib.Go.GoAPIs[0].NoSnippets {
if _, err := os.Stat(filepath.Join(snippetDir, "snippet.go")); !errors.Is(err, os.ErrNotExist) {
t.Errorf("expected snippet.go to not exist, got err: %v", err)
}
} else {
if _, err := os.Stat(filepath.Join(snippetDir, "snippet.go")); err != nil {
t.Errorf("expected snippet.go to exist, got err: %v", err)
}
}
})
}
Expand Down
1 change: 1 addition & 0 deletions internal/librarian/golang/tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func isEmptyAPI(goAPI *config.GoAPI) bool {
goAPI.ImportPath == "" &&
len(goAPI.NestedProtos) == 0 &&
!goAPI.NoMetadata &&
!goAPI.NoSnippets &&
!goAPI.ProtoOnly &&
goAPI.ProtoPackage == ""
}
Expand Down
6 changes: 6 additions & 0 deletions internal/librarian/golang/tidy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ func TestIsEmptyAPI(t *testing.T) {
NoMetadata: true,
},
},
{
name: "not empty with NoSnippets",
goAPI: &config.GoAPI{
NoSnippets: true,
},
},
{
name: "not empty with ProtoOnly",
goAPI: &config.GoAPI{
Expand Down
Loading