Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: search gapic additional protos in BUILD.bazel #2004

Merged
merged 9 commits into from Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions library_generation/README.md
Expand Up @@ -91,14 +91,30 @@ Use `--grpc_version` to specify the value.

Note that if specified, the version should be compatible with gapic-generator-java.

### contains_iam_policy (optional)
Whether to include `google/iam/v1/iam_policy.proto` into the library.
The value is either `true` or `false`.
The default value is `false`.

Use `--contains_iam_policy` to specify the value.

### contains_locations (optional)
Whether to include `google/cloud/location/locations.proto` into the library.
The value is either `true` or `false`.
The default value is `false`.

Use `--contains_locations` to specify the value.

Copy link
Member

Choose a reason for hiding this comment

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

I can see this list growing over time. Would it make sense to just have additional_protos parameter?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do you mean additional_protos as a string of protos?
For example:
additional_protos="google/iam/v1/iam_policy.proto google/cloud/location/locations.proto" or additional_protos="google/cloud/location/locations.proto"

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 to make it more generic. It would solve the problem for common shopping protos as well. I'm also curious why google/longrunning/operations.proto is not affected by this issue.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@blakeli0 post some findings in the issue that answers your question.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@blakeli0 @meltsufin I plan to name additional protos that pass to the generator gapic_additional_protos, WDYT?

So far I found google/cloud/location/locations.proto, google/iam/v1/iam_policy.proto and google/cloud/common_resources.proto may go to this list.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

### transport (optional)
One of GAPIC options passed to the generator. The value is either `grpc` or `grpc+rest`.
One of GAPIC options passed to the generator.
The value is either `grpc` or `grpc+rest`.
The default value is `grpc`.

Use `--transport` to specify the value.

### rest_numeric_enums (optional)
One of GAPIC options passed to the generator. The value is either `true` or `false`.
One of GAPIC options passed to the generator.
The value is either `true` or `false`.
The default value is `true`.

Use `--rest_numeric_enums` to specify the value.
Expand Down
18 changes: 17 additions & 1 deletion library_generation/generate_library.sh
Expand Up @@ -25,6 +25,14 @@ case $key in
protobuf_version="$2"
shift
;;
--contains_iam_policy)
contains_iam_policy="$2"
shift
;;
--contains_locations)
contains_locations="$2"
shift
;;
--grpc_version)
grpc_version="$2"
shift
Expand Down Expand Up @@ -66,6 +74,14 @@ if [ -z "${grpc_version}" ]; then
grpc_version=$(get_grpc_version "${gapic_generator_version}")
fi

if [ -z "${contains_iam_policy}" ];then
contains_iam_policy="false"
fi

if [ -z "${contains_locations}" ];then
contains_locations="false"
fi

if [ -z "${transport}" ]; then
transport="grpc"
fi
Expand Down Expand Up @@ -117,7 +133,7 @@ fi
"--plugin=protoc-gen-java_gapic=${script_dir}/gapic-generator-java-wrapper" \
"--java_gapic_out=metadata:${destination_path}/java_gapic_srcjar_raw.srcjar.zip" \
"--java_gapic_opt=$(get_gapic_opts)" \
${proto_files} $(search_additional_protos)
${proto_files} $(search_additional_protos "${contains_iam_policy}" "${contains_locations}")

unzip -o -q "${destination_path}/java_gapic_srcjar_raw.srcjar.zip" -d "${destination_path}"
# Sync'\''d to the output file name in Writer.java.
Expand Down
Expand Up @@ -68,6 +68,8 @@ grep -v '^ *#' < "${proto_path_list}" | while IFS= read -r line; do
# parse GAPIC options from proto_path/BUILD.bazel
pushd "${output_folder}"
proto_build_file_path="${proto_path}/BUILD.bazel"
contains_iam_policy=$(get_iam_policy_from_BUILD "${proto_build_file_path}")
contains_locations=$(get_locations_from_BUILD "${proto_build_file_path}")
transport=$(get_transport_from_BUILD "${proto_build_file_path}")
rest_numeric_enums=$(get_rest_numeric_enums_from_BUILD "${proto_build_file_path}")
include_samples=$(get_include_samples_from_BUILD "${proto_build_file_path}")
Expand All @@ -81,6 +83,8 @@ grep -v '^ *#' < "${proto_path_list}" | while IFS= read -r line; do
--gapic_generator_version "${gapic_generator_version}" \
--protobuf_version "${protobuf_version}" \
--grpc_version "${grpc_version}" \
--contains_iam_policy "${contains_iam_policy}" \
--contains_locations "${contains_locations}" \
--transport "${transport}" \
--rest_numeric_enums "${rest_numeric_enums}" \
--include_samples "${include_samples}"
Expand Down
52 changes: 40 additions & 12 deletions library_generation/test/generate_library_unit_tests.sh
Expand Up @@ -42,34 +42,30 @@ get_protobuf_version_failed_with_invalid_generator_version_test() {
}

