thrift --

shared.thrift
---------------
**
 * This Thrift file can be included by other Thrift files that want to share
 * these definitions.
 */
namespace cpp shared
namespace java shared
namespace perl shared
// 这里定义了一个结构体,没有定义方法,对应于生成的代码在gen-cpp中的shared_types.h中,其中有一个class叫SharedStruct,
// 有没有看到其中有两个方法叫read和write,这就是用来对其进行序列化与把序列化的方法.
// 对了,其中的i32是Thrift IDL中定义的变量类型,对应于c++语言中的int32_t
struct SharedStruct {    
  1: i32 key
  2: string value
}
// 这里定义的一个服务,它语义上类似于面向对象中的定义一个接口,thrift的编译器会对其产生一套实现其接口的客户端与服务端方法
// 服务的一般定义格式如下
// service <name>
// <returntype> <name>(<arguments>)
// [ throws (<exceptions>)]
//   ...
// }
service SharedService {
  SharedStruct getStruct(1: i32 key)
}
tutorial.thrift
----------------
/**
 * Thrift files can reference other Thrift files to include common struct
 * and service definitions. These are found using the current path, or by
 * searching relative to any paths specified with the -I compiler flag.
 *
 * Included objects are accessed using the name of the .thrift file as a
 * prefix. i.e. shared.SharedObject
 */
 // 这个IDL包含了另一个IDL,也就是说另一个IDL中的对象与服务对其时可见的
include "shared.thrift"
/**
 * Thrift files can namespace, package, or prefix their output in various
 * target languages.
 */
 // 这里定义了一些语言的namespace空间
namespace cpp tutorial
namespace java tutorial
namespace php tutorial
namespace perl tutorial
/**
 * Thrift lets you do typedefs to get pretty names for your types. Standard
 * C style here.
 */
 // 自定义类型
typedef i32 MyInteger
/**
 * Thrift also lets you define constants for use across languages. Complex
 * types and structs are specified using JSON notation.
 */
 // 定义一些变量
const i32 INT32CONSTANT = 9853
const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'}
/**
 * You can define enums, which are just 32 bit integers. Values are optional
 * and start at 1 if not supplied, C style again.
 */
 // 定义枚举类型
enum Operation {
  ADD = 1,
  SUBTRACT = 2,
  MULTIPLY = 3,
  DIVIDE = 4
}
/**
 * Structs are the basic complex data structures. They are comprised of fields
 * which each have an integer identifier, a type, a symbolic name, and an
 * optional default value.
 *
 * Fields can be declared "optional", which ensures they will not be included
 * in the serialized output if they aren't set.  Note that this requires some
 * manual management in some languages.
 */
struct Work {
  1: i32 num1 = 0,
  2: i32 num2,
  3: Operation op,        
  4: optional string comment, //这里的optional字段类型表示如果这个字段的值没有被赋值,它就不会被序列化输出
}
/**
 * Structs can also be exceptions, if they are nasty.
 */
 // 这里定义了一些异常
exception InvalidOperation {
  1: i32 what,
  2: string why
}
/**
 * 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.
 */
 // 这里是定义服务,它继承了shared的服务
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()
}

你可能感兴趣的:(thrift)