Skip to content

renderedtext/thrift-serializer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Thrift Serializer

Build Status

Encodes hashes to thrift messages and decodes thrift messages to hashes.

Installation

Add this line to your application's Gemfile:

gem "rt-thrift-serializer", :github => "renderedtext/thrift-serializer", :require => "thrift_serializer"

Encode hashes into Thrift messages

user      = User.new
user.name = "John Smith"
user.age  = 42

ThriftSerializer.encode(user)
# => "\v\x00\x01\x00\x00\x00\nJohn Smith\b\x00\x02\x00\x00\x00*\x00"

Decode Thrift messages into hashes

ThriftSerializer.decode(user, User.new)
# => { :name => "John Smith", :age => 32 }

NOTE: Make sure that app using ThriftSerializer have compiled .thrift file

Model Validation

Hashes are validated against corresponding struct defined in .thrift file in app that use ThriftSerializer

Let your .thrift file have following struct:

struct User {
  1: required string name
  2: required i32 age
}

Exception is raised in case:

  • Required field is missing
user      = User.new
user.name = "John Smith"
# Required field 'age' is missing

ThriftSerializer.encode(user)

# => raises Thrift::ProtocolException
  • If there are any extra fields
user      = User.new
user.name = "John Smith"
user.age  = 42
user.id   = 101

# => raises NoMethodError
  • Invalid struct field type
user      = User.new
user.name = "John Smith"
user.age  = "42"          # `age` is type of `i32`, got `string`

ThriftSerializer.encode(user)

# => raises ThriftSerializerError

About

Thrift to binary serialization and deserialization

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors