Fork me on GitHub
#clojure
<
2019-09-20
>
emccue00:09:31

curious most about that data validation step

seancorfield03:09:36

@emccue What do you want to know?

seancorfield03:09:54

(sorry for the slow response -- dinner and feeding cats)

emccue03:09:25

s/explain-data and then a heuristic algorithm to turn the explained failure back into an error code

emccue03:09:45

that seems like an interesting approach

emccue03:09:44

that and how do you ensure that specs are generatable

emccue03:09:17

still not clear exactly how that mechanism works - i need to read more

patientplatypus06:09:21

i have a problem getting options preflight requests to work in clojure

patientplatypus06:09:01

im using wrap-cors

patientplatypus06:09:11

(run-server
   (wrap-cors
    (wrap-json-body
     (my-routes db)
     {:keywords? true :bigdecimals? true})
    :access-control-allow-origin [#""]
    :access-control-allow-methods [:get :put :post :delete :options]
    :access-control-allow-headers ["Origin" "X-Requested-With"
                                   "Content-Type" "Accept"])

patientplatypus06:09:23

apparently this doesnt work, so i cant make post requests :C

roklenarcic08:09:24

Just a heads up everyone, the behaviour of format function changes between java 8 and java 12

roklenarcic08:09:32

(format "%.2f" (float 10000)) will produce 10000.00 in java 8 but 10000,00 in java 12 if your locale has , as decimal separator

😬 8
✔️ 4
roklenarcic09:09:54

is it possible to remove an element from vector by index?

herald09:09:03

Yes, you can use subvec and into. But it's not an efficient operation, hence why they left out a function for this.

herald09:09:39

Here's how you'd do it:

(defn remidx [v i]
  (into (subvec v 0 i)
        (subvec v (inc i))))

herald09:09:22

If you're going to do this a lot and are worried about performance, you should probably use other (or additional) data structures.

vlaaad09:09:30

remember that subvec leaks memory (it's a view over other vector, so removed item won't be GCed)

Joe Lane13:09:46

If you need to do this, check out rrb-vector, it might be closer to what you're looking for with better Big O runtimes

andy.fingerhut15:09:10

Yes rrb-vector is better suited for such operations in terms of performance. I hope to create a new release of that library in the next week or two that should fix several existing bugs in that library.

andy.fingerhut15:09:44

With that library, you can do the remidx function above and it will run in O(log N) time.

Joe Lane15:09:34

Its one of my favorite libraries, excited to hear about the potential of an upcoming release for it 🙂

roklenarcic14:09:32

Ended up simply implementing this with arraylist

vlaaad09:09:22

only in O(N) IIRC

emccue13:09:56

how do i know whether this will chunk?

emccue13:09:18

(for purposes of pinpointing which form was the issue and processing every form before it)

emccue13:09:43

so if i get a stream of "1" "2" 5 afafa

emccue13:09:55

i don't want the exception to throw until i get to the end there

vlaaad13:09:37

you mean you want to preserve "1", "2" and 5?

Alex Miller (Clojure team)13:09:31

if you care, don't use seqs

Alex Miller (Clojure team)13:09:38

or in cheshire, don't use the lazy seq parsers

patientplatypus13:09:26

im having a really hard time getting a CORS middleware to return 200 on options requests, and was hoping someone might help walk me through it https://gist.github.com/patientplatypus/0e3defaf1cfeff3b6ca3f0910993ce10

Joe Lane13:09:10

It looks like you are attempting to invoke the preflight map as a function on line 95 of your gist. What if you just unwrap the parenthesis around that?

dpsutton14:09:53

@pweyand what error are you receiving?

dpsutton14:09:33

@U0CJ19XAM mentioned what seems like an obvious bug that would prevent your preflight from working. Have you tried raising the 200 map out of the parens that surround it?

patientplatypus14:09:22

i have not tried that yet

patientplatypus14:09:28

lemme give that a shot

patientplatypus14:09:33

im thinking it should look like this

patientplatypus14:09:37

(defn wrap-preflight [handler]
    (fn [request]
      (do
        (println "inside wrap-preflight")
        (println "value of request")
        (println request)
        (println "value of handler")
        (println handler)
        (if (preflight? request)
          {:status 200
           :headers cors-headers
           :body "preflight complete"}
          (handler request)))))

Joe Lane14:09:47

Sure, that fixes the bug I saw

Joe Lane14:09:51

Does it work?

patientplatypus14:09:55

@U0CJ19XAM no...i get around the errors i was getting in the terminal logs, but im still getting 404s on get and post requests

Joe Lane14:09:57

Let me look at the gist again

Joe Lane14:09:33

Are you dead set on using ring middleware or would you be open to non-ring libraries?

Joe Lane14:09:42

When you query for "/foo" or "/bar" do you get responses?

patientplatypus14:09:27

@U0CJ19XAM I do get responses if i make a straight request in the browser (ie http:..../foo)

patientplatypus14:09:44

at this point i just want to make this work

Joe Lane14:09:54

but not from curl, correct?

patientplatypus14:09:39

huh? no, a curl get request and a browser request are the same

patientplatypus14:09:57

i cant cross domain request (ie request a get from the client to the server)

Joe Lane14:09:24

Is this for work or for play?

patientplatypus14:09:40

this is for my portfolio, so one day i can get a job and not die screaming in the gutter covered in my own filth.

patientplatypus14:09:44

not that that should matter

Joe Lane14:09:10

Are you serving this from localhost?

Joe Lane14:09:06

I might change #"" to "*" and see if that works.

Joe Lane14:09:11

Otherwise, I would be happy to pair program with you on making a CORS respecting backend api either later today or within the next few days. We could go through a pretty deep dive and make the backend robust and built in an idiomatic clojure style. I hear you, I empathize with your frustration. I was just there 3 weeks ago with CORS and AWS Cognito/ API Gateway. I think the best thing may be to take a walk or step back from the problem. If you want a full sample project for how to set up CORS with pedestal I can grab one right now, but then you have to learn pedestal.

patientplatypus15:09:56

oh, well. thanks man. i might ask around.

patientplatypus13:09:35

if anyone has any ideas

vlaaad13:09:56

@emccue take-until-throws:

(defn nums [n]
  (lazy-seq (cons n (nums (dec n)))))

(defn take-until-throws [coll]
  (try
    (let [n (first coll)]
      (lazy-seq (cons n (take-until-throws (rest coll)))))
    (catch Exception e
      nil)))

(map #(/ 10 %) (nums 10))
; =>
Error printing return value (ArithmeticException) at clojure.lang.Numbers/divide (Numbers.java:188).
Divide by zero

(take-until-throws (map #(/ 10 %) (nums 10)))
; => (1 10/9 5/4 10/7 5/3 2 5/2 10/3 5 10)

eraserhd13:09:17

I have just discovered that this parses: #uuid "1-2-3-4-5".

😀 4
chepprey13:09:37

might be just me but that doesn't look like a valid uuid

eraserhd13:09:18

I discovered this by way of pasting a UUID, minus the last digit, into the REPL, then spending a long time trying to figure out why that UUID was no longer in the database :P

eraserhd13:09:54

The thing is, I've written a function to create a UUID of all zeros, padding the last number, so the UUIDs are readable in tests. And now I can't figure out whether I like this or hate it.

chepprey13:09:23

So I'm now lost in the wonderous docs of UUID's. I never knew there were this many "versions" of them. Version 4 "random" is all I've really known. TIL.

emccue13:09:54

@alexmiller Im given a bunch of space separated json forms, so the seq parser is the only one i've found so far that works

patientplatypus14:09:10

does anyone have any idea why this wont pass cors? i have been at this for hours and hours and hours

mccraigmccraig14:09:04

are your CORS headers being served ?

patientplatypus14:09:21

this shouldnt be this hard!

cjsauer15:09:16

@pweyand total stab in the dark: is prod served via https?

cjsauer15:09:25

Your origin regex is http only

Brian19:09:15

How can I parse "2019-09-19 18:09:35" into an #inst? I'v tried java.util.Date/parse and java.time.Instant but neither seem to work although I could be implementing them improperly

hiredman19:09:40

An "#inst" is not a type, it is the serialized form of a date, so many different date time types serialize that way

hiredman19:09:21

It doesn't make sense to parse something into an "#inst" because it isn't a specific thing

hiredman19:09:08

You'll need to parse it something like a java.util.Date

hiredman19:09:39

It is unlikely that anything will parse that string out of the box, because it doesn't contain enough information to make a good date object (no timezone info) but you can create a custome parse using DateFormat

hiredman19:09:54

I would prefer something like a java.time.Instant to a java.util.Date, but Instants don't serialize(print as) to #inst tagged literals by default. It isn't to hard to make them print that way though

Alan Thompson20:09:10

You could also use Instant/parse, but you would have to replace the space with a T char.

vlaaad19:09:52

clojure.instant/read-instant-date

vlaaad19:09:43

oh, it's not valid #inst syntax

chepprey19:09:38

If it's Clojure only (not cljs), I like this wrapper lib for Java8 time: https://github.com/dm3/clojure.java-time

chepprey19:09:09

You can define custom format strings and use both for parsing and formatting

hiredman19:09:54

I would prefer something like a java.time.Instant to a java.util.Date, but Instants don't serialize(print as) to #inst tagged literals by default. It isn't to hard to make them print that way though

jjttjj19:09:31

There are also these two (similar) time literal libraries that provide this which you can either use or steal ideas from https://github.com/henryw374/time-literals https://github.com/magnars/java-time-literals

Brian19:09:46

Thanks everyone!

Alan Thompson20:09:44

I have also collected a bunch of java.time helpers here: https://cloojure.github.io/doc/tupelo/tupelo.java-time.html

Alan Thompson20:09:19

Also see the embedded reply using DateTimeFormatter and ZonedDateTime/parse