【go语言之thrift协议一】

thrift 相对于grpc而言,可能用的不是特别多,但是也是有一定的知名度,废话不多说,直接看一下实现。
首先就是安装,对于mac系统来说,直接就是

brew install thrift

当输入thrift --version 显示版本说明安装成功.
在这里插入图片描述
这里的示例代码是基于官方的示例去做的,地址是 官方示例

这一篇文件主要介绍的是thrift的文件,以后生成对应的go的代码

thrift文件

然后和protobuf,先写proto文件,然后生成不同语言的文件一样,thrift是以thrift的文件作为后缀,然后使用thrift命令来去生成不同语言的文件。
这里看一下官方的示例

shared.thrift

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/**
 * This Thrift file can be included by other Thrift files that want to share
 * these definitions.
 */

namespace cl shared
namespace cpp shared
namespace d share // "shared" would collide with the eponymous D keyword.
namespace dart shared
namespace java shared
namespace perl shared
namespace php shared
namespace haxe shared
namespace netstd shared


struct SharedStruct {
  1: i32 key
  2: string value
}

service SharedService {
  SharedStruct getStruct(1: i32 key)
}

首先这里定义了一个shared.thrift的文件,然后这里是定义了namespace,然后是SharedStruct这个结构体,这个里面定义了两个成员,第一个是key,属性是int32。然后就是value,属性是string。
然后就是SharedService。这个就是需要实现的方法,在golang中就是interface,在java中就是abstract。
接下来看一下生成的go文件

SharedStruct

type SharedStruct struct {
	Key   int32  `thrift:"key,1" db:"key" json:"key"`
	Value string `thrift:"value,2" db:"value" json:"value"`
}

func NewSharedStruct() *SharedStruct {
	return &SharedStruct{}
}

func (p *SharedStruct) GetKey() int32 {
	return p.Key
}

func (p *SharedStruct) GetValue() string {
	return p.Value
}
func (p *SharedStruct) Read(ctx context.Context, iprot thrift.TProtocol) error {
	if _, err := iprot.ReadStructBegin(ctx); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err)
	}

	for {
		_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin(ctx)
		if err != nil {
			return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err)
		}
		if fieldTypeId == thrift.STOP {
			break
		}
		switch fieldId {
		case 1:
			if fieldTypeId == thrift.I32 {
				if err := p.ReadField1(ctx, iprot); err != nil {
					return err
				}
			} else {
				if err := iprot.Skip(ctx, fieldTypeId); err != nil {
					return err
				}
			}
		case 2:
			if fieldTypeId == thrift.STRING {
				if err := p.ReadField2(ctx, iprot); err != nil {
					return err
				}
			} else {
				if err := iprot.Skip(ctx, fieldTypeId); err != nil {
					return err
				}
			}
		default:
			if err := iprot.Skip(ctx, fieldTypeId); err != nil {
				return err
			}
		}
		if err := iprot.ReadFieldEnd(ctx); err != nil {
			return err
		}
	}
	if err := iprot.ReadStructEnd(ctx); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
	}
	return nil
}

func (p *SharedStruct) ReadField1(ctx context.Context, iprot thrift.TProtocol) error {
	if v, err := iprot.ReadI32(ctx); err != nil {
		return thrift.PrependError("error reading field 1: ", err)
	} else {
		p.Key = v
	}
	return nil
}

func (p *SharedStruct) ReadField2(ctx context.Context, iprot thrift.TProtocol) error {
	if v, err := iprot.ReadString(ctx); err != nil {
		return thrift.PrependError("error reading field 2: ", err)
	} else {
		p.Value = v
	}
	return nil
}

func (p *SharedStruct) Write(ctx context.Context, oprot thrift.TProtocol) error {
	if err := oprot.WriteStructBegin(ctx, "SharedStruct"); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
	}
	if p != nil {
		if err := p.writeField1(ctx, oprot); err != nil {
			return err
		}
		if err := p.writeField2(ctx, oprot); err != nil {
			return err
		}
	}
	if err := oprot.WriteFieldStop(ctx); err != nil {
		return thrift.PrependError("write field stop error: ", err)
	}
	if err := oprot.WriteStructEnd(ctx); err != nil {
		return thrift.PrependError("write struct stop error: ", err)
	}
	return nil
}

