pinch-gen: A code generator for the pinch Thrift library.

[ bsd3, development, program ] [ Propose Tags ]


Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


  • No Candidates
Versions [RSS],,,,
Change log
Dependencies base (>=4.12 && <5), bytestring, directory, filepath, language-thrift (>=, megaparsec, mtl, optparse-applicative, prettyprinter, text, unordered-containers [details]
License BSD-3-Clause
Author Tiko Energy Systems (Philipp Hausmann)
Revised Revision 2 made by PhilippHausmann at 2024-01-08T17:27:14Z
Category Development
Home page
Bug tracker
Source repo head: git clone
Uploaded by PhilippHausmann at 2024-01-06T18:26:59Z
Executables pinch-gen
Downloads 490 total (35 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs not available [build log]
Last success reported on 2024-01-06 [all 1 reports]

Readme for pinch-gen-

[back to package description]


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.


Converts a Thrift file into Haskell code for the pinch library.


Usage: pinch-gen --in IN_FILE --out OUT_DIR --hashable-vec-mod ARG
                 [--no-generate-arbitrary] [--no-generate-nfdata] [--extra-import IMPORT]
  Generate Haskell files from a thrift input file.

The --hashable-vec-mod argument should be set to a module providing a Hashable instance for Vector. This is required as a Vector may become part of a key of a map, but neither the vector nor the hashable package provide an instance. For some background, see .The simplest solution is to depend on the vector-instances package and pass --hashable-vec-mod Data.Vector.Instances to pinch-gen.


pinch version pinch-gen version
0.4 0.4.*


Let us use this simple Thrift service as an example:


# A simple struct
struct MyStruct {
  1: required binary payload;

# Trivial exception for testing only.
exception Exception {

  # The exception simply contains a message string.
  1: required string message;

# Trivial service for testing only.
service Trivial {

  # Takes a struct and returns a string.
  string success(1: MyStruct argument);

  # Throws an arbitrary string.
  void failure() throws (1: Exception error);

  # Fire rocket.
  oneway void fireAndForget(1: i32 rocket);

To generate the corresponding Haskell code we can call pinch-gen:

pinch-gen --no-generate-arbitrary --no-generate-nfdata --hashable-vec-mod Data.Vector.Instances --in trivial.thrift --out out/

This will create the appropriate datatypes for all struct, union and exception types:


data MyStruct
  = MyStruct
  { myStruct_payload :: Data.ByteString.ByteString
  deriving (Prelude.Eq, GHC.Generics.Generic, Prelude.Show)

data Exception
  = Exception
  { exception_message :: Data.Text.Text
  deriving (Prelude.Eq, GHC.Generics.Generic, Prelude.Show)

For the server, a record-style encoding of all functions is used. Given an implementation of these functions, a Pinch.Server.ThriftServer for use with the pinch library can be created:


data Trivial
  = Trivial
  { success :: (Pinch.Server.Context) -> (MyStruct) -> (Prelude.IO Data.Text.Text)
  , failure :: (Pinch.Server.Context) -> (Prelude.IO ())
  , fireAndForget :: (Pinch.Server.Context) -> (Data.Int.Int32) -> (Prelude.IO ())

trivial_mkServer :: (Trivial) -> Pinch.Server.ThriftServer
trivial_mkServer server = ...

For the client, functions creating a Pinch.Client.ThriftCall for use with the pinch library are generated. You can use if you want to explicitly match on the success result/thrown exceptions as defined in the Thrift file. Alternatively, you may use Pinch.Client.callOrThrow to directly access the result. In case the rqeuest failed, callOrThrow will throw an exception using throwIO.


success :: (MyStruct) -> (Pinch.Client.ThriftCall Success_Result)
success argument = ...

failure :: (Pinch.Client.ThriftCall Failure_Result)
failure  = ...

fireAndForget :: (Data.Int.Int32) -> (Pinch.Client.ThriftCall Pinch.Internal.RPC.Unit)
fireAndForget rocket = ...


The generated code is currently not formatted very nicely.