-
Notifications
You must be signed in to change notification settings - Fork 69
/
impl.clj
69 lines (60 loc) · 2.46 KB
/
impl.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
60
61
62
63
64
65
66
67
68
69
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns clojure.data.xml.impl
"Shared private code for data.xml namespaces"
{:author "Herwig Hochleitner"}
(:import
[java.util Base64]
[java.nio.charset StandardCharsets]))
(defn- var-form? [form]
(and (seq? form) (= 'var (first form))))
(defn- export-form [var-name]
(let [is-var (var-form? var-name)
vsym (symbol (name (if is-var (second var-name) var-name)))]
`[(def ~vsym ~var-name)
(alter-meta! (var ~vsym)
(constantly (assoc (meta ~(if is-var
var-name
`(var ~var-name)))
:wrapped-by (var ~vsym))))]))
(defmacro export-api
"This creates vars, that take their (local) name, value and metadata from another var"
[& names]
(cons 'do (mapcat export-form names)))
(defmacro static-case
"Variant of case where keys are evaluated at compile-time"
[val & cases]
`(case ~val
~@(mapcat (fn [[field thunk]]
[(eval field) thunk])
(partition 2 cases))
~@(when (odd? (count cases))
[(last cases)])))
(defmacro extend-protocol-fns
"Helper to many types to a protocol with a method map, similar to extend"
[proto & types+mmaps]
(assert (zero? (mod (count types+mmaps) 2)))
(let [gen-extend (fn [type mmap] (list `extend type proto mmap))]
`(do ~@(for [[type mmap] (partition 2 types+mmaps)]
(if (coll? type)
(let [mm (gensym "mm-")]
`(let [~mm ~mmap]
~@(map gen-extend type (repeat mm))))
(gen-extend type mmap))))))
(defmacro compile-if
"Evaluate `exp` and if it returns logical true and doesn't error, expand to
`then`. Else expand to `else`.
see clojure.core.reducers"
[exp then else]
(if (try (eval exp)
(catch Throwable _ false))
`(do ~then)
`(do ~else)))
(defn b64-encode [^bytes ba]
(let [encoder (Base64/getEncoder)]
(String. (.encode encoder ba) StandardCharsets/ISO_8859_1)))