func (p *SharedStruct) writeField1(ctx context.Context, oprot thrift.TProtocol) (err error) {
	if err := oprot.WriteFieldBegin(ctx, "key", thrift.I32, 1); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field begin error 1:key: ", p), err)
	}
	if err := oprot.WriteI32(ctx, int32(p.Key)); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T.key (1) field write error: ", p), err)
	}
	if err := oprot.WriteFieldEnd(ctx); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field end error 1:key: ", p), err)
	}
	return err
}

func (p *SharedStruct) writeField2(ctx context.Context, oprot thrift.TProtocol) (err error) {
	if err := oprot.WriteFieldBegin(ctx, "value", thrift.STRING, 2); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field begin error 2:value: ", p), err)
	}
	if err := oprot.WriteString(ctx, string(p.Value)); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T.value (2) field write error: ", p), err)
	}
	if err := oprot.WriteFieldEnd(ctx); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field end error 2:value: ", p), err)
	}
	return err
}

func (p *SharedStruct) Equals(other *SharedStruct) bool {
	if p == other {
		return true
	} else if p == nil || other == nil {
		return false
	}
	if p.Key != other.Key {
		return false
	}
	if p.Value != other.Value {
		return false
	}
	return true
}

func (p *SharedStruct) String() string {
	if p == nil {
		return "<nil>"
	}
	return fmt.Sprintf("SharedStruct(%+v)", *p)
}

可以看出来这里除了生成通常的GetKey 和 GetValue方法,还生成了Read 还有 ReadFile等方法,当然因为用到的时候再说。

SharedService

这个方法生成了好几个方法。里面有一个getStruct方法,参数是int返回了SharedStruct 的结构体。

type SharedService interface {
	// Parameters:
	//  - Key
	GetStruct(ctx context.Context, key int32) (_r *SharedStruct, _err error)
}

type SharedServiceClient struct {
	c    thrift.TClient
	meta thrift.ResponseMeta
}

func NewSharedServiceClientFactory(t thrift.TTransport, f thrift.TProtocolFactory) *SharedServiceClient {
	return &SharedServiceClient{
		c: thrift.NewTStandardClient(f.GetProtocol(t), f.GetProtocol(t)),
	}
}

func NewSharedServiceClientProtocol(t thrift.TTransport, iprot thrift.TProtocol, oprot thrift.TProtocol) *SharedServiceClient {
	return &SharedServiceClient{
		c: thrift.NewTStandardClient(iprot, oprot),
	}
}

func NewSharedServiceClient(c thrift.TClient) *SharedServiceClient {
	return &SharedServiceClient{
		c: c,
	}
}

func (p *SharedServiceClient) Client_() thrift.TClient {
	return p.c
}

func (p *SharedServiceClient) LastResponseMeta_() thrift.ResponseMeta {
	return p.meta
}

func (p *SharedServiceClient) SetLastResponseMeta_(meta thrift.ResponseMeta) {
	p.meta = meta
}

// Parameters:
//   - Key
func (p *SharedServiceClient) GetStruct(ctx context.Context, key int32) (_r *SharedStruct, _err error) {
	var _args0 SharedServiceGetStructArgs
	_args0.Key = key
	var _result2 SharedServiceGetStructResult
	var _meta1 thrift.ResponseMeta
	_meta1, _err = p.Client_().Call(ctx, "getStruct", &_args0, &_result2)
	p.SetLastResponseMeta_(_meta1)
	if _err != nil {
		return
	}
	return _result2.GetSuccess(), nil
}

然后看一下 这里首先是生成了一个SharedService的interface,然后定义了一个GetStruct 方法,这个是符合预期。然后看一下实现。
这里是SharedServiceClient实现了这个interface。然后就是如何获取,这里是有三个方法。

  1. NewSharedServiceClientFactory
  2. NewSharedServiceClientProtocol
  3. NewSharedServiceClient
    前两个方法都调用了NewTStandardClient这个方法去生成thrift.TClient。这个thrift.TClient是一个interface是
// ResponseMeta represents the metadata attached to the response.
type ResponseMeta struct {
	// The headers in the response, if any.
	// If the underlying transport/protocol is not THeader, this will always be nil.
	Headers THeaderMap
}

type TClient interface {
	Call(ctx context.Context, method string, args, result TStruct) (ResponseMeta, error)
}

