Thryft: a code generation framework for multilingual web application development
Thryft accepts Apache Thrift interface definitions and generates:
- Java immutable models, service interfaces, abstract service stubs, unit test templates, logging service wrappers, and JSON-RPC servlet bridges to services
- Python models and JSON-RPC service clients
- TypeScript models, logging service wrappers, and JSON-RPC service clients
- ElasticSearch mappings
Thryft is written in Python using a SPARK parser and syntax-directed translation.
Thryft does not share any code with Apache Thrift, and it is not intended to be a complete rewrite of the latter. Among other things, Thryft does not currently a number of secondary IDL features such as senums. Thryft's runtime library is also quite different from Apache Thrift's.
Thryft does have a number of features that are not present in Apache Thrift:
- Support for additional "native" types using custom, dynamically-loaded code generators:
- email addresses
- Annotations in Javadoc-style comments that can generate:
- Language-specific documentation: javadoc, jsdoc, sphinx
- Validation code
git clone git://github.com/minorg/thryft.git
Reads a .thryft.json configuration file in the current directory. See
lib/.thryft.json for an example.
Thryft-generated code relies on runtime libraries in different languages.
<dependency> <groupId>org.thryft</groupId> <artifactId>libthryft</artifactId> <version>2.0.7</version> </dependency>
Software architecture / source overview
The compiler (
compiler.py) is the heart of Thryft. The compiler:
- accepts files in .thrift at its entry point (
compile), along with an instance of
generator.py), such as
- lexes and parses them with SPARK (
- creates an Abstract Syntax Tree (AST, classes in
ast.py) representing the constructs in the .thrift, such as the root document, struct types, fields, et al.
- for every node in the AST, creates a corresponding instance of the class in the Generator e.g., JavaGenerator.Document
- tells the root of the generator instance tree (
JavaGenerator.Document, in this case) to save itself to files using the
PEP8, with a few additions:
- All of the logic is part of a class method; there are no freestanding functions, except the ancient utility functions in
- There is one class per file, and the file is the underscore_separated version of the CamelCase class name.
- Protected methods are prefixed with
_. Private methods are prefixed with
- Methods, members, and parameters are listed in alphabetical order where feasible (i.e., not interdependent nested classes), ignoring leading underscores. The exception is
__init__, which is always the first method listed. Nested classes and class constants come before all methods.
The repository uses the nvie.com branching model.