Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
100 changes: 100 additions & 0 deletions internal/librariangen/protoc/protoc.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this rewriting part of generate_library.sh in go? If yes, there are some special cases need to be handled. cc: @JoeWang1127

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just scaffolding for now. Yes, we'll be rewriting the needed logic in Go.

Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package protoc

import (
"fmt"
"os"
"path/filepath"
"strings"

)

// ConfigProvider is an interface that describes the configuration needed
// by the Build function. This allows the protoc package to be decoupled
// from the source of the configuration (e.g., Bazel files, JSON, etc.).
type ConfigProvider interface {
ServiceYAML() string
GapicYAML() string
GRPCServiceConfig() string
Transport() string
HasRESTNumericEnums() bool
HasGAPIC() bool
}

// Build constructs the full protoc command arguments for a given API.
func Build(apiServiceDir string, config ConfigProvider, sourceDir, outputDir string) ([]string, error) {
// Gather all .proto files in the API's source directory.
entries, err := os.ReadDir(apiServiceDir)
if err != nil {
return nil, fmt.Errorf("librariangen: failed to read API source directory %s: %w", apiServiceDir, err)
}

var protoFiles []string
for _, entry := range entries {
if !entry.IsDir() && filepath.Ext(entry.Name()) == ".proto" {
protoFiles = append(protoFiles, filepath.Join(apiServiceDir, entry.Name()))
}
}

if len(protoFiles) == 0 {
return nil, fmt.Errorf("librariangen: no .proto files found in %s", apiServiceDir)
}

// Construct the protoc command arguments.
var gapicOpts []string
if config.HasGAPIC() {
if config.ServiceYAML() != "" {
gapicOpts = append(gapicOpts, fmt.Sprintf("api-service-config=%s", filepath.Join(apiServiceDir, config.ServiceYAML())))
}
if config.GapicYAML() != "" {
gapicOpts = append(gapicOpts, fmt.Sprintf("gapic-config=%s", filepath.Join(apiServiceDir, config.GapicYAML())))
}
if config.GRPCServiceConfig() != "" {
gapicOpts = append(gapicOpts, fmt.Sprintf("grpc-service-config=%s", filepath.Join(apiServiceDir, config.GRPCServiceConfig())))
}
if config.Transport() != "" {
gapicOpts = append(gapicOpts, fmt.Sprintf("transport=%s", config.Transport()))
}
if config.HasRESTNumericEnums() {
gapicOpts = append(gapicOpts, "rest-numeric-enums")
}
}
Comment on lines +58 to +74

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block has some repetition when constructing gapicOpts. You can refactor this to be more data-driven for the file-based options, which improves readability and makes it easier to add more options in the future.

Suggested change
if config.HasGAPIC() {
if config.ServiceYAML() != "" {
gapicOpts = append(gapicOpts, fmt.Sprintf("api-service-config=%s", filepath.Join(apiServiceDir, config.ServiceYAML())))
}
if config.GapicYAML() != "" {
gapicOpts = append(gapicOpts, fmt.Sprintf("gapic-config=%s", filepath.Join(apiServiceDir, config.GapicYAML())))
}
if config.GRPCServiceConfig() != "" {
gapicOpts = append(gapicOpts, fmt.Sprintf("grpc-service-config=%s", filepath.Join(apiServiceDir, config.GRPCServiceConfig())))
}
if config.Transport() != "" {
gapicOpts = append(gapicOpts, fmt.Sprintf("transport=%s", config.Transport()))
}
if config.HasRESTNumericEnums() {
gapicOpts = append(gapicOpts, "rest-numeric-enums")
}
}
if config.HasGAPIC() {
fileOpts := []struct{ key, val string }{
{"api-service-config", config.ServiceYAML()},
{"gapic-config", config.GapicYAML()},
{"grpc-service-config", config.GRPCServiceConfig()},
}
for _, opt := range fileOpts {
if opt.val != "" {
gapicOpts = append(gapicOpts, fmt.Sprintf("%s=%s", opt.key, filepath.Join(apiServiceDir, opt.val)))
}
}
if config.Transport() != "" {
gapicOpts = append(gapicOpts, fmt.Sprintf("transport=%s", config.Transport()))
}
if config.HasRESTNumericEnums() {
gapicOpts = append(gapicOpts, "rest-numeric-enums")
}
}


args := []string{
"protoc",
"--experimental_allow_proto3_optional",
}

args = append(args, fmt.Sprintf("--java_out=%s", outputDir))
if config.HasGAPIC() {
args = append(args, fmt.Sprintf("--java_gapic_out=metadata:%s", filepath.Join(outputDir, "java_gapic.zip")))

if len(gapicOpts) > 0 {
args = append(args, "--java_gapic_opt="+strings.Join(gapicOpts, ","))
}
}

args = append(args,
// The -I flag specifies the import path for protoc. All protos
// and their dependencies must be findable from this path.
// The /source mount contains the complete googleapis repository.
"-I="+sourceDir,
)

args = append(args, protoFiles...)

return args, nil
}
137 changes: 137 additions & 0 deletions internal/librariangen/protoc/protoc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package protoc

