Skip to content

Commit

Permalink
Merge pull request #114 from tokopedia/support_go_package
Browse files Browse the repository at this point in the history
Force replace go_package declaration
  • Loading branch information
jekiapp authored Jun 23, 2022
2 parents 7cd9dd5 + b918453 commit e074e3e
Show file tree
Hide file tree
Showing 44 changed files with 399 additions and 229 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,3 @@ jobs:
uses: ./
with:
entrypoint: example/stub-subfolders/entrypoint.sh
- name: Run no go_package example
uses: ./
with:
entrypoint: example/no-gopackage/entrypoint.sh
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ pkged.go
gripmock
.idea
.DS_Store
protogen
protogen/*
!protogen/go.mod
!protogen/example/
temp
19 changes: 10 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ RUN mkdir /proto

RUN mkdir /stubs

RUN apk -U --no-cache add git protobuf
RUN apk -U --no-cache add git protobuf bash

RUN go get -u -v github.com/golang/protobuf/protoc-gen-go \
google.golang.org/grpc \
google.golang.org/grpc/reflection \
golang.org/x/net/context \
github.com/go-chi/chi \
github.com/lithammer/fuzzysearch/fuzzy \
golang.org/x/tools/imports
RUN go install -v github.com/golang/protobuf/protoc-gen-go@latest

RUN go get github.com/markbates/pkger/cmd/pkger
RUN go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

RUN go install github.com/markbates/pkger/cmd/pkger@latest

# cloning well-known-types
RUN git clone --depth=1 https://github.com/google/protobuf.git /protobuf-repo
Expand All @@ -30,6 +26,8 @@ RUN mkdir -p /go/src/github.com/tokopedia/gripmock

COPY . /go/src/github.com/tokopedia/gripmock

RUN ln -s /go/src/github.com/tokopedia/gripmock/fix_gopackage.sh /bin/

WORKDIR /go/src/github.com/tokopedia/gripmock/protoc-gen-gripmock

RUN pkger
Expand All @@ -42,6 +40,9 @@ WORKDIR /go/src/github.com/tokopedia/gripmock
# install gripmock
RUN go install -v

# to cache necessary imports
RUN go build ./example/simple/client

# remove all .pb.go generated files
# since generating go file is part of the test
RUN find . -name "*.pb.go" -delete -type f
Expand Down
3 changes: 1 addition & 2 deletions example/multi-files/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"log"
"time"

pb "github.com/tokopedia/gripmock/example/multi-files"
pb "github.com/tokopedia/gripmock/protogen/example/multi-files"
"google.golang.org/grpc"
)

//go:generate protoc --go_out=plugins=grpc:${GOPATH}/src -I=.. ../file1.proto ../file2.proto
func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
Expand Down
5 changes: 2 additions & 3 deletions example/multi-package/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"os"
"time"

pb "github.com/tokopedia/gripmock/example/multi-package"
multi_package "github.com/tokopedia/gripmock/example/multi-package/bar"
pb "github.com/tokopedia/gripmock/protogen/example/multi-package"
multi_package "github.com/tokopedia/gripmock/protogen/example/multi-package/bar"
"google.golang.org/grpc"
)

//go:generate protoc --go_out=plugins=grpc:${GOPATH}/src -I=.. ../hello.proto ../foo.proto ../bar/bar.proto
func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
Expand Down
3 changes: 2 additions & 1 deletion example/multi-package/foo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ syntax = "proto3";
// but different package
package foo;

option go_package = "github.com/tokopedia/gripmock/example/multi-package";
// simulating dummy private repo
option go_package = "github.com/my/private/repo/multi-package";

message Response {
string response = 1;
Expand Down
3 changes: 1 addition & 2 deletions example/multi-package/hello.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ package multi_package;
import "bar/bar.proto";
import "foo.proto";

// simulate go_package alias with -
option go_package = "github.com/tokopedia/gripmock/example/multi-package";
// simulate no go_package and folder with -

service Gripmock {
rpc Greet (bar.Bar) returns (foo.Response);
Expand Down
6 changes: 0 additions & 6 deletions example/no-gopackage/bar/bar.proto

This file was deleted.

6 changes: 0 additions & 6 deletions example/no-gopackage/bar/deep/bar.proto

This file was deleted.

38 changes: 0 additions & 38 deletions example/no-gopackage/client/main.go

This file was deleted.

13 changes: 0 additions & 13 deletions example/no-gopackage/entrypoint.sh

This file was deleted.

17 changes: 0 additions & 17 deletions example/no-gopackage/stub/simple.json

This file was deleted.

3 changes: 1 addition & 2 deletions example/one-of/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"os"
"time"

oneof "github.com/tokopedia/gripmock/example/one-of"
oneof "github.com/tokopedia/gripmock/protogen/example/one-of"

"google.golang.org/grpc"
)

//go:generate protoc --go_out=plugins=grpc:${GOPATH}/src -I=.. ../oneof.proto
func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
Expand Down
5 changes: 2 additions & 3 deletions example/simple/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"os"
"time"

pb "github.com/tokopedia/gripmock/example/simple"
pb "github.com/tokopedia/gripmock/protogen/example/simple"
"google.golang.org/grpc"
)

//go:generate protoc -I=.. --go_out=plugins=grpc:${GOPATH}/src ../simple.proto
func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
Expand All @@ -25,7 +24,7 @@ func main() {
c := pb.NewGripmockClient(conn)

// Contact the server and print out its response.
name := "gripmock"
name := "tokopedia"
if len(os.Args) > 1 {
name = os.Args[1]
}
Expand Down
3 changes: 1 addition & 2 deletions example/stream/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"sync"
"time"

pb "github.com/tokopedia/gripmock/example/stream"
pb "github.com/tokopedia/gripmock/protogen/example/stream"
"google.golang.org/grpc"
)

//go:generate protoc --go_out=plugins=grpc:${GOPATH}/src -I=.. ../stream.proto
func main() {
// Set up a connection to the server.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
Expand Down
2 changes: 1 addition & 1 deletion example/stub-subfolders/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

pb "github.com/tokopedia/gripmock/example/stub-subfolders"
pb "github.com/tokopedia/gripmock/protogen/example/stub-subfolders"
"google.golang.org/grpc"
)

Expand Down
2 changes: 1 addition & 1 deletion example/well_known_types/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/golang/protobuf/ptypes/empty"
pb "github.com/tokopedia/gripmock/example/well_known_types"
pb "github.com/tokopedia/gripmock/protogen/example/well_known_types"
"google.golang.org/grpc"
)

Expand Down
31 changes: 22 additions & 9 deletions fix_gopackage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,42 @@ protos=("$@")

for proto in "${protos[@]}"
do
if grep '^option go_package' $proto;then
echo "option go_package detected in $proto, no need to append"
exit 1
fi
done

for proto in "${protos[@]}"
do
# if it's a directory then skip
if [[ -d $proto ]]; then
continue
fi

# example $proto: example/foo/bar/hello.proto

# get string from left until the last /
# example value: example/foo/bar/
dir=${proto%/*}

# remove prefix / if any
dir=$(echo $dir | sed -n 's:^/*\(.*\)$:\1:p')

# get string from right until the first /
# example value: hello.proto
file=${proto##*/}

