celtuce icon indicating copy to clipboard operation
celtuce copied to clipboard

Accept RedisURI instead of just a string

Open vincentjames501 opened this issue 3 years ago • 1 comments

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))))

vincentjames501 avatar Aug 02 '21 21:08 vincentjames501

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 ?

lerouxrgd avatar Aug 07 '21 16:08 lerouxrgd