How to read Clojure's spec :macro-syntax-check errors - clojure

I copied the following joy.gui.DynaFrame definition from "Joy of Closure" Chapter 12.2.1
(ns joy.gui
(:gen-class
:name joy.gui.DynaFrame
:extends javax.swing.JFrame
:implements [clojure.lang.IMeta]
:prefix df-
:state state
:init init
:constructors {[String] [String]
[] [String]}
:methods [[display [java.awt.Container] void]
^{:static true} [version [] String]]
)
(:import (javax.swing JFrame JPanel JComponent)
(java.awt BorderLayout Container)))
Unfortunately, the syntax for :prefix has apparently changed since 2014, so the line 6 should read :prefix "df-".
Evaluating the incorrect code above I got the following error message:
2. Unhandled clojure.lang.Compiler$CompilerException
Error compiling src/dipping_feet/gui.clj at (1:1)
#:clojure.error{:phase :macro-syntax-check,
:line 1,
:column 1,
:source
1. Caused by clojure.lang.ExceptionInfo
Call to clojure.core/ns did not conform to spec.
#:clojure.spec.alpha{:problems
[{:path [],
:reason "Extra input",
:pred
(clojure.spec.alpha/cat
:docstring
(clojure.spec.alpha/? clojure.core/string?)
:attr-map
(clojure.spec.alpha/? clojure.core/map?)
:ns-clauses
:clojure.core.specs.alpha/ns-clauses),
:val
((:gen-class
:name
joy.gui.DynaFrame
:extends
javax.swing.JFrame
:implements
[clojure.lang.IMeta]
:prefix
df-
:state
state
:init
init
:constructors
{[String] [String], [] [String]}
:methods
[[display [java.awt.Container] void]
[version [] String]])
(:import
(javax.swing JFrame JPanel JComponent)
(java.awt BorderLayout Container))),
:via [:clojure.core.specs.alpha/ns-form],
:in [1]}],
:spec
#object[clojure.spec.alpha$regex_spec_impl$reify__2509 0x3b982314 "clojure.spec.alpha$regex_spec_impl$reify__2509#3b982314"],
:value
(joy.gui
(:gen-class
:name
joy.gui.DynaFrame
:extends
javax.swing.JFrame
:implements
[clojure.lang.IMeta]
:prefix
df-
:state
state
:init
init
:constructors
{[String] [String], [] [String]}
:methods
[[display [java.awt.Container] void]
[version [] String]])
(:import
(javax.swing JFrame JPanel JComponent)
(java.awt BorderLayout Container))),
:args
(joy.gui
(:gen-class
:name
joy.gui.DynaFrame
:extends
javax.swing.JFrame
:implements
[clojure.lang.IMeta]
:prefix
df-
:state
state
:init
init
:constructors
{[String] [String], [] [String]}
:methods
[[display [java.awt.Container] void]
[version [] String]])
(:import
(javax.swing JFrame JPanel JComponent)
(java.awt BorderLayout Container)))}
I omit the stack trace here.
My question is: is there a reference in the error message somewhere which would point me to the exact location of the error in the code? I had to basically guess which part of my definition is incorrect.

Yes, I agree, this is a confusing error message. This appears to be a bug in spec, so unfortunately I don't think there is anything you can do at this time to make the error any better.
https://clojure.atlassian.net/browse/CLJ-2013?oldIssueView=true

Related

how to fix issue with body-params values not being picked up by clojure muuntaja middleware

