Skip to content

Commit

Permalink
Fix JSON scheme keywords in generator arguments (#27)
Browse files Browse the repository at this point in the history
Closes GH #26.
  • Loading branch information
remvee authored Feb 11, 2020
1 parent 1e555f8 commit fc2c5b9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/nl/surf/demo_data.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

(defn load-config
[schema]
(config/load (json/parse-string (slurp schema) keyword)))
(-> schema (slurp) (config/load-json)))

(defn generate
[schema population & [out]]
Expand Down
23 changes: 22 additions & 1 deletion src/nl/surf/demo_data/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

(ns nl.surf.demo-data.config
(:refer-clojure :exclude [load])
(:require [clojure.string :as s]
(:require [cheshire.core :as json]
[clojure.string :as s]
[nl.surf.demo-data.constraints :as constraints]
[nl.surf.demo-data.date-util :as date-util]
[nl.surf.demo-data.generators :as gen]
Expand Down Expand Up @@ -131,6 +132,26 @@
(into (mapcat (partial load-ref name) refs))))
attrs)))

(letfn [(keywordize-keys [v depth]
(if (pos? depth)
(->> v
(map (fn [[k v]]
[(keyword k)
(if (map? v)
(keywordize-keys v (dec depth))
v)]))
(into {}))
v))]
(defn load-json
"Load configuration from JSON string and return attrs definition suitable for
`nl.surf.demo-data.world/gen`."
[data]
(-> data
(json/parse-string)
(keywordize-keys 1)
(update :types (fn [types] (mapv #(keywordize-keys % 3) types)))
(load))))

;;;;;;;;;;;;;;;;;;;;

(defmethod generator "constantly" [_]
Expand Down
15 changes: 14 additions & 1 deletion test/nl/surf/demo_data/config_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns nl.surf.demo-data.config-test
(:refer-clojure :exclude [load])
(:require [clojure.test :refer [deftest is testing]]
(:require [cheshire.core :as json]
[clojure.test :refer [deftest is testing]]
[nl.surf.demo-data.config :as sut]))

(deftest load
Expand Down Expand Up @@ -64,3 +65,15 @@
(is (fn? (-> attrs first :generator)))
(is (= #{"unique-refs" "that-other/other" "that-other/that"}
(->> attrs (map :generator) (map meta) (map :name) set)))))))

(deftest load-json
(testing "generator with object argument"
(let [attr (-> {:types [{:name "this"
:attributes {:att {:generator ["weighted" {"foo" 1, "bar" 2}]}}}]}
json/generate-string
sut/load-json
first)]
(is (= :this/att (:name attr)))
(is (-> attr :generator fn?))
(is (= "weighted" (-> attr :generator meta :name)))
(is (= [{"foo" 1, "bar" 2}] (-> attr :generator meta :arguments))))))

0 comments on commit fc2c5b9

Please sign in to comment.