-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
189 additions
and
22 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
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,46 @@ | ||
package kit | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
|
||
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin" | ||
"github.com/pseudomuto/protokit" | ||
) | ||
|
||
type Plugin interface { | ||
Generate(req *plugin_go.CodeGeneratorRequest) (*plugin_go.CodeGeneratorResponse, error) | ||
} | ||
|
||
type Kit struct { | ||
} | ||
|
||
func New() Kit { | ||
return Kit{} | ||
} | ||
|
||
func (k Kit) RunPluginWithIO(p Plugin, r io.Reader, w io.Writer) error { | ||
in, err := io.ReadAll(r) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err = os.WriteFile("testdata/in.bin", in, 0o644); err != nil { | ||
panic(err) | ||
} | ||
|
||
var out bytes.Buffer | ||
|
||
errPlugin := protokit.RunPluginWithIO(p, bytes.NewBuffer(in), &out) | ||
|
||
if err = os.WriteFile("testdata/out.bin", out.Bytes(), 0o644); err != nil { | ||
panic(err) | ||
} | ||
|
||
if _, err = io.Copy(w, &out); err != nil { | ||
panic(err) | ||
} | ||
|
||
return errPlugin | ||
} |
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,39 @@ | ||
package kit_test | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"testing" | ||
|
||
"github.com/Djarvur/protoc-gen-python-grpc/internal/generator" | ||
"github.com/Djarvur/protoc-gen-python-grpc/internal/kit" | ||
"github.com/Djarvur/protoc-gen-python-grpc/internal/template" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
fileNameSuffixDefault = "_pb2_grpc.py" | ||
) | ||
|
||
var ( | ||
//go:embed testdata/in.bin | ||
inBytes []byte | ||
|
||
//go:embed testdata/out.bin | ||
outBytes []byte | ||
) | ||
|
||
func TestRunPluginWithIO(t *testing.T) { | ||
t.Parallel() | ||
|
||
out := &bytes.Buffer{} | ||
|
||
err := kit.New().RunPluginWithIO( | ||
generator.New(fileNameSuffixDefault, template.NewTemplateValue().Source()), | ||
bytes.NewBuffer(inBytes), | ||
out, | ||
) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, out.Bytes(), outBytes) | ||
} |
Binary file not shown.
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,67 @@ | ||
z� | ||
test/v1/calls_pb2_grpc.pyz�# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! | ||
"""Client and server classes corresponding to protobuf-defined services.""" | ||
import grpc | ||
|
||
from test.v1 import calls_pb2 as test_dot_v1_dot_calls__pb2 | ||
|
||
|
||
class CallsServiceStub(object): | ||
"""Missing associated documentation comment in .proto file.""" | ||
|
||
def __init__(self, channel): | ||
"""Constructor. | ||
|
||
Args: | ||
channel: A grpc.Channel. | ||
""" | ||
self.Call = channel.unary_unary( | ||
'/test.v1.CallsService/Call', | ||
request_serializer=test_dot_v1_dot_calls__pb2.CallRequest.SerializeToString, | ||
response_deserializer=test_dot_v1_dot_calls__pb2.CallResponse.FromString, | ||
) | ||
|
||
|
||
class CallsServiceServicer(object): | ||
"""Missing associated documentation comment in .proto file.""" | ||
|
||
def Call(self, request, context): | ||
"""Missing associated documentation comment in .proto file.""" | ||
context.set_code(grpc.StatusCode.UNIMPLEMENTED) | ||
context.set_details('Method not implemented!') | ||
raise NotImplementedError('Method not implemented!') | ||
|
||
|
||
def add_CallsServiceServicer_to_server(servicer, server): | ||
rpc_method_handlers = { | ||
'Call': grpc.unary_unary_rpc_method_handler( | ||
servicer.Call, | ||
request_deserializer=test_dot_v1_dot_calls__pb2.CallRequest.FromString, | ||
response_serializer=test_dot_v1_dot_calls__pb2.CallResponse.SerializeToString, | ||
), | ||
} | ||
generic_handler = grpc.method_handlers_generic_handler( | ||
'test.v1.CallsService', rpc_method_handlers) | ||
server.add_generic_rpc_handlers((generic_handler,)) | ||
|
||
|
||
# This class is part of an EXPERIMENTAL API. | ||
class CallsService(object): | ||
"""Missing associated documentation comment in .proto file.""" | ||
|
||
@staticmethod | ||
def Call(request, | ||
target, | ||
options=(), | ||
channel_credentials=None, | ||
call_credentials=None, | ||
insecure=False, | ||
compression=None, | ||
wait_for_ready=None, | ||
timeout=None, | ||
metadata=None): | ||
return grpc.experimental.unary_unary(request, target, '/test.v1.CallsService/Call', | ||
test_dot_v1_dot_calls__pb2.CallRequest.SerializeToString, | ||
test_dot_v1_dot_calls__pb2.CallResponse.FromString, | ||
options, channel_credentials, | ||
insecure, call_credentials, compression, wait_for_ready, timeout, metadata) |
File renamed without changes.
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