本文整理汇总了Golang中github.com/golang/protobuf/protoc-gen-go/generator.FileDescriptor类的典型用法代码示例。如果您正苦于以下问题:Golang FileDescriptor类的具体用法?Golang FileDescriptor怎么用?Golang FileDescriptor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FileDescriptor类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: generateService
// 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("{")
//.........这里部分代码省略.........
开发者ID:vozhyk-,项目名称:gohan,代码行数:101,代码来源:grpc.go
示例2: genServiceServer
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())
}
}
开发者ID:icetoggle,项目名称:protorpc,代码行数:76,代码来源:protorpc.go
示例3: genServiceClient
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())
}
}
开发者ID:icetoggle,项目名称:protorpc,代码行数:77,代码来源:protorpc.go
示例4: generateService
// 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 {
//.........这里部分代码省略.........
开发者ID:micro,项目名称:protobuf,代码行数:101,代码来源:micro.go
注:本文中的github.com/golang/protobuf/protoc-gen-go/generator.FileDescriptor类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论