generate SendAndClose() and CloseAndRecv() on streams. #7

Merged
itzmanish merged 1 commits from protoc-gen-micro-fix into master 2020-11-07 15:53:01 +03:00
itzmanish commented 2020-11-07 12:34:05 +03:00 (Migrated from github.com)

This PR is applicable on cmd/protoc-gen-micro plugin.

This PR adds method SendAndClose() and CloseAndRecv() on stream.
So that nobody who comes directly from using grpc vanilla client don't get confused on finding SendAndClose() and CloseAndRecv() method.

However Close() method is still presend along with these two methods.

This PR is applicable on cmd/protoc-gen-micro plugin. This PR adds method SendAndClose() and CloseAndRecv() on stream. So that nobody who comes directly from using grpc vanilla client don't get confused on finding SendAndClose() and CloseAndRecv() method. However Close() method is still presend along with these two methods.
vtolstov commented 2020-11-07 14:11:38 +03:00 (Migrated from github.com)

CloseAndRecv looks strange may be RecvAndClose ? Can you share the link to docs or for some generated code to check?

CloseAndRecv looks strange may be RecvAndClose ? Can you share the link to docs or for some generated code to check?
itzmanish commented 2020-11-07 15:44:17 +03:00 (Migrated from github.com)

CloseAndRecv looks strange may be RecvAndClose ?

RecvAndClose may sounds good but under the hood first it fires close and then Recv that's why it is CloseAndRecv.

Can you share the link to docs or for some generated code to check?

greeting.proto

syntax = "proto3";

package greeting;

service GreetService { rpc Greet(stream Request) returns (Response); }

message Request { string name = 1; }

message Response { string greeting = 1; }

greeting.pb.micro.go

// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: greeting.proto

package greeting

import (
	fmt "fmt"
	proto "github.com/golang/protobuf/proto"
	math "math"
)

import (
	context "context"
	api "github.com/unistack-org/micro/v3/api"
	client "github.com/unistack-org/micro/v3/client"
	server "github.com/unistack-org/micro/v3/server"
)

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package

// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option

// Api Endpoints for GreetService service

func NewGreetServiceEndpoints() []*api.Endpoint {
	return []*api.Endpoint{}
}

// Client API for GreetService service

type GreetService interface {
	Greet(ctx context.Context, opts ...client.CallOption) (GreetService_GreetService, error)
}

type greetService struct {
	c    client.Client
	name string
}

func NewGreetService(name string, c client.Client) GreetService {
	return &greetService{
		c:    c,
		name: name,
	}
}

func (c *greetService) Greet(ctx context.Context, opts ...client.CallOption) (GreetService_GreetService, error) {
	stream, err := c.c.Stream(ctx, c.c.NewRequest(c.name, "GreetService.Greet", &Request{}), opts...)
	if err != nil {
		return nil, err
	}
	return &greetServiceGreet{stream}, nil
}

type GreetService_GreetService interface {
	Context() context.Context
	SendMsg(interface{}) error
	RecvMsg(interface{}) error
	CloseAndRecv() (*Response, error)
	Close() error
	Send(*Request) error
}

type greetServiceGreet struct {
	stream client.Stream
}

func (x *greetServiceGreet) CloseAndRecv() (*Response, error) {
	if err := x.stream.Close(); err != nil {
		return nil, err
	}
	r := new(Response)
	err := x.RecvMsg(r)
	return r, err
}

func (x *greetServiceGreet) Close() error {
	return x.stream.Close()
}

func (x *greetServiceGreet) Context() context.Context {
	return x.stream.Context()
}

func (x *greetServiceGreet) SendMsg(m interface{}) error {
	return x.stream.Send(m)
}

func (x *greetServiceGreet) RecvMsg(m interface{}) error {
	return x.stream.Recv(m)
}

func (x *greetServiceGreet) Send(m *Request) error {
	return x.stream.Send(m)
}

// Server API for GreetService service

type GreetServiceHandler interface {
	Greet(context.Context, GreetService_GreetStream) error
}

func RegisterGreetServiceHandler(s server.Server, hdlr GreetServiceHandler, opts ...server.HandlerOption) error {
	type greetService interface {
		Greet(ctx context.Context, stream server.Stream) error
	}
	type GreetService struct {
		greetService
	}
	h := &greetServiceHandler{hdlr}
	return s.Handle(s.NewHandler(&GreetService{h}, opts...))
}

type greetServiceHandler struct {
	GreetServiceHandler
}

func (h *greetServiceHandler) Greet(ctx context.Context, stream server.Stream) error {
	return h.GreetServiceHandler.Greet(ctx, &greetServiceGreetStream{stream})
}

type GreetService_GreetStream interface {
	Context() context.Context
	SendMsg(interface{}) error
	RecvMsg(interface{}) error
	SendAndClose(*Response) error
	Close() error
	Recv() (*Request, error)
}

type greetServiceGreetStream struct {
	stream server.Stream
}

func (x *greetServiceGreetStream) SendAndClose(in *Response) error {
	if err := x.SendMsg(in); err != nil {
		return err
	}
	return x.stream.Close()
}

func (x *greetServiceGreetStream) Close() error {
	return x.stream.Close()
}

func (x *greetServiceGreetStream) Context() context.Context {
	return x.stream.Context()
}

func (x *greetServiceGreetStream) SendMsg(m interface{}) error {
	return x.stream.Send(m)
}

func (x *greetServiceGreetStream) RecvMsg(m interface{}) error {
	return x.stream.Recv(m)
}

func (x *greetServiceGreetStream) Recv() (*Request, error) {
	m := &Request{}
	if err := x.stream.Recv(m); err != nil {
		return nil, err
	}
	return m, nil
}
> CloseAndRecv looks strange may be RecvAndClose ? RecvAndClose may sounds good but under the hood first it fires close and then Recv that's why it is CloseAndRecv. > Can you share the link to docs or for some generated code to check? `greeting.proto` ``` syntax = "proto3"; package greeting; service GreetService { rpc Greet(stream Request) returns (Response); } message Request { string name = 1; } message Response { string greeting = 1; } ``` `greeting.pb.micro.go` ``` // Code generated by protoc-gen-micro. DO NOT EDIT. // source: greeting.proto package greeting import ( fmt "fmt" proto "github.com/golang/protobuf/proto" math "math" ) import ( context "context" api "github.com/unistack-org/micro/v3/api" client "github.com/unistack-org/micro/v3/client" server "github.com/unistack-org/micro/v3/server" ) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal var _ = fmt.Errorf var _ = math.Inf // This is a compile-time assertion to ensure that this generated file // is compatible with the proto package it is being compiled against. // A compilation error at this line likely means your copy of the // proto package needs to be updated. const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package // Reference imports to suppress errors if they are not otherwise used. var _ api.Endpoint var _ context.Context var _ client.Option var _ server.Option // Api Endpoints for GreetService service func NewGreetServiceEndpoints() []*api.Endpoint { return []*api.Endpoint{} } // Client API for GreetService service type GreetService interface { Greet(ctx context.Context, opts ...client.CallOption) (GreetService_GreetService, error) } type greetService struct { c client.Client name string } func NewGreetService(name string, c client.Client) GreetService { return &greetService{ c: c, name: name, } } func (c *greetService) Greet(ctx context.Context, opts ...client.CallOption) (GreetService_GreetService, error) { stream, err := c.c.Stream(ctx, c.c.NewRequest(c.name, "GreetService.Greet", &Request{}), opts...) if err != nil { return nil, err } return &greetServiceGreet{stream}, nil } type GreetService_GreetService interface { Context() context.Context SendMsg(interface{}) error RecvMsg(interface{}) error CloseAndRecv() (*Response, error) Close() error Send(*Request) error } type greetServiceGreet struct { stream client.Stream } func (x *greetServiceGreet) CloseAndRecv() (*Response, error) { if err := x.stream.Close(); err != nil { return nil, err } r := new(Response) err := x.RecvMsg(r) return r, err } func (x *greetServiceGreet) Close() error { return x.stream.Close() } func (x *greetServiceGreet) Context() context.Context { return x.stream.Context() } func (x *greetServiceGreet) SendMsg(m interface{}) error { return x.stream.Send(m) } func (x *greetServiceGreet) RecvMsg(m interface{}) error { return x.stream.Recv(m) } func (x *greetServiceGreet) Send(m *Request) error { return x.stream.Send(m) } // Server API for GreetService service type GreetServiceHandler interface { Greet(context.Context, GreetService_GreetStream) error } func RegisterGreetServiceHandler(s server.Server, hdlr GreetServiceHandler, opts ...server.HandlerOption) error { type greetService interface { Greet(ctx context.Context, stream server.Stream) error } type GreetService struct { greetService } h := &greetServiceHandler{hdlr} return s.Handle(s.NewHandler(&GreetService{h}, opts...)) } type greetServiceHandler struct { GreetServiceHandler } func (h *greetServiceHandler) Greet(ctx context.Context, stream server.Stream) error { return h.GreetServiceHandler.Greet(ctx, &greetServiceGreetStream{stream}) } type GreetService_GreetStream interface { Context() context.Context SendMsg(interface{}) error RecvMsg(interface{}) error SendAndClose(*Response) error Close() error Recv() (*Request, error) } type greetServiceGreetStream struct { stream server.Stream } func (x *greetServiceGreetStream) SendAndClose(in *Response) error { if err := x.SendMsg(in); err != nil { return err } return x.stream.Close() } func (x *greetServiceGreetStream) Close() error { return x.stream.Close() } func (x *greetServiceGreetStream) Context() context.Context { return x.stream.Context() } func (x *greetServiceGreetStream) SendMsg(m interface{}) error { return x.stream.Send(m) } func (x *greetServiceGreetStream) RecvMsg(m interface{}) error { return x.stream.Recv(m) } func (x *greetServiceGreetStream) Recv() (*Request, error) { m := &Request{} if err := x.stream.Recv(m); err != nil { return nil, err } return m, nil } ```
vtolstov (Migrated from github.com) approved these changes 2020-11-07 15:52:35 +03:00
vtolstov (Migrated from github.com) left a comment

Thanks

Thanks
Sign in to join this conversation.
No reviewers
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: unistack-org/micro#7
No description provided.