Permalink
Browse files

update README

  • Loading branch information...
1 parent 20158ce commit d6869e89c68f66dc65e2fd66d767fa4e64611c97 @ninjudd committed May 5, 2010
Showing with 43 additions and 4 deletions.
  1. +20 −4 README.md
  2. +23 −0 examples/example.proto
View
@@ -20,9 +20,9 @@ Compile the file to Java:
Now you can use the protocol buffer in clojure:
(use 'protobuf)
- (defprotobuf person Example Person)
+ (defprotobuf Person Example$Person)
- (def p (protobuf person :id 4 :name "Bob" :email "bob@example.com"))
+ (def p (protobuf Person :id 4 :name "Bob" :email "bob@example.com"))
=> {:id 4, :name "Bob", :email "bob@example.com"}
(assoc p :name "Bill"))
@@ -31,10 +31,10 @@ Now you can use the protocol buffer in clojure:
(assoc p :likes ["climbing" "running" "jumping"])
=> {:id 4, name "Bob", :email "bob@example.com", :likes ["climbing" "running" "jumping"]}
- (def b (protobuf-bytes p))
+ (def b (protobuf-dump p))
=> #<byte[] [B@7cbe41ec>
- (protobuf person b)
+ (protobuf-load Person b)
=> {:id 4, :name "Bob", :email "bob@example.com"}
A protocol buffer map is immutable just like other clojure objects. It is similar to a
@@ -71,6 +71,22 @@ Compile the file to Java:
protoc --java_out=. -I<clojure-protobuf-path>/proto/ -I. example.proto
+Then you can access the maps in clojure:
+
+ (use 'protobuf)
+ (defprotobuf Photo Example$Photo)
+
+ (def p (protobuf Photo :id 7 :path "/photos/h2k3j4h9h23" :labels #{"hawaii" "family" "surfing"}
+ :attrs {"dimensions" "1632x1224", "alpha" "no", "color space" "RGB"}
+ :tags {4 {:person_id 4, :x_coord 607, :y_coord 813, :width 25, :height 27}}))
+ => {:id 7 :path "/photos/h2k3j4h9h23" :labels #{"hawaii" "family" "surfing"}...}
+
+ (def b (protobuf-dump p))
+ => #<byte[] [B@7cbe41ec>
+
+ (protobuf-load Photo b)
+ => {:id 7 :path "/photos/h2k3j4h9h23" :labels #{"hawaii" "family" "surfing"}...}
+
## Installation
To download clojure.jar and google's protobuf source automatically and install protoc:
View
@@ -1,3 +1,26 @@
+import "collections.proto";
+
+message Photo {
+ required int32 id = 1;
+ required string path = 2;
+ repeated string labels = 3 [(set) = true];
+ repeated Attr attrs = 4 [(map) = true];
+ repeated Tag tags = 5 [(map_by) = "person_id"];
+
+ message Attr {
+ required string key = 1;
+ optional string val = 2;
+ }
+
+ message Tag {
+ required int32 person_id = 1;
+ optional int32 x_coord = 2;
+ optional int32 y_coord = 3;
+ optional int32 width = 4;
+ optional int32 height = 5;
+ }
+}
+
message Person {
required int32 id = 1;
required string name = 2;

0 comments on commit d6869e8

Please sign in to comment.