search_additional_protos_common_resources_test() {
local proto_path="${script_dir}/resources/search_additional_proto/common_resources"
local addition_protos
addition_protos=$(search_additional_protos)
addition_protos=$(search_additional_protos "false" "false")
assertEquals "google/cloud/common_resources.proto" "${addition_protos}"
}

search_additional_protos_iam_test() {
local proto_path="${script_dir}/resources/search_additional_protos/iam"
local addition_protos
addition_protos=$(search_additional_protos)
addition_protos=$(search_additional_protos "true" "false")
assertEquals \
"google/cloud/common_resources.proto google/iam/v1/iam_policy.proto" \
"${addition_protos}"
}

search_additional_protos_location_test() {
local proto_path="${script_dir}/resources/search_additional_protos/location"
search_additional_protos_locations_test() {
local addition_protos
addition_protos=$(search_additional_protos)
addition_protos=$(search_additional_protos "false" "true")
assertEquals \
"google/cloud/common_resources.proto google/cloud/location/locations.proto" \
"${addition_protos}"
}

search_additional_protos_iam_location_test() {
local proto_path="${script_dir}/resources/search_additional_protos/iam_location"
search_additional_protos_iam_locations_test() {
local addition_protos
addition_protos=$(search_additional_protos)
addition_protos=$(search_additional_protos "true" "true")
assertEquals \
"google/cloud/common_resources.proto google/iam/v1/iam_policy.proto google/cloud/location/locations.proto" \
"${addition_protos}"
Expand Down Expand Up @@ -227,6 +223,34 @@ generate_library_failed_with_invalid_grpc_version() {
cleanup "${destination}"
}

get_iam_policy_from_BUILD_without_match_test() {
local proto_path="${script_dir}/resources/search_additional_protos/BUILD_iam_without_match.bazel"
local contains_iam_policy
contains_iam_policy=$(get_iam_policy_from_BUILD "${proto_path}")
assertEquals "false" "${contains_iam_policy}"
}

get_iam_policy_from_BUILD_match_test() {
local proto_path="${script_dir}/resources/search_additional_protos/BUILD_iam_match.bazel"
local contains_iam_policy
contains_iam_policy=$(get_iam_policy_from_BUILD "${proto_path}")
assertEquals "true" "${contains_iam_policy}"
}

get_locations_from_BUILD_without_match_test() {
local proto_path="${script_dir}/resources/search_additional_protos/BUILD_locations_without_match.bazel"
local contains_locations
contains_locations=$(get_locations_from_BUILD "${proto_path}")
assertEquals "false" "${contains_locations}"
}

get_locations_from_BUILD_match_test() {
local proto_path="${script_dir}/resources/search_additional_protos/BUILD_locations_match.bazel"
local contains_locations
contains_locations=$(get_locations_from_BUILD "${proto_path}")
assertEquals "true" "${contains_locations}"
}

get_transport_from_BUILD_grpc_rest_test() {
local build_file="${script_dir}/resources/misc/BUILD_grpc_rest.bazel"
local transport
Expand Down Expand Up @@ -318,8 +342,8 @@ test_list=(
get_protobuf_version_failed_with_invalid_generator_version_test
search_additional_protos_common_resources_test
search_additional_protos_iam_test
search_additional_protos_location_test
search_additional_protos_iam_location_test
search_additional_protos_locations_test
search_additional_protos_iam_locations_test
get_gapic_opts_with_rest_test
get_gapic_opts_without_rest_test
remove_grpc_version_test
Expand All @@ -336,6 +360,10 @@ test_list=(
generate_library_failed_with_invalid_generator_version
generate_library_failed_with_invalid_protobuf_version
generate_library_failed_with_invalid_grpc_version
get_iam_policy_from_BUILD_without_match_test
get_iam_policy_from_BUILD_match_test
get_locations_from_BUILD_without_match_test
get_locations_from_BUILD_match_test
get_transport_from_BUILD_grpc_rest_test
get_transport_from_BUILD_grpc_test
get_transport_from_BUILD_rest_test
Expand Down
@@ -0,0 +1,7 @@
# this file is only used in testing `get_iam_policy_from_BUILD` in utilities.sh

proto_library_with_info(
deps = [
"//google/iam/v1:iam_policy_proto",
]
)
@@ -0,0 +1,6 @@
# this file is only used in testing `get_iam_policy_from_BUILD` in utilities.sh

proto_library_with_info(
deps = [
]
)
@@ -0,0 +1,7 @@
# this file is only used in testing `get_location_from_BUILD` in utilities.sh

proto_library_with_info(
deps = [
"//google/cloud/location:location_proto",
]
)
@@ -0,0 +1,6 @@
# this file is only used in testing `get_location_from_BUILD` in utilities.sh

proto_library_with_info(
deps = [
]
)

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

47 changes: 32 additions & 15 deletions library_generation/utilities.sh
Expand Up @@ -18,7 +18,7 @@ __get_config_from_BUILD() {
if_match=$5

result="${default}"
if grep -A 15 "${rule}" "${build_file}" | grep -q "${pattern}"; then
if grep -A 20 "${rule}" "${build_file}" | grep -q "${pattern}"; then
result="${if_match}"
fi
echo "${result}"
Expand Down Expand Up @@ -69,28 +69,19 @@ unzip_src_files() {
rm -r -f "${destination_path}/${category}-${folder_name}/src/main/java/META-INF"
}

find_additional_protos_in_yaml() {
local pattern=$1
local find_result
find_result=$(grep --include=\*.yaml -rw "${proto_path}" -e "${pattern}")
if [ -n "${find_result}" ]; then
echo "${find_result}"
fi
}

# Apart from proto files in proto_path, additional protos are needed in order
# to generate GAPIC client libraries.
# In most cases, these protos should be within google/ directory, which is
# pulled from googleapis as a prerequisite.
# Search additional protos in .yaml files.
# Search additional protos in BUILD.bazel.
search_additional_protos() {
local contains_iam_policy=$1
local contains_locations=$2
additional_protos="google/cloud/common_resources.proto" # used by every library
iam_policy=$(find_additional_protos_in_yaml "name: '*google.iam.v1.IAMPolicy'*")
if [ -n "$iam_policy" ]; then
if [[ "${contains_iam_policy}" == "true" ]]; then
additional_protos="$additional_protos google/iam/v1/iam_policy.proto"
fi
locations=$(find_additional_protos_in_yaml "name: '*google.cloud.location.Locations'*")
if [ -n "${locations}" ]; then
if [[ "${contains_locations}" == "true" ]]; then
additional_protos="$additional_protos google/cloud/location/locations.proto"
fi
echo "${additional_protos}"
Expand Down Expand Up @@ -253,6 +244,32 @@ get_version_from_WORKSPACE() {
echo "${version}"
}

get_iam_policy_from_BUILD() {
local build_file=$1
local contains_iam_policy
contains_iam_policy=$(__get_config_from_BUILD \
"${build_file}" \
"proto_library_with_info(" \
"//google/iam/v1:iam_policy_proto" \
"false" \
"true"
)
echo "${contains_iam_policy}"
}

get_locations_from_BUILD() {
local build_file=$1
local contains_locations
contains_locations=$(__get_config_from_BUILD \
"${build_file}" \
"proto_library_with_info(" \
"//google/cloud/location:location_proto" \
"false" \
"true"
)
echo "${contains_locations}"
}

get_transport_from_BUILD() {
local build_file=$1
local transport
Expand Down
4 changes: 4 additions & 0 deletions showcase/scripts/generate_showcase.sh
Expand Up @@ -39,6 +39,8 @@ fi


ggj_version=$(get_version_from_versions_txt ../../versions.txt "gapic-generator-java")
contains_iam_policy="true"
contains_locations="true"
rest_numeric_enums="false"
transport="grpc+rest"
include_samples="false"
Expand All @@ -49,6 +51,8 @@ bash "${SCRIPT_DIR}/../../library_generation/generate_library.sh" \
--proto_path "schema/google/showcase/v1beta1" \
--destination_path "showcase-output" \
--gapic_generator_version "${ggj_version}" \
--contains_iam_policy "${contains_iam_policy}" \
--contains_locations "${contains_locations}" \
--rest_numeric_enums "${rest_numeric_enums}" \
--include_samples "${include_samples}" \
--transport "${transport}"
Expand Down