import (
"path/filepath"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

// mockConfigProvider is a mock implementation of the ConfigProvider interface for testing.
type mockConfigProvider struct {
serviceYAML string
gapicYAML string
grpcServiceConfig string
transport string
restNumericEnums bool
hasGAPIC bool
}

func (m *mockConfigProvider) ServiceYAML() string { return m.serviceYAML }
func (m *mockConfigProvider) GapicYAML() string { return m.gapicYAML }
func (m *mockConfigProvider) GRPCServiceConfig() string { return m.grpcServiceConfig }
func (m *mockConfigProvider) Transport() string { return m.transport }
func (m *mockConfigProvider) HasRESTNumericEnums() bool { return m.restNumericEnums }
func (m *mockConfigProvider) HasGAPIC() bool { return m.hasGAPIC }

func TestBuild(t *testing.T) {
// The testdata directory is a curated version of a valid protoc
// import path that contains all the necessary proto definitions.
sourceDir, err := filepath.Abs("../testdata/generate/source")
if err != nil {
t.Fatalf("failed to get absolute path for sourceDir: %v", err)
}
tests := []struct {
name string
apiPath string
config mockConfigProvider
want []string
}{
{
name: "java_grpc_library rule",
apiPath: "google/cloud/workflows/v1",
config: mockConfigProvider{
transport: "grpc",
grpcServiceConfig: "workflows_grpc_service_config.json",
gapicYAML: "workflows_gapic.yaml",
serviceYAML: "workflows_v1.yaml",
restNumericEnums: true,
hasGAPIC: true,
},
want: []string{
"protoc",
"--experimental_allow_proto3_optional",
"--java_out=/output",
"--java_gapic_out=metadata:/output/java_gapic.zip",
"--java_gapic_opt=" + strings.Join([]string{
"api-service-config=" + filepath.Join(sourceDir, "google/cloud/workflows/v1/workflows_v1.yaml"),
"gapic-config=" + filepath.Join(sourceDir, "google/cloud/workflows/v1/workflows_gapic.yaml"),
"grpc-service-config=" + filepath.Join(sourceDir, "google/cloud/workflows/v1/workflows_grpc_service_config.json"),
"transport=grpc",
"rest-numeric-enums",
}, ","),
"-I=" + sourceDir,
filepath.Join(sourceDir, "google/cloud/workflows/v1/workflows.proto"),
},
},
{
name: "java_proto_library rule with legacy gRPC",
apiPath: "google/cloud/secretmanager/v1beta2",
config: mockConfigProvider{
transport: "grpc",
grpcServiceConfig: "secretmanager_grpc_service_config.json",
serviceYAML: "secretmanager_v1beta2.yaml",
restNumericEnums: true,
hasGAPIC: true,
},
want: []string{
"protoc",
"--experimental_allow_proto3_optional",
"--java_out=/output",
"--java_gapic_out=metadata:/output/java_gapic.zip",
"--java_gapic_opt=" + strings.Join([]string{
"api-service-config=" + filepath.Join(sourceDir, "google/cloud/secretmanager/v1beta2/secretmanager_v1beta2.yaml"),
"grpc-service-config=" + filepath.Join(sourceDir, "google/cloud/secretmanager/v1beta2/secretmanager_grpc_service_config.json"),
"transport=grpc",
"rest-numeric-enums",
}, ","),
"-I=" + sourceDir,
filepath.Join(sourceDir, "google/cloud/secretmanager/v1beta2/secretmanager.proto"),
},
},
{
// Note: we don't have a separate test directory with a proto-only library;
// the config is used to say "don't generate GAPIC".
name: "proto-only",
apiPath: "google/cloud/secretmanager/v1beta2",
config: mockConfigProvider{
hasGAPIC: false,
},
want: []string{
"protoc",
"--experimental_allow_proto3_optional",
"--java_out=/output",
"-I=" + sourceDir,
filepath.Join(sourceDir, "google/cloud/secretmanager/v1beta2/secretmanager.proto"),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Build(filepath.Join(sourceDir, tt.apiPath), &tt.config, sourceDir, "/output")
if err != nil {
t.Fatalf("Build() failed: %v", err)
}

if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("Build() mismatch (-want +got):\n%s", diff)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package google.api;

import "google/protobuf/descriptor.proto";

option java_multiple_files = true;
option java_outer_classname = "AnnotationsProto";
option java_package = "com.google.api";

extend google.protobuf.MethodOptions {
HttpRule http = 72295728;
}

message HttpRule {
string selector = 1;
oneof pattern {
string get = 2;
string put = 3;
string post = 4;
string delete = 5;
string patch = 6;
CustomHttpPattern custom = 8;
}
string body = 7;
repeated HttpRule additional_bindings = 11;
}

message CustomHttpPattern {
string kind = 1;
string path = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
java_proto_library(
name = "secretmanager_java_proto",
deps = [":secretmanager_proto"],
)

java_grpc_library(
name = "secretmanager_java_grpc",
srcs = [":secretmanager_proto"],
deps = [":secretmanager_java_proto"],
)

java_gapic_library(
name = "secretmanager_java_gapic",
srcs = [":secretmanager_proto_with_info"],
gapic_yaml = None,
grpc_service_config = "secretmanager_grpc_service_config.json",
rest_numeric_enums = True,
service_yaml = "secretmanager_v1beta2.yaml",
test_deps = [
"//google/cloud/location:location_java_grpc",
"//google/iam/v1:iam_java_grpc",
":secretmanager_java_grpc",
],
transport = "grpc+rest",
deps = [
":secretmanager_java_proto",
"//google/api:api_java_proto",
"//google/cloud/location:location_java_proto",
"//google/iam/v1:iam_java_proto",
],
)

java_gapic_test(
name = "secretmanager_java_gapic_test_suite",
test_classes = [
"com.google.cloud.secretmanager.v1beta2.SecretManagerServiceClientHttpJsonTest",
"com.google.cloud.secretmanager.v1beta2.SecretManagerServiceClientTest",
],
runtime_deps = [":secretmanager_java_gapic_test"],
)

# Open Source Packages
java_gapic_assembly_gradle_pkg(
name = "google-cloud-secretmanager-v1beta2-java",
transport = "grpc+rest",
deps = [
":secretmanager_java_gapic",
":secretmanager_java_grpc",
":secretmanager_java_proto",
":secretmanager_proto",
],
include_samples = True,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package google.cloud.secretmanager.v1beta2;

import "google/api/annotations.proto";

option java_multiple_files = true;
option java_outer_classname = "ServiceProto";
option java_package = "com.google.cloud.secretmanager.v1beta2";

// A Secret is a secret value.
message Secret {
string name = 1;
}

// Request for the `ListSecrets` method.
message ListSecretsRequest {
string parent = 1;
}

// Response for the `ListSecrets` method.
message ListSecretsResponse {
repeated Secret Secrets = 1;
}

// Service for managing secrets.
service Secrets {
// Lists Secrets in a given project and location.
rpc ListSecrets(ListSecretsRequest) returns (ListSecretsResponse) {
option (google.api.http) = {
get: "/v1/{parent=projects/*/locations/*}/Secrets"
};
}
}
Loading
Loading