尝试在 Clojure 中导入库

问题描述 投票:0回答:1

我在 Clojure 中开发的代码有问题:

(ns problem2 (:require [cheshire.core :as json]
                       [clojure.spec.alpha :as s]
                       [invoice-spec]))

(defn json->map [file-name]
  (-> file-name
      slurp
      json/parse-stream
      :invoice))

(defn generate-invoice [file-name]
  (let [invoice (json->map file-name)
        customer {:name (:company_name (:customer invoice))
                  :email (:email (:customer invoice))}
        items (map (fn [item]
                     {:price (:price item)
                      :quantity (:quantity item)
                      :sku (:sku item)
                      :taxes (map (fn [tax]
                                    {:category (keyword (:tax_category tax))
                                     :rate (:tax_rate tax)})
                                  (:taxes item))})
                   (:items invoice))]
    {:issue-date (java.util.Date. (:issue_date invoice))
     :customer customer
     :items items}))

(defn -main []
  (let [invoice (generate-invoice "invoice.json")]
    (if (s/valid? ::invoice-spec/invoice invoice)
      (println "Invoice is valid")
      (println "Invoice is not valid"))))

当我尝试在此处导入外部库并使用 deps.edn 时:

{:deps {org.clojure/data.json {:mvn/version "2.4.0"}
        org.clojure/clojure {:mvn/version "1.10.3"}
        org.clojure/spec.alpha {:mvn/version "0.2.194"}
        cheshire {:mvn/version "5.10.0"}}
       }

总是出现同样的错误,就像这样:

Execution error (FileNotFoundException) at problem2/eval138$loading (problem2.clj:1).
Could not locate cheshire/core__init.class, cheshire/core.clj or cheshire/core.cljc on classpath.

我不知道我做错了什么,请帮忙。 博士。我需要使用 deps.edn 开发解决方案。谢谢

clojure libraries clojurescript clojure.spec
1个回答
0
投票

这是一个简单的开始方法:

~ > mkdir expr
~ > cd expr
~/expr > git clone https://github.com/io-tupelo/clj-template.git  demo-703
Cloning into 'demo-703'...
remote: Enumerating objects: 130, done.
remote: Counting objects: 100% (130/130), done.
remote: Compressing objects: 100% (72/72), done.
remote: Total 130 (delta 46), reused 110 (delta 26), pack-reused 0
Receiving objects: 100% (130/130), 217.82 KiB | 1.47 MiB/s, done.
Resolving deltas: 100% (46/46), done.

~/expr > cd demo-703
~/expr/demo-703 > clojure -X:test

Running tests in #{"test"}

Testing tst._bootstrap

-------------------------------------
   Clojure 1.12.0-beta1    Java 21
-------------------------------------

Testing tst.demo.core

Ran 2 tests containing 5 assertions.
0 failures, 0 errors.

这是一个使用

deps.edn
的简单模板项目,所有内容都位于正确的位置和格式。

然后我修改了

deps.edn
以包括:

 :deps    {
           org.clojure/clojure {:mvn/version "1.12.0-beta1"}
           prismatic/schema    {:mvn/version "1.4.1"}
           tupelo/tupelo       {:mvn/version "23.07.04"}
           cheshire/cheshire   {:mvn/version "5.10.0"}
           }

请注意,您需要

cheshire/cheshire
。 另外,我添加到单元测试中:

  (is= (json/encode {:a 1 :b [2 3]})
       "{\"a\":1,\"b\":[2,3]}")
  )
© www.soinside.com 2019 - 2024. All rights reserved.