newdir="protogen/$dir"
newfile="$newdir/$file"

# copy to protogen directory
mkdir -p "$newdir" && cp "$proto" "$_"
mkdir -p "$newdir" && \
cp "$proto" "$_" && \

# Force remove any declaration of go_package
# then replace it with our own declaration below
sed -i 's/^option go_package.*$//g' $newfile


# get the line number of "syntax" declaration
syntaxLineNum="$(grep -n "syntax" "$newfile" | head -n 1 | cut -d: -f1)"

goPackageString="option go_package = \"github.com/tokopedia/gripmock/protogen/$dir\";"

# append our own go_package delcaration just below "syntax" declaration
sed -i "${syntaxLineNum}s~$~\n$goPackageString~" $newfile
echo $newfile
done
Expand Down
17 changes: 12 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ require (
github.com/golang/protobuf v1.5.2
github.com/kr/pretty v0.2.0 // indirect
github.com/lithammer/fuzzysearch v1.1.1
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.7.0
github.com/tokopedia/gripmock/protogen v0.0.0 // indirect
github.com/tokopedia/gripmock/protogen/example v0.0.0 // indirect
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd // indirect
golang.org/x/text v0.3.4 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20201111145450-ac7456db90a6
google.golang.org/grpc v1.33.2
google.golang.org/protobuf v1.27.1
google.golang.org/grpc v1.47.0
google.golang.org/protobuf v1.28.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)

// this is for generated server to be able to run
replace github.com/tokopedia/gripmock/protogen/example v0.0.0 => ./protogen/example

// this is for example client to be able to run
replace github.com/tokopedia/gripmock/protogen v0.0.0 => ./protogen

Loading

0 comments on commit e074e3e

Please sign in to comment.