首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Clojure specter将导航值与其他地图合并

Clojure specter将导航值与其他地图合并
EN

Stack Overflow用户
提问于 2018-04-18 20:34:37
回答 2查看 164关注 0票数 1

我尝试使用Clojure specter来编辑“数据库”中的订单信息。此应用程序基于Rest API。

我的API调用:

代码语言:javascript
复制
(PUT "/orders" []
         :return :specs.models.order/orderSpec
         :body-params [orderid :- :specs.models.order/orderid
                       {amount :- :specs.models.order/amount (orderController/getAmount orderid)}
                       {description :- :specs.models.order/description (orderController/getDescription orderid)}
                       {productid :- :specs.models.order/productid (orderController/getProductid orderid)}]
         :summary "Edits the description and/or amount and/or productid of an order"
         (getResponseFromContent (orderController/editOrder orderid amount description productid))
         )

这是我的“数据库”:

代码语言:javascript
复制
      [{:orderid 0 :productid 0 :description "A" :amount 2 :state "active"}
        {:orderid 1 :productid 1 :description "A" :amount 2 :state "active"}]

它在不同的文件中,我用它在model中调用它

代码语言:javascript
复制
(def filename "resources/mockorderDatabase.dat")
(def database (atom (try
                      (clojure.edn/read-string (slurp filename))
                      (catch Exception e []))))

这是我的控制器:

代码语言:javascript
复制
(defn editOrder
  "Edit order's description and/or amount and/or productid by orderid"
  [orderid amount description productid]
  (if-let [editedorder (order/edit-order db/config {:orderid orderid :amount amount :description description :productid productid})]
    (first editedorder)
    nil))

我被edit-order卡住了。我该如何让它工作呢?

到目前为止,我想出了edit-order

代码语言:javascript
复制
(defn edit-order                                           
  [db orderid amount description productid]
  (->> (transform [ALL (comp (partial = orderid) :orderid)]
                  #(assoc % :productid productid :amount amount :description description)
                  @database
       )
   )
  )

我得到的回应是:

代码语言:javascript
复制
{
  "type": "unknown-exception",
  "class": "java.lang.IllegalArgumentException"
}

如果我使用setval而不是transfrom会更好

感谢您的帮助!

EN

回答 2

Stack Overflow用户

发布于 2018-04-18 21:31:18

你真的不需要Specter来做这件事:

代码语言:javascript
复制
(def data
  [{:orderid 0 :productid 0 :description "A" :amount 2 :state "active"}
   {:orderid 1 :productid 1 :description "A" :amount 2 :state "active"}])

(defn update-by-orderid [orders orderid description amount productid]
  (vec
    (for [order orders]
      (if (= orderid (:orderid order))
        (assoc order          ; change if match
          :description description
          :amount amount
          :productid productid)
        order))))                 ; return unchanged if no match


(update-by-orderid data 0 "DESC" 99 123) =>
[{:orderid 0,
  :productid 123,
  :description "DESC",
  :amount 99,
  :state "active"}
 {:orderid 1,
  :productid 1,
  :description "A",
  :amount 2,
  :state "active"}]
票数 1
EN

Stack Overflow用户

发布于 2018-04-19 00:51:36

代码语言:javascript
复制
(def filename "mockorderDatabase.dat")
(def database (atom (try
                        (clojure.edn/read-string (slurp filename))
                        (catch Exception e []))))

(require '[com.rpl.specter :refer :all])

(defn change-order [*database id productid amount desc]
    (transform [ATOM ALL (comp (partial = id) :orderid)]
               #(assoc % :productid productid :amount amount :description desc)
               *database))

(prn (change-order database 0 111 222 "XXXX-YYYY"))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49899856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档