From db2ba42b1483fc626bb0620c78fa0ffcc99fcdeb Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Thu, 13 Jun 2024 12:36:28 +0200 Subject: [PATCH] :bug: Add validator and repair for duplicated slots --- common/src/app/common/files/repair.cljc | 23 +++++++++++++++++++++++ common/src/app/common/files/validate.cljc | 15 +++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/common/src/app/common/files/repair.cljc b/common/src/app/common/files/repair.cljc index c40b60227..9e9fc29d4 100644 --- a/common/src/app/common/files/repair.cljc +++ b/common/src/app/common/files/repair.cljc @@ -473,6 +473,29 @@ (pcb/with-file-data file-data) (pcb/update-shapes [(:id shape)] repair-shape)))) +(defmethod repair-error :duplicate-slot + [_ {:keys [shape page-id] :as error} file-data _] + (let [page (ctpl/get-page file-data page-id) + childs (map #(get (:objects page) %) (:shapes shape)) + child-with-duplicate (let [result (reduce (fn [[seen duplicates] item] + (let [swap-slot (ctk/get-swap-slot item)] + (if (contains? seen swap-slot) + [seen (conj duplicates item)] + [(conj seen swap-slot) duplicates]))) + [#{} []] + childs)] + (second result)) + repair-shape + (fn [shape] + ;; Remove the swap slot + (log/debug :hint " -> remove swap-slot" :child-id (:id shape)) + (ctk/remove-swap-slot shape))] + + (log/dbg :hint "repairing shape :duplicated-slot" :id (:id shape) :name (:name shape) :page-id page-id) + (-> (pcb/empty-changes nil page-id) + (pcb/with-file-data file-data) + (pcb/update-shapes (map :id child-with-duplicate) repair-shape)))) + (defmethod repair-error :missing-slot [_ {:keys [shape page-id args] :as error} file-data _] (let [repair-shape diff --git a/common/src/app/common/files/validate.cljc b/common/src/app/common/files/validate.cljc index 7caceed49..cd4f3b43a 100644 --- a/common/src/app/common/files/validate.cljc +++ b/common/src/app/common/files/validate.cljc @@ -34,6 +34,7 @@ :component-not-main :component-main-external :component-not-found + :duplicate-slot :invalid-main-instance-id :invalid-main-instance-page :invalid-main-instance @@ -296,6 +297,18 @@ "This shape should not have swap slot" shape file page))) +(defn- check-duplicate-swap-slot + "Validate that the children of this shape does not have duplicated slots." + [shape file page] + (let [shapes (map #(get (:objects page) %) (:shapes shape)) + slots (->> (map #(ctk/get-swap-slot %) shapes) + (remove nil?)) + counts (frequencies slots)] + (when (some (fn [[_ count]] (> count 1)) counts) + (report-error :duplicate-slot + "This shape has children with the same swap slot" + shape file page)))) + (defn- check-shape-main-root-top "Root shape of a top main instance: @@ -308,6 +321,7 @@ (check-component-root shape file page) (check-component-not-ref shape file page) (check-empty-swap-slot shape file page) + (check-duplicate-swap-slot shape file page) (run! #(check-shape % file page libraries :context :main-top) (:shapes shape))) (defn- check-shape-main-root-nested @@ -335,6 +349,7 @@ (check-component-root shape file page) (check-component-ref shape file page libraries) (check-empty-swap-slot shape file page) + (check-duplicate-swap-slot shape file page) (run! #(check-shape % file page libraries :context :copy-top :library-exists library-exists) (:shapes shape)))) (defn- check-shape-copy-root-nested