-
Notifications
You must be signed in to change notification settings - Fork 27
/
utils.clj
54 lines (49 loc) · 2.14 KB
/
utils.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
(ns keycloak.utils
(:require [clojure.string :as string]))
(defn ns-clean
"Remove all internal mappings from a given name space or the current one if no parameter given."
([] (ns-clean *ns*))
([ns] (map #(ns-unmap ns %) (keys (ns-interns ns)))))
(defmacro hint-typed-doto
"Evaluates x then calls all of the methods and functions with the
value of x supplied at the front of the given arguments. The forms
are evaluated in order. Returns x.
(doto (new java.util.HashMap) (.put \"a\" 1) (.put \"b\" 2))"
{:added "1.0"}
[type x & forms]
(let [gx (gensym)]
`(let [~gx ~(with-meta x {:tag type})]
~@(map (fn [f]
(with-meta
(if (seq? f)
`(~(first f) ~gx ~@(next f))
`(~f ~gx))
(meta f)))
forms)
~gx)))
(defmacro setters
"Given a compile-time literal map of attributes and values, return a function
that calls the corresponding setters on some java object."
[m type]
(when-not (map? m)
(throw (ex-info "m must be a literal map, not a symbol" {})))
(let [capitalize (fn [coll] (map string/capitalize coll))
camel-case (fn [kw] (-> (name kw) (string/split #"\W") capitalize string/join))
setter-sym (fn [kw] (->> (camel-case kw) (str ".set") symbol))
expanded (map (fn [[a val]]
(if (vector? val)
`( ~(setter-sym a) ~@val)
`( ~(setter-sym a) ~val)))
m)]
`(fn [obj#] (hint-typed-doto ~type obj# ~@expanded))))
(defn set-attributes
"call setAttributes(Map<String,String>) method on representation object with a clojure map"
[representation ^java.util.Map attributes]
(if (and attributes (not-empty (filter some? attributes)))
(doto representation (.setAttributes ^java.util.Map (java.util.HashMap. attributes)))
representation))
(defn auth-server-url
([infra-config]
(auth-server-url (get-in infra-config [:keycloak :protocol]) (get-in infra-config [:keycloak :host]) (get-in infra-config [:keycloak :port])))
([protocol host port]
(str protocol "://" host ":" port "/auth")))