thrift idl syntax

basic grammar

basic type

  • bool: boolean value corresponds to boolean in Java 
  • byte: signed byte corresponds to byte in Java 
  • i16: 16-bit signed integer corresponds to short in Java 
  • i32: 32-bit signed integer corresponds to int in Java 
  • i64: 64-bit signed integer corresponds to long in Java 
  • double: 64-bit floating point type corresponds to double in Java 
  • string: String corresponds to String in Java 
  • binary: Blob type corresponds to byte[] in Java

container type

The elements in the collection can be of any type except service, including exceptions.

  • list<T>: a series of ordered lists consisting of data of type T, elements can be repeated
  • set<T>: a series of unordered collections consisting of data of type T, the elements cannot be repeated
  • map<K, V>: a dictionary structure, the key is of type K, and the value is of type V, which is equivalent to HashMap<K, V> in Java

structure

Just like the C language, thrift also supports the struct type, the purpose is to aggregate some data together to facilitate transmission management. The definition of struct is as follows:

struct NPC
{
	1:i32 id;
	2:string name; 
}

 enumerate

The definition form of enumeration is similar to that of Java's Enum definition, for example:

enum Action {
    Idle,
      Attack,
    Run 
}

abnormal

thrift supports custom exceptions, the rules are the same as struct, as follows:

exception RequestException {
    1: i32 code;
    2: string reason;
}

Serve

The thrift definition service is equivalent to creating an Interface in Java. After the created service passes the code generation command, the framework code of the client and the server will be generated. The definition form is as follows:

service HelloWordService {
     // service中定义的函数,相当于Java interface中定义的函数
     string doAction(1: string name, 2: i32 age);
 }

type definition

 thrift supports typedef definitions similar to C++, such as:

typedef i32 Integer
typedef i64 Long

Note: No comma or semicolon at the end!

constant

thrift also supports constant definitions, using the const keyword, for example:

const i32 MAX_RETRIES_TIME = 10;
const string MY_WEBSITE = "http://qifuguang.me";

The semicolon at the end is optional, optional, and supports hexadecimal assignment

Namespaces

The namespace of thrift is equivalent to the meaning of package in Java, and the main purpose is to organize code. thrift uses the keyword namespace to define namespaces, for example:

namespace java com.game.lll.thrift

Tip: The format is the namespace language (Java) path (com.game.lll.thrift), note that there can be no semicolon at the end.

file contains

Thrift also supports file inclusion, which is equivalent to include in C/C++, import in Java, and using in C#. Defined using the keyword include, for example:

include "global.thrift"

Notes

 The thrift comment mode supports shell-style comments and C/C++-style comments, that is, statements beginning with # and // are regarded as comments alone, and statements wrapped in /**/ are also comments.

optional and required

thrift provides two keywords required, optional, which are used to indicate whether the corresponding field is required or optional. E.g:

struct People {
    1: required string name;
    2: optional i32 age;
}

Indicates that name is required and age is optional.

thrift compilation

Step 1: Create a file with the following code:

namespace java com.game.lll.thrift

struct Request {
    1: string username;      
    2: string password;           
}

exception RequestException {
    1: required i32 code;
    2: optional string reason;
}

// 服务名
service LoginService {
    string doAction(1: Request request) throws (1:RequestException qe); // 可能抛出异常。
}

Step 2: After entering the command thrift -gen java login.thrift in the terminal, the gen-java folder will be generated in the current directory, and the folder will be generated layer by layer according to the path name defined by the namespace, to gen-java You can see the three generated .java classes in the /com/game/lll/thrift/ directory. Here is my directory:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325153957&siteId=291194637