-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from tokopedia/support_no_gopackage
Support no go_package
- Loading branch information
Showing
11 changed files
with
158 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ pkged.go | |
gripmock | ||
.idea | ||
.DS_Store | ||
protogen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
syntax = "proto3"; | ||
package bar; | ||
|
||
message Bar{ | ||
string name = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
syntax = "proto3"; | ||
package bar; | ||
|
||
message DeepBar{ | ||
string address = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
pb "github.com/tokopedia/gripmock/protogen/example/no-gopackage" | ||
no_gopackage "github.com/tokopedia/gripmock/protogen/example/no-gopackage/bar" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
//go:generate protoc --go_out=plugins=grpc:${GOPATH}/src -I=../../protogen ../hello.proto ../foo.proto ../bar/bar.proto ../bar/deep/bar.proto | ||
func main() { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
|
||
// Set up a connection to the server. | ||
conn, err := grpc.DialContext(ctx, "localhost:4770", grpc.WithInsecure(), grpc.WithBlock()) | ||
if err != nil { | ||
log.Fatalf("did not connect: %v", err) | ||
} | ||
defer conn.Close() | ||
|
||
c := pb.NewGripmockClient(conn) | ||
|
||
// Contact the server and print out its response. | ||
name := "tokopedia" | ||
if len(os.Args) > 1 { | ||
name = os.Args[1] | ||
} | ||
r, err := c.Greet(context.Background(), &no_gopackage.Bar{Name: name}) | ||
if err != nil { | ||
log.Fatalf("error from grpc: %v", err) | ||
} | ||
log.Printf("Greeting: %s\nAddress:%s", r.Response, r.Deepbar.Address) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env sh | ||
|
||
# this file is used by .github/workflows/integration-test.yml | ||
|
||
gripmock --stub=example/no-gopackage/stub \ | ||
example/no-gopackage/foo.proto example/no-gopackage/hello.proto \ | ||
example/no-gopackage/bar/bar.proto \ | ||
example/no-gopackage/bar/deep/bar.proto & | ||
|
||
# wait for generated files to be available and gripmock is up | ||
sleep 2 | ||
|
||
go run example/no-gopackage/client/*.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
syntax = "proto3"; | ||
|
||
// simulating neighboring .proto file | ||
// but different package | ||
package foo; | ||
|
||
import "bar/deep/bar.proto"; | ||
|
||
message Response { | ||
string response = 1; | ||
bar.DeepBar deepbar = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
syntax = "proto3"; | ||
|
||
package multi_package; | ||
|
||
import "bar/bar.proto"; | ||
import "foo.proto"; | ||
|
||
service Gripmock { | ||
rpc Greet (bar.Bar) returns (foo.Response); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"service":"Gripmock", | ||
"method":"Greet", | ||
"input":{ | ||
"equals":{ | ||
"name":"tokopedia" | ||
} | ||
}, | ||
"output":{ | ||
"data":{ | ||
"response":"Hello Tokopedia", | ||
"deepbar": { | ||
"address": "Jakarta" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/bin/bash | ||
|
||
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 [[ -d $proto ]]; then | ||
continue | ||
fi | ||
|
||
dir=${proto%/*} | ||
file=${proto##*/} | ||
|
||
newdir="protogen/$dir" | ||
newfile="$newdir/$file" | ||
# copy to protogen directory | ||
mkdir -p "$newdir" && cp "$proto" "$_" | ||
|
||
syntaxLineNum="$(grep -n "syntax" "$newfile" | head -n 1 | cut -d: -f1)" | ||
|
||
goPackageString="option go_package = \"github.com/tokopedia/gripmock/protogen/$dir\";" | ||
sed -i "${syntaxLineNum}s~$~\n$goPackageString~" $newfile | ||
echo $newfile | ||
done | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters