Re: Question, Help needed GRPC transport

115 views
Skip to first unread message

Peter Bourgon

unread,
Jun 24, 2019, 7:31:58 AM6/24/19
to Dan Rusei, Go kit
Let's talk it out.

NewGRPCServer takes a set of endpoints and makes them callable by gRPC
clients. This means the "outside" of the server needs to speak
protobufs to clients (e.g. pb.TableReply) and the "inside" needs to
speak whatever your endpoints expect (e.g. endpoints.TableReply).

So the job of the e.g. decodeListTableRequest function is to take the
pb.TableRequest input from a client, and return a
endpoints.TableRequest output for further processing by your
microservice. The function is responsible for converting from one to
the other. You're doing that part correctly.

Similarly, the job of the encodeListTableResponse function is to go
the other direction: take the endpoints.TableReply input from your
microservice, and return a pb.TableReply output to return to the
client. But (assuming the commented-out code is what you're talking
about) you can't just json.Unmarshal one type to another, the Go type
system doesn't really allow it. You have to do it manually. I think
that's what the code that's not currently commented-out is basically
doing, except it seems to be leaving off the last team for some
reason? I might write that function like this:

func encodeListTableResponse(_ context.Context, response
interface{}) (interface{}, error) {
resp := response.(endpoints.TableReply)

teams := make([]*pb.Table, len(resp.Teams))
for i := range resp.Teams {
teams[i] = &pb.Table{
TeamName: resp.Teams[i].TeamName,
TeamPlayed: resp.Teams[i].TeamPlayed,
TeamWon: resp.Teams[i].TeamWon,
TeamDrawn: resp.Teams[i].TeamDrawn,
TeamLost: resp.Teams[i].TeamLost,
TeamGF: resp.Teams[i].TeamGF,
TeamGA: resp.Teams[i].TeamGA,
TeamGD: resp.Teams[i].TeamGD,
TeamPoints: resp.Teams[i].TeamPoints,
TeamCapital: resp.Teams[i].TeamCapital,
}
}

return &pb.TableReply{
Teams: teams,
Err: err2str(resp.Err),
}, nil
}

Isn't this a lot of boilerplate? In a sense, yes, absolutely. But what
it's accomplishing is separating the data transfer object (the
protobuf file) from the types you use within your service. This allows
you to attach an e.g. HTTP transport to the same service without
having to import protobufs there. If this isn't useful to you, you can
delete your endpoints.TableReply type and just use pb.TableReply
directly throughout all your packages. I don't recommend it, as it
couples your business logic to the transport, which is a layer
violation, but some people consider that easier.

I hope this helps.
Peter.

On Sat, Jun 22, 2019 at 1:27 PM Dan Rusei <dan....@gmail.com> wrote:
>
> I would appreciate if someone can give me a hand to sort it out.
>
> On Saturday, June 15, 2019 at 9:01:45 PM UTC+3, Dan Rusei wrote:
>>
>> Hello All,
>>
>> I'm doing a small personal project to get used with GO-KIT for microservices but I run into an issue and I'm not sure how to fix it.
>> I would appreciate any guidance. I'm getting this error :
>> in the grpc.go file :
>> cannot use resp.Teams (type []*service.Table) as type []*pb.Table in field value
>> cannot use resp.Players (type []service.Player) as type []*pb.Player in field value
>> cannot use resp.Players (type []service.Player) as type []*pb.Player in field value
>>
>> The script is here https://github.com/Danr17/microservices_project/tree/master/stats .
>>
>> Thank you,
>> Dan Rusei
>>
>>
> --
> You received this message because you are subscribed to the Google Groups "Go kit" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to go-kit+un...@googlegroups.com.
> To post to this group, send email to go-...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/go-kit/19897b45-13bc-46be-b591-9fb8a11c3665%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages