This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-08-07
Channels
- # aleph (4)
- # announcements (7)
- # arachne (1)
- # beginners (138)
- # calva (5)
- # cider (1)
- # clara (14)
- # clj-kondo (1)
- # cljsrn (2)
- # clojars (4)
- # clojure (247)
- # clojure-dev (33)
- # clojure-europe (3)
- # clojure-italy (71)
- # clojure-losangeles (6)
- # clojure-romania (6)
- # clojure-spain (11)
- # clojure-uk (17)
- # clojurescript (95)
- # core-async (2)
- # cursive (19)
- # datomic (7)
- # duct (27)
- # figwheel (1)
- # graalvm (22)
- # juxt (7)
- # kaocha (8)
- # leiningen (1)
- # luminus (7)
- # lumo (4)
- # off-topic (38)
- # reagent (4)
- # reitit (11)
- # shadow-cljs (30)
- # spacemacs (42)
- # tools-deps (103)
- # xtdb (5)
defresource is for serving content out of a jar that started as a file in your repo before building
so if you want a static file that isn't represented by code or a string in a source file, and want to be able to serve it directly from the jar without unzipping or whatever
commonly used for index.html
liberator is a collection of http libs, you typically call whatever functions you need in order to serve a request from inside your handler function for an individual route / method
correcting the above: that liberator calls a "resource" isn't what we'd usually call a resource - it's a way ot describe an endpoint (and can include arbitrary function calls)
I'm sad they decided to use the word "resource" for this, as it already has a meaning in the clojure http handling world :/
yeah, they define endpoints that can be called if a certian route is hit
@gagan.chohan this intro shows using an arbitrary function for :handle-ok
(the non-error case) http://clojure-liberator.github.io/liberator/tutorial/getting-started.html
(defroutes app
(ANY "/foo" [] (resource :available-media-types ["text/html"]
:handle-ok (fn [ctx]
(format "<html>It's %d milliseconds since the beginning of the epoch."
(System/currentTimeMillis))))))
and of course that function can call other functions
not that I know of (it may well exist in a third party lib I haven't used), clojure (and fp generally) tends to lead you toward using example based and property based tests that are focused on input values and return values, and not the control flow of the code executed
There’s a function that calls S3 or cache to retrieve data, I want to test both scenarios, in case of S3 and cache, they both call different functions
come to think of it, I have seen this done with with-redefs
I take back what I said before: the pattern is (let [executed (atom [])] (with-redefs [some-ns/call-s3 (fn [& args] (swap! executed conj {:f :some-ns/call-s3 :args [args]}))] (some-other-fn))
after you execute some-other-fn
you check the contents of executed
(it could contain zero or more entries of course)
and you would "redef" every function you want to track / stub
I'm personally not a fan of this sort of test, and with-redefs shouldn't be used in a multi-threaded context, but it does work
so , is there any preferred approach to test what is being called, S3 or Cache, considering both of them returns same data
that's what the above will do (add another redef for other-ns/hit-cache
)
it replaces the function you want to track with something that records its invocation - which is then data you can make an assertion about
user=> (def calls (atom []))
#'user/calls
user=> (with-redefs [clojure.core/println (fn [& args] (swap! calls conj {:f 'println :args args}))] (println :OK))
[{:f println, :args (:OK)}]
user=> @calls
[{:f println, :args (:OK)}]
it shouldn't be used with multiple threads in use, because two overlapping with-redefs calls can lead to losing the original function definition
so you're committing to single-threaded execution during tests if using this
(or very careful usage of threads and redefs)
Difficulty in testing is (to me) a sign that you need to refactor. If you can change this to have two (or arbitrarily many) strategies to do whatever you might get more adaptable code and more easily tested code at the same time
don’t know why this doesn’t work (map #(.autoSizeColumn sheet %) (range 2))
while call it one-by-one (.autoSizeColumn sheet 0)
& (.autoSizeColumn sheet 1)
works?
does .autoSizeColumn return a new value? If its all just for side effects, use doseq
instead.
Hi. After setting JAVA_CMD
to C:\Program Files\Java\jdk1.8.0_211\bin\javac.exe
running lein install
produces
javac: invalid flag: -Dclojure.compile.path=C:\git\xx\mranderson/target/classes
Usage: javac <options> <source files>
use -help for a list of possible options
Does anyone know what I'm doing wrong/how to fix?JAVA_CMD
should have been set to C:\Program Files\Java\jdk1.8.0_211\bin\java
. 🙂
I try to export a large excel file using docjure, and the jvm keep crashing due to OutOfMemoryError, GC overhead limit exceed. Which jvm options should I alter? Thx in advance 🙇:skin-tone-2:
often i'm faced with making a serious of changes to a map (as part of some data munging), I generally use the ->
thread macro to change update/assoc ops, however it gets awkward when I want to , for example, change/add a key based on the value of another key. is there no function in cloure core for doing this?
i can workaround it by using as->
but .. 😕 i don't see that macro used much in code in the wild vs ->
, which makes me think i'm missing something
I just use destructuring in let's in combination with the threaded assoc/updates or bind to one var (usually it's %) and use get-in's.
like
(->> collection
(map #(let [{:keys [one two]} %]
(-> %
(assoc-in [:key :path 1] (get-in % [:other :key :path]))
(assoc :two one)
(assoc :one two)
(update-in [:yet :another] inc)))))
you can also destructure in the (fn ...) arg list instead of using anonymous inline func
(->> collection
(map (fn [{:keys [one two] :as arg}]
(-> arg
(assoc-in [:key :path 1] (get-in arg [:other :key :path]))
(assoc :two one)
(assoc :one two)
(update-in [:yet :another] inc)))))
@ramblurr Clojure core is intentionally limited when it comes to stuff like this, so I recommend either writing your own helper functions or to check out something like https://github.com/nathanmarz/specter/ and see if that might be of use
I have a dev scenario / lein
question.
Say that I have two Clojure projects, libfoo and svcbar. svcbar has libfoo as one of its dependencies. Now I want to make a change in libfoo and try it out in svcbar. I bump the version i libfoo from 1.1
to 1.2-SNAPSHOT
, I test, I build a JAR -- I'm happy. Then I change over to the svcbar dir and change its dependency on libfoo to 1.2-SNAPSHOT
, but of course that build fails because that version isn't published.
How do I make a locally built version of a library available to lein
when building something that depends on it?
https://github.com/technomancy/leiningen/blob/master/doc/TUTORIAL.md#checkout-dependencies
@magnus_clojurians there are 2 ways: if you are done with dev work on libfoo
you can run lein install
from the root directory of libfoo
on your dev machine. This will install the new version of libfoo
on your local machine and should allow svcbar
to find it as a dependency.
If you are still working on libfoo
and want to dynamically pick up changes in svcbar
leiningen has a feature called checkouts
. You can set up a directory in svcbar
that has a symbolic link to libfoo
s project root. Lein should use this as a source for dependency resolution. The main advantage to this approach is you don’t have to run lein install
every time you make a change to libfoo
.
But both approaches should be the same otherwise.
I've seen lein install
, but it refers to lein deploy
which only refers to publishing to public repositories. Is there some documentation on how to use it locally?
I've never seen anything about checkins, it's not a command I guess... so where can I find information about how to actually use it?
Correct. It’s actually checkouts
and it would be a sub directory of svcbar
. You should be able to find the documentation here if you search the page: https://github.com/technomancy/leiningen/blob/master/doc/TUTORIAL.md. Sorry I couldn’t find a direct link, responding from my phone. 😳
Also, deploy does push the Jar but I think install simply installs it on the machine where the command is run. Could be wrong about that but I think that is how it works.
That’s correct
is reduce
lazy? It doesn't look like it, but my experiments are proving inconclusive
on second thought, i (naively) assume every iteration tool in clojure is lazy. the language does a good job with being consistent on that front (all of the seq-related tools are lazy), and the reduce docs don't say either way. the impl isn't wrapped in a lazy-seq
, which points to it not being lazy
my bad programming, i think
it's not lazy
i had a doseq
, and wanted to save the results, so i switched to a for
loop wrapped in a doall
, but on closer inspection i was seeing some weird behavior, so i thought i'd try using reduce
to make sure, and then weird behavior persisted, so the problem looks to be an error in my logic somewhere
thanks for the quick answer!
8, 11, 14 are the LTS (Long Term Support) versions which is why folks tend to prefer those versions, even in the openjdk camp.
We're stuck on openjdk8 because some pieces of our infrastructure won't run on jdk9+ but I'm testing a lot of stuff on both jdk8 and jdk11 at this point.
for Clojure, we're trying to emphasize the LTS versions so we recommend 8 and 11 as primary right now (but the matrix tests with 12 too)
we are making no effort to test with 9 or 10 right now (although we did prior to 11 and there are no particular issues with them)
I need some help - I'm doing my first ever POST request and so I'm a little unsure what to expect. I have a successful connection between API and website but the data I POST just seemingly disappears. Its the end of my night and I feel like I don't know where to go from here. Here's a gist with my code and the output I've got from print statements: https://gist.github.com/Ashe/fa13a7a1ee44736676b7116663f8f751
I don't use Compojure but I think I see the issue: In the line 32 of core.clj you have
(c/POST "/forum/submit" post (db/submit-forum-post post))
But in this case, post
is not the parameters you sent in the request. It is the whole request object. If you wanted the params you'd have to destructure it like:
(c/POST "/forum/submit" {params :params} (db/submit-forum-post params))
If you look at the gist I sent, the params
inside the request is empty - I was trying that at first but I thought for the sake of getting some help with this I'd show the whole request in the print statement
Another thing I noticed is that you're not ussing any middleware. If you expect JSON you should probably use something like https://github.com/ring-clojure/ring-json If you're using url encoded forms there is already a function on ring.middleware.params called wrap-params that handles that.
@ULLSSKY00 Yep! That's due to not using any middleware to handle the request.
Yeah I don't really understand any of this middleware stuff - so is that why my data is just disappearing?
okay getting somwhere
Do you know how hard it'd be to get that middleware up and running? Do I wrap it like I do with cors or something?
It is not dissappearing! It is on the :body property IIRC. It's just not in a format you can use. You're already using a middleware! wrap-cors is exactly that.
So you could write (wrap-params (wrap-cors router .....))
Or in a more readable way:
(-> router
wrap-params
(wrap-cors :access-control.......))
I don't really like that syntax unless the brackets get reaally bad
;; Wraps middleware around router
(def api-handler
(rjson/wrap-json-response
(cors/wrap-cors router
:access-control-allow-origin [#""]
:access-control-allow-methods [:get :put :post :delete])))
Does this look okay? As my data still doesn't appear in my print statemntsorry if im missing something, I just really want to get this done before bed if its a simple thing
WOO I GOT IT
Thank you ❤️
Thank you thank you thank you! ❤️
I got past that problem, my current problem is with postgresql
2019-08-08 00:06:21.167:WARN:oejs.HttpChannel:qtp878530251-18: /forum/submit
org.postgresql.util.PSQLException: This ResultSet is closed.
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkClosed(AbstractJdbc2ResultSet.java:2654)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.setFetchSize(AbstractJdbc2ResultSet.java:1771)
at org.postgresql.jdbc4.Jdbc4Statement.createResultSet(Jdbc4Statement.java:39)
at least the POST is out of the way!
That could mean you're closing the connection to postgres before you read from the ResultSet 😮
I'm afraid I'm not quite sure of what you mean, but if you mean what the actual statement is here you go https://gist.github.com/Ashe/ff245da7595a7afde100d896e3dc64ce
thank you for trying to help!
I cannot wait to sleep but if I don't finish this I will be awake all night on my laptop trying to do it
Don't worry, I have some free time at work today so this keeps me busy give me a minute to check that
its my first time working with all these things so hopefully its just an amateur mistake you're looking for 🙂
thank you @UF9QZAGQ5 ❤️
Is this where you are getting the error? 😮 I can't really find anything wrong with this.
error triggers when I do submit-forum-post
It says the error comes from opening and closing the connection or something but there's only two things I do with SQL - my GET for the forum posts and pages (done on another page) and this POST
and the get stuff works fine even now.
Commenting out the sql/insert!
stuff makes this work
let me try deleting my db maybe?
Same problems
CREATE TABLE Tags (
TagID bigserial NOT NULL,
TagLabel varchar(18)
);
CREATE TABLE Users (
UserID bigserial NOT NULL,
Username varchar(18) NOT NULL,
UserHandle varchar(12) NOT NULL UNIQUE,
JoinDate timestamptz NOT NULL,
IsAdmin bool NOT NULL,
PRIMARY KEY (UserID)
);
CREATE TABLE Posts (
PostID bigserial NOT NULL,
PosterID bigserial,
PostDate timestamptz NOT NULL,
PostTitle varchar(100) NOT NULL,
PostContent text NOT NULL,
PRIMARY KEY (PostID),
FOREIGN KEY (PosterID) REFERENCES Users(UserID)
);
These are my tables
is there something in the tablet that's making it crash?
Oh- could it be that I'm not passing PostDate
? I was expecting that to just insert the current time in though?
If only I knew how to pass current time in
oh really???
Just tag it on after not null?
hmm the error is still there
Another thing, PosterID is defined as a bigserial. Which means it will try to autoincrement whenever you create a new record. I imagine you want to add it yourself, so bigint
would be a better type
Does it not have to match the same type as the other table? It's like linked to the Users table?
Nice! Right now I'm not worrying about the user as im doing a guest post so I'm expecting that to be null
unfortunately I am still getting my error 😞