pinch: An alternative implementation of Thrift for Haskell.

[ bsd3, development, library ] [ Propose Tags ] [ Report a vulnerability ]

This library provides machinery for types to specify how they can be serialized and deserialized into/from Thrift payloads. It makes no assumptions on how these payloads are sent or received and performs no code generation. Types may specify how to be serialized and deserialized by defining instances of the Pinchable typeclass by hand, with automatically derived instances by using generics or by using the pinch-gen code generator. Check the documentation in the Pinch module for more information.

What is Thrift? Apache Thrift provides an interface description language, a set of communication protocols, and a code generator and libraries for various programming languages to interact with the generated code. Pinch aims to provide an alternative implementation of Thrift for Haskell.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.2.0.0, 0.2.0.1, 0.2.0.2, 0.3.0.0, 0.3.0.1, 0.3.0.2, 0.3.1.0, 0.3.2.0, 0.3.3.0, 0.3.4.0, 0.3.4.1, 0.3.5.0, 0.4.0.0, 0.4.1.0, 0.4.1.1, 0.4.1.2, 0.4.2.0, 0.4.3.0, 0.5.0.0, 0.5.1.0, 0.5.2.0 (info)
Change log CHANGES.md
Dependencies array (>=0.5), base (>=4.7 && <5), bytestring (>=0.10 && <0.13), cereal (>=0.5.8.1 && <0.6), containers (>=0.5 && <0.8), deepseq (>=1.3 && <1.6), ghc-prim, hashable (>=1.2 && <1.6), network (>=3.1.2.0 && <3.3), semigroups (>=0.18 && <0.21), text (>=1.2 && <1.3 || >=2.0), unordered-containers (>=0.2 && <0.3), vector (>=0.10 && <0.14) [details]
Tested with ghc ==8.10.2, ghc ==8.8.3, ghc ==8.6.5, ghc ==8.4.4
License BSD-3-Clause
Author Abhinav Gupta
Maintainer mail@abhinavg.net
Category Development
Home page https://github.com/abhinav/pinch#readme
Bug tracker https://github.com/abhinav/pinch/issues
Source repo head: git clone https://github.com/abhinav/pinch
Uploaded by abhinav at 2024-07-09T02:44:30Z
Distributions LTSHaskell:0.5.2.0, NixOS:0.5.2.0, Stackage:0.5.2.0
Reverse Dependencies 4 direct, 0 indirect [details]
Downloads 11135 total (15 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-07-09 [all 1 reports]

Readme for pinch-0.5.2.0

[back to package description]

build

pinch aims to provide an alternative implementation of Apache Thrift for Haskell. The pinch library itself acts only as a serialization library. Types specify their Thrift encoding by defining instances of the Pinchable typeclass, which may be done by hand or automatically with the use of Generics.

Haddock documentation for this package is avilable on Hackage and here.

Overview

Types which can be encoded into Thrift payloads implement the Pinchable typeclass.

Given the Thrift struct,

struct Person {
    1: required string name
    2: optional i64 dateOfBirth
}

You can write a Pinchable instance like so,

data Person = Person { name :: Text, dateOfBirth :: Maybe Int64 }
    deriving (Eq)

instance Pinchable Person where
    type Tag Person = TStruct
    -- The Tag tells the system that this represents a struct.

    pinch (Person name dateOfBirth) =
        struct [1 .= name, 2 ?= dateOfBirth]

    unpinch value =
        Person <$> value .:  1
               <*> value .:? 2

Better yet, you can derive an instance automatically.

{-# LANGUAGE DeriveGeneric, DataKinds #-}
import GHC.Generics (Generic)

data Person = Person
    { name        :: Field 1 Text
    , dateOfBirth :: Field 2 (Maybe Int64)
    } deriving (Eq, Generic)

instance Pinchable Person

Objects can be serialized and deserialized using the encode and decode methods. These methods accept a Protocol as an argument.

decode binaryProtocol (encode binaryProtocol person) == person

For more information, check the documentation and the examples.

Supported Protocols

The following Thrift protocols are supported:

  • Binary
  • Compact

Supported Transports

The following Thrift transports are supported:

  • Framed
  • Unframed

Code Generation

If you prefer to generate Haskell code from the Thrift files instead of writing the necessary Haskell code by hand, you can use the experimental pinch-gen code generator to do so. For further details see https://github.com/phile314/pinch-gen/ .