Fork me on GitHub
#clojure
<
2021-05-20
>
tlonist07:05:56

has anybody succeded in using lacinia-graphql on top of ring server? I’m thinking of routing requests to /graphql to lacinia, while keeping all the other RESTful requests working as usual.

simongray07:05:49

I want to convert a map containing some SAML assertions to an EDN-string. Currently doing this to get nice-looking output:

(-> (get-in req [:session :saml :assertions])
    (pprint)
    (with-out-str))
However, the map contains a Java instant and that comes out looking like
...
:not-on-or-after #object[java.time.Instant 0x4f69adf "2021-05-20T07:48:00Z"]
...
and I would like it to be a string without the Clojure/JVM-specific extra garbage. How do I do this? Also, would love to keep it pretty-printed.

simongray10:05:41

That worked great! Thank you so much, @U4ZDX466T.

flowthing10:05:03

Glad to hear it.

simongray10:05:14

What’s a smart way to implement a submap? fn to compare two maps, taking into account subsets inside the map too? E.g. {:a {:b #{:c}} should be a valid submap of {:a {:b #{:c :d}} .

Yehonathan Sharvit10:05:57

(defn submap [a b]
  (nil? (first (clojure.data/diff a b)))

4
🙏 2
💥 2
Yehonathan Sharvit10:05:09

Leveraging clojure.data/diff

simongray10:05:27

That was incredibly succinct! WOW.

raspasov11:05:54

Wonder how that would look like in Java 😜

Ben Sless11:05:42

@U0L91U7A8 playing around with diffs a few weeks ago paid off? 😄

borkdude11:05:56

I have a more involved one here: https://github.com/clj-kondo/clj-kondo/blob/4f1252748b128da6ea23033f14b2bec8662dc5fd/test/clj_kondo/test_utils.clj#L15 which also treats strings as "sub-things" of regexes

👍 2
Yehonathan Sharvit11:05:31

Actually, it was more a few months ago when I create this online EDN diff tool leveraging self-hosted ClojureScript https://blog.klipse.tech/clojure/2020/10/08/edn-diff.html

simongray11:05:53

@U04V15CAJ That seems very useful. I think I will stick with the more succinct solution, but keep yours in mind if I end up needing to support regexes.

ghadi13:05:49

has a ton of stuff in this ballpark

Jakub Holý (HolyJak)11:05:12

Is there a way to hint the JIT to replace a fn call with its result? Namely the count call in

(subs "Bearer xyz" (count "Bearer "))
I like the code because it is much clearer than the alternative (subs "Bearer xyz" 7) yet I do not want to have to count the length of the string upon every invocation. (Or perhaps I am worrying too much and Java will make "Bearer " into a static string with its length known so that the whole thing will end up as a single, cheap memory access on every execution?) 🙏

p-himik11:05:08

(defmacro embed [form]
  (eval form))

p-himik11:05:33

And then

(embed (count "Bearer "))

Jakub Holý (HolyJak)12:05:33

Thank you! But I would prefer a solution not involving a new macro 🙂 Something line ^:static if it exists/applies here. Or to hear that it is not an issue b/c JIT is smart enough...

borkdude12:05:44

possibly you can also use a data reader for this

borkdude12:05:04

#holyjak/inline (count "Bearer")

borkdude12:05:16

but honestly a macro isn't a bad idea either

☝️ 2
reborg12:05:24

This sounds a good case for (def ^:const Bearer (count "Bearer")) and (subs "Bearer xyz" Bearer)

2
thumbnail13:05:57

> possibly you can also use a data reader for this In that case #=(count "Bearer ") would even work (although #= is not public api iirc). To be honest; unless this is running in a tight loop i wouldn't worry too much about it.

👍 2
ghadi13:05:37

let the JIT deal with it

☝️ 4
👍 2
borkdude13:05:33

I don't like the "the JIT will solve it" argument. If you can write efficient code with little effort, I would prefer that over some other tool to solve the slowness (which may not even work in all targets)

ghadi13:05:43

premature optimization...

Thuan13:05:20

How about regex:

(some->> bearer
           (re-find #"((?i)bearer[\s]+)(.*)$")
           last)

Ben Sless17:05:53

Cheat

(alter-meta!
 #'clojure.core/count
 assoc
 :inline
 (fn [x]
   (if (or (vector? x) (string? x))
     (. clojure.lang.RT (count x))
     `(. clojure.lang.RT (count ~x)))))

Jakub Holý (HolyJak)17:05:10

@UK0810AQ2 could you be so kind and explain a little how will :inline be used?

Ben Sless17:05:14

@U0522TWDA at macro expansion time, if a function has :inline key it will be called like a macro on the call site. Lets functions behave like macros some of the time. You can (ab)use it

❤️ 2
ghadi17:05:46

Don’t use it :)

👍 2
vemv10:05:13

@U054W022G’s solution is simple and intuitive. I'd remove ^:const though as it isn't exactly a public feature (it's not advertised in http://clojure.org I think) I'd simply configure direct-linking in production - it's equivalent. That way one uses supported features, and ^:const won't get in your way while developing (it can interfere with a repl workflow among other things)

reborg13:05:45

Yeah not that public but not even that private https://github.com/clojure/clojure/blob/master/changes.md#215-const-defs . It’s not that equivalent to direct-linking as the call to count would happen everytime (it just removes the var indirection). And true, ^:const should be used only on computations that can happen at compile time. However, do not confuse that with defonce (which is probably even more problematic when reloading).

svt12:05:12

Is there a way to intercept all the calls happens via clj-http? Without touching each call individually?

msolli06:05:54

clj-http uses the Apache HttpComponent client, which can log all outgoing calls using log4j2. See https://github.com/dakrone/clj-http#logging. Using Logback, to enable logging of outgoing requests add this to logback.xml: <logger name="org.apache.http.wire" level="debug"/>

msolli06:05:08

You can even change it runtime:

(.. (org.slf4j.LoggerFactory/getLogger "org.apache.http.wire")
    (setLevel (ch.qos.logback.classic.Level/DEBUG)))

borkdude12:05:36

if clj-http doesn't have a built-in way to do it, you could try monkey-patching (if this is for debugging purposes)

svt12:05:42

No, it’s not for debugging purposes, I need request details for all outgoing request via it.

borkdude12:05:47

the probably make you own wrapper, if clj-http doesn't support it

👍 2
borkdude12:05:43

you could probably use this

ghadi16:05:02

@US3UEE2VB I'm not sure where your application is in the development process, I would suggest passing in an http client into your application as an argument, rather than intercepting outgoing calls

💯 2
ghadi16:05:26

the argument might just be a plain fn that takes a request map and returns a response map

ghadi16:05:02

that avoids the mocking/stubbing problem by design