-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.clj
59 lines (51 loc) · 1.64 KB
/
util.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
(ns lore.util
(:require [clojure.java.io :as io]
[clojure.spec.alpha :as s]
[cognitect.anomalies :as anomalies])
(:import [java.nio ByteBuffer]
[java.util Base64]
[java.io InputStream ByteArrayOutputStream FileNotFoundException]))
(defn input-stream?
[v]
(instance? InputStream v))
(defn byte-buf?
[v]
(instance? ByteBuffer v))
(defn byte-buf->bytes
"Return all readable bytes from the given byte buffer as a byte array."
[^ByteBuffer b]
(let [bs (byte-array (.remaining b))]
(.get b bs)
bs))
(defn ->bytes
[v]
(cond
(input-stream? v) (let [b (ByteArrayOutputStream.)]
(io/copy v b)
(.close v)
(.toByteArray b))
(byte-buf? v) (byte-buf->bytes v)
(string? v) (.decode (Base64/getDecoder) ^String v)
(bytes? v) v
:else (throw (IllegalArgumentException. (str "can't coerce " (type v) " to bytes")))))
(defn error?
[v]
(not (s/invalid? (s/conform ::anomalies/anomaly v))))
(defn dynaload
[gaid fsym]
(try
(require (symbol (namespace fsym)))
(catch FileNotFoundException fnf
(throw (IllegalArgumentException.
(str "Unable to load client, make sure " gaid " is on your classpath")
fnf)))))
(defn dynarun
[gaid fsym arg-map]
(if-let [s (resolve fsym)]
(s arg-map)
(throw (IllegalArgumentException.
(str "Unable to resolve entry point, make sure you have the correct version of " gaid " on your classpath")))))
(defn dynacall
[gaid fsym arg-map]
(dynaload gaid fsym)
(dynarun gaid fsym arg-map))