Skip to content

Commit e074e3e

Browse files
authored
Merge pull request tokopedia#114 from tokopedia/support_go_package
Force replace go_package declaration
2 parents 7cd9dd5 + b918453 commit e074e3e

File tree

44 files changed

+399
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+399
-229
lines changed

.github/workflows/integration-test.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,3 @@ jobs:
4242
uses: ./
4343
with:
4444
entrypoint: example/stub-subfolders/entrypoint.sh
45-
- name: Run no go_package example
46-
uses: ./
47-
with:
48-
entrypoint: example/no-gopackage/entrypoint.sh

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ pkged.go
22
gripmock
33
.idea
44
.DS_Store
5-
protogen
5+
protogen/*
6+
!protogen/go.mod
7+
!protogen/example/
8+
temp

Dockerfile

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ RUN mkdir /proto
44

55
RUN mkdir /stubs
66

7-
RUN apk -U --no-cache add git protobuf
7+
RUN apk -U --no-cache add git protobuf bash
88

9-
RUN go get -u -v github.com/golang/protobuf/protoc-gen-go \
10-
google.golang.org/grpc \
11-
google.golang.org/grpc/reflection \
12-
golang.org/x/net/context \
13-
github.com/go-chi/chi \
14-
github.com/lithammer/fuzzysearch/fuzzy \
15-
golang.org/x/tools/imports
9+
RUN go install -v github.com/golang/protobuf/protoc-gen-go@latest
1610

17-
RUN go get github.com/markbates/pkger/cmd/pkger
11+
RUN go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
12+
13+
RUN go install github.com/markbates/pkger/cmd/pkger@latest
1814

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

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

29+
RUN ln -s /go/src/github.com/tokopedia/gripmock/fix_gopackage.sh /bin/
30+
3331
WORKDIR /go/src/github.com/tokopedia/gripmock/protoc-gen-gripmock
3432

3533
RUN pkger
@@ -42,6 +40,9 @@ WORKDIR /go/src/github.com/tokopedia/gripmock
4240
# install gripmock
4341
RUN go install -v
4442

43+
# to cache necessary imports
44+
RUN go build ./example/simple/client
45+
4546
# remove all .pb.go generated files
4647
# since generating go file is part of the test
4748
RUN find . -name "*.pb.go" -delete -type f

example/multi-files/client/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import (
55
"log"
66
"time"
77

8-
pb "github.com/tokopedia/gripmock/example/multi-files"
8+
pb "github.com/tokopedia/gripmock/protogen/example/multi-files"
99
"google.golang.org/grpc"
1010
)
1111

12-
//go:generate protoc --go_out=plugins=grpc:${GOPATH}/src -I=.. ../file1.proto ../file2.proto
1312
func main() {
1413
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
1514
defer cancel()

example/multi-package/client/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import (
66
"os"
77
"time"
88

9-
pb "github.com/tokopedia/gripmock/example/multi-package"
10-
multi_package "github.com/tokopedia/gripmock/example/multi-package/bar"
9+
pb "github.com/tokopedia/gripmock/protogen/example/multi-package"
10+
multi_package "github.com/tokopedia/gripmock/protogen/example/multi-package/bar"
1111
"google.golang.org/grpc"
1212
)
1313

14-
//go:generate protoc --go_out=plugins=grpc:${GOPATH}/src -I=.. ../hello.proto ../foo.proto ../bar/bar.proto
1514
func main() {
1615
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
1716
defer cancel()

example/multi-package/foo.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ syntax = "proto3";
44
// but different package
55
package foo;
66

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

910
message Response {
1011
string response = 1;

example/multi-package/hello.proto

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ package multi_package;
55
import "bar/bar.proto";
66
import "foo.proto";
77

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

1110
service Gripmock {
1211
rpc Greet (bar.Bar) returns (foo.Response);

example/no-gopackage/bar/bar.proto

Lines changed: 0 additions & 6 deletions
This file was deleted.

example/no-gopackage/bar/deep/bar.proto

Lines changed: 0 additions & 6 deletions
This file was deleted.

example/no-gopackage/client/main.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

example/no-gopackage/entrypoint.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

example/no-gopackage/stub/simple.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

example/one-of/client/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import (
66
"os"
77
"time"
88

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

1111
"google.golang.org/grpc"
1212
)
1313

14-
//go:generate protoc --go_out=plugins=grpc:${GOPATH}/src -I=.. ../oneof.proto
1514
func main() {
1615
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
1716
defer cancel()

example/simple/client/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import (
66
"os"
77
"time"
88

9-
pb "github.com/tokopedia/gripmock/example/simple"
9+
pb "github.com/tokopedia/gripmock/protogen/example/simple"
1010
"google.golang.org/grpc"
1111
)
1212

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

2726
// Contact the server and print out its response.
28-
name := "gripmock"
27+
name := "tokopedia"
2928
if len(os.Args) > 1 {
3029
name = os.Args[1]
3130
}

example/stream/client/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import (
77
"sync"
88
"time"
99

10-
pb "github.com/tokopedia/gripmock/example/stream"
10+
pb "github.com/tokopedia/gripmock/protogen/example/stream"
1111
"google.golang.org/grpc"
1212
)
1313

14-
//go:generate protoc --go_out=plugins=grpc:${GOPATH}/src -I=.. ../stream.proto
1514
func main() {
1615
// Set up a connection to the server.
1716
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)

example/stub-subfolders/client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66
"time"
77

8-
pb "github.com/tokopedia/gripmock/example/stub-subfolders"
8+
pb "github.com/tokopedia/gripmock/protogen/example/stub-subfolders"
99
"google.golang.org/grpc"
1010
)
1111

example/well_known_types/client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"time"
77

88
"github.com/golang/protobuf/ptypes/empty"
9-
pb "github.com/tokopedia/gripmock/example/well_known_types"
9+
pb "github.com/tokopedia/gripmock/protogen/example/well_known_types"
1010
"google.golang.org/grpc"
1111
)
1212

fix_gopackage.sh

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,42 @@ protos=("$@")
44

55
for proto in "${protos[@]}"
66
do
7-
if grep '^option go_package' $proto;then
8-
echo "option go_package detected in $proto, no need to append"
9-
exit 1
10-
fi
11-
done
12-
13-
for proto in "${protos[@]}"
14-
do
7+
# if it's a directory then skip
158
if [[ -d $proto ]]; then
169
continue
1710
fi
1811

12+
# example $proto: example/foo/bar/hello.proto
13+
14+
# get string from left until the last /
15+
# example value: example/foo/bar/
1916
dir=${proto%/*}
17+
18+
# remove prefix / if any
19+
dir=$(echo $dir | sed -n 's:^/*\(.*\)$:\1:p')
20+
21+
# get string from right until the first /
22+
# example value: hello.proto
2023
file=${proto##*/}
2124

