• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

awakesecurity/proto3-suite: Haskell Protobuf Implementation

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

awakesecurity/proto3-suite

开源软件地址(OpenSource Url):

https://github.com/awakesecurity/proto3-suite

开源编程语言(OpenSource Language):

Haskell 88.8%

开源软件介绍(OpenSource Introduction):

proto3-suite

Hackage Build Status

This package defines tools for working with protocol buffers version 3 in Haskell.

This library provides a higher-level API to the proto3-wire library that supports:

  • Type classes for encoding and decoding messages, and instances for all wire formats identified in the specification
  • A higher-level approach to encoding and decoding, based on GHC.Generics
  • A way of creating .proto files from Haskell types

See the Proto3.Suite.Tutorial module for more details.

Building

Nix shell + Cabal (recommended)

The Nix shell provides an incremental build environment (but see below for testing). From the root of this repository, run:

$ nix-shell
[nix-shell]$ cabal build

Once your source code compiles and you want to test, run this instead:

$ nix-shell
[nix-shell]$ cabal configure --enable-tests
[nix-shell]$ cabal build
[nix-shell]$ cabal test

Nix

Building with Nix is simple, but not incremental. From the root of this repository, run:

$ nix-build --attr proto3-suite

The build products will be available via the ./result symlink.

Stack

We use Nix and Cabal at Awake Security, so those are the most exercised paths. Stack support is provided on a best effort basis.

Building the library and executable components is straightforward. From the root of this repository, run:

$ stack build

Building and running tests is more complicated when using Stack. You'll need to use the compile-proto-file executable you just compiled to convert test_*.proto files to Haskell modules, by running the following from the root of this repository:

$ mkdir gen
$ for proto in $(find test-files -name 'test_*.proto'); do
    stack run compile-proto-file -- --out gen --includeDir test-files --proto "${proto#test-files/}"
  done
$ stack test

Running the language interop tests

We test inter-language interop using protoc's built-in Python code generation. In order to successfully run these tests, you'll need to install the Google protobuf Python library. It's best to create a virtualenv and then use pip to install the right version (virtualenv is a Python utility which can be installed with pip).

$ virtualenv pyenv
$ source pyenv/bin/activate
$ pip install protobuf==3.0.0b3  # Need the latest version for the newest protoc

brew install python may also work.

Installing compile-proto-file and canonicalize-proto-file

To install the compile-proto-file and canonicalize-proto-file executables, run the following commmand from the root of this repository:

$ nix-env --file default.nix --install --attr proto3-suite

To uninstall, removing the executables from your Nix user profile PATH, run:

$ nix-env --uninstall proto3-suite

Using compile-proto-file

$ compile-proto-file --help
Usage: compile-proto-file [--includeDir DIR] [--extraInstanceFile FILE]
                          --proto FILE --out DIR
  Compiles a .proto file to a Haskell module

Available options:
  -h,--help                Show this help text
  --includeDir DIR         Path to search for included .proto files (can be
                           repeated, and paths will be searched in order; the
                           current directory is used if this option is not
                           provided)
  --extraInstanceFile FILE Additional file to provide instances that would
                           otherwise be generated. Can be used multiple times.
                           Types for which instance overrides are given must be
                           fully qualified.
  --proto FILE             Path to input .proto file
  --out DIR                Output directory path where generated Haskell modules
                           will be written (directory is created if it does not
                           exist; note that files in the output directory may be
                           overwritten!)

compile-proto-file bases the name (and hence, path) of the generated Haskell module on the filename of the input .proto file, relative to the include path where it was found, converting snake case to camel case as needed.

As an example, let's assume this is our current directory structure before performing any code generation:

.
├── my_protos
│   └── my_package.proto
└── other_protos
    └── google
        └── protobuf
            ├── duration.proto
            └── timestamp.proto

...where my_package.proto is:

syntax = "proto3";
package some_package_name;
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";
message MyMessage {
  Timestamp timestamp = 1;
  Duration  duration  = 2;
}

Then, after running the following commands:

$ compile-proto-file --out gen --includeDir my_protos --includeDir other_protos --proto google/protobuf/duration.proto
$ compile-proto-file --out gen --includeDir my_protos --includeDir other_protos --proto google/protobuf/timestamp.proto
$ compile-proto-file --out gen --includeDir my_protos --includeDir other_protos --proto my_package.proto

...the directory tree will look like this:

.
├── gen
│   ├── Google
│   │   └── Protobuf
│   │       ├── Duration.hs
│   │       └── Timestamp.hs
│   └── MyPackage.hs
├── my_protos
│   └── my_package.proto
└── other_protos
    └── google
        └── protobuf
            ├── duration.proto
            └── timestamp.proto

Note that delimiting . characters in the input .proto basename are treated as / characters, so the input filenames google.protobuf.timestamp.proto and google/protobuf/timestamp.proto would produce the same generated Haskell module name and path.

This is essentially the same module naming scheme as the protoc Python plugin uses when compiling .proto files.

Docker

For those unable to run Nix locally, a Dockerfile is provided:

docker build -t compile-proto-file .
docker run --rm -v $PWD:/opt compile-proto-file --proto proto/test.proto --out src/gen



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
hreinhardt/amqp: Haskell AMQP client library发布时间:2022-06-22
下一篇:
morpheusgraphql/morpheus-graphql: Haskell GraphQL Api, Client and Tools发布时间:2022-06-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap