Remove magic ListOptionsContainer macros in favour of explicit fn mapping.

This commit is contained in:
phil 2010-03-26 01:09:21 +00:00
parent 91f09690b6
commit a87560113e
1 changed files with 13 additions and 33 deletions

View File

@ -34,8 +34,7 @@ See http://code.google.com/p/jclouds for details."
Options for communication style Options for communication style
:sync and :async. :sync and :async.
Options can also be specified for extension modules Options can also be specified for extension modules
:log4j :enterprise :httpnio :apachehc :bouncycastle :joda :gae :log4j :enterprise :httpnio :apachehc :bouncycastle :joda :gae"
"
[#^String service #^String account #^String key & options] [#^String service #^String account #^String key & options]
(let [context (let [context
(.createContext (.createContext
@ -77,48 +76,30 @@ Options can also be specified for extension modules
`(binding [*blobstore* (as-blobstore ~@blobstore-or-args)] `(binding [*blobstore* (as-blobstore ~@blobstore-or-args)]
~@body)) ~@body))
(defn- parse-args
"Parses arguments, recognises keywords in the set single as boolean switches."
[args single default]
(loop [[arg :as args] args
opts default]
(if-not args
opts
(if (single arg)
(recur (next args) (assoc opts arg true))
(recur (nnext args) (assoc opts arg (second args)))))))
(def list-options
(apply array-map
(concat (make-option-map option-fn-0arg [:recursive])
(make-option-map option-fn-1arg [:after-marker :in-directory
:max-results]))))
(defn- list-options-apply
[single target key value]
(if (single key)
((list-options key) target)
((list-options key) target value))
target)
(defn containers (defn containers
"List all containers in a blobstore." "List all containers in a blobstore."
([] (containers *blobstore*)) ([] (containers *blobstore*))
([blobstore] (.list blobstore))) ([blobstore] (.list blobstore)))
(def #^{:private true} list-option-map
{:after-marker #(.afterMarker %1 %2)
:in-directory #(.inDirectory %1 %2)
:max-results #(.maxResults %1 %2)
:recursive #(when %2 (.recursive %1))})
(defn list-container (defn list-container
"List a container. Options are: "List a container. Options are:
:after-marker string :after-marker string
:in-direcory path :in-direcory path
:max-results n :max-results n
:recursive" :recursive true"
[blobstore & args] [blobstore & args]
(if (blobstore? blobstore) (if (blobstore? blobstore)
(let [single-keywords #{:recursive} (let [options (apply hash-map args)
options (parse-args (next args) single-keywords {})
list-options (reduce list-options (reduce
#(list-options-apply single-keywords %1 (fn [lco [k v]]
(first %2) (second %2)) ((list-option-map k) lco v)
lco)
(ListContainerOptions.) (ListContainerOptions.)
options)] options)]
(.list blobstore (first args) list-options)) (.list blobstore (first args) list-options))
@ -228,8 +209,7 @@ example:
(pprint (pprint
(blobs (blobs
(blobstore-context flightcaster-creds) (blobstore-context flightcaster-creds)
\"somecontainer\" \"some-dir\")) \"somecontainer\" \"some-dir\"))"
"
([blobstore container-name] ([blobstore container-name]
(.list (as-blobstore blobstore) container-name)) (.list (as-blobstore blobstore) container-name))