• Stars
    star
    95
  • Rank 342,418 (Top 7 %)
  • Language
    Crystal
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Protobuf generator, encoder and decoder.

protobuf Crystal Spec

Crystal shard to decode, encode and generate protobuf messages.

Installation

Add this to your application's shard.yml:

dependencies:
  protobuf:
    github: jeromegn/protobuf.cr

1. Install protobuf

macOS: brew install protobuf

Ubuntu sudo apt-get install -y protobuf-compiler

2. Install the protoc plugin

macOS:
brew install jeromegn/tap/protoc-gen-crystal
Ubuntu:
crystal build bin/protoc-gen-crystal.cr -o ~/bin/protoc-gen-crystal

Usage

Generate .pb.cr files

Protobuf provides the protoc executable to encode, decode and generate language-specific protobuf mappings via plugins.

protoc -I <.protos_basepath> --crystal_out <path_to_folder_for_protobufs> <path_to_{.proto,*.protos}>

Generator options

The generator is configurable via environment variables:

  • PROTOBUF_NS - If your want to namespace everything under a module (default: ""). Please write with a CamelCase format (ie: "MesosMessage" would produce module MesosMessage)
  • STRIP_FROM_PACKAGE - Protobuf has package namespaces and sometimes messages reference other namespaces (ie: mesos.v1.scheduler.Call), but you want those to be namespaced in a Crystal-like fashion (ie: Scheduler::Call), then you can specify a string to strip from the packages for each file (ie: STRIP_FROM_PACKAGE=mesos.v1) and the rest will be CamelCased

Decoding and encoding messages

to_protobuf(io : IO) and from_protobuf(io : IO) are available on enums and anything that includes Protobuf::Message and defines a contract

Example:

require "protobuf"

enum Foo
  FOO
end

# Struct generated from a .proto file.  Don't create this yourself
struct MyMessage
  include Protobuf::Message
  contract do
    # some required properties
    required :prop_name, :int32, 1, default: 123
    required :prop2, Foo, 2

    # optional properties
    optional :optional_prop_name, :string, 3

    # repeated fields
    repeated :my_array, :int32, 4 # produces a property of type Array(Int32)?
  end

  # write your methods like you normally would here, if you like.
end

proto_io = File.read("path/to/encoded/protobuf") # get your IO in some way

msg = MyMessage.from_protobuf(proto_io) # returns a an instance of MyMessage
                                  # from a valid protobuf encoded message

msg.to_protobuf # return a IO::Memory filled with the encoded message

some_io = IO::Memory.new
msg.to_protobuf(some_io) # fills up the provided IO with the encoded message

Field types

All field types supported by the protobuf protocol v2 are available as symbols or the name of a Crysta; struct or class.

Constructor

Using the contract block creates an initializer with all the properties defined in it. It also creates an initializer which can consume a Protobuf::Buffer (used by from_protobuf method), not unlike JSON::PullParser.

Known Limitations

  • Does not support non-UTF8 strings...

Versioning

This library does not follow semver conventions for versioning. It started off at v2.0.0 because it supports protobuf v2. From there, it'll increment versions based on semver, except for breaking changes, where I will not increment the major version.

Development

Fixture data (used for specs) can be autogenerated:

make fixtures

See spec/fixtures/Makefile for details.

Contributing

  1. Fork it ( https://github.com/jeromegn/protobuf.cr/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • jeromegn Jerome Gravel-Niquet - creator, maintainer

More Repositories

1

Backbone.localStorage

A localStorage adapter for Backbone.js
JavaScript
1,899
star
2

DocumentUp

Pretty documentation generator for Github projects with proper Readme.
Ruby
886
star
3

slang

Slim-inspired templating language for Crystal
Crystal
232
star
4

kilt

Generic template interface for Crystal
Crystal
148
star
5

localtodos

Simple Todos App built on Backbone.js
JavaScript
99
star
6

poutine

MongoDB ORM for Node.js
CoffeeScript
35
star
7

docker.cr

Docker API client in Crystal.
Crystal
26
star
8

influxdb.cr

InfluxDB driver for Crystal
Crystal
25
star
9

bson.cr

BSON implementation written in Crystal
Crystal
24
star
10

v8.cr

V8 bindings for Crystal
C++
16
star
11

dry-types-rails

Dry::Types railties
Ruby
11
star
12

beautifier

A safari extension which smooths fonts!
10
star
13

Warping-Shadow-CSS3

Warping Shadows can be done in CSS3
Ruby
8
star
14

mongol

Light modeling for MongoDB.
JavaScript
8
star
15

jeromegn.github.com

Personal site
HTML
5
star
16

rapid

Redis ORM-ish api for nodejs
JavaScript
3
star
17

backbone.pjax

Backbone utilities for web apps using PJAX.
JavaScript
3
star
18

rhai-tpl

Simple template engine using rhai for logic
Rust
2
star
19

qml.cr

QML library for Crystal
Crystal
2
star
20

quinn-plaintext

QUIC pluggable crypto to use the protocol as plaintext (for use when cryptography is already handled at another layer, e.g. Wireguard)
Rust
1
star
21

delicious-safari-extension

Safari 5.0 Extension: Save URL of current page to Delicious
1
star
22

Radiophonique

Objective-C
1
star
23

containerd-rs

Rust
1
star
24

lg-ultrafine-brightness

Control LG Ultrafine brightness on linux
Rust
1
star
25

documentup.js

experimental DocumentUp running on fly.io
JavaScript
1
star
26

portfolio

My portfolio, built with StaticMatic
JavaScript
1
star
27

node-api-boilerplate

Simple Node.js API server boilerplate using CoffeeScript and Express
CoffeeScript
1
star