Skip to content

Commit

Permalink
propagate data about original var names/values to get better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin committed Oct 16, 2016
1 parent 6894a82 commit 0c5cc03
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
18 changes: 10 additions & 8 deletions src/lib/env_config/impl/coerce.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns env-config.impl.coerce
(:require [clojure.string :as string]
[env-config.impl.report :as report]
[env-config.impl.helpers :refer [dissoc-in]]))
[env-config.impl.helpers :refer [dissoc-in make-var-description]]))

(deftype Coerced [val])

Expand Down Expand Up @@ -41,9 +41,9 @@
(try
(->Coerced (read-string code)) ; TODO: should we rather use edn/read-string here?
(catch Throwable e
(report/report-warning! (str "unable to read-string for config path " (pr-str path) ", "
"invalid code: '" code "', "
"problem: " (.getMessage e) "."))
(report/report-warning! (str "unable to read-string from " (make-var-description (meta path)) ", "
"attempted to eval code: '" code "', "
"got problem: " (.getMessage e) "."))
:omit)))))

; -- default coercers -------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -96,7 +96,8 @@
new-state (reduce-kv (:reducer state) (push-key state key) val)]
(restore-keys new-state current-keys))
(let [path (conj (:keys state) key)
coerced-val (apply-coercers coercers path val)]
metadata (get-in (:metadata state) path)
coerced-val (apply-coercers coercers (with-meta path metadata) val)]
(if (= ::omit coerced-val)
(update state :config dissoc-in path)
(update state :config assoc-in path coerced-val)))))
Expand All @@ -105,9 +106,10 @@

(defn naked-coerce-config [config coercers]
(let [reducer (partial coercer-worker coercers)
init {:keys []
:reducer reducer
:config {}}]
init {:keys []
:reducer reducer
:metadata (meta config)
:config {}}]
(:config (reduce-kv reducer init config))))

(defn coerce-config [config coercers]
Expand Down
15 changes: 8 additions & 7 deletions src/lib/env_config/impl/read.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@
(assoc m k (overwriting-assoc-in (get-maps-only m k) ks v))
(assoc m k v)))

(defn assoc-config-value [m name value]
(let [segments (get-name-segments name)
ks (map name-to-keyword segments)]
(overwriting-assoc-in m ks value)))

; -- reducers ---------------------------------------------------------------------------------------------------------------

(defn filterer-for-matching-vars [prefix v var-name var-value]
Expand All @@ -50,12 +45,18 @@
(if (matching-var-name? prefix prefix+name)
(let [value (normalize-value var-value)
name (strip-prefix prefix prefix+name)]
(conj v [name value]))))
(conj v (with-meta [name value] {:var-name var-name :var-value var-value})))))
v))

(defn config-builder
([] {})
([m [name value]] (assoc-config-value m name value)))
([config item]
(let [[name value] item]
(let [segments (get-name-segments name)
ks (map name-to-keyword segments)
new-config (overwriting-assoc-in config ks value)
new-metadata (overwriting-assoc-in (meta config) ks (meta item))]
(with-meta new-config new-metadata)))))

; -- reader -----------------------------------------------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion test/src/tests/env_config/tests/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@
(testing "make-config-with-logging"
(let [reports-atom (atom [])]
(make-config-with-logging "p" {"p/c" "~#!@#xxx"} default-coercers (fn [reports] (reset! reports-atom reports)))
(is (= [[:warn "env-config: unable to read-string for config path [:c], invalid code: '#!@#xxx', problem: EOF while reading."]] @reports-atom)))))
(is (= [[:warn "env-config: unable to read-string from variable 'p/c' with value \"~#!@#xxx\", attempted to eval code: '#!@#xxx', got problem: EOF while reading."]] @reports-atom)))))

0 comments on commit 0c5cc03

Please sign in to comment.