Skip to content

Instantly share code, notes, and snippets.

@fmw fmw/gist:c1d38d2107ec75239d67 Secret
Created Oct 31, 2011

Embed
What would you like to do?
clojureScript macro problem
; macro working in Clojure:
(defmacro routes [uri-path & clauses]
(when-let [[route-segments body & clauses] clauses]
`(if-let [~'params (route-matches ~route-segments (str ~@uri-path))]
~body
(routes ~uri-path ~@clauses))))
; passing testcase:
(deftest test-routes
(is (= (routes "/admin/blog/edit"
["admin" "foo"] :unmatched
["admin" :feed "edit"] (identity (:feed params))
["admin" "bar"] :unmatched)
"blog"))
(is (= (routes "/admin/blog/edit/foo"
["admin" "foo"] :unmatched
["admin" :feed "edit"] (identity (:feed params))
["admin" "bar"] :unmatched
:fallback :fallen-back)
:fallen-back)))
; The modified macro in ClojureScript (the original macro expects a route-matches fn local to the Clojure macro module,
; while this one uses one local to the ClojureScript code):
(defmacro routes [uri-path & clauses]
(when-let [[route-segments body & clauses] clauses]
`(if-let [~'params ~'(route-matches route-segments uri-path)]
~body
(routes ~uri-path ~@clauses))))
; The ClojureScript code calling the macro:
(defn routes [uri-path]
(macros/routes uri-path
["admin"]
(feed/list-feeds)
["admin" "new-feed"]
(feed/display-new-feed-form)
["admin" "edit-feed" :language :feed-name]
(feed/display-edit-feed-form (:language params)
(:feed-name params))
["admin" :language :feed-name "new"]
(editor/start (:language params) (:feed-name params) :new)
["admin" :language :feed-name "edit" :*]
(editor/start (:language params)
(:feed-name params)
(:* params))
["admin" :language :feed-name "overview"]
(feed-list-documents (:language params) (:feed-name params))
:fallback
(navigate-replace-state "" "Vix overview")))
; Javascript output output:
vix.core.routes = (function routes(uri_path){
var temp__3695__auto____2390 = vix.core.route_matches.call(null,vix.core.route_segments,uri_path);
if(cljs.core.truth_(temp__3695__auto____2390))
{var params__2391 = temp__3695__auto____2390;
return vix.views.feed.list_feeds.call(null);
} else
{var temp__3695__auto____2392 = vix.core.route_matches.call(null,vix.core.route_segments,uri_path);
if(cljs.core.truth_(temp__3695__auto____2392))
{var params__2393 = temp__3695__auto____2392;
return vix.views.feed.display_new_feed_form.call(null);
} else
{var temp__3695__auto____2394 = vix.core.route_matches.call(null,vix.core.route_segments,uri_path);
if(cljs.core.truth_(temp__3695__auto____2394))
{var params__2395 = temp__3695__auto____2394;
return vix.views.feed.display_edit_feed_form.call(null," 'language".call(null,params__2395)," 'feed-name".call(null,params__2395));
} else
{var temp__3695__auto____2396 = vix.core.route_matches.call(null,vix.core.route_segments,uri_path);
if(cljs.core.truth_(temp__3695__auto____2396))
{var params__2397 = temp__3695__auto____2396;
return vix.views.editor.start.call(null," 'language".call(null,params__2397)," 'feed-name".call(null,params__2397," 'new"));
} else
{var temp__3695__auto____2398 = vix.core.route_matches.call(null,vix.core.route_segments,uri_path);
if(cljs.core.truth_(temp__3695__auto____2398))
{var params__2399 = temp__3695__auto____2398;
return vix.views.editor.start.call(null," 'language".call(null,params__2399)," 'feed-name".call(null,params__2399)," '*".call(null,params__2399));
} else
{var temp__3695__auto____2400 = vix.core.route_matches.call(null,vix.core.route_segments,uri_path);
if(cljs.core.truth_(temp__3695__auto____2400))
{var params__2401 = temp__3695__auto____2400;
return vix.core.feed_list_documents.call(null," 'language".call(null,params__2401)," 'feed-name".call(null,params__2401));
} else
{var temp__3695__auto____2402 = vix.core.route_matches.call(null,vix.core.route_segments,uri_path);
if(cljs.core.truth_(temp__3695__auto____2402))
{var params__2403 = temp__3695__auto____2402;
return vix.core.navigate_replace_state.call(null,"","Vix overview");
} else
{return null;
}
}
}
}
}
}
}
});
; modified macro, that at at least creates a local let for the route-segments:
; (the double when let is because the initial one is also required at compile time):
(defmacro routes [uri-path & clauses]
(when-let [[route-segments body & clauses] clauses]
`(when-let [[route-segments# body# & clauses#] ~clauses]
(if-let [~'params ~'(route-matches route-segments# uri-path)]
~body
(routes ~uri-path ~@clauses)))))
; the route-segments is still not what I want it to be
; (e.g. vix.core.route_segments_SHARP_ instead of route_segments__2353__auto____2396)
vix.core.routes = (function routes(uri_path){
var temp__3698__autoroute_segments__2353__auto____2396____2393 = cljs.core.Vector.fromArray(["admin","new-feed"]).call(null,vix.views.feed.display_new_feed_form.call(null),cljs.core.Vector.fromArray(["admin","edit-feed"," 'language"," 'feed-name"]),vix.views.feed.display_edit_feed_form.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params)),cljs.core.Vector.fromArray(["admin"," 'language"," 'feed-name","new"]),vix.views.editor.start.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params," 'new")),cljs.core.Vector.fromArray(["admin"," 'language"," 'feed-name","edit"," '*"]),vix.views.editor.start.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params)," '*".call(null,vix.core.params)),cljs.core.Vector.fromArray(["admin"," 'language"," 'feed-name","overview"]),vix.core.feed_list_documents.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params))," 'fallback",vix.core.navigate_replace_state.call(null,"","Vix overview"));
if(cljs.core.truth_(temp__3698__auto____2393))
{var vec__2394__2395 = temp__3698__auto____2393;
var route_segments__2353__auto____2396 = cljs.core.nth.call(null,vec__2394__2395,0,null);
var body__2354__auto____2397 = cljs.core.nth.call(null,vec__2394__2395,1,null);
var clauses__2355__auto____2398 = cljs.core.nthnext.call(null,vec__2394__2395,2);
var temp__3695__auto____2399 = vix.core.route_matches.call(null,vix.core.route_segments_SHARP_,uri_path);
if(cljs.core.truth_(temp__3695__auto____2399))
{var params__2400 = temp__3695__auto____2399;
return vix.views.feed.list_feeds.call(null);
} else
{var temp__3698__auto____2401 = cljs.core.Vector.fromArray(["admin","edit-feed"," 'language"," 'feed-name"]).call(null,vix.views.feed.display_edit_feed_form.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params)),cljs.core.Vector.fromArray(["admin"," 'language"," 'feed-name","new"]),vix.views.editor.start.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params," 'new")),cljs.core.Vector.fromArray(["admin"," 'language"," 'feed-name","edit"," '*"]),vix.views.editor.start.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params)," '*".call(null,vix.core.params)),cljs.core.Vector.fromArray(["admin"," 'language"," 'feed-name","overview"]),vix.core.feed_list_documents.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params))," 'fallback",vix.core.navigate_replace_state.call(null,"","Vix overview"));
if(cljs.core.truth_(temp__3698__auto____2401))
{var vec__2402__2403 = temp__3698__auto____2401;
var route_segments__2353__auto____2404 = cljs.core.nth.call(null,vec__2402__2403,0,null);
var body__2354__auto____2405 = cljs.core.nth.call(null,vec__2402__2403,1,null);
var clauses__2355__auto____2406 = cljs.core.nthnext.call(null,vec__2402__2403,2);
var temp__3695__auto____2407 = vix.core.route_matches.call(null,vix.core.route_segments_SHARP_,uri_path);
if(cljs.core.truth_(temp__3695__auto____2407))
{var params__2408 = temp__3695__auto____2407;
return vix.views.feed.display_new_feed_form.call(null);
} else
{var temp__3698__auto____2409 = cljs.core.Vector.fromArray(["admin"," 'language"," 'feed-name","new"]).call(null,vix.views.editor.start.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params," 'new")),cljs.core.Vector.fromArray(["admin"," 'language"," 'feed-name","edit"," '*"]),vix.views.editor.start.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params)," '*".call(null,vix.core.params)),cljs.core.Vector.fromArray(["admin"," 'language"," 'feed-name","overview"]),vix.core.feed_list_documents.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params))," 'fallback",vix.core.navigate_replace_state.call(null,"","Vix overview"));
if(cljs.core.truth_(temp__3698__auto____2409))
{var vec__2410__2411 = temp__3698__auto____2409;
var route_segments__2353__auto____2412 = cljs.core.nth.call(null,vec__2410__2411,0,null);
var body__2354__auto____2413 = cljs.core.nth.call(null,vec__2410__2411,1,null);
var clauses__2355__auto____2414 = cljs.core.nthnext.call(null,vec__2410__2411,2);
var temp__3695__auto____2415 = vix.core.route_matches.call(null,vix.core.route_segments_SHARP_,uri_path);
if(cljs.core.truth_(temp__3695__auto____2415))
{var params__2416 = temp__3695__auto____2415;
return vix.views.feed.display_edit_feed_form.call(null," 'language".call(null,params__2416)," 'feed-name".call(null,params__2416));
} else
{var temp__3698__auto____2417 = cljs.core.Vector.fromArray(["admin"," 'language"," 'feed-name","edit"," '*"]).call(null,vix.views.editor.start.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params)," '*".call(null,vix.core.params)),cljs.core.Vector.fromArray(["admin"," 'language"," 'feed-name","overview"]),vix.core.feed_list_documents.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params))," 'fallback",vix.core.navigate_replace_state.call(null,"","Vix overview"));
if(cljs.core.truth_(temp__3698__auto____2417))
{var vec__2418__2419 = temp__3698__auto____2417;
var route_segments__2353__auto____2420 = cljs.core.nth.call(null,vec__2418__2419,0,null);
var body__2354__auto____2421 = cljs.core.nth.call(null,vec__2418__2419,1,null);
var clauses__2355__auto____2422 = cljs.core.nthnext.call(null,vec__2418__2419,2);
var temp__3695__auto____2423 = vix.core.route_matches.call(null,vix.core.route_segments_SHARP_,uri_path);
if(cljs.core.truth_(temp__3695__auto____2423))
{var params__2424 = temp__3695__auto____2423;
return vix.views.editor.start.call(null," 'language".call(null,params__2424)," 'feed-name".call(null,params__2424," 'new"));
} else
{var temp__3698__auto____2425 = cljs.core.Vector.fromArray(["admin"," 'language"," 'feed-name","overview"]).call(null,vix.core.feed_list_documents.call(null," 'language".call(null,vix.core.params)," 'feed-name".call(null,vix.core.params))," 'fallback",vix.core.navigate_replace_state.call(null,"","Vix overview"));
if(cljs.core.truth_(temp__3698__auto____2425))
{var vec__2426__2427 = temp__3698__auto____2425;
var route_segments__2353__auto____2428 = cljs.core.nth.call(null,vec__2426__2427,0,null);
var body__2354__auto____2429 = cljs.core.nth.call(null,vec__2426__2427,1,null);
var clauses__2355__auto____2430 = cljs.core.nthnext.call(null,vec__2426__2427,2);
var temp__3695__auto____2431 = vix.core.route_matches.call(null,vix.core.route_segments_SHARP_,uri_path);
if(cljs.core.truth_(temp__3695__auto____2431))
{var params__2432 = temp__3695__auto____2431;
return vix.views.editor.start.call(null," 'language".call(null,params__2432)," 'feed-name".call(null,params__2432)," '*".call(null,params__2432));
} else
{var temp__3698__auto____2433 = " 'fallback".call(null,vix.core.navigate_replace_state.call(null,"","Vix overview"));
if(cljs.core.truth_(temp__3698__auto____2433))
{var vec__2434__2435 = temp__3698__auto____2433;
var route_segments__2353__auto____2436 = cljs.core.nth.call(null,vec__2434__2435,0,null);
var body__2354__auto____2437 = cljs.core.nth.call(null,vec__2434__2435,1,null);
var clauses__2355__auto____2438 = cljs.core.nthnext.call(null,vec__2434__2435,2);
var temp__3695__auto____2439 = vix.core.route_matches.call(null,vix.core.route_segments_SHARP_,uri_path);
if(cljs.core.truth_(temp__3695__auto____2439))
{var params__2440 = temp__3695__auto____2439;
return vix.core.feed_list_documents.call(null," 'language".call(null,params__2440)," 'feed-name".call(null,params__2440));
} else
{var temp__3698__auto____2441 = null;
if(cljs.core.truth_(temp__3698__auto____2441))
{var vec__2442__2443 = temp__3698__auto____2441;
var route_segments__2353__auto____2444 = cljs.core.nth.call(null,vec__2442__2443,0,null);
var body__2354__auto____2445 = cljs.core.nth.call(null,vec__2442__2443,1,null);
var clauses__2355__auto____2446 = cljs.core.nthnext.call(null,vec__2442__2443,2);
var temp__3695__auto____2447 = vix.core.route_matches.call(null,vix.core.route_segments_SHARP_,uri_path);
if(cljs.core.truth_(temp__3695__auto____2447))
{var params__2448 = temp__3695__auto____2447;
return vix.core.navigate_replace_state.call(null,"","Vix overview");
} else
{return null;
}
} else
{return null;
}
}
} else
{return null;
}
}
} else
{return null;
}
}
} else
{return null;
}
}
} else
{return null;
}
}
} else
{return null;
}
}
} else
{return null;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.