Compare commits

...

2 Commits

Author SHA1 Message Date
Andrey Antukh
a4bd5801bf Add the ability to make json/->clj non-recursive 2026-01-22 10:04:54 +01:00
Andrey Antukh
b8a82e4416 Add the ability to customize decoder and schema on obj/reify 2026-01-22 10:04:48 +01:00
2 changed files with 126 additions and 23 deletions

View File

@@ -75,20 +75,25 @@
#?(:cljs
(defn ->clj
[o & {:keys [key-fn val-fn] :or {key-fn read-kebab-key val-fn identity}}]
[o & {:keys [key-fn val-fn recursive] :or {key-fn read-kebab-key val-fn identity recursive true}}]
(let [f (fn this-fn [x]
(let [x (val-fn x)]
(cond
(array? x)
(persistent!
(.reduce ^js/Array x
#(conj! %1 (this-fn %2))
#(conj! %1 (if recursive
(this-fn %2)
%2))
(transient [])))
(identical? (type x) js/Object)
(persistent!
(.reduce ^js/Array (js-keys x)
#(assoc! %1 (key-fn %2) (this-fn (unchecked-get x %2)))
#(assoc! %1 (key-fn %2)
(if recursive
(this-fn (unchecked-get x %2))
(unchecked-get x %2)))
(transient {})))
:else

View File

@@ -172,6 +172,22 @@
schema-1 (c/get params :schema-1)
this? (c/get params :this false)
decode-expr
(c/get params :decode/fn)
decode-options
(c/get params :decode/options)
decode-options
(if (and decode-expr decode-options)
(do
(println "WARN: decode/fn and decode/options are excluding, ignoring decode/options")
nil)
decode-options)
decode-expr
(or decode-expr 'app.common.json/->clj)
fn-sym
(-> (gensym (str "internal-fn-" (str/slug pname) "-"))
(with-meta {:tag 'function}))
@@ -180,9 +196,20 @@
(-> (gensym (str "coercer-fn-" (str/slug pname) "-"))
(with-meta {:tag 'function}))
decode-sym
(-> (gensym (str "decode-fn-" (str/slug pname) "-"))
(with-meta {:tag 'function}))
schema-sym
(-> (gensym (str "schema-" (str/slug pname) "-"))
(with-meta {:tag 'function}))
wrap-sym
(-> (gensym (str "wrap-fn-" (str/slug pname) "-"))
(with-meta {:tag 'function}))]
(with-meta {:tag 'function}))
val-sym
(gensym (str "val-" (str/slug pname) "-"))]
(concat
(when wrap
@@ -198,20 +225,66 @@
get-expr)])
(when set-expr
[(make-sym pname "set-fn")
(if this?
`(fn [v#]
(let [~this-sym (~'js* "this")
~fn-sym ~set-expr]
(.call ~fn-sym ~this-sym ~this-sym v#)))
set-expr)])
(concat
(when (and schema-n (not (list? schema-n)))
[coercer-sym `(sm/coercer ~schema-n)])
(when (and schema-n (list? schema-n))
[schema-sym schema-n])
[decode-sym decode-expr]
[(make-sym pname "set-fn")
(if this?
`(fn [~val-sym]
(let [~@(if (and schema-n (list? schema-n))
[schema-sym `(~schema-sym ~val-sym)
coercer-sym `(sm/coercer ~schema-sym)]
[])
~this-sym (~'js* "this")
~fn-sym ~set-expr
~val-sym ~(if schema-n
(if decode-expr
`(~decode-sym ~val-sym)
`(~decode-sym ~val-sym ~decode-options))
val-sym)
~val-sym ~(if schema-n
`(~coercer-sym ~val-sym)
val-sym)]
(.call ~fn-sym ~this-sym ~this-sym ~val-sym)))
`(fn [~val-sym]
(let [~@(if (and schema-n (list? schema-n))
[schema-sym `(~schema-sym ~val-sym)
coercer-sym `(sm/coercer ~schema-sym)]
[])
~this-sym (~'js* "this")
~fn-sym ~set-expr
~val-sym ~(if schema-n
(if decode-expr
`(~decode-sym ~val-sym)
`(~decode-sym ~val-sym ~decode-options))
val-sym)
~val-sym ~(if schema-n
`(~coercer-sym ~val-sym)
val-sym)]
(.call ~fn-sym ~this-sym ~val-sym))))]))
(when fn-expr
(concat
(when schema-1
[coercer-sym `(sm/coercer ~schema-1)])
(when schema-n
[coercer-sym `(sm/coercer ~schema-n)])
(cond
(and schema-n (not (list? schema-n)))
[coercer-sym `(sm/coercer ~schema-n)]
(and schema-n (list? schema-n))
[schema-sym schema-n]
(and schema-1 (not (list? schema-1)))
[coercer-sym `(sm/coercer ~schema-1)]
(and schema-1 (list? schema-1))
[schema-sym schema-1])
[decode-sym decode-expr]
[(make-sym pname "get-fn")
`(fn []
@@ -222,17 +295,42 @@
`(.bind ~fn-sym ~this-sym))
~@(if schema-1
[fn-sym `(fn* [param#]
(let [param# (json/->clj param#)
param# (~coercer-sym param#)]
(~fn-sym param#)))]
[fn-sym `(fn* [~val-sym]
(let [~val-sym
~(if decode-expr
`(~decode-sym ~val-sym)
`(~decode-sym ~val-sym ~decode-options))
~@(if (or (and schema-n (list? schema-n))
(and schema-1 (list? schema-1)))
[schema-sym `(~schema-sym ~val-sym)
coercer-sym `(sm/coercer ~schema-sym)]
[])
~val-sym
(~coercer-sym ~val-sym)]
(~fn-sym ~val-sym)))]
[])
~@(if schema-n
[fn-sym `(fn* []
(let [params# (into-array (cljs.core/js-arguments))
params# (mfu/bean params#)
params# (~coercer-sym params#)]
(apply ~fn-sym params#)))]
(let [~val-sym
(into-array (cljs.core/js-arguments))
~val-sym
~(if decode-expr
`(~decode-sym ~val-sym)
`(~decode-sym ~val-sym ~decode-options))
~@(if (or (and schema-n (list? schema-n))
(and schema-1 (list? schema-1)))
[schema-sym `(~schema-sym ~val-sym)
coercer-sym `(sm/coercer ~schema-sym)]
[])
~val-sym
(~coercer-sym ~val-sym)]
(apply ~fn-sym ~val-sym)))]
[])
~@(if wrap
[fn-sym `(~wrap-sym ~fn-sym)]