celtuce
celtuce copied to clipboard
Accept RedisURI instead of just a string
We want to be able to supply a RedisURI object instead of just a string as it works a little bit better for us and how we construct those objects. I'll post this here too if it helps anyone who wants to build a RedisURI from a clj map:
(defn ^RedisURI build-redis-uri
[{:keys [uri
host
port
username
password
ssl?
verify-peer?
start-tls?
database
client-name
timeout-ms]}]
(if uri
(RedisURI/create ^String uri)
(let [^RedisURI$Builder builder (RedisURI/builder)]
(.withHost builder host)
(when port
(.withPort builder port))
(when (and (not username) password)
(.withPassword builder (.toCharArray ^String password)))
(when (and username password)
(.withAuthentication builder ^String username (.toCharArray ^String password)))
(when (boolean? ssl?)
(.withSsl builder ^boolean ssl?))
(when (boolean? verify-peer?)
(.withVerifyPeer builder ^boolean verify-peer?))
(when (boolean? start-tls?)
(.withStartTls builder start-tls?))
(when database
(.withDatabase builder database))
(when client-name
(.withClientName builder client-name))
(when timeout-ms
(.withTimeout builder (Duration/ofMillis timeout-ms)))
(.build builder))))
Thanks it looks good.
However I think the if
are getting a bit too nested, could you refactor it with a cond
and and
conditions ?