// TStandardClient implements TClient, and uses the standard message format for Thrift.
// It is not safe for concurrent use.
func NewTStandardClient(inputProtocol, outputProtocol TProtocol) *TStandardClient {
	return &TStandardClient{
		iprot: inputProtocol,
		oprot: outputProtocol,
	}
}

可以看出来这个NewTStandardClient的接收参数是 TProtocol 这个interface。然后只不过第一个NewSharedServiceClientFactory是通过thrift.TProtocolFactory去获取的。然后NewSharedServiceClientProtocol是直接传进去的,然后NewSharedServiceClient是直接通过thrift.TClient 去调用的。

SharedServiceProcessor

然后这里还生成了SharedServiceProcessor 这样的的一个结构体,然后看一下这个结构体以及生成的方法。

type SharedServiceProcessor struct {
	processorMap map[string]thrift.TProcessorFunction
	handler      SharedService
}

func (p *SharedServiceProcessor) AddToProcessorMap(key string, processor thrift.TProcessorFunction) {
	p.processorMap[key] = processor
}

func (p *SharedServiceProcessor) GetProcessorFunction(key string) (processor thrift.TProcessorFunction, ok bool) {
	processor, ok = p.processorMap[key]
	return processor, ok
}

func (p *SharedServiceProcessor) ProcessorMap() map[string]thrift.TProcessorFunction {
	return p.processorMap
}

func NewSharedServiceProcessor(handler SharedService) *SharedServiceProcessor {
	self3 := &SharedServiceProcessor{handler: handler, processorMap: make(map[string]thrift.TProcessorFunction)}
	self3.processorMap["getStruct"] = &sharedServiceProcessorGetStruct{handler: handler}
	return self3
}

func (p *SharedServiceProcessor) Process(ctx context.Context, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) {
	name, _, seqId, err2 := iprot.ReadMessageBegin(ctx)
	if err2 != nil {
		return false, thrift.WrapTException(err2)
	}
	if processor, ok := p.GetProcessorFunction(name); ok {
		return processor.Process(ctx, seqId, iprot, oprot)
	}
	iprot.Skip(ctx, thrift.STRUCT)
	iprot.ReadMessageEnd(ctx)
	x4 := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, "Unknown function "+name)
	oprot.WriteMessageBegin(ctx, name, thrift.EXCEPTION, seqId)
	x4.Write(ctx, oprot)
	oprot.WriteMessageEnd(ctx)
	oprot.Flush(ctx)
	return false, x4

}

type sharedServiceProcessorGetStruct struct {
	handler SharedService
}

func (p *sharedServiceProcessorGetStruct) Process(ctx context.Context, seqId int32, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) {
	args := SharedServiceGetStructArgs{}
	var err2 error
	if err2 = args.Read(ctx, iprot); err2 != nil {
		iprot.ReadMessageEnd(ctx)
		x := thrift.NewTApplicationException(thrift.PROTOCOL_ERROR, err2.Error())
		oprot.WriteMessageBegin(ctx, "getStruct", thrift.EXCEPTION, seqId)
		x.Write(ctx, oprot)
		oprot.WriteMessageEnd(ctx)
		oprot.Flush(ctx)
		return false, thrift.WrapTException(err2)
	}
	iprot.ReadMessageEnd(ctx)

	tickerCancel := func() {}
	// Start a goroutine to do server side connectivity check.
	if thrift.ServerConnectivityCheckInterval > 0 {
		var cancel context.CancelFunc
		ctx, cancel = context.WithCancel(ctx)
		defer cancel()
		var tickerCtx context.Context
		tickerCtx, tickerCancel = context.WithCancel(context.Background())
		defer tickerCancel()
		go func(ctx context.Context, cancel context.CancelFunc) {
			ticker := time.NewTicker(thrift.ServerConnectivityCheckInterval)
			defer ticker.Stop()
			for {
				select {
				case <-ctx.Done():
					return
				case <-ticker.C:
					if !iprot.Transport().IsOpen() {
						cancel()
						return
					}
				}
			}
		}(tickerCtx, cancel)
	}

	result := SharedServiceGetStructResult{}
	var retval *SharedStruct
	if retval, err2 = p.handler.GetStruct(ctx, args.Key); err2 != nil {
		tickerCancel()
		if err2 == thrift.ErrAbandonRequest {
			return false, thrift.WrapTException(err2)
		}
		x := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, "Internal error processing getStruct: "+err2.Error())
		oprot.WriteMessageBegin(ctx, "getStruct", thrift.EXCEPTION, seqId)
		x.Write(ctx, oprot)
		oprot.WriteMessageEnd(ctx)
		oprot.Flush(ctx)
		return true, thrift.WrapTException(err2)
	} else {
		result.Success = retval
	}
	tickerCancel()
	if err2 = oprot.WriteMessageBegin(ctx, "getStruct", thrift.REPLY, seqId); err2 != nil {
		err = thrift.WrapTException(err2)
	}
	if err2 = result.Write(ctx, oprot); err == nil && err2 != nil {
		err = thrift.WrapTException(err2)
	}
	if err2 = oprot.WriteMessageEnd(ctx); err == nil && err2 != nil {
		err = thrift.WrapTException(err2)
	}
	if err2 = oprot.Flush(ctx); err == nil && err2 != nil {
		err = thrift.WrapTException(err2)
	}
	if err != nil {
		return
	}
	return true, err
}