2225
newdir="protogen/$dir"
2326
newfile="$newdir/$file"
27+
2428
# copy to protogen directory
25-
mkdir -p "$newdir" && cp "$proto" "$_"
29+
mkdir -p "$newdir" && \
30+
cp "$proto" "$_" && \
2631

32+
# Force remove any declaration of go_package
33+
# then replace it with our own declaration below
34+
sed -i 's/^option go_package.*$//g' $newfile
35+
36+
37+
# get the line number of "syntax" declaration
2738
syntaxLineNum="$(grep -n "syntax" "$newfile" | head -n 1 | cut -d: -f1)"
2839

2940
goPackageString="option go_package = \"github.com/tokopedia/gripmock/protogen/$dir\";"
41+
42+
# append our own go_package delcaration just below "syntax" declaration
3043
sed -i "${syntaxLineNum}s~$~\n$goPackageString~" $newfile
3144
echo $newfile
3245
done

go.mod

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@ require (
88
github.com/golang/protobuf v1.5.2
99
github.com/kr/pretty v0.2.0 // indirect
1010
github.com/lithammer/fuzzysearch v1.1.1
11-
github.com/stretchr/testify v1.6.1
11+
github.com/stretchr/testify v1.7.0
12+
github.com/tokopedia/gripmock/protogen v0.0.0 // indirect
13+
github.com/tokopedia/gripmock/protogen/example v0.0.0 // indirect
1214
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
13-
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd // indirect
1415
golang.org/x/text v0.3.4 // indirect
15-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
1616
google.golang.org/genproto v0.0.0-20201111145450-ac7456db90a6
17-
google.golang.org/grpc v1.33.2
18-
google.golang.org/protobuf v1.27.1
17+
google.golang.org/grpc v1.47.0
18+
google.golang.org/protobuf v1.28.0
1919
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
2020
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
2121
)
22+
23+
// this is for generated server to be able to run
24+
replace github.com/tokopedia/gripmock/protogen/example v0.0.0 => ./protogen/example
25+
26+
// this is for example client to be able to run
27+
replace github.com/tokopedia/gripmock/protogen v0.0.0 => ./protogen
28+

0 commit comments

Comments
 (0)