ttrpc: remove use of typeurl

Rather than employ the typeurl package, we now generate code to
correctly allocate the incoming types from the caller. As a side-effect
of this activity, the services definitions have been split out into a
separate type that handles the full resolution and dispatch of the
method, incuding correctly mapping the RPC status.

This work is a pre-cursor to larger protocol change that will allow us
to handle multiple, concurrent requests.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day
2017-11-21 18:03:52 -08:00
parent 2d76dba1df
commit 2a81659f49
8 changed files with 225 additions and 136 deletions

View File

@@ -61,16 +61,22 @@ func (p *ttrpcGenerator) genService(fullName string, service *descriptor.Service
p.P()
// registration method
p.P("func Register", serviceName, "(srv *", p.ttrpcPkg.Use(), ".Server, svc ", serviceName, ") error {")
p.P("func Register", serviceName, "(srv *", p.ttrpcPkg.Use(), ".Server, svc ", serviceName, ") {")
p.In()
p.P(`return srv.Register("`, fullName, `", map[string]`, p.ttrpcPkg.Use(), ".Handler{")
p.P(`srv.Register("`, fullName, `", map[string]`, p.ttrpcPkg.Use(), ".Method{")
p.In()
for _, method := range service.Method {
p.P(`"`, method.GetName(), `": `, p.ttrpcPkg.Use(), `.HandlerFunc(func(ctx context.Context, req interface{}) (interface{}, error) {`)
p.P(`"`, method.GetName(), `": `, `func(ctx context.Context, unmarshal func(interface{}) error) (interface{}, error) {`)
p.In()
p.P("return svc.", method.GetName(), "(ctx, req.(*", p.typeName(method.GetInputType()), "))")
p.P("var req ", p.typeName(method.GetInputType()))
p.P(`if err := unmarshal(&req); err != nil {`)
p.In()
p.P(`return nil, err`)
p.Out()
p.P("}),")
p.P(`}`)
p.P("return svc.", method.GetName(), "(ctx, &req)")
p.Out()
p.P("},")
}
p.Out()
p.P("})")
@@ -103,13 +109,13 @@ func (p *ttrpcGenerator) genService(fullName string, service *descriptor.Service
"req *", p.typeName(method.GetInputType()), ") ",
"(*", p.typeName(method.GetOutputType()), ", error) {")
p.In()
p.P("resp, err := c.client.Call(ctx, ", `"`+fullName+`", `, `"`+method.GetName()+`"`, ", req)")
p.P("if err != nil {")
p.P("var resp ", p.typeName(method.GetOutputType()))
p.P("if err := c.client.Call(ctx, ", `"`+fullName+`", `, `"`+method.GetName()+`"`, ", req, &resp); err != nil {")
p.In()
p.P("return nil, err")
p.Out()
p.P("}")
p.P("return resp.(*", p.typeName(method.GetOutputType()), "), nil")
p.P("return &resp, nil")
p.Out()
p.P("}")
}