clojureamazonica

Idiomatic way to proxy named parameters in Clojure


I need a function that thinly wraps amazonica's sqs/receive-message in order to add a default wait time. The function requires a queue URL, and then accepts any number of optional named parameters, which should be passed along to sqs/receive-message untouched. I would like to call it like this:

(my-receive-message "https://sqs.us-east-1.amazonaws.com/123/test-q"
                    :max-number-of-messages 10
                    :delete true)

This should result in a call to sqs/receive-message like this:

(sqs/receive-message :queue-url "https://sqs.us-east-1.amazonaws.com/123/test-q"
                     :wait-time-seconds 20
                     :max-number-of-messages 10
                     :delete true)

This is something I find myself wanting to do fairly often, but I haven't found a nice way yet. Is there an idiomatic way to do this?


Solution

  • Use apply over the merged parameters.

    (defn my-receive-message
      [url & {:as args}]
      (apply sqs/receive-message (-> {:queue-url url
                                      :wait-time-seconds 20}
                                     (merge args)
                                     seq 
                                     flatten)))