Beispiel #1
0
func fillTreeWithService(tree *tree, key string, proto *descriptor.ServiceDescriptorProto, loc string, locs map[string]*descriptor.SourceCodeInfo_Location) *service {
	key = fmt.Sprintf("%s.%s", key, proto.GetName())
	tree.services[key] = &service{key: key, comment: getComment(loc, locs), ServiceDescriptorProto: proto}

	// Methods
	for idx, proto := range proto.GetMethod() {
		method := fillTreeWithMethod(tree, key, proto, fmt.Sprintf("%s,2,%d", loc, idx), locs)
		tree.services[key].methods = append(tree.services[key].methods, method)
	}

	return tree.services[key]
}
Beispiel #2
0
// generateService generates all the code for the named service.
func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.ServiceDescriptorProto, index int) {
	//path := fmt.Sprintf("6,%d", index) // 6 means service.

	origServName := service.GetName()
	//fullServName := file.GetPackage() + "." + origServName
	servName := generator.CamelCase(origServName)
	g.P("// Server API for ", servName, " service")
	g.P()

	// Server interface.
	serverType := servName + "Server"
	g.P()

	g.P("func New", servName, "Mux(h ", serverType, ", prefix string) *web.Mux {")
	g.P("	t := _", serverType, "{}")
	g.P("	t.handler = h")
	g.P("	router := web.New()")
	for _, method := range service.Method {
		path := strings.ToLower(servName) + "/" + method.GetName()
		methName := generator.CamelCase(method.GetName())
		// there should be a better way to get the options
		m := method.GetOptions().String()
		if m != "" {
			parts := strings.Split(m, "\"")
			if len(parts) == 3 {
				if parts[0] == "10000:" {
					path = parts[1]
				}
			}
		}
		g.P("router.Handle(prefix+\"", strings.ToLower(path), "\", t.", methName, ")")
	}
	g.P("	return router")
	g.P("}")
	g.P()

	g.P("type _", serverType, " struct {")
	g.P("	handler ", serverType)
	g.P("}")
	g.P()

	// Server handler implementations.
	for _, method := range service.Method {
		g.generateServerMethod(servName, method)
	}

}
func (p *protoBufErrors) lintProtoService(
	pathIndex int32,
	protoService *descriptor.ServiceDescriptorProto,
) {
	path := []int32{
		pathService,
		pathIndex,
	}
	if !isCamelCase(protoService.GetName()) {
		p.addError(&protoBufError{
			path:        path,
			errorCode:   errorServiceCase,
			errorString: protoService.GetName(),
		})
	}

	for i, v := range protoService.GetMethod() {
		p.lintProtoRPCMethod(int32(i), path, v)
	}
}
Beispiel #4
0
func (p *protorpcPlugin) genServiceInterface(
	file *generator.FileDescriptor,
	svc *descriptor.ServiceDescriptorProto,
) {
	const serviceInterfaceTmpl = `
type {{.ServiceName}} interface {
	{{.CallMethodList}}
}
`
	const callMethodTmpl = `
{{.MethodName}}(in *{{.ArgsType}}, out *{{.ReplyType}}) error`

	// gen call method list
	var callMethodList string
	for _, m := range svc.Method {
		out := bytes.NewBuffer([]byte{})
		t := template.Must(template.New("").Parse(callMethodTmpl))
		t.Execute(out, &struct{ ServiceName, MethodName, ArgsType, ReplyType string }{
			ServiceName: generator.CamelCase(svc.GetName()),
			MethodName:  generator.CamelCase(m.GetName()),
			ArgsType:    p.TypeName(p.ObjectNamed(m.GetInputType())),
			ReplyType:   p.TypeName(p.ObjectNamed(m.GetOutputType())),
		})
		callMethodList += out.String()

		p.RecordTypeUse(m.GetInputType())
		p.RecordTypeUse(m.GetOutputType())
	}

	// gen all interface code
	{
		out := bytes.NewBuffer([]byte{})
		t := template.Must(template.New("").Parse(serviceInterfaceTmpl))
		t.Execute(out, &struct{ ServiceName, CallMethodList string }{
			ServiceName:    generator.CamelCase(svc.GetName()),
			CallMethodList: callMethodList,
		})
		p.P(out.String())
	}
}
Beispiel #5
0
// generateService generates all the code for the named service.
func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.ServiceDescriptorProto, index int) {
	path := fmt.Sprintf("6,%d", index) // 6 means service.

	origServName := service.GetName()
	fullServName := origServName
	if pkg := file.GetPackage(); pkg != "" {
		fullServName = pkg + "." + fullServName
	}
	servName := generator.CamelCase(origServName)

	g.P()
	g.P("// Client API for ", servName, " service")
	g.P()

	// Client interface.
	g.P("type ", servName, "Client interface {")
	for i, method := range service.Method {
		g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
		g.P(g.generateClientSignature(servName, method))
	}
	g.P("}")
	g.P()

	// Client structure.
	g.P("type ", unexport(servName), "Client struct {")
	g.P("cc *", grpcPkg, ".ClientConn")
	g.P("}")
	g.P()

	// NewClient factory.
	g.P("func New", servName, "Client (cc *", grpcPkg, ".ClientConn) ", servName, "Client {")
	g.P("return &", unexport(servName), "Client{cc}")
	g.P("}")
	g.P()

	var methodIndex, streamIndex int
	serviceDescVar := "_" + servName + "_serviceDesc"
	// Client method implementations.
	for _, method := range service.Method {
		var descExpr string
		if !method.GetServerStreaming() && !method.GetClientStreaming() {
			// Unary RPC method
			descExpr = fmt.Sprintf("&%s.Methods[%d]", serviceDescVar, methodIndex)
			methodIndex++
		} else {
			// Streaming RPC method
			descExpr = fmt.Sprintf("&%s.Streams[%d]", serviceDescVar, streamIndex)
			streamIndex++
		}
		g.generateClientMethod(servName, fullServName, serviceDescVar, method, descExpr)
	}

	g.P("// Server API for ", servName, " service")
	g.P()

	// Server interface.
	serverType := servName + "Server"
	g.P("type ", serverType, " interface {")
	for i, method := range service.Method {
		g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
		g.P(g.generateServerSignature(servName, method))
	}
	g.P("}")
	g.P()

	// Server registration.
	g.P("func Register", servName, "Server(s *", grpcPkg, ".Server, srv ", serverType, ") {")
	g.P("s.RegisterService(&", serviceDescVar, `, srv)`)
	g.P("}")
	g.P()

	// Server handler implementations.
	var handlerNames []string
	for _, method := range service.Method {
		hname := g.generateServerMethod(servName, method)
		handlerNames = append(handlerNames, hname)
	}

	// Service descriptor.
	g.P("var ", serviceDescVar, " = ", grpcPkg, ".ServiceDesc {")
	g.P("ServiceName: ", strconv.Quote(fullServName), ",")
	g.P("HandlerType: (*", serverType, ")(nil),")
	g.P("Methods: []", grpcPkg, ".MethodDesc{")
	for i, method := range service.Method {
		if method.GetServerStreaming() || method.GetClientStreaming() {
			continue
		}
		g.P("{")
		g.P("MethodName: ", strconv.Quote(method.GetName()), ",")
		g.P("Handler: ", handlerNames[i], ",")
		g.P("},")
	}
	g.P("},")
	g.P("Streams: []", grpcPkg, ".StreamDesc{")
	for i, method := range service.Method {
		if !method.GetServerStreaming() && !method.GetClientStreaming() {
			continue
		}
		g.P("{")
		g.P("StreamName: ", strconv.Quote(method.GetName()), ",")
		g.P("Handler: ", handlerNames[i], ",")
		if method.GetServerStreaming() {
			g.P("ServerStreams: true,")
		}
		if method.GetClientStreaming() {
			g.P("ClientStreams: true,")
		}
		g.P("},")
	}
	g.P("},")
	g.P("}")
	g.P()
}
Beispiel #6
0
func (p *protorpcPlugin) genServiceClient(
	file *generator.FileDescriptor,
	svc *descriptor.ServiceDescriptorProto,
) {
	const clientHelperFuncTmpl = `
type {{.ServiceName}}Client struct {
	*rpc.Client
}

// New{{.ServiceName}}Client returns a {{.ServiceName}} stub to handle
// requests to the set of {{.ServiceName}} at the other end of the connection.
func New{{.ServiceName}}Client(conn io.ReadWriteCloser) (*{{.ServiceName}}Client) {
	c := rpc.NewClientWithCodec(protorpc.NewClientCodec(conn))
	return &{{.ServiceName}}Client{c}
}

{{.MethodList}}

// Dial{{.ServiceName}} connects to an {{.ServiceName}} at the specified network address.
func Dial{{.ServiceName}}(network, addr string) (*{{.ServiceName}}Client, error) {
	c, err := protorpc.Dial(network, addr)
	if err != nil {
		return nil, err
	}
	return &{{.ServiceName}}Client{c}, nil
}

// Dial{{.ServiceName}}Timeout connects to an {{.ServiceName}} at the specified network address.
func Dial{{.ServiceName}}Timeout(network, addr string, timeout time.Duration) (*{{.ServiceName}}Client, error) {
	c, err := protorpc.DialTimeout(network, addr, timeout)
	if err != nil {
		return nil, err
	}
	return &{{.ServiceName}}Client{c}, nil
}
`
	const clientMethodTmpl = `
func (c *{{.ServiceName}}Client) {{.MethodName}}(in *{{.ArgsType}}) (out *{{.ReplyType}}, err error) {
	if in == nil {
		in = new({{.ArgsType}})
	}
	out = new({{.ReplyType}})
	if err = c.Call("{{.ServiceRegisterName}}.{{.MethodName}}", in, out); err != nil {
		return nil, err
	}
	return out, nil
}`

	// gen client method list
	var methodList string
	for _, m := range svc.Method {
		out := bytes.NewBuffer([]byte{})
		t := template.Must(template.New("").Parse(clientMethodTmpl))
		t.Execute(out, &struct{ ServiceName, ServiceRegisterName, MethodName, ArgsType, ReplyType string }{
			ServiceName: generator.CamelCase(svc.GetName()),
			ServiceRegisterName: p.makeServiceRegisterName(
				file, file.GetPackage(), generator.CamelCase(svc.GetName()),
			),
			MethodName: generator.CamelCase(m.GetName()),
			ArgsType:   p.TypeName(p.ObjectNamed(m.GetInputType())),
			ReplyType:  p.TypeName(p.ObjectNamed(m.GetOutputType())),
		})
		methodList += out.String()
	}

	// gen all client code
	{
		out := bytes.NewBuffer([]byte{})
		t := template.Must(template.New("").Parse(clientHelperFuncTmpl))
		t.Execute(out, &struct{ PackageName, ServiceName, MethodList string }{
			PackageName: file.GetPackage(),
			ServiceName: generator.CamelCase(svc.GetName()),
			MethodList:  methodList,
		})
		p.P(out.String())
	}
}
Beispiel #7
0
func (p *protorpcPlugin) genServiceServer(
	file *generator.FileDescriptor,
	svc *descriptor.ServiceDescriptorProto,
) {
	const serviceHelperFunTmpl = `
// Accept{{.ServiceName}}Client accepts connections on the listener and serves requests
// for each incoming connection.  Accept blocks; the caller typically
// invokes it in a go statement.
func Accept{{.ServiceName}}Client(lis net.Listener, x {{.ServiceName}}) {
	srv := rpc.NewServer()
	if err := srv.RegisterName("{{.ServiceRegisterName}}", x); err != nil {
		log.Fatal(err)
	}

	for {
		conn, err := lis.Accept()
		if err != nil {
			log.Fatalf("lis.Accept(): %v\n", err)
		}
		go srv.ServeCodec(protorpc.NewServerCodec(conn))
	}
}

// Register{{.ServiceName}} publish the given {{.ServiceName}} implementation on the server.
func Register{{.ServiceName}}(srv *rpc.Server, x {{.ServiceName}}) error {
	if err := srv.RegisterName("{{.ServiceRegisterName}}", x); err != nil {
		return err
	}
	return nil
}

// New{{.ServiceName}}Server returns a new {{.ServiceName}} Server.
func New{{.ServiceName}}Server(x {{.ServiceName}}) *rpc.Server {
	srv := rpc.NewServer()
	if err := srv.RegisterName("{{.ServiceRegisterName}}", x); err != nil {
		log.Fatal(err)
	}
	return srv
}

// ListenAndServe{{.ServiceName}} listen announces on the local network address laddr
// and serves the given {{.ServiceName}} implementation.
func ListenAndServe{{.ServiceName}}(network, addr string, x {{.ServiceName}}) error {
	lis, err := net.Listen(network, addr)
	if err != nil {
		return err
	}
	defer lis.Close()

	srv := rpc.NewServer()
	if err := srv.RegisterName("{{.ServiceRegisterName}}", x); err != nil {
		return err
	}

	for {
		conn, err := lis.Accept()
		if err != nil {
			log.Fatalf("lis.Accept(): %v\n", err)
		}
		go srv.ServeCodec(protorpc.NewServerCodec(conn))
	}
}
`
	{
		out := bytes.NewBuffer([]byte{})
		t := template.Must(template.New("").Parse(serviceHelperFunTmpl))
		t.Execute(out, &struct{ PackageName, ServiceName, ServiceRegisterName string }{
			PackageName: file.GetPackage(),
			ServiceName: generator.CamelCase(svc.GetName()),
			ServiceRegisterName: p.makeServiceRegisterName(
				file, file.GetPackage(), generator.CamelCase(svc.GetName()),
			),
		})
		p.P(out.String())
	}
}
Beispiel #8
0
// generateService generates all the code for the named service.
func (g *micro) generateService(file *generator.FileDescriptor, service *pb.ServiceDescriptorProto, index int) {
	path := fmt.Sprintf("6,%d", index) // 6 means service.

	origServName := service.GetName()
	serviceName := strings.ToLower(service.GetName())
	if pkg := file.GetPackage(); pkg != "" {
		serviceName = pkg
	}
	servName := generator.CamelCase(origServName)

	g.P()
	g.P("// Client API for ", servName, " service")
	g.P()

	// Client interface.
	g.P("type ", servName, "Client interface {")
	for i, method := range service.Method {
		g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
		g.P(g.generateClientSignature(servName, method))
	}
	g.P("}")
	g.P()

	// Client structure.
	g.P("type ", unexport(servName), "Client struct {")
	g.P("c ", clientPkg, ".Client")
	g.P("serviceName string")
	g.P("}")
	g.P()

	// NewClient factory.
	g.P("func New", servName, "Client (serviceName string, c ", clientPkg, ".Client) ", servName, "Client {")
	g.P("if c == nil {")
	g.P("c = ", clientPkg, ".NewClient()")
	g.P("}")
	g.P("if len(serviceName) == 0 {")
	g.P(`serviceName = "`, serviceName, `"`)
	g.P("}")
	g.P("return &", unexport(servName), "Client{")
	g.P("c: c,")
	g.P("serviceName: serviceName,")
	g.P("}")
	g.P("}")
	g.P()
	var methodIndex, streamIndex int
	serviceDescVar := "_" + servName + "_serviceDesc"
	// Client method implementations.
	for _, method := range service.Method {
		var descExpr string
		if !method.GetServerStreaming() {
			// Unary RPC method
			descExpr = fmt.Sprintf("&%s.Methods[%d]", serviceDescVar, methodIndex)
			methodIndex++
		} else {
			// Streaming RPC method
			descExpr = fmt.Sprintf("&%s.Streams[%d]", serviceDescVar, streamIndex)
			streamIndex++
		}
		g.generateClientMethod(serviceName, servName, serviceDescVar, method, descExpr)
	}

	g.P("// Server API for ", servName, " service")
	g.P()

	// Server interface.
	serverType := servName + "Handler"
	g.P("type ", serverType, " interface {")
	for i, method := range service.Method {
		g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
		g.P(g.generateServerSignature(servName, method))
	}
	g.P("}")
	g.P()
	// Server registration.
	g.P("func Register", servName, "Handler(s ", serverPkg, ".Server, hdlr ", serverType, ", opts ...", serverPkg, ".HandlerOption) {")
	g.P("s.Handle(s.NewHandler(&", servName, "{hdlr}, opts...))")
	g.P("}")
	g.P()

	// Handler type
	g.P("type ", servName, " struct {")
	g.P(serverType)
	g.P("}")
	g.P()

	// Server handler implementations.
	var handlerNames []string
	for _, method := range service.Method {
		hname := g.generateServerMethod(servName, method)
		handlerNames = append(handlerNames, hname)
	}

	/*
		// Service descriptor.
		g.P("var ", serviceDescVar, " = ", serverPkg, ".ServiceDesc {")
		g.P("ServiceName: ", strconv.Quote(fullServName), ",")
		g.P("HandlerType: (*", serverType, ")(nil),")
		g.P("Methods: []", serverPkg, ".MethodDesc{")
		for i, method := range service.Method {
			if method.GetServerStreaming() || method.GetClientStreaming() {
				continue
			}
			g.P("{")
			g.P("MethodName: ", strconv.Quote(method.GetName()), ",")
			g.P("Handler: ", handlerNames[i], ",")
			g.P("},")
		}
		g.P("},")
		g.P("Streams: []", serverPkg, ".StreamDesc{")
		for i, method := range service.Method {
			if !method.GetServerStreaming() && !method.GetClientStreaming() {
				continue
			}
			g.P("{")
			g.P("StreamName: ", strconv.Quote(method.GetName()), ",")
			g.P("Handler: ", handlerNames[i], ",")
			if method.GetServerStreaming() {
				g.P("ServerStreams: true,")
			}
			if method.GetClientStreaming() {
				g.P("ClientStreams: true,")
			}
			g.P("},")
		}
		g.P("},")
		g.P("}")
		g.P()
	*/
}
Beispiel #9
0
// generateService generates all the code for the named service.
func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.ServiceDescriptorProto, index int) {
	//path := fmt.Sprintf("6,%d", index) // 6 means service.

	origServName := service.GetName()
	fullServName := file.GetPackage() + "." + origServName
	servName := generator.CamelCase(origServName)
	/*
		g.P()
		g.P("// Client API for ", servName, " service")
		g.P()

		// Client interface.
		g.P("type ", servName, "Client interface {")
		for i, method := range service.Method {
			g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
			g.P(g.generateClientSignature(servName, method))
		}
		g.P("}")
		g.P()

		// Client structure.
		g.P("type ", unexport(servName), "Client struct {")
		g.P("cc *", grpcPkg, ".ClientConn")
		g.P("}")
		g.P()

		// NewClient factory.
		g.P("func New", servName, "Client (cc *", grpcPkg, ".ClientConn) ", servName, "Client {")
		g.P("return &", unexport(servName), "Client{cc}")
		g.P("}")
		g.P()

		var methodIndex, streamIndex int
	*/
	serviceDescVar := "_" + servName + "_serviceDesc"
	/*
		// Client method implementations.
		for _, method := range service.Method {
			var descExpr string
			if !method.GetServerStreaming() && !method.GetClientStreaming() {
				// Unary RPC method
				descExpr = fmt.Sprintf("&%s.Methods[%d]", serviceDescVar, methodIndex)
				methodIndex++
			} else {
				// Streaming RPC method
				descExpr = fmt.Sprintf("&%s.Streams[%d]", serviceDescVar, streamIndex)
				streamIndex++
			}
			g.generateClientMethod(servName, fullServName, serviceDescVar, method, descExpr)
		}
	*/
	g.P("// Server API for ", servName, " service")
	g.P()

	g.P("// fullServName:   ", fullServName)
	g.P("// serviceDescVar: ", serviceDescVar)
	g.P("// servName:       ", servName)
	g.P()

	// Server interface.
	serverType := servName + "Server"
	//g.P("type ", serverType, " interface {")
	//for i, method := range service.Method {
	//	g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
	//	g.P(g.generateServerSignature(servName, method))
	//}
	//g.P("}")
	g.P()

	g.P("func NewHandler(h ", serverType, ", prefix string) *web.Mux {")
	g.P("	t := _", serverType, "{}")
	g.P("	t.handler = h")
	g.P("	router := web.New()")
	for _, method := range service.Method {
		path := strings.ToLower(servName) + "/" + method.GetName()
		methName := generator.CamelCase(method.GetName())
		// there should be a better way to get the options
		m := method.GetOptions().String()
		if m != "" {
			parts := strings.Split(m, "\"")
			if len(parts) == 3 {
				if parts[0] == "10000:" {
					path = parts[1]
				}
			}
		}
		g.P("router.Handle(prefix+\"", strings.ToLower(path), "\", t._", methName, ")")
	}
	g.P("	return router")
	g.P("}")
	g.P()

	g.P("type _", serverType, " struct {")
	g.P("	handler ", serverType)
	g.P("}")
	g.P()

	// Server registration.
	//g.P("func Register", servName, "Server(s *", grpcPkg, ".Server, srv ", serverType, ") {")
	//g.P("s.RegisterService(&", serviceDescVar, `, srv)`)
	//g.P("}")
	//g.P()

	// Server handler implementations.
	//var handlerNames []string
	for _, method := range service.Method {
		g.generateServerMethod(servName, method)
		//hname := g.generateServerMethod(servName, method)
		//handlerNames = append(handlerNames, hname)
	}

	// Service descriptor.
	/*
		g.P("var ", serviceDescVar, " = ", grpcPkg, ".ServiceDesc {")
		g.P("ServiceName: ", strconv.Quote(fullServName), ",")
		g.P("HandlerType: (*", serverType, ")(nil),")
		g.P("Methods: []", grpcPkg, ".MethodDesc{")
		for i, method := range service.Method {
			if method.GetServerStreaming() || method.GetClientStreaming() {
				continue
			}
			g.P("{")
			g.P("MethodName: ", strconv.Quote(method.GetName()), ",")
			g.P("Handler: ", handlerNames[i], ",")
			g.P("},")
		}
		g.P("},")
		g.P("Streams: []", grpcPkg, ".StreamDesc{")
		for i, method := range service.Method {
			if !method.GetServerStreaming() && !method.GetClientStreaming() {
				continue
			}
			g.P("{")
			g.P("StreamName: ", strconv.Quote(method.GetName()), ",")
			g.P("Handler: ", handlerNames[i], ",")
			if method.GetServerStreaming() {
				g.P("ServerStreams: true,")
			}
			if method.GetClientStreaming() {
				g.P("ClientStreams: true,")
			}
			g.P("},")
		}
		g.P("},")
		g.P("}")
		g.P()
	*/
}