2015-01-07 01:44:59 +00:00
|
|
|
# Thrift Utilities for Elixir
|
|
|
|
|
2016-01-12 02:06:55 +00:00
|
|
|
[![Build Status](https://travis-ci.org/pinterest/elixir-thrift.svg?branch=master)](https://travis-ci.org/pinterest/elixir-thrift)
|
|
|
|
[![Coverage Status](https://coveralls.io/repos/pinterest/elixir-thrift/badge.svg?branch=master&service=github)](https://coveralls.io/github/pinterest/elixir-thrift?branch=master)
|
|
|
|
![License](https://img.shields.io/badge/license-Apache%202-blue.svg)
|
|
|
|
|
2015-01-07 01:44:59 +00:00
|
|
|
This package contains a handful of useful utilities for working with
|
|
|
|
[Thrift](https://thrift.apache.org/) in Elixir.
|
|
|
|
|
|
|
|
In particular, it includes a copy of the Erlang Thrift runtime library.
|
|
|
|
|
|
|
|
## Setup
|
|
|
|
|
|
|
|
Start by adding this package to your project as a dependency:
|
|
|
|
|
|
|
|
```elixir
|
2016-01-12 02:06:55 +00:00
|
|
|
{:thrift, github: "pinterest/elixir-thrift", submodules: true}
|
2015-01-07 01:44:59 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Mix
|
|
|
|
|
|
|
|
This package includes a Mix compiler task that can be used to automate Thrift
|
|
|
|
code generation. Start by adding `:thrift` to your project's `:compilers` list.
|
|
|
|
For example:
|
|
|
|
|
|
|
|
```elixir
|
2015-01-08 23:43:05 +00:00
|
|
|
compilers: [:thrift | Mix.compilers]
|
2015-01-07 01:44:59 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
It's important to add `:thrift` *before* the `:erlang` entry. The Thrift
|
|
|
|
compiler will generate Erlang source files, and we currently rely on this
|
|
|
|
ordering to ensure those generated source files get compiled.
|
|
|
|
|
|
|
|
Next, define the list of `:thrift_files` that should be compiled. In this
|
|
|
|
example, we gather all of the `.thrift` files under the `thrift` directory:
|
|
|
|
|
|
|
|
```elixir
|
2016-02-02 18:05:04 +00:00
|
|
|
thrift_files: Mix.Utils.extract_files(["thrift"], [:thrift])
|
2015-01-07 01:44:59 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
By default, the generated source files will be written to the `src` directory,
|
|
|
|
but you can change that using the `thrift_output` option.
|
|
|
|
|
|
|
|
You can also pass additional options to the Thrift compiler by listing them in
|
2016-02-08 15:41:37 +00:00
|
|
|
the `thrift_options` option:
|
|
|
|
|
|
|
|
```elixir
|
|
|
|
thrift_options: ~w[-I my/include/dir]
|
|
|
|
```
|
2016-01-19 03:18:31 +00:00
|
|
|
|
2016-02-02 18:05:04 +00:00
|
|
|
If you require a specific version of the Thrift compiler, you can specify a
|
|
|
|
version requirement using the `thrift_version` option. Version requirements
|
|
|
|
use the [SemVer 2.0 schema][semver]. For example:
|
|
|
|
|
|
|
|
```elixir
|
|
|
|
thrift_version: ">= 0.9.3" # Erlang maps support
|
|
|
|
```
|
|
|
|
|
2016-05-28 00:59:15 +00:00
|
|
|
You can also override the name of the Thrift compiler executable itself:
|
|
|
|
|
|
|
|
```elixir
|
|
|
|
thrift_executable: "thrift-0.9.3"
|
|
|
|
```
|
2016-02-08 15:41:37 +00:00
|
|
|
|
|
|
|
If you get something like `type set() undefined` when compiling the generated files
|
|
|
|
you can try:
|
|
|
|
|
|
|
|
```elixir
|
|
|
|
thrift_options: ~w[--gen erl:maps]
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2016-01-19 03:18:31 +00:00
|
|
|
## Thrift IDL Parsing
|
|
|
|
|
|
|
|
This package also contains experimental support for parsing [Thrift IDL][idl]
|
|
|
|
files. For the moment, only an Erlang lexer is available, but the goal is to
|
|
|
|
provide more advanced parser support over time.
|
|
|
|
|
|
|
|
```erlang
|
|
|
|
:thrift_lexer.string('enum Colors { RED, GREEN, BLUE}')
|
|
|
|
{:ok,
|
|
|
|
[{:enum, 1}, {:ident, 1, 'Colors'}, {:symbol, 1, '{'}, {:ident, 1, 'RED'},
|
|
|
|
{:symbol, 1, ','}, {:ident, 1, 'GREEN'}, {:symbol, 1, ','},
|
|
|
|
{:ident, 1, 'BLUE'}, {:symbol, 1, '}'}], 1}
|
|
|
|
```
|
|
|
|
|
2016-02-02 18:05:04 +00:00
|
|
|
[semver]: http://semver.org/
|
2016-01-19 03:18:31 +00:00
|
|
|
[idl]: https://thrift.apache.org/docs/idl
|