这里生成了一个process的方法,然后核心结构体是SharedServiceProcessor 从结构体成员可以看出来主要成员是 processorMap 然后属性是 map[string]thrift.TProcessorFunction。
然后NewSharedServiceProcessor的时候可以是看出来key的名称getStruct,value是sharedServiceProcessorGetStruct然后实现了Processde 的方法。至于用处后面在使用的时候再具体说一下。

SharedServiceGetStructArgs

这个SharedServiceGetStructArgs还得是说到SharedService的成员GetStruct方法,这个方法里面有一个参数key,类型是int32.这里为了这个key还专门生成了一个结构体.然后看一下成员

type SharedServiceGetStructArgs struct {
	Key int32 `thrift:"key,1" db:"key" json:"key"`
}

func NewSharedServiceGetStructArgs() *SharedServiceGetStructArgs {
	return &SharedServiceGetStructArgs{}
}

func (p *SharedServiceGetStructArgs) GetKey() int32 {
	return p.Key
}
func (p *SharedServiceGetStructArgs) Read(ctx context.Context, iprot thrift.TProtocol) error {
	if _, err := iprot.ReadStructBegin(ctx); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err)
	}

	for {
		_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin(ctx)
		if err != nil {
			return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err)
		}
		if fieldTypeId == thrift.STOP {
			break
		}
		switch fieldId {
		case 1:
			if fieldTypeId == thrift.I32 {
				if err := p.ReadField1(ctx, iprot); err != nil {
					return err
				}
			} else {
				if err := iprot.Skip(ctx, fieldTypeId); err != nil {
					return err
				}
			}
		default:
			if err := iprot.Skip(ctx, fieldTypeId); err != nil {
				return err
			}
		}
		if err := iprot.ReadFieldEnd(ctx); err != nil {
			return err
		}
	}
	if err := iprot.ReadStructEnd(ctx); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
	}
	return nil
}

func (p *SharedServiceGetStructArgs) ReadField1(ctx context.Context, iprot thrift.TProtocol) error {
	if v, err := iprot.ReadI32(ctx); err != nil {
		return thrift.PrependError("error reading field 1: ", err)
	} else {
		p.Key = v
	}
	return nil
}

func (p *SharedServiceGetStructArgs) Write(ctx context.Context, oprot thrift.TProtocol) error {
	if err := oprot.WriteStructBegin(ctx, "getStruct_args"); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
	}
	if p != nil {
		if err := p.writeField1(ctx, oprot); err != nil {
			return err
		}
	}
	if err := oprot.WriteFieldStop(ctx); err != nil {
		return thrift.PrependError("write field stop error: ", err)
	}
	if err := oprot.WriteStructEnd(ctx); err != nil {
		return thrift.PrependError("write struct stop error: ", err)
	}
	return nil
}

func (p *SharedServiceGetStructArgs) writeField1(ctx context.Context, oprot thrift.TProtocol) (err error) {
	if err := oprot.WriteFieldBegin(ctx, "key", thrift.I32, 1); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field begin error 1:key: ", p), err)
	}
	if err := oprot.WriteI32(ctx, int32(p.Key)); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T.key (1) field write error: ", p), err)
	}
	if err := oprot.WriteFieldEnd(ctx); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field end error 1:key: ", p), err)
	}
	return err
}

