Skip to content

Instantly share code, notes, and snippets.

@swannodette
Forked from myguidingstar-zz/macro.cljx
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swannodette/332c574cb4d9df41aa35 to your computer and use it in GitHub Desktop.
Save swannodette/332c574cb4d9df41aa35 to your computer and use it in GitHub Desktop.
(ns strange.macros
#+cljs (:require-macros [cemerick.cljs.test :refer [is deftest]]
[strange.macros :refer [my-response edn]]))
;; I want an abstraction over a javascript library. These macros are simplified versions
#+clj
(defmacro my-response
[x]
(let [{:keys [status headers body]} (macroexpand x)]
`(str ~status ~body)))
#+clj
(defmacro edn
;; ([body] `(edn 200 ~body))
([status body]
{:status status
:headers {"Content-Type" "application/edn"}
:body `(pr-str ~body)}))
#+cljs
(deftest dummy-macro-tests
;; I use (+ [] {}) just to make sure the code is evaluated in javascript
(is (= (my-response (edn [] {})) "[]{}")))
;; This is what I do with the real my-response
(comment
;; inside a request handler
(my-response response-object {:status 200 :headers {"Content-type" "..."} {:hello "world"}})
(my-response response-object (edn {:hello "world"}))
)
@myguidingstar-zz
Copy link

It turns out that cljsbuild sometimes compiles before cljx completes which result the output javascript is a mix of old and new code. This macro itself is guiltless :)

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