Skip to content

Commit

Permalink
Fix insert transform only keys in first row (#138)
Browse files Browse the repository at this point in the history
* fix insert transform only keys that are in first row

* a better approach?

* adds a test description
  • Loading branch information
qnkhuat authored Mar 27, 2023
1 parent 651dd55 commit e254745
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/toucan2/tools/transformed.clj
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,15 @@

;;;; before insert

;;; TODO -- this shares a lot of code with [[transform-update-changes]]
(defn- transform-insert-rows [[first-row :as rows] k->transform]
{:pre [(map? first-row) (map? k->transform)]}
;; all rows should have the same keys, so we just need to look at the keys in the first row
(let [row-xforms (for [k (keys first-row)
:let [xform (get k->transform k)]
:when xform]
(fn [row]
(update row k (fn [v]
(if (some? v)
(xform v)
v)))))
row-xform (apply comp row-xforms)]
(map row-xform rows)))
(let [x-forms (for [[k transform] k->transform]
(fn [row]
(if (some? (get row k))
(update row k transform)
row)))
x-form (apply comp x-forms)]
(map x-form rows)))

(m/defmethod pipeline/build [#_query-type :toucan.query-type/insert.*
#_model ::transformed.model
Expand Down
17 changes: 17 additions & 0 deletions test/toucan2/tools/transformed_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,20 @@
(is (= 'transforms-primary-method-model-2
(generated-name `(transformed/deftransforms :model-2
{:x {:in ~'inc}})))))))

(derive ::venues.edn-category ::test/venues)

(transformed/deftransforms ::venues.edn-category
{:category {:in pr-str
:out (fn [s]
(binding [*read-eval* false]
(read-string s)))}})

(deftest ^:parallel transform-insert-rows-test
(testing "insert multiple rows in which each row has different key set will still do transformation properly (#130)"
(is (= [{:name "Venue 1"}
{:name "Venue 2", :category "{:name \"Category 2\"}"}]
(#'toucan2.tools.transformed/transform-insert-rows
'[{:name "Venue 1"}
{:name "Venue 2", :category {:name "Category 2"}}]
(#'toucan2.tools.transformed/in-transforms ::venues.edn-category))))))

0 comments on commit e254745

Please sign in to comment.