Sunday, 15 August 2010

prismatic schema - ClassNotFoundException when using instance? on a defrecord type in Clojure -



prismatic schema - ClassNotFoundException when using instance? on a defrecord type in Clojure -

i've written function selects keys of map named in specific schema:

(ns foo.schema (:require [schema.core :as s])) (defn select-schema "given schema , map m, selects keys of m named in schema." [m schema] (let [optional? #(instance? schema.core.optionalkey %) wildcard? #(= s/keyword %)] (if (some wildcard? (keys schema)) m ; schema allows keyword key, homecoming map (let [ks (->> schema keys (map #(if (optional? %) (:k %) %)))] (select-keys m ks)))))

this works fine in unit tests:

(testing "required key , 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)))))

however, when utilize foo.schema/select-schema in separate project (i.e. lein install in foo project build jar , stick in ~/.m2/repository , name dependency), classnotfoundexception:

exception in thread "main" java.lang.exceptionininitializererror, compiling:(insurrection/test/handler.clj:1:1) @ clojure.lang.compiler.load(compiler.java:7142) ... caused by: java.lang.exceptionininitializererror @ foo.schema__init.load(unknown source) @ foo.schema__init.<clinit>(unknown source) @ java.lang.class.forname0(native method) ... caused by: java.lang.classnotfoundexception: schema.core.optionalkey @ java.net.urlclassloader$1.run(urlclassloader.java:366) ...

i looked @ prismatic schema source , found optionalkey defrecord type. little googling revealed defrecord generates java class, needs imported after requiring namespace defines it, trying in project contains foo.schema doesn't create difference: works in unit tests, doesn't work in other project uses foo dependency.

you imported schema.core s. if that's exact code you're running, should using #(instance? s/optionalkey %) optional? function.

not mention schema local variable (argument) in select-schema.

clojure prismatic-schema

No comments:

Post a Comment