Skip to content

Instantly share code, notes, and snippets.

@camsaul
Created February 21, 2024 18:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camsaul/2e919d539523d3a0dfc335b4ad66a80d to your computer and use it in GitHub Desktop.
Save camsaul/2e919d539523d3a0dfc335b4ad66a80d to your computer and use it in GitHub Desktop.
Dataset Hating Map
;;; it's a map that hates if you try to get the key `:dataset` from it. NOCOMMIT
(declare ->DatasetHatingMap)
(defn- check-for-dataset-key [k]
(when (= k :dataset)
(throw (ex-info ":dataset is deprecated, use :type instead!" {}))))
(p/def-map-type DatasetHatingMap [m]
(get [_this k default-value]
(check-for-dataset-key k)
(get m k default-value))
(assoc [this k v]
(check-for-dataset-key k)
(let [m' (assoc m k v)]
(if (identical? m m')
this
(->DatasetHatingMap m'))))
(dissoc [this k]
(check-for-dataset-key k)
(let [m' (dissoc m k)]
(if (identical? m m')
this
(->DatasetHatingMap m'))))
(keys [_this]
(keys m))
(meta [_this]
(meta m))
(with-meta [this metta]
(let [m' (with-meta m metta)]
(if (identical? m m')
this
(->DatasetHatingMap m'))))
pretty/PrettyPrintable
(pretty [_this]
(list `->DatasetHatingMap m)))
(t2/define-after-select :model/Card
[card]
(let [card (public-settings/remove-public-uuid-if-public-sharing-is-disabled card)
m* (into (DatasetHatingMap. {}) card)]
(t2.instance/->Instance (t2/model card) m* m* (meta card))))
@camsaul
Copy link
Author

camsaul commented Feb 21, 2024

the last line is a workaround for camsaul/toucan2#164, you would think you could do

(t2/instance :model/Card card)

or whatever and it would preserve card's map type, but it doesn't

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment