Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#14] Prepare for clojurescript support #18

Merged
merged 2 commits into from
Dec 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject protojure "1.0.2-SNAPSHOT"
(defproject protojure "1.1.0-SNAPSHOT"
:description "Support library for protoc-gen-clojure, providing native Clojure support for Google Protocol Buffers and GRPC applications"
:url "http://github.com/protojure/library"
:license {:name "Apache License 2.0"
Expand Down
36 changes: 16 additions & 20 deletions src/protojure/grpc/codec/lpm.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"Utility functions for GRPC [length-prefixed-message](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests) encoding."
(:require [clojure.core.async :refer [<! >! go go-loop] :as async]
[promesa.core :as p]
[protojure.protobuf :as pb]
[protojure.protobuf :refer [->pb]]
[protojure.grpc.codec.compression :as compression]
[clojure.tools.logging :as log])
(:import (protojure.internal.grpc.io InputStream
Expand Down Expand Up @@ -151,38 +151,34 @@ The value for the **content-coding** option must be one of
;;--------------------------------------------------------------------------------------
;; Encoder
;;--------------------------------------------------------------------------------------
(defn- encode-header [os compressed? len]
(defn- encode-buffer [buf len compressed? os]
(.write os (int (if compressed? 1 0)))
(.write os (num->bytes len)))

(defn- encode-uncompressed
([msg os]
(encode-uncompressed msg (pb/length msg) os))
([msg len os]
(encode-header os false len)
(pb/->pb msg os)))

(defn- encode-compressed-buffer [buf len os]
(encode-header os true len)
(.write os (num->bytes len))
(.write os buf))

(defn- compress-msg [compressor msg]
(defn- encode-uncompressed [msg os]
(let [buf (->pb msg)
len (count buf)]
(encode-buffer buf len false os)))

(defn- compress-buffer [compressor buf]
(let [os (ByteArrayOutputStream.)
cos (compressor os)]
(pb/->pb msg cos)
(.write cos buf)
(.close cos)
(.toByteArray os)))

(defn- encode-maybe-compressed
"This function will encode the message either with or without compression,
depending on whichever results in the smaller message"
[msg compressor os]
(let [buf (compress-msg compressor msg)
clen (count buf)
len (pb/length msg)]
(let [buf (->pb msg)
len (count buf)
cbuf (compress-buffer compressor buf)
clen (count buf)]
(if (< clen len)
(encode-compressed-buffer buf clen os)
(encode-uncompressed msg len os))))
(encode-buffer cbuf clen true os)
(encode-buffer buf len false os))))

;;--------------------------------------------------------------------------------------------
(defn encode
Expand Down
16 changes: 6 additions & 10 deletions src/protojure/protobuf.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@

(ns protojure.protobuf
"Main API entry point for protobuf applications"
(:import (com.google.protobuf
CodedOutputStream)))

(defprotocol Writer
(serialize [this os])
(length [this]))
(:require [protojure.protobuf.protocol :refer [serialize]])
(:import (com.google.protobuf CodedOutputStream)
(java.io ByteArrayOutputStream)))

(defn ->pb
"Serialize a record implementing the [[Writer]] protocol into protobuf bytes."
([msg]
(let [len (length msg)
data (byte-array len)]
(->pb msg data)
data))
(let [os (ByteArrayOutputStream.)]
(->pb msg os)
(.toByteArray os)))
([msg output]
(let [os (CodedOutputStream/newInstance output)]
(serialize msg os)
Expand Down
9 changes: 9 additions & 0 deletions src/protojure/protobuf/protocol.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
;; Copyright © 2019 State Street Bank and Trust Company. All rights reserved
;; Copyright © 2019 Manetu, Inc. All rights reserved
;;
;; SPDX-License-Identifier: Apache-2.0

(ns protojure.protobuf.protocol)

(defprotocol Writer
(serialize [this os]))
249 changes: 0 additions & 249 deletions src/protojure/protobuf/serdes.clj

This file was deleted.

Loading