CrossClj

0.1.3 docs

SourceDocs



RECENT

    pitbull

    Clojars

    Sep 13, 2014


    OWNER
    Sam Neubardt
    google
    samneubardt@gmail.com

    Readme

    Index of all namespaces


    The README below is fetched from the published project artifact. Some relative links may be broken.

    pitbull

    Clojure Protocol Buffers

    Pitbull

    is a rapper.

    pitbull

    is a wrapper around Protocol Buffer objects that exposes an IPersistentMap-like interface.

    Installation

    pitbull is available on Clojars.

    [pitbull "0.1.3"]
    

    Usage

    See the docs

    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

    Travis CI

    Pitbulls

    are the best kind of dog. If you could be a good guardian you should consider adopting one. so cute!

    License

    Copyright © 2013 @samn

    Distributed under the Eclipse Public License, the same as Clojure.