I'm trying to load two namespaces from the library "spurious-aws-sdk-helper" (which by the way I've installed locally - this is me testing it before deploying to Clojars). And I'm loading the namespaces from inside an if statement.
Once the namespaces are loaded I call a function which is provided by one of the loaded namespaces.
The problem is that when executing the code via lein ring server I get a Java exception informing me that the namespace I'm trying to access isn't available.
But if I run lein repl and then (use 'spurious-clojure-example.routes.home) the relevant top level namespace; then (require '[spurious-aws-sdk-helper.core :as core]) the namespace - much like in the code I'll linked to in a moment demonstrates - then the namespace WILL be available and subsequently the call to the function won't error?
I'm not sure if it's one of those errors which are misleading and in fact it's not the namespace I'm trying to require that's the problem, but something inside of it that's the issue? But if that was true then why would it work when called manually by myself within lein repl?
The code I'm referring to is: https://github.com/Integralist/spurious-clojure-example/blob/baseline/src/spurious_clojure_example/routes/home.clj#L9-L10
(ns spurious-clojure-example.routes.home
(:use [amazonica.aws.s3])
(:require [compojure.core :refer :all]
[environ.core :refer [env]]
[spurious-clojure-example.views.layout :as layout]))
(if (env :debug)
(do
(require '[spurious-aws-sdk-helper.core :as core])
(require '[spurious-aws-sdk-helper.utils :refer [endpoint cred]])
(core/configure {:s3 "test-bucket4"
:sqs "test-queue4"
:ddb (slurp "./resources/config/schema.yaml")})))
(def bucket-path "news-archive/dev/election2014-council_title")
(def content
(apply str (line-seq
(clojure.java.io/reader
(:object-content
(get-object (cred (endpoint :spurious-s3)) :bucket-name "shared" :key bucket-path))))))
(defn home []
(layout/common [:h1 content]))
(defroutes home-routes
(GET "/" [] (home)))
It's the (core/configure ...) call that triggers a Java exception saying "core" namespace isn't available. But running the following code from lein repl works fine...
(use 'spurious-clojure-example.routes.home)
(require '[spurious-aws-sdk-helper.core :as core])
(core/configure ...rest of code...)
UPDATE 1:
Just to clarify I've updated the code as follows...
(when (env :debug)
(require '[spurious-aws-sdk-helper.core :as core])
(require '[spurious-aws-sdk-helper.utils :refer [endpoint cred]])
(core/configure
{:s3 "test-bucket7"
:sqs "test-queue9"
:ddb (slurp "./resources/config/schema.yaml")}))
...and when running it within the REPL it works fine.
The problem is when running it via lein ring server.
I've started reading about (ns-resolve) here: http://technomancy.us/143
But the solution it suggests: (ns-resolve 'core 'configure) didn't work; it just threw an Unable to resolve symbol: core in this context error.
I created a app with lein new compojure-app and when :debug value is truthy, require clojure.string :as str and then also printing something to shell.
The code below works via lein ring server. I tested it with :debug values true and false. I see in your example, you use environ so, I put {:debug true} or {:debug false} in .lein-env.
(ns integralist.handler
(:require [compojure.core :refer [defroutes routes]]
[ring.middleware.resource :refer [wrap-resource]]
[ring.middleware.file-info :refer [wrap-file-info]]
[hiccup.middleware :refer [wrap-base-url]]
[compojure.handler :as handler]
[compojure.route :as route]
[integralist.routes.home :refer [home-routes]]
[environ.core :refer [env]]))
(when (env :debug)
(require '[clojure.string :as str]))
(when (env :debug)
(defn it-works! []
(println "It works!:" (str/split "Clojure is Awesome" #" "))))
(defn init []
(println "integralist is starting")
(when (env :debug)
(it-works!)))
(defn destroy []
(println "integralist is shutting down"))
(defroutes app-routes
(route/resources "/")
(route/not-found "Not Found"))
(def app
(-> (routes home-routes app-routes)
(handler/site)
(wrap-base-url)))
Tried it with:
(when true ; also tried with false
(require '[clojure.string :as str])
(str/split "Clojure is awesome!" #" "))
=> No such namespace: str
I'm a tiny bit surprised as well since if and when should only evaluate the body of their expressions for the appropriate branch and not touch the other expressions. I did not expect a namespace error on false.
More surprising is I did not expect a namespace error on true as well. I'd guess it's some java compilation thing trying to resolve the namespace even before code evaluation. I don't know the specifics of why.
As for what you should do, this code is funky and I've never thought of or seen anyone doing anything similar. Unless there is some specific reason for doing this, the solution is simple: shove your requires to the very top in ns. There's no need to change anything else.
Related
I am using swagger to provide an API for a db access program. During development I normally have 2 versions running, the dev version and the prod version that I automatically start on login. I want to have a different title visible on the swagger front page so I don't accidentally trash my live database. So far I have been hand editing the title field in the swagger setup, but this is error prone, I often forget to change it before I run lein uberjar to build the prod version.
The env setup seems like an ideal way to do this. The luminus lein template already uses an env map that is built from dev and prod config files which works fine, allowing me to automatically specify different ports for the 2 builds. I added an entry to these that gives me a title that is different in prod and dev versions. I can see it from the repl, but including it in the swagger specification just gives null.
This is the :swagger definition from the start of my photo-api.routes.services.clj file:
(ns photo-api.routes.services
(:require [cheshire.core :as json]
[compojure.api.sweet :refer :all]
[image-lib.images :as ilim]
[image-lib.preferences :as ilpf]
[image-lib.projects :as ilpr]
[image-lib.write :as ilwr]
[photo-api.db.core :as db]
[photo-api.config :refer [env]]
[photo-api.routes.helpers.build :as build]
[photo-api.routes.helpers.keywords :as keywords]
[photo-api.routes.helpers.open :as open]
[photo-api.routes.helpers.photos :as photos]
[photo-api.routes.helpers.projects :as projects]
[ring.util.codec :refer [url-decode]]
[ring.util.http-response :refer [ok]]
[schema.core :as s]
[clojure.string :as str]))
(defapi service-routes
{:swagger {:ui "/swagger-ui"
:spec "/swagger.json"
:data
{:info
{:version "1.0.1"
;; Switch to correct title before lein uberjar
;; TODO Automate this so swagger page always shows dev or prod version
;;:title "Photo API"
:title (:title env)
:description "Access a mongo database containing details of photos"}}}}
The commented out :title specification works fine, but the (:title env) call doesn't although it is the exact same call I can successfully use from the repl. I believe the env map is built as part of photo-api.config and from the startup messages when I start the server it looks like this is being successfully started before the http-server:
{:started
["#'photo-api.config/env"
"#'photo-api.db.core/db*"
"#'photo-api.db.core/db"
"#'photo-api.handler/init-app"
"#'photo-api.handler/app"
"#'photo-api.core/http-server"]}
user>
This is photo-api.config, unchanged from the luminus default:
(ns photo-api.config
(:require [cprop.core :refer [load-config]]
[cprop.source :as source]
[mount.core :refer [args defstate]]))
(defstate env :start (load-config
:merge
[(args)
(source/from-system-props)
(source/from-env)]))
and the dev config.edn file:
{:title "**** Photos Development API ****"
:dev true
:port 31999
;; when :nrepl-port is set the application starts the nREPL server on load
:nrepl-port 57251}
Am I missing something obvious here? Is there another step necessary to make the env map visible to the swagger setup?
EDIT:
Changing the call from (:title env) to (env :title) causes cider-jack-in to fail with a long error message/stack trace that includes:
Caused by: java.lang.ClassCastException: mount.core.DerefableState cannot be cast to clojure.lang.IFn
Changing it again to (#env :title) then gives a similarly long error message/stack trace that contains:
Caused by: java.lang.ClassCastException: mount.core.NotStartedState cannot be cast to clojure.lang.IFn, compiling:(services.clj:29:23)
So it looks like env is not being started till after the call to it from the swagger setup. I still have no idea why as the when cider-jack-in did work it clearly showed the config.env state starting before the http-server. (see above)
It looks like (defstate env) is an atom that needs to be derefed. Mount's README points to the tests to for some examples .
You might try (:title #env) in service-routes
(defapi service-routes
{:swagger {:ui "/swagger-ui"
:spec "/swagger.json"
:data
{:info
{:version "1.0.1"
;; Switch to correct title before lein uberjar
;; TODO Automate this so swagger page always shows dev or prod version
;;:title "Photo API"
:title (:title #env) ;;;--------> UPDATED
:description "Access a mongo database containing details of photos"}}}}
EDIT --
Not an atom. See this issue for the same deref error, which, presumably means that no deref is needed.
New clojure developer trying to experiment with the HTTP kit clojure library in a REPL.
I created a new project in leinengen, lein new app kit-expt.
Then I modified the :dependencies block in project.clj to include [http-kit "2.2.0"].
Then I run lein deps, then lein repl.
In the REPL I try to run (:require [org.httpkit.client :as http]).
However, when I run this I get the error
CompilerException java.lang.ClassNotFoundException: org.httpkit.client, compiling:(/private/var/folders/cs/b0kcg6fx0335crbvn6xtgq7xl5c29j/T/form-init7575648818353088270.clj:1:1)
What am I doing wrong?
The :require form you're using is invalid, a keyword only for use in an ns (namespace) form. Try removing the : and just use (require ..., which is commonly used in a REPL. See more require examples here.
The HTTP Client docs you refer to assume you're in a source file using ns.
There appears to be something wrong in your environment. If I run it (Ubuntu 16.04) it works great:
(require '[org.httpkit.client :as http])
(pr-str (clojure.core/deref (http/get "http://google.com"))))
=> "{:opts {:method :get, :url \"http://www.google.com/\", :query-params nil, :form-params nil, :trace-re".....
Update
As Micah pointed out, in the repl you need the above form of require. Notice that it doesn't have the leading colon, and it does have a single-quote before the left square bracket. It must also be inside parentheses instead of square brackets.
In the ns form (which I prefer), everything has the opposite convention:
(ns tst.clj.core
(:use clj.core clojure.test tupelo.test)
(:require
[tupelo.core :as t]
[org.httpkit.client :as http] ))
I have a namespace that exposes common data-related functions (get-images, insert-user). I then have two database backends that have those same functions and implement them differently. They implement the interface as it were. Each backend is contained within a namespace.
I can't seem to be able to find a good solution on how to accomplish this.
I tried dynamically loading the ns but no luck. Once you do (:require [abc :as x]), the x isn't a real value.
I tried using defprotocol and deftype but that's all kinds of weird because the functions in the deftype need to be imported, too and that messes everything up for me.
Is there some idiomatic solution to this?
I don't see why protocols are not sufficient?
In ns data.api:
(ns data.api)
(defprotocol DB
(get-images [this])
(insert-user [this]))
In ns data.impl1:
(ns data.impl1
(:require [data.api :refer :all]))
(defrecord Database1 [connection-params]
DB
(get-images [_] ...)
(insert-user [_] ...))
Same thing in ns data.impl2.
Then when you go to use a particular db, just create the correct record:
(ns data.user
(:require [data.api :refer :all])
[data.impl1 :refer (->Database1)])
(defn use-db []
(let [db1 (->Database1 {})]
(get-images db1)))
I want to import dependencies in a function block. w These dependencies are not publicly available thus I cant include them through project.clj and require them in the ns block.
However these jars are available in the server env, and the server calls the below function when the below deps are in the classpath.
However when I compile the below function outside the server env, I still get compiler error that it is not able to find util and web ns.
(defn imm
[]
(require '[immutant.web :as web]
'[immutant.utilities :as util])
(server/load-views (io/file (util/app-root) "src/pm/views"))
(web/start "/dev" handler))
You have to delay the Var resolution.
(defn imm
[]
(require '[immutant.web :as web] '[immutant.utilities :as util])
(server/load-views (io/file #(resolve 'util/app-root) "src/pm/views"))
(#(resolve 'web/start) "/dev" handler))
My namespace declaration looks like this:
(ns test.foo
(:use
[clj-http.client :only (get) :as client]
[net.cgrand.enlive-html :only (select) :as html]))
It works fine in the REPL, the first time I use it. Then, when I modify the code and try the following in the REPL:
(use :reload 'test.foo)
I get:
java.lang.IllegalStateException: get already refers to: #'clj-http.client/get in namespace: test.foo (foo.clj:1)
I'm on windows with counterclockwise and also tried with leiningen (lein repl).
You should not shadow core fns by accident. You have to be explicit about your intent:
(ns test.foo
(:refer-clojure :exclude [get]) ; suppress the shadowing warning
(:require [clojure.core :as core]) ; allow to still reach clojure.core/get through core/get
(:use
[clj-http.client :only (get) :as client]
[net.cgrand.enlive-html :only (select) :as html]))