https://github.com/samn/pitbull
Clojure Protocol Buffers
https://github.com/samn/pitbull
Last synced: 10 months ago
JSON representation
Clojure Protocol Buffers
- Host: GitHub
- URL: https://github.com/samn/pitbull
- Owner: samn
- Created: 2013-11-04T23:40:03.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2014-09-13T20:50:39.000Z (almost 12 years ago)
- Last Synced: 2025-06-27T09:55:53.951Z (12 months ago)
- Language: Clojure
- Size: 316 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
Awesome Lists containing this project
README
# pitbull
Clojure Protocol Buffers
### [Pitbull](https://en.wikipedia.org/wiki/Pitbull_%28rapper%29)
is a rapper.
###[pitbull](https://en.wikipedia.org/wiki/Pitbull)
is a wrapper around Protocol Buffer objects that exposes an `IPersistentMap`-like interface.
## Installation
pitbull is available on [Clojars](https://clojars.org/pitbull).
[pitbull "0.1.3"]
## Usage
*See [the docs](https://github.com/samn/pitbull/blob/master/doc/intro.md)*
Given a Protocol Buffer Message:
package Pets;
option java_package = "com.pets";
message Bear {
required string name = 1;
optional bool hungry = 2;
}
that has been used to generate a Java class (`com.pets.Pets$Bear`).
(require '[pitbull :as pb])
(import '[com.pets Pets$Bear])
You can create a new wrapped `Pets$Bear` instance from a regular `Map`:
(def bear-map {:name "yogi" :hungry true})
(def bear-protobuf-map (pb/map->ProtobufMap Pets$Bear bear-map))
(:name bear-protobuf-map) ; => "yogi"
;; fields can be accessed using String or Keyword keys
(get bear-protobuf-map "hungry") ; => true
A `ProtobufMap` will throw an `IllegalArgumentException` if a key that doesn't exist on the Protobuf definition is added:
(assoc bear-protobuf-map :color "brown") ; => IllegalArgumentException
or if a key that isn't present on the protobuf definition is read:
(get bear-protobuf-map "dogs") ; => IllegalArgumentException
An Exception will be thrown if you try to create a `ProtobufMap` with an invalid key, or if a required field is missing:
(pb/map->ProtobufMap Pets$Bear {:hungry false}) ; => com.google.protobuf.UninitializedMessageException
(pb/map->ProtobufMap Pets$Bear {:name "boo boo" :size "small"}) ; => IllegalArgumentException
You can serialize a `ProtobufMap` to a given `OutputStream`
(pb/serialize-to bear-protobuf-map (java.io.FileOutputStream "yogi.bear"))
A convenience function is provided to serialize directly to a byte array:
(pb/serialize-to-bytes bear-protobuf-map)
Or even a Map directly to a byte-array (or vice versa):
(->> {:name "Misha"}
(pb/map->proto-bytes Pets$Bear)
(bytes->ProtobufMap Pets$Bear))
## Build Status
[](https://travis-ci.org/samn/pitbull)
## [Pitbulls](https://en.wikipedia.org/wiki/Pitbull)
are the best kind of dog. If you could be a good guardian you should consider adopting one.

## License
Copyright © 2013 @samn
Distributed under the Eclipse Public License, the same as Clojure.