I'm trying to adapt the code here https://github.com/danownsthisspace/shorturl/blob/main/src/shorturl/core.clj with this:
(ns todo.core
(:require [clojure.pprint :as pprint]
[muuntaja.core :as m]
[reitit.ring :as ring]
[reitit.ring.middleware.muuntaja :as muuntaja]
[ring.adapter.jetty :as ring-jetty]
[ring.util.response :as r]
[todo.db :as db]))
(defn todo-items-save [req]
(clojure.pprint/pprint req)
(let [title (get-in req [:body-params :title])
content (get-in req [:body-params :content])]
(r/response (str "foooo" title))))
(def app
(ring/ring-handler
(ring/router
[["/"
["" {:handler (fn [req] {:body "hello" :status 200})}]]
["/api/todo" {:post {:handler todo-items-save}
:get (fn [req]
(let [todos db/get-todos]
(r/response todos)))}]
{:data {:muuntaja m/instance :middleware [muuntaja/format-middleware]}}])))
(defn start []
(ring-jetty/run-jetty #'app {:port 3002 :join? false}))
(def server (start))
(.stop server)
but I'm seeing that the body-params values are null. I was wondering why that is when I'm making a post request with the body {"content":"only a test", "title":"second"}. Thank you.
The problem was, that the muuntaja config was passed as part of the
routes instead of argument to ring/router
(def app
(ring/ring-handler
(ring/router
[["/"
["" {:handler (fn [req] {:body "hello" :status 200})}]]
["/api/todo" {:post {:handler todo-items-save}
:get (fn [req]
(let [todos db/get-todos]
(r/response todos)))}]]
; XXX this must be the second argument to `ring/router`
{:data {:muuntaja m/instance
:middleware [muuntaja/format-middleware]}})))

Clojure nested json response

I am new to clojure and I am trying to make a simple API with 3 endpoints.
I am trying to implement an endpoint which will take each row of a query and put it to a json.
So I have an SQLite database. These are my entries for example:
{:timestamp 2020-09-11 14:29:30, :lat 36.0, :long 36.0, :user michav}
{:timestamp 2020-09-11 14:31:47, :lat 36.0, :long 36.0, :user michav}
So I want a json response like the below:
{:get
:status 200
:body {:timestamp "2020-09-11 14:29:30"
:lat 36.0
:long 36.0
:user "michav"}
{:timestamp "2020-09-11 14:31:47"
:lat 36.0
:long 36.0
:user "michav"}
}
}
Here is my code that I am trying to fix it in order to get the above result.
(def db
{:classname "org.sqlite.JDBC"
:subprotocol "sqlite"
:subname "db/database.db"
})
(defn getparameter [req pname] (get (:params req) pname))
(defn output
"execute query and return lazy sequence"
[]
(query db ["select * from traffic_users"]))
(defn print-result-set
"prints the result set in tabular form"
[result-set]
(doseq [row result-set]
(println row)))
(defn request-example [req]
(response {:get {:status 200
:body (-> (doseq [row output]
{:timestamp (getparameter row :timestamp) :lat (getparameter row :lat) :long (getparameter row :long) :user (getparameter row :user)} ))
}}))
(defroutes my_routes
(GET "/request" [] request-example)
(route/resources "/"))
(def app (-> #'my_routes wrap-cookies wrap-keyword-params wrap-params wrap-json-response))
The error I encounter is the following :
java.lang.IllegalArgumentException: Don't know how to create ISeq from: mybank.core$output
RT.java:557 clojure.lang.RT.seqFrom
RT.java:537 clojure.lang.RT.seq
core.clj:137 clojure.core/seq
core.clj:137 clojure.core/seq
core.clj:65 mybank.core/request-example[fn]
core.clj:65 mybank.core/request-example
core.clj:63 mybank.core/request-example
response.clj:47 compojure.response/eval1399[fn]
response.clj:7 compojure.response/eval1321[fn]
core.clj:158 compojure.core/wrap-response[fn]
core.clj:128 compojure.core/wrap-route-middleware[fn]
core.clj:137 compojure.core/wrap-route-info[fn]
core.clj:146 compojure.core/wrap-route-matches[fn]
core.clj:185 compojure.core/routing[fn]
core.clj:2701 clojure.core/some
core.clj:2692 clojure.core/some
core.clj:185 compojure.core/routing
core.clj:182 compojure.core/routing
RestFn.java:139 clojure.lang.RestFn.applyTo
core.clj:667 clojure.core/apply
core.clj:660 clojure.core/apply
core.clj:192 compojure.core/routes[fn]
Var.java:384 clojure.lang.Var.invoke
cookies.clj:171 ring.middleware.cookies/wrap-cookies[fn]
keyword_params.clj:32 ring.middleware.keyword-params/wrap-keyword-params[fn]
params.clj:57 ring.middleware.params/wrap-params[fn]
json.clj:42 ring.middleware.json/wrap-json-response[fn]
Var.java:384 clojure.lang.Var.invoke
reload.clj:18 ring.middleware.reload/wrap-reload[fn]
stacktrace.clj:17 ring.middleware.stacktrace/wrap-stacktrace-log[fn]
stacktrace.clj:80 ring.middleware.stacktrace/wrap-stacktrace-web[fn]
jetty.clj:27 ring.adapter.jetty/proxy-handler[fn]
(Unknown Source) ring.adapter.jetty.proxy$org.eclipse.jetty.server.handler.AbstractHandler$ff19274a.handle
HandlerWrapper.java:127 org.eclipse.jetty.server.handler.HandlerWrapper.handle
Server.java:500 org.eclipse.jetty.server.Server.handle
HttpChannel.java:386 org.eclipse.jetty.server.HttpChannel.lambda$handle$1
HttpChannel.java:562 org.eclipse.jetty.server.HttpChannel.dispatch
HttpChannel.java:378 org.eclipse.jetty.server.HttpChannel.handle
HttpConnection.java:270 org.eclipse.jetty.server.HttpConnection.onFillable
AbstractConnection.java:311 org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded
FillInterest.java:103 org.eclipse.jetty.io.FillInterest.fillable
ChannelEndPoint.java:117 org.eclipse.jetty.io.ChannelEndPoint$2.run
EatWhatYouKill.java:336 org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask
EatWhatYouKill.java:313 org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce
EatWhatYouKill.java:171 org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce
EatWhatYouKill.java:135 org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce
QueuedThreadPool.java:806 org.eclipse.jetty.util.thread.QueuedThreadPool.runJob
QueuedThreadPool.java:938 org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run
(Unknown Source) java.lang.Thread.run
Your mistake is on this line:
(doseq [row output] ...
The error message
Don't know how to create ISeq from: mybank.core$output
gives the clue. The variable output is the function, not a sequence like doseq expects. You meant to call the output function, so you need to use parentheses to create a function call like:
(doseq [row (output)] ...
Update
You need to include an external library to convert between EDN data and a JSON string. My favorite way is my own library Tupelo Clojure. Use it like this:
(ns tst.demo.core
(:use tupelo.core tupelo.test))
(let [data [{:timestamp "2020-09-11 14:29:30", :lat 36.0, :long 36.0, :user "michav"}
{:timestamp "2020-09-11 14:31:47", :lat 36.0, :long 36.0, :user "michav"}]]
(println (edn->json data)))
with result:
[{"timestamp":"2020-09-11 14:29:30","lat":36.0,"long":36.0,"user":"michav"},
{"timestamp":"2020-09-11 14:31:47","lat":36.0,"long":36.0,"user":"michav"}]
You will need a line like this in your project.clj:
[tupelo "20.08.27"]
Please also see this template project for an easy way to get started. Enjoy!

Can't eval/load-string clojure function from string

I need to read a clojure function from an edn file which outputs hiccup to generate html content.
But I'm stuck at the part where the function needs to be evaluated.
I receive the error message:
java.lang.RuntimeException: Unable to resolve symbol: fn in this context, compiling:(null:1:1)
((eval (read-string "(fn [] (list [:div\"Hello\"]))")))
and
((load-string "(fn [] (list [:div\"Hello\"]))"))
are working inside a clojure REPL and output the expected result
([:div "Hello"])
project.clj
(defproject infocenter "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/clojurescript "1.9.225"]
[hiccup "1.0.5"]]
:plugins [[lein-figwheel "0.5.4-7"]]
:clean-targets ^{:protect false} [:target-path "out" "resources/public/cljs"]
:cljsbuild {
:builds [{:id "dev"
:source-paths ["src"]
:figwheel true
:compiler {:main "templ.core"
:asset-path "cljs/out"
:output-to "resources/public/cljs/main.js"
:output-dir "resources/public/cljs/out"}}]}
:figwheel {
:css-dirs ["resources/public/template"]})
core.cljs
(ns templ.core
(:require-macros [templ.edn :refer [read-edn]]))
(let [div (. js/document getElementById "content")]
(set! (. div -innerHTML) (read-edn "fn.edn")))
edn.clj
(ns templ.edn
(:use [hiccup core form]))
(defmacro read-edn
"Read template from file in resources/"
[edn-name]
;(slurp edn-name)
;((eval (read-string "(fn [] (list [:div\"Hello\"]))")))
((load-string "(fn [] (list [:div\"Hello\"]))"))
)
Try this:
(ns xyz.core
(:gen-class))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "main - enter")
(println (eval '(+ 2 3)))
(println ((eval (read-string "(fn [] (list [:div\"Hello\"]))"))))
(println "main - exit")
)
with results:
> lein run
main - enter
5
([:div Hello])
main - exit
This is just plain Clojure, and I see you are using CLJS as well. There may be some problem in your setup with the CLJS part of it. What are you trying to do with read-edn and fn.edn? I would avoid macros completely whenever possible.

Clojure - use a core.async channel with Yada/Aleph

I am trying to use Clojure manifold library, and in order to understand it, I need wanted to convert a core.async channel into a manifold stream.
I would like to create the equivalent the following using a core.async channel :
(require '[manifold.stream :as s])
(s/periodically 100 #(str " ok "))
;; Here is what I tried, it fails with an error 500
(let [ch (chan)]
(go-loop []
(>! ch " ok ")
(<! (timeout 100))
(recur))
(s/->source ch))
I am trying to feed a core.async channel into yada. The first code sample, using manifold.stream/periodic works, not the others using core.async. I tried on yada 1.0.0 and 1.1.0-SNAPSHOT.
Using manifold.stream/periodic works :
(def get-stream
(yada (fn [ctx]
(-> (:response ctx)
(assoc :status 202)
(assoc :body (s/periodically 1000 #(str (System/currentTimeMillis) " ")))))
{:representations [{:media-type "application/json"
:charset "UTF-8"}
{:media-type "application/edn"
:charset "UTF-8"}]}))
Using manifold.stream/->source returns an error 500 :
(def get-stream
(yada (fn [ctx]
(-> (:response ctx)
(assoc :status 202)
;; Similar to this : https://github.com/juxt/yada/blob/94f3ee93de155a8513b27e0508608691ed556a55/dev/src/yada/dev/async.clj
(assoc :body (let [ch (chan)]
(go-loop []
(>! ch " ok ")
(<! (timeout 100))
(recur))
(s/->source ch)))))
{:representations [{:media-type "application/json"
:charset "UTF-8"}
{:media-type "application/edn"
:charset "UTF-8"}]}))
;; Error on the page :
;; 500: Unknown
;; Error on GET
;; #error {
;; :cause "No implementation of method: :to-body of protocol: #'yada.body/MessageBody found for class: manifold.stream.async.CoreAsyncSource"
;; :via
;; [{:type clojure.lang.ExceptionInfo
;; :message "Error on GET"
;; :data {:response #yada.response.Response{:representation {:media-type #yada.media-type[application/json;q=1.0], :charset #yada.charset.CharsetMap{:alias "UTF-8", :quality 1.0}}, :vary #{:media-type}}, :resource #function[backend.routes.examples.ts/fn--57734]}
;; :at [clojure.core$ex_info invoke "core.clj" 4593]}
;; {:type java.lang.IllegalArgumentException
;; :message "No implementation of method: :to-body of protocol: #'yada.body/MessageBody found for class: manifold.stream.async.CoreAsyncSource"
;; :at [clojure.core$_cache_protocol_fn invoke "core_deftype.clj" 554]}]
;; :trace
Third attempt, with a core.async channel (different error 500) :
(def get-stream
(yada (fn [ctx]
(-> (:response ctx)
(assoc :status 202)
(assoc :body (chan)))
{:representations [{:media-type "application/json"
:charset "UTF-8"}
{:media-type "application/edn"
:charset "UTF-8"}]}))
;; Error on the page :
;; 500: Unknown
;; Error on GET
;; #error {
;; :cause "No implementation of method: :to-body of protocol: #'yada.body/MessageBody found for class: clojure.core.async.impl.channels.ManyToManyChannel"
;; :via
;; [{:type clojure.lang.ExceptionInfo
;; :message "Error on GET"
;; :data {:response #yada.response.Response{:representation {:media-type #yada.media-type[application/json;q=1.0], :charset #yada.charset.CharsetMap{:alias "UTF-8", :quality 1.0}}, :vary #{:media-type}}, :resource #function[backend.routes.api.subscribe/subscribe$fn--64130]}
;; :at [clojure.core$ex_info invoke "core.clj" 4593]}
;; {:type java.lang.IllegalArgumentException
;; :message "No implementation of method: :to-body of protocol: #'yada.body/MessageBody found for class: clojure.core.async.impl.channels.ManyToManyChannel"
;; :at [clojure.core$_cache_protocol_fn invoke "core_deftype.clj" 554]}]
;; :trace
The key error is this:
"No implementation of method: :to-body of protocol:
#'yada.body/MessageBody
found for class: manifold.stream.async.CoreAsyncSource"
It reveals that the yada version you are using is trying to coerce a body type it doesn't understand. More recent versions of yada are more permissive about what you can send through to the web-server, and allow anything through that it doesn't know how to transform.

java.lang.IllegalArgumentException: No implementation of method: :route-matches of protocol: #'clout.core/Route

I have two files of interest:
build.boot
(set-env!
:source-paths #{"src/clj" "src/cljs" "test/clj"}
:resource-paths #{"html" "target/main.js"}
:dependencies '[[adzerk/boot-cljs "0.0-3308-0"]
[adzerk/boot-cljs-repl "0.1.10-SNAPSHOT"]
[adzerk/boot-reload "0.3.1"]
[adzerk/boot-test "1.0.4"]
[cljsjs/hammer "2.0.4-4"]
[compojure "1.3.1"]
[com.datomic/datomic-pro "0.9.5186"]
[hiccup "1.0.5"]
[org.clojure/clojure "1.7.0-RC1"]
[org.clojure/clojurescript "0.0-3308"]
[org.clojure/core.async "0.1.346.0-17112a-alpha"]
[org.clojure/test.check "0.7.0"]
[org.omcljs/om "0.8.8"]
[pandeiro/boot-http "0.6.3-SNAPSHOT"]
[ring/ring-devel "1.4.0-RC1"]
[http-kit "2.1.18"]])
(require
'[adzerk.boot-cljs :refer [cljs]]
'[adzerk.boot-cljs-repl :refer [cljs-repl start-repl]]
'[adzerk.boot-reload :refer [reload]]
'[adzerk.boot-test :refer [test]]
'[pandeiro.boot-http :refer [serve]])
(task-options!
cljs {:source-map true
:optimizations :none
:pretty-print true})
(deftask build
"Build an uberjar of this project that can be run with java -jar"
[]
(comp
(cljs)
(aot :namespace '#{vidiot.server})
(pom :project 'vidiot
:version "0.1.0")
(uber)
(jar :main 'vidiot.server)))
and src/clj/vidiot/server.clj
(ns vidiot.server
(:gen-class)
(:require
[compojure.core :refer :all]
[compojure.route :as route]
[hiccup.core :refer :all]
[org.httpkit.server :refer :all]
[ring.middleware.reload :as reload]
[ring.util.response :as response]))
(defonce server (atom nil))
(defroutes all-routes
(GET "/" [] (response/redirect "index.html"))
(GET "/ws" [request]
(with-channel request channel
(on-close
channel
(fn [status]
(println "channel closed: " status)))
(on-receive
channel
(fn [data] ;; echo it back
(send! channel data)))))
(route/files "/" {:root "target"})
(route/not-found (response/response (html [:div#erro "Page Not Found"]))))
(defn -main [& args]
(run-server all-routes {:port 8080}))
Then I,
> boot build
> java -jar target/vidiot-0.1.0.jar
Followed by going to localhost:9090 in my browser, the terminal prints.
java.lang.IllegalArgumentException: No implementation of method: :route-matches of protocol: #'clout.core/Route found for class: clout.core.CompiledRoute
at clojure.core$_cache_protocol_fn.invoke(core_deftype.clj:554)
at clout.core$eval5590$fn__5591$G__5581__5598.invoke(core.clj:39)
at compojure.core$if_route$fn__5887.invoke(core.clj:40)
at compojure.core$if_method$fn__5879.invoke(core.clj:27)
at compojure.core$routing$fn__5918.invoke(core.clj:127)
at clojure.core$some.invoke(core.clj:2568)
at compojure.core$routing.doInvoke(core.clj:127)
at clojure.lang.RestFn.applyTo(RestFn.java:139)
at clojure.core$apply.invoke(core.clj:630)
at compojure.core$routes$fn__5922.invoke(core.clj:132)
at org.httpkit.server.HttpHandler.run(RingHandler.java:91)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
I can fix this issue by downgrading :dependencies in build.boot to [compojure "1.1.6"].
So, my question is, why can't I use [compojure "1.3.4"] (the most recent version at this writing) when building my uberjar?
Placing aot task after uber fixes the issue.
(task-options! pom {:project 'my-project
:version "0.1.0"}
jar {:main 'my-project.core}
aot {:namespace '#{my-project.core}})
(deftask build []
(comp (pom)
(uber)
(aot)
(jar)))
I was able to fix this by adding an exclude for the clout folder. It looks like uberjar is unpacking some compiled clout files on top of those compiled from project source. Example from my project:
(comp (cljs :compiler-options {:output-to "js/main.js"})
(aot :namespace '#{zoondka-maps.server zoondka-maps.handler})
(pom :project (symbol (:name project))
:version (:version project))
(uber :exclude (conj pod/standard-jar-exclusions #".*\.html" #"clout/.*"))
(jar :file (str (:name project) ".jar")
:main 'zoondka-maps.server)))