The protocol-buffers package
Parse proto files and generate Haskell code.
[Skip to ReadMe]
|Versions||0.0.1, 0.0.2, 0.0.5, 0.1.0, 0.2.8, 0.2.9, 0.3.1, 1.0.0, 1.0.1, 1.2.1, 1.2.2, 1.4.0, 1.5.0, 1.6.0, 1.7.9, 1.8.0, 1.8.2, 1.8.3, 2.0.0, 2.0.2, 2.0.5, 2.0.6, 2.0.7, 2.0.8, 2.0.9, 2.0.10, 2.0.11, 2.0.12, 2.0.14, 2.0.17, 2.1.0, 2.1.1, 2.1.2, 2.1.3, 2.1.4, 2.1.5, 2.1.6, 2.1.7|
|Change log||None available|
|Dependencies||array, base (>=4.7.0 && <5), binary, bytestring, containers, directory, filepath, mtl, parsec, syb, utf8-string [details]|
|Copyright||(c) 2008-2015 Christopher Edward Kuklewicz|
|Author||Christopher Edward Kuklewicz|
|Maintainer||Kostiantyn Rybnikov <email@example.com>|
|Source repository||head: git clone git://github.com/k-bx/protocol-buffers.git|
|Uploaded||Thu Sep 17 14:09:00 UTC 2015 by k_bx|
|Distributions||LTSHaskell:2.1.7, NixOS:2.1.7, Stackage:2.1.7|
|Downloads||7427 total (187 in last 30 days)|
|Status||Docs available [build log]|
Last success reported on 2015-09-17 [all 1 reports]
- protocol-buffers-2.1.7.tar.gz [browse] (Cabal source package)
- Package description (included in the package)
For package maintainers and hackage trustees
Readme for protocol-buffers-2.1.7
This the README file for
hprotoc. These are three
interdependent Haskell packages originally written by Chris Kuklewicz.
Currently, maintainership was taken by Kostiantyn Rybnikov. It is planned to only support GHC 7.8 and newer unless someone explicitly asks for support of earlier versions.
(Needs check) This README was updated most recently to reflect version
2.0.7. This code should be compatible with Google protobuf version
2.3.0. Changes to keep up with Google protobuf version
What is this for? What does it do? Why?
It is a pure Haskell re-implementation of the Google code at https://developers.google.com/protocol-buffers/docs/overview which is "...a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more." Google's project produces C++, Java, and Python code. This one produces Haskell code.
How well does this Haskell package duplicate Google's project?
This provides non-mutable messages that ought to be wire-compatible with Google.
These messages support extensions.
These messages support unknown fields if hprotoc is passed the proper flag (-u or --unknown_fields).
This does not generate anything for Services/Methods.
Adding support for services has not been considered.
I think that Google's code checks for some policy violations that are not well documented enough for me to reverse engineer. Some (all?) of Google's APIs include the possibility of mutable messages. I suspect that my message reflection is not as useful at runtime as in some of Google's APIs.
What is protocol-buffers?
The protocol-buffers part is the main library which has two faces:
It provides an external API exported by module
Text.ProtocolBuffersfor users to read and write the binary format and manipulate the message data structures created by hprotoc.
It provides an internal API for the messages under module
Text.ProtocolBuffers.Headerto implement their tasks.
What is protocol-buffers-descriptor?
It uses the
It provides the code generated by hprotoc from
This supports hprotoc which is used to describe proto files and the code they will generate.
Text.DescriptorProtos.Optionswhich help in looking up the new style custom options.
What is hprotoc?
It is a command line tool that reads in
.protofiles and produces Haskell source trees like Google's protoc.
...and it contains a very nice lexer and parser for the
The hprotoc part is a executable program which reads
and uses the
protocol-buffers package to produce a tree of Haskell
source files. The program is called
hprotoc. Usage is given by the
program itself, the options themselves are processed in order. It can
take several input search paths, and allow an additional module
prefix, a selectable output directory, and ends with a list of of
proto file to generate from.
The output has to be a tree of modules since each message is given its own namespace, and a module is the only partitioning of namespace in Haskell. The keys for extension fields are defined alongside the message whose namespace they share. Since message names are both a data type and a namespace the filename and the message name match (aside from the .hs file extension).
And what are the examples and tests sub-directories?
The examples sub-directory is for duplicating the
example that Google has with its code. The
ABF2 file are
included as binary addressbooks. These can be read by the C++
examples from Google, and vice-versa.
tests sub-directory is where I have written some test code to
UnittestProto code generated from Google's
unittest_import.proto) files. The
file has the needed file patches to fix up the recursive imports (no
What do I need to compile the code?
- Install Haskell Stack Tool
Alternatively, go with old-fashioned
How mature is this code?
It can write the wire encoding and read it back. It has been tested
for interoperability against Google's read/write code with
hprotoc generates and uses the
Text.DescriptorProtos tree from
hprotoc has generated code from
Google/protobuf.unittest_import. These compile after adding hs-boot
TestMutualRecursionA.hs-boot to resolve mutual recursion. The
TestEnumWithDupValue has duplicated values which cause a compilation
There has been QuickCheck tests done for
UnittestProto/TestAllExtensions.hs in the tests subdirectory. These
pass as of
2008-09-19 for version
0.2.7. These test that random
messages can be roundtripped to the wire format without changing —
with the caveat that the new extension keys are read back as raw bytes
but compare equal because of the parsing done by (==).
Mutual recursion is a problem?
Not using ghc. The haskell-src-exts let me generate code with
SOURCE #-} annotated imports. And
hprotoc generates the needed
hs-boot files for ghc. And key import cycles are broken by creating
Key.hs files, which users can ignore.
How stable is the API?
This is the first working release of the code. I do not promise to keep any of the API but I am lazy so most things will not change. The reflection capabilities may get improved/altered. Stricter warnings and error detection may be added. Code will move between protocol-buffers and hprotoc projects. The internals of reading from the wire may be improved.
Where is the API documentation?
Generate haddock with
stack haddock command.
You can also view API documentation online at Hackage page.
The imports of
Text.ProtocolBuffers are the public API. The
generated code's API is
Text.ProtocolBuffers.Header. The only usage
examples are in the
examples sub-directory and the
sub-directory. Since the messages are simply Haskell data types most
of the manipulation should be easy.
The main thing that is weird is that messages with extension ranges
get an ExtField record field that holds ... an internal data
structure. This is currently a
Map from field number to a rather
complicated existential + GADT combination that should really only be
touched by the
MessageAPI type class methods. The
ExtField data constructor is not hidden, though it could be and
probably ought to be.
Note that extension fields are inherently slower, especially in ghci
-O2 helps quite a bit).
The entire proto file is stored in the top level module in
wire-encoded form and can be accessed as a
Haskell code also defines its own reflection data types, with one
stored in each generated module and also in a master data type in the
top level module (via
Who reads this far?
I suspect no one ever will.
Why define your own Haskell reflection types in addition to
This allows for the protocol-buffers library package to not depend on
a single thing defined in the
This lack of recursion made for much simpler bootstrapping and allows
descriptor.proto generated files to be build separately.
descriptor.proto files are a great fit as output from parsing
a proto file they are not as good a fit for code generation. They mix
fields and extension keys, they have all optional fields even though
some things (especially names) are compulsory. They obscure which
descriptors are groups. They have a nested structure which is useful
when resolving the names but not for iterating over for code
What are the pieces of protocol-buffers doing?
Basic.hsdefines the core data types (that are not already in
Prelude) and many classes.
Mergeable.hsdefines the standard instances of
Mergeablefor combining types.
Default.hsdefines the standard default of the basic data types.
Reflections.hsdefines the Haskell reflection data types (stored with each generated module).
Get.hsis here because I needed a slightly different style of binary
Getmonad (see binary and binary-strict packages). This is standalone and could be put into any project. It has long comments inside.
WireMessage.hsdefines 3 things:
- The Wire instances for the basic data types
- The API for the generated module to use to define their own Wire instances
- The API for the user to load and save messages This file would not compile with ghc-6.8.3 on a G4 (Mac OS X 10.5.4, XCode 3.1) without -fvia-C as the cabal file states.
Extensions.hsis rather large because it add everything needed for extension fields (see haddock API docs). It should not export ExtField's constructor, but it currently does.
Header.hsre-exports what is needed for the instance messages.
ProtocolBuffer.hsre-exports what is needed for the user API.
What are the pieces of hprotoc doing?
Lexer.x to generated
Lexer.hs which slices up the
.proto file into tokens. The
.proto layout is well designed,
quite unambiguous, and easy to tokenize. The lexer also does the jobs
of decoding the backslash escape codes in quotes strings, and
interpreting floating point numbers. Errors and unexpected input are
inserted into the token list, with at least line number level
Parser.hs file has a
Parsec parser which are really used as
nested parsers (allowing for the type of the user state to change).
.proto grammar is well designed and the system never needs to
backtrack over tokens. The default values and options' values parsed
according to the expected type, and string default are check for valid
utf8 encoding. (This also import the
Resolve.hs has code to resolve all the names to a fully
qualified form, including name mangling where necessary. This includes
code to load and parse all the imported
.proto files, reusing parses
for efficiency, and detecting import loops. The context built from
each imported file is combined to change the
into a modified
FileDescriptorProto. This stage also determines that
extension keys are in a valid extensions range declaration, and enum
default values exists.
MakeReflections.hs file converts the nested
into a flatter Haskell reflection data structure. This includes
parsing the default value stored in the
BreakRecursion.hs file builds graphs describing the imports and
works out whether and how to create hs-boot and
Key.hs files to allow
allow for warning-free compilation with ghc (as of 6.10.1).
Gen.hs file takes a Haskell data structure from
MakeReflections and builds a module syntax data structure. The
syntax data is quite verbose and several helper functions are used to
help with the composition. The result is easy to print as a string to
ProtoCompile.hs file is the Main module which defines the
command line program
hprotoc. This manages most of the interaction
with the file system (aside from import loading in Resolve).
Everything that is needed is collected into the Options data type
which is passed to "run". The output style can be tweaked by changing
"style" and "myMode".