Пример #1
0
func generateServices(g *generator.Generator, file *generator.FileDescriptor) {
	g.P("// Services")
	g.P()
	for i, s := range file.Service {
		if i > 0 {
			g.P("/////////////////////////////////")
		}
		generateService(g, s)
	}
}
Пример #2
0
func generateService(g *generator.Generator, s *descriptor.ServiceDescriptorProto) {

	// interface
	g.P()
	g.P("type ", s.Name, " interface {")
	g.In()
	for _, m := range s.Method {
		g.P(generator.CamelCase(*m.Name),
			"(req *", typeName(g, *m.InputType),
			", resp *", typeName(g, *m.OutputType), ") error")
	}
	g.Out()
	g.P("}")

	// register
	g.P()
	g.P("func Register", s.Name, "(service ", s.Name, ")  {")
	g.In()
	g.P("rpc.RegisterName(", strconv.Quote(*s.Name), ", service)")
	g.Out()
	g.P("}")

	client_name := *s.Name + "Client"
	// new client
	g.P()
	g.P("func New", client_name, "(conn io.ReadWriteCloser) *", client_name, " {")
	g.In()
	g.P("codec := muduorpc.NewClientCodec(conn)")
	g.P("client := rpc.NewClientWithCodec(codec)")
	g.P("return &", client_name, "{client}")
	g.Out()
	g.P("}")

	// client
	g.P()
	g.P("type ", client_name, " struct {")
	g.In()
	g.P("client *rpc.Client")
	g.Out()
	g.P("}")

	// client methods
	g.P()
	g.P("func (c *", client_name, ") Close () error {")
	g.In()
	g.P("return c.client.Close()")
	g.Out()
	g.P("}")

	for _, m := range s.Method {
		g.P()
		g.P("func (c *", client_name, ") ", generator.CamelCase(*m.Name),
			"(req *", typeName(g, *m.InputType),
			", resp *", typeName(g, *m.OutputType), ") error {")
		g.In()
		name := *s.Name + "." + *m.Name
		g.P("return c.client.Call(", strconv.Quote(name), ", req, resp)")
		g.Out()
		g.P("}")
	}
}
Пример #3
0
func typeName(g *generator.Generator, t string) string {
	return g.TypeName(g.ObjectNamed(t))
}