func (p *SharedServiceGetStructArgs) String() string {
	if p == nil {
		return "<nil>"
	}
	return fmt.Sprintf("SharedServiceGetStructArgs(%+v)", *p)
}

这个看起来很多,然后很多无用的方法其实是不用去看的。

SharedServiceGetStructResult

这个其实是和上面的SharedServiceGetStructArgs类型,只不是上面的是SharedService里面成员GetStruct方法的成员,然后这个SharedServiceGetStructResult是GetStruct的结果的封装,所以成员success的属性是SharedStruct。然后看一下官方的实现

type SharedServiceGetStructResult struct {
	Success *SharedStruct `thrift:"success,0" db:"success" json:"success,omitempty"`
}

func NewSharedServiceGetStructResult() *SharedServiceGetStructResult {
	return &SharedServiceGetStructResult{}
}

var SharedServiceGetStructResult_Success_DEFAULT *SharedStruct

func (p *SharedServiceGetStructResult) GetSuccess() *SharedStruct {
	if !p.IsSetSuccess() {
		return SharedServiceGetStructResult_Success_DEFAULT
	}
	return p.Success
}
func (p *SharedServiceGetStructResult) IsSetSuccess() bool {
	return p.Success != nil
}

func (p *SharedServiceGetStructResult) Read(ctx context.Context, iprot thrift.TProtocol) error {
	if _, err := iprot.ReadStructBegin(ctx); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err)
	}

	for {
		_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin(ctx)
		if err != nil {
			return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err)
		}
		if fieldTypeId == thrift.STOP {
			break
		}
		switch fieldId {
		case 0:
			if fieldTypeId == thrift.STRUCT {
				if err := p.ReadField0(ctx, iprot); err != nil {
					return err
				}
			} else {
				if err := iprot.Skip(ctx, fieldTypeId); err != nil {
					return err
				}
			}
		default:
			if err := iprot.Skip(ctx, fieldTypeId); err != nil {
				return err
			}
		}
		if err := iprot.ReadFieldEnd(ctx); err != nil {
			return err
		}
	}
	if err := iprot.ReadStructEnd(ctx); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
	}
	return nil
}

func (p *SharedServiceGetStructResult) ReadField0(ctx context.Context, iprot thrift.TProtocol) error {
	p.Success = &SharedStruct{}
	if err := p.Success.Read(ctx, iprot); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", p.Success), err)
	}
	return nil
}

func (p *SharedServiceGetStructResult) Write(ctx context.Context, oprot thrift.TProtocol) error {
	if err := oprot.WriteStructBegin(ctx, "getStruct_result"); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
	}
	if p != nil {
		if err := p.writeField0(ctx, oprot); err != nil {
			return err
		}
	}
	if err := oprot.WriteFieldStop(ctx); err != nil {
		return thrift.PrependError("write field stop error: ", err)
	}
	if err := oprot.WriteStructEnd(ctx); err != nil {
		return thrift.PrependError("write struct stop error: ", err)
	}
	return nil
}

func (p *SharedServiceGetStructResult) writeField0(ctx context.Context, oprot thrift.TProtocol) (err error) {
	if p.IsSetSuccess() {
		if err := oprot.WriteFieldBegin(ctx, "success", thrift.STRUCT, 0); err != nil {
			return thrift.PrependError(fmt.Sprintf("%T write field begin error 0:success: ", p), err)
		}
		if err := p.Success.Write(ctx, oprot); err != nil {
			return thrift.PrependError(fmt.Sprintf("%T error writing struct: ", p.Success), err)
		}
		if err := oprot.WriteFieldEnd(ctx); err != nil {
			return thrift.PrependError(fmt.Sprintf("%T write field end error 0:success: ", p), err)
		}
	}
	return err
}

func (p *SharedServiceGetStructResult) String() string {
	if p == nil {
		return "<nil>"
	}
	return fmt.Sprintf("SharedServiceGetStructResult(%+v)", *p)
}

tutorial.thrift

这个文件里面其实说的挺多的,主要是对thrift协议的介绍,看起来要不grpc的协议语法更多一些。

