1

我编写了一个函数,它只选择在特定模式中命名的映射键:

(ns foo.schema
  (:require [schema.core :as s]))

(defn select-schema
  "Given a schema and a map m, selects only the keys of m that are named in the schema."
  [m schema]
  (let [optional? #(instance? schema.core.OptionalKey %)
        wildcard? #(= s/Keyword %)]
    (if (some wildcard? (keys schema))
      m  ; the schema allows any keyword as a key, so just return the map
      (let [ks (->> schema keys (map #(if (optional? %) (:k %) %)))]
        (select-keys m ks)))))

这在我的单元测试中工作得很好:

(testing "Required key and wildcard"
    (let [schema {:foo s/Str, s/Keyword s/Any}]
      (is (= {:foo "Yup", :bar 42, :baz true} (select-schema {:foo "Yup", :bar 42, :baz true} schema)))
      (is (= {:foo "Yup", :bar 42} (select-schema {:foo "Yup", :bar 42} schema)))
      (is (= {:foo "Yup"} (select-schema {:foo "Yup"} schema)))))

但是,当我foo.schema/select-schema在一个完全独立的项目中使用(即lein install在我的 foo 项目中构建一个 jar 并将其粘贴到我的~/.m2/repository并将其命名为依赖项)时,我得到一个ClassNotFoundException

Exception in thread "main" java.lang.ExceptionInInitializerError, compiling:(insurrection/test/handler.clj:1:1)
    at clojure.lang.Compiler.load(Compiler.java:7142)
    ...
Caused by: java.lang.ExceptionInInitializerError
    at foo.schema__init.load(Unknown Source)
    at foo.schema__init.<clinit>(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    ...
Caused by: java.lang.ClassNotFoundException: schema.core.OptionalKey
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    ...

我查看了 Prismatic Sc​​hema 源代码,发现它OptionalKey是一个 defrecord 类型。一点谷歌搜索显示 defrecord 生成一个 Java 类,有时需要在需要定义它的命名空间后导入该类,但尝试在包含的项目中执行此foo.schema操作没有任何区别:它在单元测试中工作,但是在foo用作依赖项的其他项目中不起作用。

4

1 回答 1

-1

您导入schema.cores. 如果那是您正在运行的确切代码,那么您可能应该将其#(instance? s/OptionalKey %)用作您的optional?函数。

更不用说schemaselect-schema.

于 2014-10-15T13:32:07.340 回答