Skip to content

Commit

Permalink
Rename *code-all functions to *code-seq instead.
Browse files Browse the repository at this point in the history
This better reflects the fact that the sequences involved may be lazy,
especially the return of `decode-seq`.
  • Loading branch information
greglook committed Nov 7, 2017
1 parent 0a458f1 commit 0c0498a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- *Breaking:* the default set tag is now 258, matching the IANA registry.
[#6](//github.com/greglook/clj-cbor/issues/6)
- *Breaking:* `clj-cbor.core/decode` now only decodes a single value; previous
behavior moved to `decode-all`.
behavior moved to `decode-seq`.
[#7](//github.com/greglook/clj-cbor/issues/7)

### Added
- `clj-cbor.core/encode-all` writes a sequence of values to a byte array or
- `clj-cbor.core/encode-seq` writes a sequence of values to a byte array or
output stream.
- `clj-cbor.core/spit-all` similarly writes a sequence of values to an output
file like `spit`.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ output stream, and the value to encode.
; 0xD827623A61187BF563666F6F

=> (with-open [input (java.io.ByteArrayInputStream. *1)]
(doall (cbor/decode-all codec input)))
(doall (cbor/decode-seq codec input)))
(:a 123 true "foo")
```

In this mode, `encode` returns the number of bytes written instead of a byte
array. We can read multiple items from an input stream using `decode-all`, which
array. We can read multiple items from an input stream using `decode-seq`, which
returns a lazy sequence. If the input is a file you must realize the values
before closing the input. Similarly, `encode-all` will write a sequence of
before closing the input. Similarly, `encode-seq` will write a sequence of
multiple values to an output stream.

As a convenience, the library also provides the `spit`, `spit-all`, `slurp`, and
Expand Down
14 changes: 7 additions & 7 deletions src/clj_cbor/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,19 @@
(codec/write-value encoder data-output value))))


(defn encode-all
(defn encode-seq
"Encode a sequence of values as CBOR data. This eagerly consumes the
input sequence.
In the full argument form, this writes a value to an output stream and
returns the number of bytes written. If output is omitted, the function
returns a byte array instead. Uses the `default-codec` if none is provided."
([values]
(encode-all default-codec values))
(encode-seq default-codec values))
([encoder values]
(let [buffer (ByteArrayOutputStream.)]
(with-open [output (data-output-stream buffer)]
(encode-all encoder output values))
(encode-seq encoder output values))
(.toByteArray buffer)))
([encoder ^OutputStream output values]
(let [data-output (data-output-stream output)]
Expand Down Expand Up @@ -175,14 +175,14 @@
(try-read-value decoder input header)))))


(defn decode-all
(defn decode-seq
"Decode a sequence of CBOR values from the input.
The input may be a byte array or coercible to an `input-stream`. Uses the
`default-codec` if none is provided. This returns a lazy sequence, so take
care that the input stream is not closed before the entries are realized."
([input]
(decode-all default-codec input))
(decode-seq default-codec input))
([decoder input]
(let [eof-guard (Object.)
data-input (data-input-stream input)
Expand Down Expand Up @@ -213,7 +213,7 @@
truncating."
[f values & opts]
(with-open [out ^OutputStream (apply io/output-stream f opts)]
(encode-all default-codec out values)))
(encode-seq default-codec out values)))


(defn slurp
Expand All @@ -229,7 +229,7 @@
stream."
[f & opts]
(with-open [in ^InputStream (apply io/input-stream f opts)]
(doall (decode-all default-codec in))))
(doall (decode-seq default-codec in))))


(defn self-describe
Expand Down
2 changes: 1 addition & 1 deletion test/clj_cbor/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


(deftest multi-value-coding
(is (= [true 123 :abc] (cbor/decode-all (cbor/encode-all [true 123 :abc])))))
(is (= [true 123 :abc] (cbor/decode-seq (cbor/encode-seq [true 123 :abc])))))


(deftest eof-handling
Expand Down
2 changes: 1 addition & 1 deletion test/clj_cbor/test_utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
([string]
(decode-hex-all (cbor/cbor-codec) string))
([decoder string]
(doall (cbor/decode-all decoder (hex->bin string)))))
(doall (cbor/decode-seq decoder (hex->bin string)))))


(defn encoded-hex
Expand Down

0 comments on commit 0c0498a

Please sign in to comment.