Skip to content

Commit

Permalink
Add support for go module prefix option (#243)
Browse files Browse the repository at this point in the history
* Add support for go module prefix option

* fix typo
  • Loading branch information
ido-namely authored Apr 22, 2021
1 parent b5c1406 commit 89631f0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
15 changes: 14 additions & 1 deletion all/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ printUsage() {
echo " --with-validator Generate validations for (${VALIDATOR_SUPPORTED_LANGUAGES[@]}) - see https://github.com/envoyproxy/protoc-gen-validate"
echo " --validator-source-relative Make the output dirctory for protoc-gen-validate 'source relative' - see https://github.com/envoyproxy/protoc-gen-validate#go"
echo " --go-source-relative Make go import paths 'source_relative' - see https://github.com/golang/protobuf#parameters"
echo " --go-module-prefix Specify the module prefix to remove from the import path - see https://developers.google.com/protocol-buffers/docs/reference/go-generated#invocation"
echo " --go-package-map Map proto imports to go import paths"
echo " --go-plugin-micro Replaces the Go gRPC plugin with go-micro"
echo " --go-proto-validator Generate Go proto validations - see https://github.com/mwitkow/go-proto-validators"
Expand Down Expand Up @@ -52,6 +53,7 @@ SUPPORTED_LANGUAGES=("go" "ruby" "csharp" "java" "python" "objc" "gogo" "php" "n
EXTRA_INCLUDES=""
OUT_DIR=""
GO_SOURCE_RELATIVE=""
GO_MODULE_PREFIX=""
GO_PACKAGE_MAP=""
GO_PLUGIN="grpc"
GO_VALIDATOR=false
Expand Down Expand Up @@ -149,6 +151,11 @@ while test $# -gt 0; do
GO_SOURCE_RELATIVE="paths=source_relative,"
shift
;;
--go-module-prefix)
shift
GO_MODULE_PREFIX="module=$1,"
shift
;;
--go-package-map)
if [ "$#" -gt 1 ] && [[ $2 != -* ]]; then
GO_PACKAGE_MAP=$2,
Expand Down Expand Up @@ -274,6 +281,12 @@ if [[ "$GEN_TYPESCRIPT" == true && "$GEN_LANG" != "node" ]]; then
exit 1
fi

if [[ ! -z $GO_SOURCE_RELATIVE && ! -z $GO_MODULE_PREFIX ]]; then
echo "Error: You may specifiy --go-source-relative or --go-module-prefix but not both"
printUsage
exit 1
fi

PLUGIN_LANG=$GEN_LANG
if [ $PLUGIN_LANG == 'objc' ] ; then
PLUGIN_LANG='objective_c'
Expand Down Expand Up @@ -302,7 +315,7 @@ fi
GEN_STRING=''
case $GEN_LANG in
"go")
GEN_STRING="--go_out=${GO_SOURCE_RELATIVE}${GO_PACKAGE_MAP}plugins=grpc:$OUT_DIR"
GEN_STRING="--go_out=${GO_SOURCE_RELATIVE}${GO_MODULE_PREFIX}${GO_PACKAGE_MAP}plugins=grpc:$OUT_DIR"
if [[ ${GO_PLUGIN} == "micro" ]]; then
GEN_STRING="$GEN_STRING --micro_out=$OUT_DIR"
fi
Expand Down
55 changes: 39 additions & 16 deletions all/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,25 @@ testGeneration() {
shift
expected_output_dir=$1
shift
expectedExitCode=$1
shift
extra_args=$@
echo "Testing language $lang $expected_output_dir $extra_args"

# Test calling a file directly.
docker run --rm -v=`pwd`:/defs $CONTAINER -f all/test/test.proto -l $lang -i all/test/ $extra_args
docker run --rm -v=`pwd`:/defs $CONTAINER -f all/test/test.proto -l $lang -i all/test/ $extra_args > /dev/null

exitCode=$?

if [[ $expectedExitCode != $exitCode ]]; then
echo "exit code must be $expectedExitCode but is $exitCode instead"
exit 1
elif [[ "$expectedExitCode" != 0 ]]; then
# no need to continue test of expected failure
echo "expected failure passed!"
return
fi

if [[ ! -d "$expected_output_dir" ]]; then
echo "generated directory $expected_output_dir does not exist"
exit 1
Expand All @@ -32,6 +46,8 @@ testGeneration() {
expected_file_name="/all/test.pb.go"
if [[ "$extra_args" == *"--go-source-relative"* ]]; then
expected_file_name="/all/test/test.pb.go"
elif [[ "$extra_args" == *"--go-module-prefix"* ]]; then
expected_file_name="/test.pb.go"
fi
if [[ ! -f "$expected_output_dir$expected_file_name" ]]; then
echo "$expected_file_name file was not generated in $expected_output_dir"
Expand Down Expand Up @@ -187,41 +203,48 @@ testGeneration() {
}

# Test grpc-gateway generation (only valid for Go)
testGeneration go "gen/pb-go" --with-gateway
testGeneration go "gen/pb-go" 0 --with-gateway

# Test grpc-gateway generation + json (only valid for Go)
testGeneration go "gen/pb-go" --with-gateway --with-openapi-json-names
testGeneration go "gen/pb-go" 0 --with-gateway --with-openapi-json-names

# Test grpc-gateway generation + json (deprecated) (only valid for Go)
testGeneration go "gen/pb-go" --with-gateway --with-swagger-json-names
testGeneration go "gen/pb-go" 0 --with-gateway --with-swagger-json-names

# Test go source relative generation
testGeneration go "gen/pb-go" --go-source-relative
testGeneration go "gen/pb-go" 0 --go-source-relative

# Test go module prefix
testGeneration go "gen/pb-go" 0 --go-module-prefix all

# Test expected failure for source relative and module prefix combination
testGeneration go "gen/pb-go" 1 --go-module-prefix all --go-source-relative
testGeneration go "gen/pb-go" 1 --go-source-relative --go-module-prefix all

# Test go validator
testGeneration go "gen/pb-go" --with-validator
testGeneration go "gen/pb-go" 0 --with-validator

# Test go validator with source relative option
testGeneration go "gen/pb-go" --with-validator --validator-source-relative
testGeneration go "gen/pb-go" 0 --with-validator --validator-source-relative

# Test go-micro generations
testGeneration go "gen/pb-go" --go-plugin-micro
testGeneration go "gen/pb-go" 0 --go-plugin-micro

# Test Sorbet RBI declaration file generation (only valid for Ruby)
testGeneration ruby "gen/pb-ruby" --with-rbi
testGeneration ruby "gen/pb-ruby" 0 --with-rbi

# Test TypeScript declaration file generation (only valid for Node)
testGeneration node "gen/pb-node" --with-typescript
testGeneration node "gen/pb-node" 0 --with-typescript

# Test node alternative import style (only valid for node and web)
testGeneration node "gen/pb-node" --js-out library=testlib
testGeneration node "gen/pb-node" 0 --js-out library=testlib

# Test node grpc-out alternative import style (only valid for node and web)
testGeneration node "gen/pb-node" --grpc-out grpc-js
testGeneration node "gen/pb-node" 0 --grpc-out grpc-js

# Test grpc web alternative import style (only valid for web)
testGeneration web "gen/pb-web" --grpc-web-out import_style=typescript
testGeneration web "gen/pb-web" --grpc-web-out import_style=commonjs+dts
testGeneration web "gen/pb-web" 0 --grpc-web-out import_style=typescript
testGeneration web "gen/pb-web" 0 --grpc-web-out import_style=commonjs+dts

# Generate proto files
for lang in ${LANGS[@]}; do
Expand All @@ -233,11 +256,11 @@ for lang in ${LANGS[@]}; do
fi

# Test without an output directory.
testGeneration "$lang" "$expected_output_dir"
testGeneration "$lang" "$expected_output_dir" 0

# Test with an output directory.
test_dir="gen/foo/bar"
testGeneration "$lang" "$test_dir" -o "$test_dir"
testGeneration "$lang" "$test_dir" 0 -o "$test_dir"
done


Expand Down
2 changes: 1 addition & 1 deletion variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
BUILDS=("protoc-all" "protoc" "prototool" "grpc-cli" "gen-grpc-gateway")
DOCKER_REPO=${DOCKER_REPO}
NAMESPACE=${NAMESPACE:-namely}
BUILD_VERSION=${BUILD_VERSION:-0}
BUILD_VERSION=${BUILD_VERSION:-1}
CONTAINER=${DOCKER_REPO}${NAMESPACE}
LATEST=${1:false}

Expand Down

0 comments on commit 89631f0

Please sign in to comment.