Skip to content

Instantly share code, notes, and snippets.

@humorless
Created August 29, 2023 17:02
Show Gist options
  • Save humorless/b6aa17db96ea1ee22a873bdc8074d4d6 to your computer and use it in GitHub Desktop.
Save humorless/b6aa17db96ea1ee22a873bdc8074d4d6 to your computer and use it in GitHub Desktop.
Example to show postwalk
(ns myproject
"FIXME: my new org.corfield.new/scratch project."
(:require [clojure.walk :as walk]))
(def tree
(list #:cil{:id 2
:children
(list #:citl{:id 2 :ip 2
:children
(list #:citl{:id 5 :tp 2}
#:citl{:id 6 :tp 2
:children (list #:citl{:id 7 :tp 6}
#:citl{:id 8 :tp 6})})}
#:citl{:id 3 :ip 2}
#:citl{:id 4 :ip 2})}))
(defn hiccup-tag
[ele]
(cond
(some? (:ci/reference-number ele)) :customer-invoice
(some? (:cil/id ele)) :customer-invoice-line
(some? (:citl/id ele)) :customer-invoice-tax-line
:else :x))
(defmulti hiccupize
(fn [x] (hiccup-tag x)))
(defmethod hiccupize :customer-invoice
[{:ci/keys [reference-number status]}]
[:customer-invoice
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto (str "Reference-number: " (or reference-number "- id -"))]
[:p.flex-auto (str "status: " status)]]])
(defmethod hiccupize :customer-invoice-line
[{:cil/keys [children id invoice tax-amount] :as cil}]
[:customer-invoice-line
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto (str "Id: " (or id "- id -"))]
[:p.flex-auto (str "Invoice: " (or invoice "- invoice -"))]
[:p.flex-auto (str "Amount: " tax-amount)]]
(when children
children)])
(defmethod hiccupize :customer-invoice-tax-line
[{:citl/keys [children id tax-amount] :as citl}]
[:customer-invoice-tax-line
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto (str "Id: " (or id "- id -"))]
[:p.flex-auto (str "Amount: " tax-amount)]]
(when children
children)])
(defmethod hiccupize :x
[e]
:x)
(defn f
[x]
;(prn (type x) x)
(cond
(map? x) (hiccupize x)
(seq? x) (map identity x)
:else x))
(walk/postwalk f tree)
([:customer-invoice-line
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto "Id: 2"]
[:p.flex-auto "Invoice: - invoice -"]
[:p.flex-auto "Amount: "]]
([:customer-invoice-tax-line
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto "Id: 2"]
[:p.flex-auto "Amount: "]]
([:customer-invoice-tax-line
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto "Id: 5"]
[:p.flex-auto "Amount: "]] nil]
[:customer-invoice-tax-line
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto "Id: 6"]
[:p.flex-auto "Amount: "]]
([:customer-invoice-tax-line
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto "Id: 7"]
[:p.flex-auto "Amount: "]] nil]
[:customer-invoice-tax-line
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto "Id: 8"]
[:p.flex-auto "Amount: "]] nil])])]
[:customer-invoice-tax-line
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto "Id: 3"]
[:p.flex-auto "Amount: "]] nil]
[:customer-invoice-tax-line
[:div.bg-violet-100.m-2.p-2.flex
[:p.flex-auto "Id: 4"]
[:p.flex-auto "Amount: "]] nil])])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment