Go語言: 生成Protobuf的Service介面

chai2010發表於2013-04-24

Protobuf 是Google釋出的開源編碼規範, 官方支援C++/Java/Python等幾種語言.

Go語言釋出之後, Go的官方團隊釋出的GoProtobuf也實現了Protobuf支援.

不過GoProtobuf官方版本並沒有實現rpc的支援. protoc-gen-go 甚至連 service 的介面也未生成.

如果看過 "JSON-RPC: a tale of interfaces" 文章, 會發現Go語言支援rpc非常容易.

我們現在就開始嘗試給GoProtobuf增加rpc的支援.

當然, 第一步是要生成Service介面.

建立 service.go 檔案:

// Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package generator

// ServicePlugin produce the Service interface.
type ServicePlugin struct {
    *Generator
}

// Name returns the name of the plugin.
func (p *ServicePlugin) Name() string { return "ServiceInterface" }

// Init is called once after data structures are built but before
// code generation begins.
func (p *ServicePlugin) Init(g *Generator) {
    p.Generator = g
}

// Generate produces the code generated by the plugin for this file.
func (p *ServicePlugin) GenerateImports(file *FileDescriptor) {
    //
}

// Generate generates the Service interface.
func (p *ServicePlugin) Generate(file *FileDescriptor) {
    for _, svc := range file.Service {
        name := CamelCase(*svc.Name)
        p.P("type ", name, " interface {")
        p.In()
        for _, m := range svc.Method {
            method := CamelCase(*m.Name)
            iType := p.ObjectNamed(*m.InputType)
            oType := p.ObjectNamed(*m.OutputType)
            p.P(method, "(in *", p.TypeName(iType), ", out *", p.TypeName(oType), ") error")
        }
        p.Out()
        p.P("}")
    }
}

func init() {
    RegisterPlugin(new(ServicePlugin))
}

將 service.go 檔案放到 code.google.com/p/goprotobuf/protoc-gen-go/generator 目錄. 重新編譯安裝 code.google.com/p/goprotobuf/protoc-gen-go.

新建 echo.proto 檔案:

// Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package echo;

option cc_generic_services = true;
option java_generic_services = true;
option py_generic_services = true;

message EchoRequest {
    required string message = 1;
}

message EchoResponse {
    required string message = 1;
}

service EchoService {
    rpc echo (EchoRequest) returns (EchoResponse);
}

編譯 echo.proto 檔案:

protoc --go_out=. echo.proto

生成 echo.pb.go 檔案:

// Code generated by protoc-gen-go.
// source: echo.proto
// DO NOT EDIT!

package echo

import proto "code.google.com/p/goprotobuf/proto"
import json "encoding/json"
import math "math"

// Reference proto, json, and math imports to suppress error if they are not otherwise used.
var _ = proto.Marshal
var _ = &json.SyntaxError{}
var _ = math.Inf

type EchoRequest struct {
    Message          *string `protobuf:"bytes,1,req,name=message" json:"message,omitempty"`
    XXX_unrecognized []byte  `json:"-"`
}

func (m *EchoRequest) Reset()         { *m = EchoRequest{} }
func (m *EchoRequest) String() string { return proto.CompactTextString(m) }
func (*EchoRequest) ProtoMessage()    {}

func (m *EchoRequest) GetMessage() string {
    if m != nil && m.Message != nil {
        return *m.Message
    }
    return ""
}

type EchoResponse struct {
    Message          *string `protobuf:"bytes,1,req,name=message" json:"message,omitempty"`
    XXX_unrecognized []byte  `json:"-"`
}

func (m *EchoResponse) Reset()         { *m = EchoResponse{} }
func (m *EchoResponse) String() string { return proto.CompactTextString(m) }
func (*EchoResponse) ProtoMessage()    {}

func (m *EchoResponse) GetMessage() string {
    if m != nil && m.Message != nil {
        return *m.Message
    }
    return ""
}

func init() {
}

type EchoService interface {
    Echo(in *EchoRequest, out *EchoResponse) error
}

注意最後的介面部分是由我們新增的 service.go 生成的:

type EchoService interface {
    Echo(in *EchoRequest, out *EchoResponse) error
}

最基本的準備工作已經完成, 下面就是參考JSON-RPC的例子, 實現一個protobuf版本的rpc了.

下次繼續...


更多Go文章請訪問: http://my.oschina.net/chai2010

相關文章