0

我试图找到一种方法来建立一个参数传递给这个函数(它是托盘的一部分):

(defn node-spec [& {:keys [image hardware location network qos] :as options}]
  {:pre [(or (nil? image) (map? image))]}
  options)

有效的是这种用法:

(node-spec :location {:location-id "eu-west-1a"}, :image {:image-id "eu-west-1/ami-937474e7"} :network {})

但是 :location 和 :image 位对于我要配置的所有机器都是通用的,而 :network {} 位对于每个节点都是不同的。所以我想把共同点排除在外,然后做这样的事情:

(def my-common-location-and-image {:location {:location-id "eu-west-1a"}, :image {:image-id "eu-west-1/ami-937474e7"}} )
(node-spec (merge {:network {:security-groups [ "group1" ] }} my-common-location-and-image ))
(node-spec (merge {:network {:security-groups [ "group1" ] }} my-common-location-and-image ))

但这不起作用。合并的映射被解析为缺少其值的单个键。所以我尝试了

(node-spec :keys (merge {:network {:security-groups [ "group1" ] }} my-common-location-and-image ))

(node-spec :options (merge {:network {:security-groups [ "group1" ] }} my-common-location-and-image ))

但这也不起作用。我觉得我正试图扭转或超越节点规范参数中的解构。我究竟做错了什么?或者我的目标是分解出一些键/值对是不可能的?

4

1 回答 1

0

问题是该node-spec函数需要一个序列而不是一个映射。这是因为被解构的是一系列可以组合成键值对的事物。

所以,而不是通过这个:

{:image {:image-id "eu-west-1/ami-937474e7"}, :location {:location-id "eu-west-1a"}, :network {:security-groups ["group1"]}}

我们需要通过这个:

'(:image {:image-id "eu-west-1/ami-937474e7"} :location {:location-id "eu-west-1a"} :network {:security-groups ["group1"]})

这意味着这将起作用:

(apply node-spec
       (reduce concat
               (merge {:network {:security-groups ["group1"]}}
                      my-common-location-and-image)))
于 2013-02-22T18:14:43.423 回答