The protocol-buffers package

[Tags: bsd3, library]

Parse proto files and generate Haskell code.


[Skip to ReadMe]

Properties

Versions0.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
Change logNone available
Dependenciesarray, base (>=4.7.0 && <5), binary, bytestring, containers, directory, filepath, mtl, parsec, syb, utf8-string [details]
LicenseBSD3
Copyright(c) 2008-2015 Christopher Edward Kuklewicz
AuthorChristopher Edward Kuklewicz
MaintainerKostiantyn Rybnikov <k-bx@k-bx.com>
CategoryText
Home pagehttps://github.com/k-bx/protocol-buffers
Source repositoryhead: git clone git://github.com/k-bx/protocol-buffers.git
UploadedTue Aug 25 09:47:24 UTC 2015 by k_bx
DistributionsLTSHaskell:2.1.4, NixOS:2.1.5, Stackage:2.1.5
Downloads6647 total (364 in last 30 days)
Votes
0 []
StatusDocs available [build log]
Last success reported on 2015-08-25 [all 1 reports]

Modules

[Index]

Downloads

Maintainers' corner

For package maintainers and hackage trustees

Readme for protocol-buffers-2.1.5

Build Status Haskell Protocol Buffers

This the README file for protocol-buffers, protocol-buffers-descriptors, and 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 2.4.0 are being considered.

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?

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:

  1. It provides an external API exported by module Text.ProtocolBuffers for users to read and write the binary format and manipulate the message data structures created by hprotoc.

  2. It provides an internal API for the messages under module Text.ProtocolBuffers.Header to implement their tasks.

What is protocol-buffers-descriptor?

What is hprotoc?

The hprotoc part is a executable program which reads .proto files 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 addressbook.proto example that Google has with its code. The ABF and ABF2 file are included as binary addressbooks. These can be read by the C++ examples from Google, and vice-versa.

The tests sub-directory is where I have written some test code to drive the UnittestProto code generated from Google's unittest.proto (and unittest_import.proto) files. The patchBoot file has the needed file patches to fix up the recursive imports (no longer needed!).

What do I need to compile the code?

  1. Install Haskell Stack Tool
  2. Run stack build

Alternatively, go with old-fashioned cabal build.

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 addressbook.proto.

hprotoc generates and uses the Text.DescriptorProtos tree from Google descriptor.proto file.

hprotoc has generated code from Google/protobuf/unittest.proto and Google/protobuf.unittest_import. These compile after adding hs-boot files TestAllExtensions.hs-boot, TestFieldOrderings.hs-boot, and TestMutualRecursionA.hs-boot to resolve mutual recursion. The TestEnumWithDupValue has duplicated values which cause a compilation warning.

There has been QuickCheck tests done for UnittestProto/TestAllType.hs and 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 tests 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 ExtKey and 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 (though ghc's -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 FileDescriptorProto. The 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 Show and Read).

Who reads this far?

I suspect no one ever will.

Why define your own Haskell reflection types in addition to FileDescriptorProtos types?

This allows for the protocol-buffers library package to not depend on a single thing defined in the protocol-buffers-descriptor package. This lack of recursion made for much simpler bootstrapping and allows the descriptor.proto generated files to be build separately.

While 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 generation.

What are the pieces of protocol-buffers doing?

What are the pieces of hprotoc doing?

alex uses 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 precision.

The Parser.hs file has a Parsec parser which are really used as nested parsers (allowing for the type of the user state to change). The .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 Instances.hs file)

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 FileDescriptorProto into a modified FileDescriptorProto. This stage also determines that extension keys are in a valid extensions range declaration, and enum default values exists.

The MakeReflections.hs file converts the nested FileDescriptorProto into a flatter Haskell reflection data structure. This includes parsing the default value stored in the FileDescriptorProto.

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).

The 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 a file.

The 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".