基本数据类型

 *  bool        Boolean, one byte
 *  i8 (byte)   Signed 8-bit integer
 *  i16         Signed 16-bit integer
 *  i32         Signed 32-bit integer
 *  i64         Signed 64-bit integer
 *  double      64-bit floating point value
 *  string      String
 *  binary      Blob (byte array)
 *  map<t1,t2>  Map from one type to another
 *  list<t1>    Ordered list of one type
 *  set<t1>     Set of unique elements of one type

引入其他thrift文件

以上面介绍的shared.thrift为例,这里就是

include "shared.thrift"

自定义类型

thrift的实现是

typedef i32 MyInteger

然后生成的是

type MyInteger int32

func MyIntegerPtr(v MyInteger) *MyInteger { return &v }

定义常量

thrift的实现是

const i32 INT32CONSTANT = 9853
const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'}

这个很奇怪在go中没有找到

enum

thrift的实现是

 */
enum Operation {
  ADD = 1,
  SUBTRACT = 2,
  MULTIPLY = 3,
  DIVIDE = 4
}

go的实现是

type Operation int64

const (
	Operation_ADD      Operation = 1
	Operation_SUBTRACT Operation = 2
	Operation_MULTIPLY Operation = 3
	Operation_DIVIDE   Operation = 4
)

func (p Operation) String() string {
	switch p {
	case Operation_ADD:
		return "ADD"
	case Operation_SUBTRACT:
		return "SUBTRACT"
	case Operation_MULTIPLY:
		return "MULTIPLY"
	case Operation_DIVIDE:
		return "DIVIDE"
	}
	return "<UNSET>"
}

func OperationFromString(s string) (Operation, error) {
	switch s {
	case "ADD":
		return Operation_ADD, nil
	case "SUBTRACT":
		return Operation_SUBTRACT, nil
	case "MULTIPLY":
		return Operation_MULTIPLY, nil
	case "DIVIDE":
		return Operation_DIVIDE, nil
	}
	return Operation(0), fmt.Errorf("not a valid Operation string")
}

func OperationPtr(v Operation) *Operation { return &v }

func (p Operation) MarshalText() ([]byte, error) {
	return []byte(p.String()), nil
}

func (p *Operation) UnmarshalText(text []byte) error {
	q, err := OperationFromString(string(text))
	if err != nil {
		return err
	}
	*p = q
	return nil
}

func (p *Operation) Scan(value interface{}) error {
	v, ok := value.(int64)
	if !ok {
		return errors.New("Scan value is not int64")
	}
	*p = Operation(v)
	return nil
}

func (p *Operation) Value() (driver.Value, error) {
	if p == nil {
		return nil, nil
	}
	return int64(*p), nil
}

继承

这里thrift的实现是

service Calculator extends shared.SharedService {

  /**
   * A method definition looks like C code. It has a return type, arguments,
   * and optionally a list of exceptions that it may throw. Note that argument
   * lists and exception lists are specified using the exact same syntax as
   * field lists in struct or exception definitions.
   */

   void ping(),

   i32 add(1:i32 num1, 2:i32 num2),

   i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),

   /**
    * This method has a oneway modifier. That means the client only makes
    * a request and does not listen for any response at all. Oneway methods
    * must be void.
    */
   oneway void zip()

}

这里是Calculator继承了shared模块中的SharedService。然后go中的实现是

type Calculator interface {
	shared.SharedService
	//Ahh, now onto the cool part, defining a service. Services just need a name
	//and can optionally inherit from another service using the extends keyword.

	// A method definition looks like C code. It has a return type, arguments,
	// and optionally a list of exceptions that it may throw. Note that argument
	// lists and exception lists are specified using the exact same syntax as
	// field lists in struct or exception definitions.
	Ping(ctx context.Context) (_err error)
	// Parameters:
	//  - Num1
	//  - Num2
	Add(ctx context.Context, num1 int32, num2 int32) (_r int32, _err error)
	// Parameters:
	//  - Logid
	//  - W
	Calculate(ctx context.Context, logid int32, w *Work) (_r int32, _err error)
	// This method has a oneway modifier. That means the client only makes
	// a request and does not listen for any response at all. Oneway methods
	// must be void.
	Zip(ctx context.Context) (_err error)
}

嗯在go中继承就是这么简单,我看注释中也是这个意思,然后其他的就是和前面说的SharedService一样。
生成client,process,arg,result等一些结构体和响应的方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值