forked from metabase/metabase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[metabase-lib] Dev experience and CLJC infra improvements
Dev experience: - CLJS now has source maps - Devtools extension renders CLJS data in Chrome (et al) Devtools - Use a debug build of CLJS in the tests - Fixed line numbers and breakpoints being off-by-one in Devtools CLJC infra: - More robust direct approach to JS<->CLJS conversions, powered by Malli transformers. - Powerful `define-getters-and-setters` macro to generate the accessors.
- Loading branch information
1 parent
c25066c
commit 4580eab
Showing
19 changed files
with
606 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
(ns macros.metabase.domain-entities.malli) | ||
|
||
(defmacro define-getters-and-setters [schema & defs] | ||
`(do | ||
(comment ~schema) ;; Reference the schema | ||
~@(for [[sym _path] (partition 2 defs)] | ||
`(do | ||
(defn ~sym "docs" [_x#] nil) | ||
(defn ~(symbol (str "with-" (name sym))) "docs" [x# _new#] x#) | ||
(defn ~(symbol (str (name sym) "-js")) "docs" [x#] x#))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
import "@pmmmwh/react-refresh-webpack-plugin/client/ReactRefreshEntry.js"; | ||
import "cljs/metabase.util.devtools"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// A quirk of the way the tests are loaded causes a double-inclusion warning from Closure's goog.isProvided_ check. | ||
// It's actually harmless, just an overzealous dev-mode warning, so we monkey-patch goog.isProvided_ to return false. | ||
module.exports = function () { | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const cljs_env = path.join(__dirname, "..", "src", "cljs", "cljs_env.js"); | ||
const contents = fs.readFileSync(cljs_env, { encoding: "utf8" }); | ||
const replaced = contents.replace( | ||
new RegExp("goog.isProvided_ = function\\(name\\) {\\n.*?\\n\\s*};"), | ||
"goog.isProvided_ = function(name) { return false; };", | ||
); | ||
fs.writeFileSync(cljs_env, replaced); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,114 @@ | ||
(ns metabase.domain-entities.converters | ||
(:require | ||
[camel-snake-kebab.core :as csk] | ||
[cljs-bean.core :as bean] | ||
)) | ||
[malli.core :as mc] | ||
[malli.transform :as mtx])) | ||
|
||
(defn- ->entity | ||
"Conversion of incoming vanilla JS objects and arrays to CLJS maps and vectors. | ||
(defn- decode-map [schema _] | ||
(let [drop-nil? (set (for [[map-key props val-schema] (mc/children schema) | ||
:when (and (:optional props) | ||
(not (#{'nil? :nil} (mc/type val-schema))))] | ||
map-key))] | ||
{:enter (fn [x] | ||
(cond | ||
(map? x) x | ||
(object? x) (into {} (for [prop (js/Object.keys x) | ||
:let [js-val (unchecked-get x prop) | ||
map-key (csk/->kebab-case-keyword prop)] | ||
;; If the value is nil, and it's both :optional and not a :maybe, | ||
;; we just discard the value. | ||
:when (not (and (nil? js-val) | ||
(drop-nil? map-key)))] | ||
[map-key js-val])))) | ||
:leave (fn [x] | ||
(if (map? x) | ||
x | ||
(throw (ex-info "decode-map leaving with a JS object not a CLJS map" | ||
{:value x | ||
:schema schema}))))})) | ||
|
||
You should not need to call this directly; it's an implementation detail of [[defn]]." | ||
[x] | ||
(bean/->clj x | ||
:prop->key csk/->kebab-case-keyword | ||
:key->prop csk/->camelCaseString)) | ||
(defn- decode-map-of [keydec x] | ||
(cond | ||
(map? x) x | ||
(object? x) (into {} (for [prop (js/Object.keys x)] | ||
[(keydec prop) (unchecked-get x prop)])))) | ||
|
||
(defn- entity-> | ||
"Conversion of cljs-bean wrapped CLJS maps and vectors back into vanilla JS objects and arrays. | ||
(defn- encode-map [x keyenc] | ||
(cond | ||
(object? x) x | ||
(map? x) (reduce-kv (fn [obj k v] | ||
(unchecked-set obj (keyenc k) v) | ||
obj) | ||
#js {} | ||
x))) | ||
|
||
You should not need to call this directly; it's an implementation detail of [[defn]]." | ||
[x] | ||
(bean/->js x :key->prop csk/->camelCaseString)) | ||
(def js-transformer | ||
"Malli transformer for converting JavaScript data to and from CLJS data. | ||
(defn- opaque? [schema] | ||
(-> schema :properties :bean/opaque boolean)) | ||
This is a bit more flexible than a JSON transformer. In particular, it normalizes the keys of `:map` | ||
schema objects to `:kebab-case-keywords`, and restores them to strings with the original spelling when | ||
converting back. | ||
(defmulti incoming | ||
"Note that this works on the map syntax, the Malli AST, not the raw vectors. | ||
It's easier to process that way." | ||
(fn [schema] | ||
(if (opaque? schema) | ||
:bean/opaque | ||
(:type schema)))) | ||
**On keyword conversion** | ||
(defmethod incoming :default [_] | ||
identity) | ||
Note that `\"snake_case\"` is the default spelling we expect in the JS data. | ||
This can be overridden with the `{:js/prop \"correctSpelling\"}` property on the schema, eg. | ||
``` | ||
[:map | ||
[:camel-case {:js/prop \"camelCase\"} string?] | ||
[:kebab-case {:js/prop \"kebab-case\"} number?] | ||
[:snake-case [:enum \"foo\" \"bar\"]]] | ||
``` | ||
(defmethod incoming :bean/opaque [_] | ||
identity) | ||
Observe that `:snake-case` does not need a `:js/prop` setting, since that is the default. | ||
(defmethod incoming :vector [schema] | ||
(let [kf (incoming (:child schema))] | ||
#(mapv kf %))) | ||
**On `:map-of`** | ||
(defmethod incoming :tuple [schema] | ||
(let [inner (map incoming (:children schema))] | ||
(fn [value] | ||
(mapv #(%1 %2) inner value)))) | ||
Note that `:map-of` is not `:map`. The spelling of the keys in a `:map-of` is not changed. If the key | ||
schema is `keyword?`, they will be converted to keywords and back, but with the original spelling. | ||
(def ^:private conversion-needed? #{:tuple :vector :map :map-of}) | ||
**On sequences** | ||
`:tuple`, `:vector` and `:sequential` all get transformed into CLJS vectors. When converting back to JS, | ||
they are JS arrays." | ||
(mtx/transformer | ||
{:name :js | ||
:decoders {:keyword keyword | ||
'keyword? keyword | ||
:qualified-keyword keyword | ||
:vector {:enter vec} | ||
:sequential {:enter vec} | ||
:tuple {:enter vec} | ||
:map {:compile decode-map} | ||
:map-of {:compile (fn [schema _] | ||
(let [[key-schema] (mc/children schema) | ||
keydec (mc/decoder key-schema js-transformer)] | ||
{:enter #(decode-map-of keydec %)}))}} | ||
:encoders {:keyword name | ||
'keyword? name | ||
:qualified-keyword #(str (namespace %) "/" (name %)) | ||
:vector {:leave clj->js} | ||
:sequential {:leave clj->js} | ||
:tuple {:leave clj->js} | ||
:map {:compile | ||
(fn [schema _] | ||
(let [js-props (into {} (for [[k props] (mc/children schema) | ||
:when (:js/prop props)] | ||
[k (:js/prop props)])) | ||
keyenc (fn [k] (or (get js-props k) | ||
(csk/->snake_case_string k)))] | ||
{:leave #(encode-map % keyenc)}))} | ||
:map-of {:leave #(encode-map % name)}}})) | ||
|
||
(defmethod incoming :map-of [schema] | ||
(let [vf (incoming (:value schema)) | ||
keywordize? (-> schema :key :type (#{:keyword 'keyword?})) | ||
recursive? (-> schema :value :type conversion-needed?) | ||
transform (when (and (not (opaque? (:value schema))) | ||
(not= identity vf)) | ||
vf) | ||
opts (cond-> {:recursive (boolean recursive?) | ||
:keywordize-keys (boolean keywordize?)} | ||
keywordize? (assoc :prop->key csk/->kebab-case-keyword | ||
:key->prop csk/->camelCaseString) | ||
transform (assoc :transform transform))] | ||
#(bean/bean % opts))) | ||
(defn incoming | ||
"Returns a function for converting a JS value into CLJS data structures, based on a schema." | ||
[schema] | ||
;; TODO This should be a mc/coercer that decodes and then validates, throwing if it doesn't match. | ||
;; However, enabling that now breaks loads of tests that pass input data with lots of holes. The JS | ||
;; tests (as opposed to TS) are particularly bad for this. | ||
;; Don't forget the nested `mc/decoder` calls elsewhere in this file! | ||
(mc/decoder schema js-transformer)) | ||
|
||
(defmethod incoming :map [_schema] | ||
->entity) | ||
|
||
;; TODO Does this handle cases where we didn't convert string keys to keywords? Does it need to be schema-smart too? | ||
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} ;; This is used by macro-generated code. | ||
(defn outgoing | ||
"Converts back to pure JS form." | ||
[x] | ||
(entity-> x)) | ||
"Returns a function for converting a CLJS value back into a plain JS one, based on its schema." | ||
[schema] | ||
(mc/encoder schema js-transformer)) |
Oops, something went wrong.