From 624b646c7dd45a508d47287c4b9aa72247654d28 Mon Sep 17 00:00:00 2001 From: Ahmad Muzakki Date: Fri, 28 Jan 2022 17:59:29 +0700 Subject: [PATCH 1/6] support no go_package append the go_package automatically using fix_gopackage.sh. --- .gitignore | 1 + fix_gopackage.sh | 33 +++++++++++++++++++++++++++++++++ gripmock.go | 47 +++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 75 insertions(+), 6 deletions(-) create mode 100755 fix_gopackage.sh diff --git a/.gitignore b/.gitignore index 00f277dc..eadf978d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ pkged.go gripmock .idea .DS_Store +protogen diff --git a/fix_gopackage.sh b/fix_gopackage.sh new file mode 100755 index 00000000..67577773 --- /dev/null +++ b/fix_gopackage.sh @@ -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/$dir\";" + sed -i "${syntaxLineNum}s~$~\n$goPackageString~" $newfile + echo $newfile +done + diff --git a/gripmock.go b/gripmock.go index 18a05f4a..83cae790 100644 --- a/gripmock.go +++ b/gripmock.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "flag" "fmt" "log" @@ -101,13 +102,11 @@ type protocParam struct { } func generateProtoc(param protocParam) { - protodirs := strings.Split(param.protoPath[0], "/") - protodir := "" - if len(protodirs) > 0 { - protodir = strings.Join(protodirs[:len(protodirs)-1], "/") + "/" - } - args := []string{"-I", protodir} + fixGoPackageUnderImportPath(getDirFromFile(param.protoPath[0])) + param.protoPath = fixGoPackage(param.protoPath) + + args := []string{"-I", getDirFromFile(param.protoPath[0])} // include well-known-types for _, i := range param.imports { args = append(args, "-I", i) @@ -130,6 +129,42 @@ func generateProtoc(param protocParam) { } +func getDirFromFile(filepath string) string { + protodirs := strings.Split(filepath, "/") + dir := "." + if len(protodirs) > 0 { + dir = strings.Join(protodirs[:len(protodirs)-1], "/") + "/" + } + return dir +} + +func fixGoPackageUnderImportPath(importPath string) { + findProtos := exec.Command("find", importPath, "-mindepth", "2", "-name", "*.proto") + buf := &bytes.Buffer{} + findProtos.Stdout = buf + err := findProtos.Run() + if err != nil { + log.Println(err) + return + } + + fixGoPackage(strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")) +} + +func fixGoPackage(protoPaths []string) []string { + fixgopackage := exec.Command("fix_gopackage.sh", protoPaths...) + buf := &bytes.Buffer{} + fixgopackage.Stdout = buf + fixgopackage.Stderr = os.Stderr + err := fixgopackage.Run() + if err != nil { + log.Println("error on fixGoPackage", err) + return protoPaths + } + + return strings.Split(strings.TrimRight(buf.String(), "\n"), "\n") +} + func buildServer(output string) { args := []string{"build", "-o", output + "grpcserver", output} From 82f4f1ebe039ad70924d1297691199efca49eeb5 Mon Sep 17 00:00:00 2001 From: Ahmad Muzakki Date: Fri, 28 Jan 2022 18:25:54 +0700 Subject: [PATCH 2/6] add example and test --- example/no-gopackage/bar/bar.pb.go | 144 +++++++++++++++++++++ example/no-gopackage/bar/bar.proto | 6 + example/no-gopackage/client/main.go | 38 ++++++ example/no-gopackage/deep/bar/bar.pb.go | 145 +++++++++++++++++++++ example/no-gopackage/deep/bar/bar.proto | 6 + example/no-gopackage/entrypoint.sh | 14 ++ example/no-gopackage/foo.pb.go | 162 ++++++++++++++++++++++++ example/no-gopackage/foo.proto | 12 ++ example/no-gopackage/hello.pb.go | 160 +++++++++++++++++++++++ example/no-gopackage/hello.proto | 11 ++ example/no-gopackage/stub/simple.json | 17 +++ 11 files changed, 715 insertions(+) create mode 100644 example/no-gopackage/bar/bar.pb.go create mode 100644 example/no-gopackage/bar/bar.proto create mode 100644 example/no-gopackage/client/main.go create mode 100644 example/no-gopackage/deep/bar/bar.pb.go create mode 100644 example/no-gopackage/deep/bar/bar.proto create mode 100755 example/no-gopackage/entrypoint.sh create mode 100644 example/no-gopackage/foo.pb.go create mode 100644 example/no-gopackage/foo.proto create mode 100644 example/no-gopackage/hello.pb.go create mode 100644 example/no-gopackage/hello.proto create mode 100644 example/no-gopackage/stub/simple.json diff --git a/example/no-gopackage/bar/bar.pb.go b/example/no-gopackage/bar/bar.pb.go new file mode 100644 index 00000000..46b0b5a5 --- /dev/null +++ b/example/no-gopackage/bar/bar.pb.go @@ -0,0 +1,144 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.17.3 +// source: bar/bar.proto + +package bar + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Bar struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` +} + +func (x *Bar) Reset() { + *x = Bar{} + if protoimpl.UnsafeEnabled { + mi := &file_bar_bar_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Bar) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Bar) ProtoMessage() {} + +func (x *Bar) ProtoReflect() protoreflect.Message { + mi := &file_bar_bar_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Bar.ProtoReflect.Descriptor instead. +func (*Bar) Descriptor() ([]byte, []int) { + return file_bar_bar_proto_rawDescGZIP(), []int{0} +} + +func (x *Bar) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +var File_bar_bar_proto protoreflect.FileDescriptor + +var file_bar_bar_proto_rawDesc = []byte{ + 0x0a, 0x0d, 0x62, 0x61, 0x72, 0x2f, 0x62, 0x61, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, + 0x03, 0x62, 0x61, 0x72, 0x22, 0x19, 0x0a, 0x03, 0x42, 0x61, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x6e, + 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x42, + 0x38, 0x5a, 0x36, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x6f, + 0x6b, 0x6f, 0x70, 0x65, 0x64, 0x69, 0x61, 0x2f, 0x67, 0x72, 0x69, 0x70, 0x6d, 0x6f, 0x63, 0x6b, + 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x6e, 0x6f, 0x2d, 0x67, 0x6f, 0x70, 0x61, + 0x63, 0x6b, 0x61, 0x67, 0x65, 0x2f, 0x62, 0x61, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, +} + +var ( + file_bar_bar_proto_rawDescOnce sync.Once + file_bar_bar_proto_rawDescData = file_bar_bar_proto_rawDesc +) + +func file_bar_bar_proto_rawDescGZIP() []byte { + file_bar_bar_proto_rawDescOnce.Do(func() { + file_bar_bar_proto_rawDescData = protoimpl.X.CompressGZIP(file_bar_bar_proto_rawDescData) + }) + return file_bar_bar_proto_rawDescData +} + +var file_bar_bar_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_bar_bar_proto_goTypes = []interface{}{ + (*Bar)(nil), // 0: bar.Bar +} +var file_bar_bar_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_bar_bar_proto_init() } +func file_bar_bar_proto_init() { + if File_bar_bar_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_bar_bar_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Bar); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_bar_bar_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_bar_bar_proto_goTypes, + DependencyIndexes: file_bar_bar_proto_depIdxs, + MessageInfos: file_bar_bar_proto_msgTypes, + }.Build() + File_bar_bar_proto = out.File + file_bar_bar_proto_rawDesc = nil + file_bar_bar_proto_goTypes = nil + file_bar_bar_proto_depIdxs = nil +} diff --git a/example/no-gopackage/bar/bar.proto b/example/no-gopackage/bar/bar.proto new file mode 100644 index 00000000..e8fd8511 --- /dev/null +++ b/example/no-gopackage/bar/bar.proto @@ -0,0 +1,6 @@ +syntax = "proto3"; +package bar; + +message Bar{ + string name = 1; +} diff --git a/example/no-gopackage/client/main.go b/example/no-gopackage/client/main.go new file mode 100644 index 00000000..521e3e47 --- /dev/null +++ b/example/no-gopackage/client/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "context" + "log" + "os" + "time" + + pb "github.com/tokopedia/gripmock/example/no-gopackage" + no_gopackage "github.com/tokopedia/gripmock/example/no-gopackage/bar" + "google.golang.org/grpc" +) + +//go:generate protoc --go_out=plugins=grpc:${GOPATH}/src -I=.. ../hello.proto ../foo.proto ../bar/bar.proto ../deep/bar/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) +} diff --git a/example/no-gopackage/deep/bar/bar.pb.go b/example/no-gopackage/deep/bar/bar.pb.go new file mode 100644 index 00000000..14787f46 --- /dev/null +++ b/example/no-gopackage/deep/bar/bar.pb.go @@ -0,0 +1,145 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.17.3 +// source: deep/bar/bar.proto + +package bar + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type DeepBar struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"` +} + +func (x *DeepBar) Reset() { + *x = DeepBar{} + if protoimpl.UnsafeEnabled { + mi := &file_deep_bar_bar_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *DeepBar) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DeepBar) ProtoMessage() {} + +func (x *DeepBar) ProtoReflect() protoreflect.Message { + mi := &file_deep_bar_bar_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use DeepBar.ProtoReflect.Descriptor instead. +func (*DeepBar) Descriptor() ([]byte, []int) { + return file_deep_bar_bar_proto_rawDescGZIP(), []int{0} +} + +func (x *DeepBar) GetAddress() string { + if x != nil { + return x.Address + } + return "" +} + +var File_deep_bar_bar_proto protoreflect.FileDescriptor + +var file_deep_bar_bar_proto_rawDesc = []byte{ + 0x0a, 0x12, 0x64, 0x65, 0x65, 0x70, 0x2f, 0x62, 0x61, 0x72, 0x2f, 0x62, 0x61, 0x72, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x62, 0x61, 0x72, 0x22, 0x23, 0x0a, 0x07, 0x44, 0x65, 0x65, + 0x70, 0x42, 0x61, 0x72, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x42, 0x3d, + 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x6f, 0x6b, + 0x6f, 0x70, 0x65, 0x64, 0x69, 0x61, 0x2f, 0x67, 0x72, 0x69, 0x70, 0x6d, 0x6f, 0x63, 0x6b, 0x2f, + 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x6e, 0x6f, 0x2d, 0x67, 0x6f, 0x70, 0x61, 0x63, + 0x6b, 0x61, 0x67, 0x65, 0x2f, 0x64, 0x65, 0x65, 0x70, 0x2f, 0x62, 0x61, 0x72, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_deep_bar_bar_proto_rawDescOnce sync.Once + file_deep_bar_bar_proto_rawDescData = file_deep_bar_bar_proto_rawDesc +) + +func file_deep_bar_bar_proto_rawDescGZIP() []byte { + file_deep_bar_bar_proto_rawDescOnce.Do(func() { + file_deep_bar_bar_proto_rawDescData = protoimpl.X.CompressGZIP(file_deep_bar_bar_proto_rawDescData) + }) + return file_deep_bar_bar_proto_rawDescData +} + +var file_deep_bar_bar_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_deep_bar_bar_proto_goTypes = []interface{}{ + (*DeepBar)(nil), // 0: bar.DeepBar +} +var file_deep_bar_bar_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_deep_bar_bar_proto_init() } +func file_deep_bar_bar_proto_init() { + if File_deep_bar_bar_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_deep_bar_bar_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*DeepBar); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_deep_bar_bar_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_deep_bar_bar_proto_goTypes, + DependencyIndexes: file_deep_bar_bar_proto_depIdxs, + MessageInfos: file_deep_bar_bar_proto_msgTypes, + }.Build() + File_deep_bar_bar_proto = out.File + file_deep_bar_bar_proto_rawDesc = nil + file_deep_bar_bar_proto_goTypes = nil + file_deep_bar_bar_proto_depIdxs = nil +} diff --git a/example/no-gopackage/deep/bar/bar.proto b/example/no-gopackage/deep/bar/bar.proto new file mode 100644 index 00000000..1befae86 --- /dev/null +++ b/example/no-gopackage/deep/bar/bar.proto @@ -0,0 +1,6 @@ +syntax = "proto3"; +package bar; + +message DeepBar{ + string address = 1; +} diff --git a/example/no-gopackage/entrypoint.sh b/example/no-gopackage/entrypoint.sh new file mode 100755 index 00000000..982ddcb1 --- /dev/null +++ b/example/no-gopackage/entrypoint.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env sh + +# this file is used by .github/workflows/integration-test.yml + +# we need to add example/multi-package ar as included import path +# without it protoc could not find the bar/bar.proto +gripmock --stub=example/multi-package/stub --imports=example/multi-package/ \ + example/multi-package/foo.proto example/multi-package/hello.proto \ + example/multi-package/bar/bar.proto & + +# wait for generated files to be available and gripmock is up +sleep 2 + +go run example/multi-package/client/*.go \ No newline at end of file diff --git a/example/no-gopackage/foo.pb.go b/example/no-gopackage/foo.pb.go new file mode 100644 index 00000000..a4fff64a --- /dev/null +++ b/example/no-gopackage/foo.pb.go @@ -0,0 +1,162 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.17.3 +// source: foo.proto + +// simulating neighboring .proto file +// but different package + +package no_gopackage + +import ( + bar "github.com/tokopedia/gripmock/example/no-gopackage/deep/bar" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Response struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Response string `protobuf:"bytes,1,opt,name=response,proto3" json:"response,omitempty"` + Deepbar *bar.DeepBar `protobuf:"bytes,2,opt,name=deepbar,proto3" json:"deepbar,omitempty"` +} + +func (x *Response) Reset() { + *x = Response{} + if protoimpl.UnsafeEnabled { + mi := &file_foo_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Response) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Response) ProtoMessage() {} + +func (x *Response) ProtoReflect() protoreflect.Message { + mi := &file_foo_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Response.ProtoReflect.Descriptor instead. +func (*Response) Descriptor() ([]byte, []int) { + return file_foo_proto_rawDescGZIP(), []int{0} +} + +func (x *Response) GetResponse() string { + if x != nil { + return x.Response + } + return "" +} + +func (x *Response) GetDeepbar() *bar.DeepBar { + if x != nil { + return x.Deepbar + } + return nil +} + +var File_foo_proto protoreflect.FileDescriptor + +var file_foo_proto_rawDesc = []byte{ + 0x0a, 0x09, 0x66, 0x6f, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x66, 0x6f, 0x6f, + 0x1a, 0x12, 0x64, 0x65, 0x65, 0x70, 0x2f, 0x62, 0x61, 0x72, 0x2f, 0x62, 0x61, 0x72, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x4e, 0x0a, 0x08, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0x12, 0x1a, 0x0a, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x26, 0x0a, 0x07, + 0x64, 0x65, 0x65, 0x70, 0x62, 0x61, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, + 0x62, 0x61, 0x72, 0x2e, 0x44, 0x65, 0x65, 0x70, 0x42, 0x61, 0x72, 0x52, 0x07, 0x64, 0x65, 0x65, + 0x70, 0x62, 0x61, 0x72, 0x42, 0x34, 0x5a, 0x32, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, + 0x6f, 0x6d, 0x2f, 0x74, 0x6f, 0x6b, 0x6f, 0x70, 0x65, 0x64, 0x69, 0x61, 0x2f, 0x67, 0x72, 0x69, + 0x70, 0x6d, 0x6f, 0x63, 0x6b, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x6e, 0x6f, + 0x2d, 0x67, 0x6f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, +} + +var ( + file_foo_proto_rawDescOnce sync.Once + file_foo_proto_rawDescData = file_foo_proto_rawDesc +) + +func file_foo_proto_rawDescGZIP() []byte { + file_foo_proto_rawDescOnce.Do(func() { + file_foo_proto_rawDescData = protoimpl.X.CompressGZIP(file_foo_proto_rawDescData) + }) + return file_foo_proto_rawDescData +} + +var file_foo_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_foo_proto_goTypes = []interface{}{ + (*Response)(nil), // 0: foo.Response + (*bar.DeepBar)(nil), // 1: bar.DeepBar +} +var file_foo_proto_depIdxs = []int32{ + 1, // 0: foo.Response.deepbar:type_name -> bar.DeepBar + 1, // [1:1] is the sub-list for method output_type + 1, // [1:1] is the sub-list for method input_type + 1, // [1:1] is the sub-list for extension type_name + 1, // [1:1] is the sub-list for extension extendee + 0, // [0:1] is the sub-list for field type_name +} + +func init() { file_foo_proto_init() } +func file_foo_proto_init() { + if File_foo_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_foo_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Response); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_foo_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_foo_proto_goTypes, + DependencyIndexes: file_foo_proto_depIdxs, + MessageInfos: file_foo_proto_msgTypes, + }.Build() + File_foo_proto = out.File + file_foo_proto_rawDesc = nil + file_foo_proto_goTypes = nil + file_foo_proto_depIdxs = nil +} diff --git a/example/no-gopackage/foo.proto b/example/no-gopackage/foo.proto new file mode 100644 index 00000000..bfca5893 --- /dev/null +++ b/example/no-gopackage/foo.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; + +// simulating neighboring .proto file +// but different package +package foo; + +import "deep/bar/bar.proto"; + +message Response { + string response = 1; + bar.DeepBar deepbar = 2; +} diff --git a/example/no-gopackage/hello.pb.go b/example/no-gopackage/hello.pb.go new file mode 100644 index 00000000..48a3d8ba --- /dev/null +++ b/example/no-gopackage/hello.pb.go @@ -0,0 +1,160 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.1 +// protoc v3.17.3 +// source: hello.proto + +package no_gopackage + +import ( + context "context" + bar "github.com/tokopedia/gripmock/example/no-gopackage/bar" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +var File_hello_proto protoreflect.FileDescriptor + +var file_hello_proto_rawDesc = []byte{ + 0x0a, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0d, 0x6d, + 0x75, 0x6c, 0x74, 0x69, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x1a, 0x0d, 0x62, 0x61, + 0x72, 0x2f, 0x62, 0x61, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x09, 0x66, 0x6f, 0x6f, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x32, 0x2c, 0x0a, 0x08, 0x47, 0x72, 0x69, 0x70, 0x6d, 0x6f, + 0x63, 0x6b, 0x12, 0x20, 0x0a, 0x05, 0x47, 0x72, 0x65, 0x65, 0x74, 0x12, 0x08, 0x2e, 0x62, 0x61, + 0x72, 0x2e, 0x42, 0x61, 0x72, 0x1a, 0x0d, 0x2e, 0x66, 0x6f, 0x6f, 0x2e, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x34, 0x5a, 0x32, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, + 0x6f, 0x6d, 0x2f, 0x74, 0x6f, 0x6b, 0x6f, 0x70, 0x65, 0x64, 0x69, 0x61, 0x2f, 0x67, 0x72, 0x69, + 0x70, 0x6d, 0x6f, 0x63, 0x6b, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x6e, 0x6f, + 0x2d, 0x67, 0x6f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, +} + +var file_hello_proto_goTypes = []interface{}{ + (*bar.Bar)(nil), // 0: bar.Bar + (*Response)(nil), // 1: foo.Response +} +var file_hello_proto_depIdxs = []int32{ + 0, // 0: multi_package.Gripmock.Greet:input_type -> bar.Bar + 1, // 1: multi_package.Gripmock.Greet:output_type -> foo.Response + 1, // [1:2] is the sub-list for method output_type + 0, // [0:1] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_hello_proto_init() } +func file_hello_proto_init() { + if File_hello_proto != nil { + return + } + file_foo_proto_init() + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_hello_proto_rawDesc, + NumEnums: 0, + NumMessages: 0, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_hello_proto_goTypes, + DependencyIndexes: file_hello_proto_depIdxs, + }.Build() + File_hello_proto = out.File + file_hello_proto_rawDesc = nil + file_hello_proto_goTypes = nil + file_hello_proto_depIdxs = nil +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConnInterface + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion6 + +// GripmockClient is the client API for Gripmock service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type GripmockClient interface { + Greet(ctx context.Context, in *bar.Bar, opts ...grpc.CallOption) (*Response, error) +} + +type gripmockClient struct { + cc grpc.ClientConnInterface +} + +func NewGripmockClient(cc grpc.ClientConnInterface) GripmockClient { + return &gripmockClient{cc} +} + +func (c *gripmockClient) Greet(ctx context.Context, in *bar.Bar, opts ...grpc.CallOption) (*Response, error) { + out := new(Response) + err := c.cc.Invoke(ctx, "/multi_package.Gripmock/Greet", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// GripmockServer is the server API for Gripmock service. +type GripmockServer interface { + Greet(context.Context, *bar.Bar) (*Response, error) +} + +// UnimplementedGripmockServer can be embedded to have forward compatible implementations. +type UnimplementedGripmockServer struct { +} + +func (*UnimplementedGripmockServer) Greet(context.Context, *bar.Bar) (*Response, error) { + return nil, status.Errorf(codes.Unimplemented, "method Greet not implemented") +} + +func RegisterGripmockServer(s *grpc.Server, srv GripmockServer) { + s.RegisterService(&_Gripmock_serviceDesc, srv) +} + +func _Gripmock_Greet_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(bar.Bar) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(GripmockServer).Greet(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/multi_package.Gripmock/Greet", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(GripmockServer).Greet(ctx, req.(*bar.Bar)) + } + return interceptor(ctx, in, info, handler) +} + +var _Gripmock_serviceDesc = grpc.ServiceDesc{ + ServiceName: "multi_package.Gripmock", + HandlerType: (*GripmockServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Greet", + Handler: _Gripmock_Greet_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "hello.proto", +} diff --git a/example/no-gopackage/hello.proto b/example/no-gopackage/hello.proto new file mode 100644 index 00000000..14435d99 --- /dev/null +++ b/example/no-gopackage/hello.proto @@ -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); +} + diff --git a/example/no-gopackage/stub/simple.json b/example/no-gopackage/stub/simple.json new file mode 100644 index 00000000..38c6eb21 --- /dev/null +++ b/example/no-gopackage/stub/simple.json @@ -0,0 +1,17 @@ +{ + "service":"Gripmock", + "method":"Greet", + "input":{ + "equals":{ + "name":"tokopedia" + } + }, + "output":{ + "data":{ + "response":"Hello Tokopedia", + "deepbar": { + "address": "Jakarta" + } + } + } +} \ No newline at end of file From 945f4c0724f5f1b873b4a9ad0e9a608809766f93 Mon Sep 17 00:00:00 2001 From: Ahmad Muzakki Date: Fri, 28 Jan 2022 18:28:42 +0700 Subject: [PATCH 3/6] put it in github actions --- .github/workflows/integration-test.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index a2f7890b..5029e065 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -38,3 +38,11 @@ jobs: uses: ./ with: entrypoint: example/one-of/entrypoint.sh + - name: Run stub subfolders example + uses: ./ + with: + entrypoint: example/stub-subfulders/entrypoint.sh + - name: Run no go_package example + uses: ./ + with: + entrypoint: example/no-gopackage/entrypoint.sh From 219c31da98ce80299ad2a65683a08b112ac0f329 Mon Sep 17 00:00:00 2001 From: Ahmad Muzakki Date: Wed, 2 Feb 2022 17:19:13 +0700 Subject: [PATCH 4/6] cleanup fix unnecessary logic and remove unused functions. --- fix_gopackage.sh | 2 +- gripmock.go | 50 +++++++----------------------------------------- 2 files changed, 8 insertions(+), 44 deletions(-) diff --git a/fix_gopackage.sh b/fix_gopackage.sh index 67577773..94d61119 100755 --- a/fix_gopackage.sh +++ b/fix_gopackage.sh @@ -26,7 +26,7 @@ do syntaxLineNum="$(grep -n "syntax" "$newfile" | head -n 1 | cut -d: -f1)" - goPackageString="option go_package = \"github.com/tokopedia/gripmock/$dir\";" + goPackageString="option go_package = \"github.com/tokopedia/gripmock/protogen/$dir\";" sed -i "${syntaxLineNum}s~$~\n$goPackageString~" $newfile echo $newfile done diff --git a/gripmock.go b/gripmock.go index 83cae790..c02ae202 100644 --- a/gripmock.go +++ b/gripmock.go @@ -86,12 +86,6 @@ func main() { } } -func getProtoName(path string) string { - paths := strings.Split(path, "/") - filename := paths[len(paths)-1] - return strings.Split(filename, ".")[0] -} - type protocParam struct { protoPath []string adminPort string @@ -102,11 +96,14 @@ type protocParam struct { } func generateProtoc(param protocParam) { - - fixGoPackageUnderImportPath(getDirFromFile(param.protoPath[0])) param.protoPath = fixGoPackage(param.protoPath) + protodirs := strings.Split(param.protoPath[0], "/") + protodir := "" + if len(protodirs) > 0 { + protodir = strings.Join(protodirs[:len(protodirs)-1], "/") + "/" + } - args := []string{"-I", getDirFromFile(param.protoPath[0])} + args := []string{"-I", protodir} // include well-known-types for _, i := range param.imports { args = append(args, "-I", i) @@ -129,28 +126,7 @@ func generateProtoc(param protocParam) { } -func getDirFromFile(filepath string) string { - protodirs := strings.Split(filepath, "/") - dir := "." - if len(protodirs) > 0 { - dir = strings.Join(protodirs[:len(protodirs)-1], "/") + "/" - } - return dir -} - -func fixGoPackageUnderImportPath(importPath string) { - findProtos := exec.Command("find", importPath, "-mindepth", "2", "-name", "*.proto") - buf := &bytes.Buffer{} - findProtos.Stdout = buf - err := findProtos.Run() - if err != nil { - log.Println(err) - return - } - - fixGoPackage(strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")) -} - +// append gopackage in proto files if doesn't have any func fixGoPackage(protoPaths []string) []string { fixgopackage := exec.Command("fix_gopackage.sh", protoPaths...) buf := &bytes.Buffer{} @@ -165,18 +141,6 @@ func fixGoPackage(protoPaths []string) []string { return strings.Split(strings.TrimRight(buf.String(), "\n"), "\n") } -func buildServer(output string) { - args := []string{"build", "-o", output + "grpcserver", output} - - build := exec.Command("go", args...) - build.Stdout = os.Stdout - build.Stderr = os.Stderr - err := build.Run() - if err != nil { - log.Fatal(err) - } -} - func runGrpcServer(output string) (*exec.Cmd, <-chan error) { run := exec.Command("go", "run", output+"server.go") run.Stdout = os.Stdout From f41078412590eb54cde1ed7ea28b50e7b5d96176 Mon Sep 17 00:00:00 2001 From: Ahmad Muzakki Date: Wed, 2 Feb 2022 17:20:31 +0700 Subject: [PATCH 5/6] fix example move deep/bar under bar. remove pb.go here since it should be generated under protogen/ --- example/no-gopackage/bar/bar.pb.go | 144 ---------------- .../{deep/bar => bar/deep}/bar.proto | 0 example/no-gopackage/client/main.go | 6 +- example/no-gopackage/deep/bar/bar.pb.go | 145 ---------------- example/no-gopackage/foo.pb.go | 162 ------------------ example/no-gopackage/foo.proto | 2 +- example/no-gopackage/hello.pb.go | 160 ----------------- 7 files changed, 4 insertions(+), 615 deletions(-) delete mode 100644 example/no-gopackage/bar/bar.pb.go rename example/no-gopackage/{deep/bar => bar/deep}/bar.proto (100%) delete mode 100644 example/no-gopackage/deep/bar/bar.pb.go delete mode 100644 example/no-gopackage/foo.pb.go delete mode 100644 example/no-gopackage/hello.pb.go diff --git a/example/no-gopackage/bar/bar.pb.go b/example/no-gopackage/bar/bar.pb.go deleted file mode 100644 index 46b0b5a5..00000000 --- a/example/no-gopackage/bar/bar.pb.go +++ /dev/null @@ -1,144 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.27.1 -// protoc v3.17.3 -// source: bar/bar.proto - -package bar - -import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" -) - -const ( - // Verify that this generated code is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) - // Verify that runtime/protoimpl is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) -) - -type Bar struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` -} - -func (x *Bar) Reset() { - *x = Bar{} - if protoimpl.UnsafeEnabled { - mi := &file_bar_bar_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *Bar) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*Bar) ProtoMessage() {} - -func (x *Bar) ProtoReflect() protoreflect.Message { - mi := &file_bar_bar_proto_msgTypes[0] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use Bar.ProtoReflect.Descriptor instead. -func (*Bar) Descriptor() ([]byte, []int) { - return file_bar_bar_proto_rawDescGZIP(), []int{0} -} - -func (x *Bar) GetName() string { - if x != nil { - return x.Name - } - return "" -} - -var File_bar_bar_proto protoreflect.FileDescriptor - -var file_bar_bar_proto_rawDesc = []byte{ - 0x0a, 0x0d, 0x62, 0x61, 0x72, 0x2f, 0x62, 0x61, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, - 0x03, 0x62, 0x61, 0x72, 0x22, 0x19, 0x0a, 0x03, 0x42, 0x61, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x6e, - 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x42, - 0x38, 0x5a, 0x36, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x6f, - 0x6b, 0x6f, 0x70, 0x65, 0x64, 0x69, 0x61, 0x2f, 0x67, 0x72, 0x69, 0x70, 0x6d, 0x6f, 0x63, 0x6b, - 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x6e, 0x6f, 0x2d, 0x67, 0x6f, 0x70, 0x61, - 0x63, 0x6b, 0x61, 0x67, 0x65, 0x2f, 0x62, 0x61, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x33, -} - -var ( - file_bar_bar_proto_rawDescOnce sync.Once - file_bar_bar_proto_rawDescData = file_bar_bar_proto_rawDesc -) - -func file_bar_bar_proto_rawDescGZIP() []byte { - file_bar_bar_proto_rawDescOnce.Do(func() { - file_bar_bar_proto_rawDescData = protoimpl.X.CompressGZIP(file_bar_bar_proto_rawDescData) - }) - return file_bar_bar_proto_rawDescData -} - -var file_bar_bar_proto_msgTypes = make([]protoimpl.MessageInfo, 1) -var file_bar_bar_proto_goTypes = []interface{}{ - (*Bar)(nil), // 0: bar.Bar -} -var file_bar_bar_proto_depIdxs = []int32{ - 0, // [0:0] is the sub-list for method output_type - 0, // [0:0] is the sub-list for method input_type - 0, // [0:0] is the sub-list for extension type_name - 0, // [0:0] is the sub-list for extension extendee - 0, // [0:0] is the sub-list for field type_name -} - -func init() { file_bar_bar_proto_init() } -func file_bar_bar_proto_init() { - if File_bar_bar_proto != nil { - return - } - if !protoimpl.UnsafeEnabled { - file_bar_bar_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Bar); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - } - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_bar_bar_proto_rawDesc, - NumEnums: 0, - NumMessages: 1, - NumExtensions: 0, - NumServices: 0, - }, - GoTypes: file_bar_bar_proto_goTypes, - DependencyIndexes: file_bar_bar_proto_depIdxs, - MessageInfos: file_bar_bar_proto_msgTypes, - }.Build() - File_bar_bar_proto = out.File - file_bar_bar_proto_rawDesc = nil - file_bar_bar_proto_goTypes = nil - file_bar_bar_proto_depIdxs = nil -} diff --git a/example/no-gopackage/deep/bar/bar.proto b/example/no-gopackage/bar/deep/bar.proto similarity index 100% rename from example/no-gopackage/deep/bar/bar.proto rename to example/no-gopackage/bar/deep/bar.proto diff --git a/example/no-gopackage/client/main.go b/example/no-gopackage/client/main.go index 521e3e47..b2d7c445 100644 --- a/example/no-gopackage/client/main.go +++ b/example/no-gopackage/client/main.go @@ -6,12 +6,12 @@ import ( "os" "time" - pb "github.com/tokopedia/gripmock/example/no-gopackage" - no_gopackage "github.com/tokopedia/gripmock/example/no-gopackage/bar" + 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=.. ../hello.proto ../foo.proto ../bar/bar.proto ../deep/bar/bar.proto +//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() diff --git a/example/no-gopackage/deep/bar/bar.pb.go b/example/no-gopackage/deep/bar/bar.pb.go deleted file mode 100644 index 14787f46..00000000 --- a/example/no-gopackage/deep/bar/bar.pb.go +++ /dev/null @@ -1,145 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.27.1 -// protoc v3.17.3 -// source: deep/bar/bar.proto - -package bar - -import ( - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" -) - -const ( - // Verify that this generated code is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) - // Verify that runtime/protoimpl is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) -) - -type DeepBar struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"` -} - -func (x *DeepBar) Reset() { - *x = DeepBar{} - if protoimpl.UnsafeEnabled { - mi := &file_deep_bar_bar_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *DeepBar) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*DeepBar) ProtoMessage() {} - -func (x *DeepBar) ProtoReflect() protoreflect.Message { - mi := &file_deep_bar_bar_proto_msgTypes[0] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use DeepBar.ProtoReflect.Descriptor instead. -func (*DeepBar) Descriptor() ([]byte, []int) { - return file_deep_bar_bar_proto_rawDescGZIP(), []int{0} -} - -func (x *DeepBar) GetAddress() string { - if x != nil { - return x.Address - } - return "" -} - -var File_deep_bar_bar_proto protoreflect.FileDescriptor - -var file_deep_bar_bar_proto_rawDesc = []byte{ - 0x0a, 0x12, 0x64, 0x65, 0x65, 0x70, 0x2f, 0x62, 0x61, 0x72, 0x2f, 0x62, 0x61, 0x72, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x62, 0x61, 0x72, 0x22, 0x23, 0x0a, 0x07, 0x44, 0x65, 0x65, - 0x70, 0x42, 0x61, 0x72, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x42, 0x3d, - 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x6f, 0x6b, - 0x6f, 0x70, 0x65, 0x64, 0x69, 0x61, 0x2f, 0x67, 0x72, 0x69, 0x70, 0x6d, 0x6f, 0x63, 0x6b, 0x2f, - 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x6e, 0x6f, 0x2d, 0x67, 0x6f, 0x70, 0x61, 0x63, - 0x6b, 0x61, 0x67, 0x65, 0x2f, 0x64, 0x65, 0x65, 0x70, 0x2f, 0x62, 0x61, 0x72, 0x62, 0x06, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x33, -} - -var ( - file_deep_bar_bar_proto_rawDescOnce sync.Once - file_deep_bar_bar_proto_rawDescData = file_deep_bar_bar_proto_rawDesc -) - -func file_deep_bar_bar_proto_rawDescGZIP() []byte { - file_deep_bar_bar_proto_rawDescOnce.Do(func() { - file_deep_bar_bar_proto_rawDescData = protoimpl.X.CompressGZIP(file_deep_bar_bar_proto_rawDescData) - }) - return file_deep_bar_bar_proto_rawDescData -} - -var file_deep_bar_bar_proto_msgTypes = make([]protoimpl.MessageInfo, 1) -var file_deep_bar_bar_proto_goTypes = []interface{}{ - (*DeepBar)(nil), // 0: bar.DeepBar -} -var file_deep_bar_bar_proto_depIdxs = []int32{ - 0, // [0:0] is the sub-list for method output_type - 0, // [0:0] is the sub-list for method input_type - 0, // [0:0] is the sub-list for extension type_name - 0, // [0:0] is the sub-list for extension extendee - 0, // [0:0] is the sub-list for field type_name -} - -func init() { file_deep_bar_bar_proto_init() } -func file_deep_bar_bar_proto_init() { - if File_deep_bar_bar_proto != nil { - return - } - if !protoimpl.UnsafeEnabled { - file_deep_bar_bar_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*DeepBar); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - } - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_deep_bar_bar_proto_rawDesc, - NumEnums: 0, - NumMessages: 1, - NumExtensions: 0, - NumServices: 0, - }, - GoTypes: file_deep_bar_bar_proto_goTypes, - DependencyIndexes: file_deep_bar_bar_proto_depIdxs, - MessageInfos: file_deep_bar_bar_proto_msgTypes, - }.Build() - File_deep_bar_bar_proto = out.File - file_deep_bar_bar_proto_rawDesc = nil - file_deep_bar_bar_proto_goTypes = nil - file_deep_bar_bar_proto_depIdxs = nil -} diff --git a/example/no-gopackage/foo.pb.go b/example/no-gopackage/foo.pb.go deleted file mode 100644 index a4fff64a..00000000 --- a/example/no-gopackage/foo.pb.go +++ /dev/null @@ -1,162 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.27.1 -// protoc v3.17.3 -// source: foo.proto - -// simulating neighboring .proto file -// but different package - -package no_gopackage - -import ( - bar "github.com/tokopedia/gripmock/example/no-gopackage/deep/bar" - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" - sync "sync" -) - -const ( - // Verify that this generated code is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) - // Verify that runtime/protoimpl is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) -) - -type Response struct { - state protoimpl.MessageState - sizeCache protoimpl.SizeCache - unknownFields protoimpl.UnknownFields - - Response string `protobuf:"bytes,1,opt,name=response,proto3" json:"response,omitempty"` - Deepbar *bar.DeepBar `protobuf:"bytes,2,opt,name=deepbar,proto3" json:"deepbar,omitempty"` -} - -func (x *Response) Reset() { - *x = Response{} - if protoimpl.UnsafeEnabled { - mi := &file_foo_proto_msgTypes[0] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) - } -} - -func (x *Response) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*Response) ProtoMessage() {} - -func (x *Response) ProtoReflect() protoreflect.Message { - mi := &file_foo_proto_msgTypes[0] - if protoimpl.UnsafeEnabled && x != nil { - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - if ms.LoadMessageInfo() == nil { - ms.StoreMessageInfo(mi) - } - return ms - } - return mi.MessageOf(x) -} - -// Deprecated: Use Response.ProtoReflect.Descriptor instead. -func (*Response) Descriptor() ([]byte, []int) { - return file_foo_proto_rawDescGZIP(), []int{0} -} - -func (x *Response) GetResponse() string { - if x != nil { - return x.Response - } - return "" -} - -func (x *Response) GetDeepbar() *bar.DeepBar { - if x != nil { - return x.Deepbar - } - return nil -} - -var File_foo_proto protoreflect.FileDescriptor - -var file_foo_proto_rawDesc = []byte{ - 0x0a, 0x09, 0x66, 0x6f, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x66, 0x6f, 0x6f, - 0x1a, 0x12, 0x64, 0x65, 0x65, 0x70, 0x2f, 0x62, 0x61, 0x72, 0x2f, 0x62, 0x61, 0x72, 0x2e, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x4e, 0x0a, 0x08, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, - 0x12, 0x1a, 0x0a, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x26, 0x0a, 0x07, - 0x64, 0x65, 0x65, 0x70, 0x62, 0x61, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, - 0x62, 0x61, 0x72, 0x2e, 0x44, 0x65, 0x65, 0x70, 0x42, 0x61, 0x72, 0x52, 0x07, 0x64, 0x65, 0x65, - 0x70, 0x62, 0x61, 0x72, 0x42, 0x34, 0x5a, 0x32, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x74, 0x6f, 0x6b, 0x6f, 0x70, 0x65, 0x64, 0x69, 0x61, 0x2f, 0x67, 0x72, 0x69, - 0x70, 0x6d, 0x6f, 0x63, 0x6b, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x6e, 0x6f, - 0x2d, 0x67, 0x6f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, -} - -var ( - file_foo_proto_rawDescOnce sync.Once - file_foo_proto_rawDescData = file_foo_proto_rawDesc -) - -func file_foo_proto_rawDescGZIP() []byte { - file_foo_proto_rawDescOnce.Do(func() { - file_foo_proto_rawDescData = protoimpl.X.CompressGZIP(file_foo_proto_rawDescData) - }) - return file_foo_proto_rawDescData -} - -var file_foo_proto_msgTypes = make([]protoimpl.MessageInfo, 1) -var file_foo_proto_goTypes = []interface{}{ - (*Response)(nil), // 0: foo.Response - (*bar.DeepBar)(nil), // 1: bar.DeepBar -} -var file_foo_proto_depIdxs = []int32{ - 1, // 0: foo.Response.deepbar:type_name -> bar.DeepBar - 1, // [1:1] is the sub-list for method output_type - 1, // [1:1] is the sub-list for method input_type - 1, // [1:1] is the sub-list for extension type_name - 1, // [1:1] is the sub-list for extension extendee - 0, // [0:1] is the sub-list for field type_name -} - -func init() { file_foo_proto_init() } -func file_foo_proto_init() { - if File_foo_proto != nil { - return - } - if !protoimpl.UnsafeEnabled { - file_foo_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { - switch v := v.(*Response); i { - case 0: - return &v.state - case 1: - return &v.sizeCache - case 2: - return &v.unknownFields - default: - return nil - } - } - } - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_foo_proto_rawDesc, - NumEnums: 0, - NumMessages: 1, - NumExtensions: 0, - NumServices: 0, - }, - GoTypes: file_foo_proto_goTypes, - DependencyIndexes: file_foo_proto_depIdxs, - MessageInfos: file_foo_proto_msgTypes, - }.Build() - File_foo_proto = out.File - file_foo_proto_rawDesc = nil - file_foo_proto_goTypes = nil - file_foo_proto_depIdxs = nil -} diff --git a/example/no-gopackage/foo.proto b/example/no-gopackage/foo.proto index bfca5893..dfee27c1 100644 --- a/example/no-gopackage/foo.proto +++ b/example/no-gopackage/foo.proto @@ -4,7 +4,7 @@ syntax = "proto3"; // but different package package foo; -import "deep/bar/bar.proto"; +import "bar/deep/bar.proto"; message Response { string response = 1; diff --git a/example/no-gopackage/hello.pb.go b/example/no-gopackage/hello.pb.go deleted file mode 100644 index 48a3d8ba..00000000 --- a/example/no-gopackage/hello.pb.go +++ /dev/null @@ -1,160 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// versions: -// protoc-gen-go v1.27.1 -// protoc v3.17.3 -// source: hello.proto - -package no_gopackage - -import ( - context "context" - bar "github.com/tokopedia/gripmock/example/no-gopackage/bar" - grpc "google.golang.org/grpc" - codes "google.golang.org/grpc/codes" - status "google.golang.org/grpc/status" - protoreflect "google.golang.org/protobuf/reflect/protoreflect" - protoimpl "google.golang.org/protobuf/runtime/protoimpl" - reflect "reflect" -) - -const ( - // Verify that this generated code is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) - // Verify that runtime/protoimpl is sufficiently up-to-date. - _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) -) - -var File_hello_proto protoreflect.FileDescriptor - -var file_hello_proto_rawDesc = []byte{ - 0x0a, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0d, 0x6d, - 0x75, 0x6c, 0x74, 0x69, 0x5f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x1a, 0x0d, 0x62, 0x61, - 0x72, 0x2f, 0x62, 0x61, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x09, 0x66, 0x6f, 0x6f, - 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x32, 0x2c, 0x0a, 0x08, 0x47, 0x72, 0x69, 0x70, 0x6d, 0x6f, - 0x63, 0x6b, 0x12, 0x20, 0x0a, 0x05, 0x47, 0x72, 0x65, 0x65, 0x74, 0x12, 0x08, 0x2e, 0x62, 0x61, - 0x72, 0x2e, 0x42, 0x61, 0x72, 0x1a, 0x0d, 0x2e, 0x66, 0x6f, 0x6f, 0x2e, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x34, 0x5a, 0x32, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, - 0x6f, 0x6d, 0x2f, 0x74, 0x6f, 0x6b, 0x6f, 0x70, 0x65, 0x64, 0x69, 0x61, 0x2f, 0x67, 0x72, 0x69, - 0x70, 0x6d, 0x6f, 0x63, 0x6b, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x6e, 0x6f, - 0x2d, 0x67, 0x6f, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, -} - -var file_hello_proto_goTypes = []interface{}{ - (*bar.Bar)(nil), // 0: bar.Bar - (*Response)(nil), // 1: foo.Response -} -var file_hello_proto_depIdxs = []int32{ - 0, // 0: multi_package.Gripmock.Greet:input_type -> bar.Bar - 1, // 1: multi_package.Gripmock.Greet:output_type -> foo.Response - 1, // [1:2] is the sub-list for method output_type - 0, // [0:1] is the sub-list for method input_type - 0, // [0:0] is the sub-list for extension type_name - 0, // [0:0] is the sub-list for extension extendee - 0, // [0:0] is the sub-list for field type_name -} - -func init() { file_hello_proto_init() } -func file_hello_proto_init() { - if File_hello_proto != nil { - return - } - file_foo_proto_init() - type x struct{} - out := protoimpl.TypeBuilder{ - File: protoimpl.DescBuilder{ - GoPackagePath: reflect.TypeOf(x{}).PkgPath(), - RawDescriptor: file_hello_proto_rawDesc, - NumEnums: 0, - NumMessages: 0, - NumExtensions: 0, - NumServices: 1, - }, - GoTypes: file_hello_proto_goTypes, - DependencyIndexes: file_hello_proto_depIdxs, - }.Build() - File_hello_proto = out.File - file_hello_proto_rawDesc = nil - file_hello_proto_goTypes = nil - file_hello_proto_depIdxs = nil -} - -// Reference imports to suppress errors if they are not otherwise used. -var _ context.Context -var _ grpc.ClientConnInterface - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the grpc package it is being compiled against. -const _ = grpc.SupportPackageIsVersion6 - -// GripmockClient is the client API for Gripmock service. -// -// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. -type GripmockClient interface { - Greet(ctx context.Context, in *bar.Bar, opts ...grpc.CallOption) (*Response, error) -} - -type gripmockClient struct { - cc grpc.ClientConnInterface -} - -func NewGripmockClient(cc grpc.ClientConnInterface) GripmockClient { - return &gripmockClient{cc} -} - -func (c *gripmockClient) Greet(ctx context.Context, in *bar.Bar, opts ...grpc.CallOption) (*Response, error) { - out := new(Response) - err := c.cc.Invoke(ctx, "/multi_package.Gripmock/Greet", in, out, opts...) - if err != nil { - return nil, err - } - return out, nil -} - -// GripmockServer is the server API for Gripmock service. -type GripmockServer interface { - Greet(context.Context, *bar.Bar) (*Response, error) -} - -// UnimplementedGripmockServer can be embedded to have forward compatible implementations. -type UnimplementedGripmockServer struct { -} - -func (*UnimplementedGripmockServer) Greet(context.Context, *bar.Bar) (*Response, error) { - return nil, status.Errorf(codes.Unimplemented, "method Greet not implemented") -} - -func RegisterGripmockServer(s *grpc.Server, srv GripmockServer) { - s.RegisterService(&_Gripmock_serviceDesc, srv) -} - -func _Gripmock_Greet_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(bar.Bar) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(GripmockServer).Greet(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/multi_package.Gripmock/Greet", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(GripmockServer).Greet(ctx, req.(*bar.Bar)) - } - return interceptor(ctx, in, info, handler) -} - -var _Gripmock_serviceDesc = grpc.ServiceDesc{ - ServiceName: "multi_package.Gripmock", - HandlerType: (*GripmockServer)(nil), - Methods: []grpc.MethodDesc{ - { - MethodName: "Greet", - Handler: _Gripmock_Greet_Handler, - }, - }, - Streams: []grpc.StreamDesc{}, - Metadata: "hello.proto", -} From dfe47068664546f6c5d2f9ce188d1de9e4189b85 Mon Sep 17 00:00:00 2001 From: Ahmad Muzakki Date: Wed, 2 Feb 2022 17:39:16 +0700 Subject: [PATCH 6/6] fix typo fix typo in entrypoint.sh and integration-test.yml --- .github/workflows/integration-test.yml | 2 +- example/no-gopackage/entrypoint.sh | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index 5029e065..be6a261e 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -41,7 +41,7 @@ jobs: - name: Run stub subfolders example uses: ./ with: - entrypoint: example/stub-subfulders/entrypoint.sh + entrypoint: example/stub-subfolders/entrypoint.sh - name: Run no go_package example uses: ./ with: diff --git a/example/no-gopackage/entrypoint.sh b/example/no-gopackage/entrypoint.sh index 982ddcb1..e10bcb63 100755 --- a/example/no-gopackage/entrypoint.sh +++ b/example/no-gopackage/entrypoint.sh @@ -2,13 +2,12 @@ # this file is used by .github/workflows/integration-test.yml -# we need to add example/multi-package ar as included import path -# without it protoc could not find the bar/bar.proto -gripmock --stub=example/multi-package/stub --imports=example/multi-package/ \ - example/multi-package/foo.proto example/multi-package/hello.proto \ - example/multi-package/bar/bar.proto & +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/multi-package/client/*.go \ No newline at end of file +go run example/no-gopackage/client/*.go \ No newline at end of file