dm3/clojure.java-time

defining existing conversion destroys the conversion graph as the function returns `nil` in `swap!`

Closed this issue · 1 comments

(defn conversion!
  ([from to f] (conversion! from to f 1))
  ([from-type-vec to-type-vec f cost]
   (let [from (g/types (to-seq from-type-vec))
         tos (combinations (to-seq to-type-vec) f cost)]
     (doseq [[f to cost] tos]
       (swap! graph
              (fn [g]
                (if-let [existing (g/get-conversion g from to)]
                  (when *fail-on-duplicate-conversion?*
                    (throw (ex-info (format "Conversion %s -> %s already exists: %s!" from to existing)
                                    {:from from, :to to, :existing existing})))
                  (let [f (wrap-validation from to f)]
                    (g/assoc-conversion g from to f cost)))))))))

should be:

(defn conversion!
  ([from to f] (conversion! from to f 1))
  ([from-type-vec to-type-vec f cost]
   (let [from (g/types (to-seq from-type-vec))
         tos (combinations (to-seq to-type-vec) f cost)]
     (doseq [[f to cost] tos]
       (swap! graph
              (fn [g]
                (if-let [existing (g/get-conversion g from to)]
                  (if *fail-on-duplicate-conversion?*
                    (throw (ex-info (format "Conversion %s -> %s already exists: %s!" from to existing)
                                    {:from from, :to to, :existing existing}))
                     g)
                  (let [f (wrap-validation from to f)]
                    (g/assoc-conversion g from to f cost)))))))))
dm3 commented

Yeah, this is a good fix, thanks!