Skip to content

Commit

Permalink
fix array types
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolap committed May 27, 2023
1 parent e62cb43 commit 9b5e1e1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/route_craft/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[route-craft.routes :as routes]
[route-craft.xray-ext :as xray-ext]))

(def base-malli-type-mappings
(def ^:private base-malli-type-mappings
{:integer :int
:bool :boolean
:text :string
Expand All @@ -32,6 +32,7 @@
;; handling defaults

(defn generate-reitit-crud-routes
""
[{:keys [table-definitions
role-parser-fn
malli-type-mappings
Expand Down
16 changes: 10 additions & 6 deletions src/route_craft/handlers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,23 @@

(defmulti generate-handler (fn [_opts handler] handler))

(defn malli-type
[malli-type-mappings {:keys [column-type] :rc/keys [array?]}]
(let [base-malli-type (get malli-type-mappings column-type)]
(if array?
[:vector base-malli-type]
base-malli-type)))

(defn id-type
[{:keys [malli-type-mappings table pk-key]}]
(->> (get-in table [:columns pk-key :column-type])
(get malli-type-mappings)))
(malli-type malli-type-mappings)))

(defn malli-column-key
([malli-type-mappings column-key column-meta]
(malli-column-key malli-type-mappings column-key column-meta {:force-optional? false}))
([malli-type-mappings column-key {:keys [column-type nullable? array?]} {:keys [force-optional?]}]
(let [base-malli-type (get malli-type-mappings column-type)
malli-type (if array?
[:vector base-malli-type]
base-malli-type)]
([malli-type-mappings column-key {:keys [column-type nullable?] :rc/keys [array?]} {:keys [force-optional?]}]
(let [malli-type (malli-type malli-type-mappings column-type)]
(cond
nullable?
[:maybe [column-key {:optional true} malli-type]]
Expand Down
21 changes: 13 additions & 8 deletions src/route_craft/xray_ext.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#{}
columns))

;; TODO: refactor, test
(defn extend-db-xray
[db-xray]
(update db-xray :tables
Expand All @@ -25,13 +26,17 @@
(assoc acc table-kw
(-> table
(assoc :rc/permitted-columns (column-names tables nil columns true))
(update :columns (fn [{:keys [column-type] :as column}]
(let [array? (boolean
(some-> column-type
(name)
(string/starts-with? "_")))]
(cond-> column
true (assoc :rc/array? array?)
array? (update :column-type #(keyword (string/replace (name %) #"^_" ""))))))))))
(update :columns (fn [columns]
(reduce-kv (fn [acc column-key {:keys [column-type] :as column}]
(assoc acc column-key
(let [array? (boolean
(some-> column-type
(name)
(string/starts-with? "_")))]
(cond-> column
true (assoc :rc/array? array?)
array? (update :column-type #(keyword (string/replace (name %) #"^_" "")))))))
{}
columns))))))
{}
tables))))

0 comments on commit 9b5e1e1

Please sign in to comment.