Fork me on GitHub
#beginners
<
2023-01-13
>
Roee Mazor10:01:10

Hey hey, I have a program that takes a JSON as input and outputs multiple (or 0) JSONs as a response. I would like to map which fields are actually being used from the input JSON, how and why. I was thinking for phase 1 to replace the hash-maps with some type that will document all the gets from the input JSON. I would love some feedback on my idea / more suggestions. we can always do the whole thing manually but that could be tedious and we might miss some things that the automatic way would not (and vice versa I guess) *lets assume the input JSONs have hundreds of fields and the values are numbers, arrays, booleans, strings and more JSONs

jumar11:01:48

Can you post an example of input and corresponding output together with the mapping?

teodorlu12:01:39

Suggestion: use keys to get json keys, and clojure.set/union to merge each key set. (does not work for hierarchical / recursive json - then perhaps use clojure.walk/prewalk) Please let me know if this doesn't make any sense! I'm not 100 % sure whether I know what you want to achieve.

Roee Mazor12:01:08

@U06BE1L6T, I cannot but lets assume it contains a lot of ifs, modifications on the data, decisions about how many outputs are gonna be and most of the decisions are complex

Roee Mazor12:01:46

@U3X7174KS, can you explain what i am trying to achieve by using keys and union? I am not sure how to use these for this goal

teodorlu12:01:35

You return some data. You want to know what keys are present. A way to solve that is to iterate collect a set of keys. In the shallow case, you can perhaps (reduce set/union (set) (map keys objects)), where objects is a list of objects. In the nested case it gets harder, but not impossible.

teodorlu12:01:27

Like jumar suggested - can you assume there's a function magic that does what you need, and provide an example call to magic for us?

(magic ,,,)
;; => ,,, 

Roee Mazor12:01:26

the input could be {:a 1} and the output could be [{:b 5}, {:c 7}] or []

Roee Mazor13:01:01

it is not really a magic function, the prgoram does a lot of calculations, I need to know which fields it used in the calculation. (regardless of what got passed to the output) let's say my service is something like this:

(defn foo [input-map] [{:b 3} {:c 5}]) 
then magic should tell me "nothing was used in the calculation" lets say the service does this:
(defn foo [input-map] (if {:a input-map}    
    [{:b (+ 1 (:a input-mmap))} {:c 5}]
    [{:b (* 9999 (:c input-mmap))} {:c 5}])
then magic should say: • :a is used in line 1 • :a is used in line 2 • :c is used in line 3

Ben Sless13:01:04

You could try to use typed Clojure's inference or instrumentation, but I haven't managed to get it to work yet

jumar13:01:54

Perhaps you could use metadata for that but I feel like it's mostly about how do you construct the descriptions of what has been used in the function.

emccue18:01:42

I think what you want is a custom implementation of a map which has a hook for spying on accesses

2
emccue18:01:23

(def-map-type LazyMap [m mta]
  (get [_ k default-value]
    (if (contains? m k)
      (let [v (get m k)]
        (if (instance? clojure.lang.Delay v)
          @v
          v))
      default-value))
  (assoc [_ k v]
    (LazyMap. (assoc m k v) mta))
  (dissoc [_ k]
     (LazyMap. (dissoc m k) mta))
  (keys [_]
    (keys m))
  (meta [_]
    mta)
  (with-meta [_ mta]
    (LazyMap. m mta)))
seems like you could use this library to do what you want https://github.com/clj-commons/potemkin

emccue18:01:10

just recursively replace all maps with a custom map type

Andrew Carlile19:01:09

hi, I'm having an issue getting a shadow-cljs app to run.

Andrew Carlile19:01:05

here's the stacktrace that prints out when I try to run "npm run":

{:clojure.main/message                                                                                                                                                                                                                        "Execution error (FileNotFoundException) at java.io.FileInputStream/open0 (FileInputStream.java:-2).\n-M (No such file or directory)\n",                                                                                                     :clojure.main/triage                                                                                                                                                                                                                         {:clojure.error/class java.io.FileNotFoundException,                                                                                                                                                                                          :clojure.error/line -2,                                                                                                                                                                                                                      :clojure.error/cause "-M (No such file or directory)",                                                                                                                                                                                       :clojure.error/symbol java.io.FileInputStream/open0,                                                                                                                                                                                         :clojure.error/source "FileInputStream.java",                                                                                                                                                                                                :clojure.error/phase :execution},                                                                                                                                                                                                           :clojure.main/trace                                                                                                                                                                                                                          {:via                                                                                                                                                                                                                                         [{:type java.io.FileNotFoundException,                                                                                                                                                                                                         :message "-M (No such file or directory)",                                                                                                                                                                                                   :at [java.io.FileInputStream open0 "FileInputStream.java" -2]}],                                                                                                                                                                           :trace                                                                                                                                                                                                                                       [[java.io.FileInputStream open0 "FileInputStream.java" -2]                                                                                                                                                                                    [java.io.FileInputStream open "FileInputStream.java" 216]                                                                                                                                                                                    [java.io.FileInputStream <init> "FileInputStream.java" 157]                                                                                                                                                                                  [java.io.FileInputStream <init> "FileInputStream.java" 111]                                                                                                                                                                                  [clojure.lang.Compiler loadFile "Compiler.java" 7575]                                                                                                                                                                                        [clojure.main$load_script invokeStatic "main.clj" 475]                                                                                                                                                                                       [clojure.main$script_opt invokeStatic "main.clj" 535]                                                                                                                                                                                        [clojure.main$script_opt invoke "main.clj" 530]                                                                                                                                                                                              [clojure.main$main invokeStatic "main.clj" 664]                                                                                                                                                                                              [clojure.main$main doInvoke "main.clj" 616]                                                                                                                                                                                                  [clojure.lang.RestFn applyTo "RestFn.java" 137]                                                                                                                                                                                              [clojure.lang.Var applyTo "Var.java" 705]                                                                                                                                                                                                    [clojure.main main "main.java" 40]],                                                                                                                                                                                                        :cause "-M (No such file or directory)"}} 
it looks like the filenotfound in question is related to java itself?

Andrew Carlile19:01:29

here's my shadow-cljs.edn:

{:deps true                                                                                                                                                                                                                                   :builds {:backend {:target :node-script                                                                                                                                                                                                                         :main com.apogenius.backend.dev-server/main                                                                                                                                                                                                  :output-dir "out/backend/"                                                                                                                                                                                                                   :output-to "out/backend/main.js"                                                                                                                                                                                                             :devtools {:before-load-async com.apogenius.backend.dev-server/stop                                                                                                                                                                                     :after-load com.apogenius.backend.dev-server/start}}                                                                                                                                                                                                                                                                                                                                                                                                                 :frontend {:target :browser                                                                                                                                                                                                                             :output-dir "frontend/public/js"                                                                                                                                                                                                             :asset-path "/js"                                                                                                                                                                                                                            :modules {:app {:entries [com.apogenius.frontend.core]}}                                                                                                                                                                                     :devtools {:http-root    "frontend/public"                                                                                                                                                                                                              :http-port    8888                                                                                                                                                                                                                           :reload-strategy :full}}}}  

Andrew Carlile19:01:39

here's package.json :

{                                                                                                                                                                                                                                              "name": "pathom",                                                                                                                                                                                                                            "version": "1.0.0",                                                                                                                                                                                                                          "description": "This project can test both front-end and back-end capabilities. It uses `pathom3` running on `nodejs` (very similar to our production environment).",                                                                        "main": "index.js",                                                                                                                                                                                                                          "scripts": {                                                                                                                                                                                                                                   "start": "npm-run-all --parallel start:*",                                                                                                                                                                                                   "start:repl": "node ./scripts/repl.js",                                                                                                                                                                                                      "start:shadow": "shadow-cljs watch backend frontend",                                                                                                                                                                                        "test": "echo \"Error: no test specified\" && exit 1"                                                                                                                                                                                      },                                                                                                                                                                                                                                           "author": "",                                                                                                                                                                                                                                "license": "ISC",                                                                                                                                                                                                                            "devDependencies": {                                                                                                                                                                                                                           "npm-run-all": "^4.1.5",                                                                                                                                                                                                                     "shadow-cljs": "^2.15.10"                                                                                                                                                                                                                  },                                                                                                                                                                                                                                           "dependencies": {                                                                                                                                                                                                                              "react": "17.0.1",                                                                                                                                                                                                                           "react-dom": "17.0.1"                                                                                                                                                                                                                      }                                                                                                                                                                                                                                          }   

Andrew Carlile19:01:45

and here's deps.edn:

{:deps {applied-science/js-interop {:mvn/version "0.2.7"}                                                                                                                                                                                            bidi/bidi {:mvn/version "2.1.6"}                                                                                                                                                                                                             cider/cider-nrepl {:mvn/version "0.25.5"}                                                                                                                                                                                                    cider/piggieback {:mvn/version "0.5.2"}                                                                                                                                                                                                      cljs-bean/cljs-bean {:mvn/version "1.7.0"}                                                                                                                                                                                                   cljs-ajax/cljs-ajax {:mvn/version "0.8.3"}                                                                                                                                                                                                   com.bhauman/rebel-readline-cljs {:mvn/version "0.1.4"}                                                                                                                                                                                       com.cognitect/transit-cljs {:mvn/version "0.8.269"}                                                                                                                                                                                          com.wsscode/pathom3 {:mvn/version "2021.08.14-alpha"}                                                                                                                                                                                        kibu/pushy {:mvn/version "0.3.8"}                                                                                                                                                                                                            metosin/potpuri {:mvn/version "0.5.3"}                                                                                                                                                                                                       re-frame/re-frame {:mvn/version "0.10.6"}                                                                                                                                                                                                    reagent/reagent {:mvn/version "1.0.0"                                                                                                                                                                                                                         :exclusions [cljsjs/react                                                                                                                                                                                                                                 cljsjs/react-dom                                                                                                                                                                                                                             cljsjs/create-react-class]}                                                                                                                                                                                    thheller/shadow-cljs {:mvn/version "2.15.10"}}                                                                                                                                                                                                                                                                                                                                                                                                                                     :paths ["backend/main/src"                                                                                                                                                                                                                           "frontend/main/src"                                                                                                                                                                                                                          "shared/main/src"]                                                                                                                                                                                                                                                                                                                                                                                                                                                                :aliases {:test                                                                                                                                                                                                                                        {:extra-deps {olical/cljs-test-runner {:mvn/version "3.8.0"}}                                                                                                                                                                                 :main-opts ["--main" "cljs-test-runner.main"                                                                                                                                                                                                             "--dir" "backend/main/test"                                                                                                                                                                                                                  "--dir" "shared/main/test"]                                                                                                                                                                                                      :extra-paths ["backend/main/test"                                                                                                                                                                                                                          "shared/main/test"]}}}   

phronmophobic20:01:10

Yea, as I was saying in the other thread, it looks like stacktrace you're getting is from running clojure or clj rather than npm

phronmophobic20:01:44

how are you running the command?

Andrew Carlile20:01:03

$ java -version                                                                                      openjdk version "17.0.4" 2022-07-19                                                                                     OpenJDK Runtime Environment (build 17.0.4+8-Raspbian-1deb11u1rpt1)                                                      OpenJDK Client VM (build 17.0.4+8-Raspbian-1deb11u1rpt1, mixed mode, emulated-client)    

phronmophobic20:01:15

npm doesn't rely on java

Andrew Carlile20:01:55

$ git --version                                                                                      git version 2.30.2     

Andrew Carlile20:01:10

$ node --version                                                                                     v19.3.0             

Andrew Carlile20:01:27

$ npm --version                                                                                      9.2.0               

phronmophobic20:01:00

oh ok. it looks like there's an extra new line somewhere

Andrew Carlile20:01:36

I could be wrong about the backend

phronmophobic20:01:16

also, I'm not sure it's a great idea to post a company's code challenge publicly.

phronmophobic20:01:22

unless it was already public

Andrew Carlile20:01:57

I've made it private

phronmophobic20:01:11

what version of the clojure command do you have?

phronmophobic20:01:20

clojure --version

Andrew Carlile20:01:25

$ clj --version Clojure CLI version 1.11.1.1208

Andrew Carlile20:01:32

$ clojure --version Execution error (FileNotFoundException) at http://java.io.FileInputStream/open0 (FileInputStream.java:-2). --version (No such file or directory) Full report at: /tmp/clojure-17190885051890988348.edn

Andrew Carlile20:01:48

also, I am apogenius

Andrew Carlile20:01:37

when I follow /tmp/clojure-17190885051890988348.edn the stacktrace looks just like the one I get for trying to run npm start

Andrew Carlile20:01:15

weird though it's running now. could have been a version issue with clojure

seancorfield21:01:38

If clojure --version is giving you a file not found error then it is not the correct script (although clj seems to be) -- so I suspect you have an old, unofficial clojure command installed on your system which is going to mess a lot of things up.

seancorfield21:01:23

What is the output of which clj and which clojure @U03HD1DNGDP?

Andrew Carlile05:01:43

thanks @U04V70XH6 fixed it up with a new install of clojure

2
Andrew Carlile19:01:10

here's the stacktrace that prints out when I try to run "npm run":

{:clojure.main/message                                                                                                                                                                                                                        "Execution error (FileNotFoundException) at java.io.FileInputStream/open0 (FileInputStream.java:-2).\n-M (No such file or directory)\n",                                                                                                     :clojure.main/triage                                                                                                                                                                                                                         {:clojure.error/class java.io.FileNotFoundException,                                                                                                                                                                                          :clojure.error/line -2,                                                                                                                                                                                                                      :clojure.error/cause "-M (No such file or directory)",                                                                                                                                                                                       :clojure.error/symbol java.io.FileInputStream/open0,                                                                                                                                                                                         :clojure.error/source "FileInputStream.java",                                                                                                                                                                                                :clojure.error/phase :execution},                                                                                                                                                                                                           :clojure.main/trace                                                                                                                                                                                                                          {:via                                                                                                                                                                                                                                         [{:type java.io.FileNotFoundException,                                                                                                                                                                                                         :message "-M (No such file or directory)",                                                                                                                                                                                                   :at [java.io.FileInputStream open0 "FileInputStream.java" -2]}],                                                                                                                                                                           :trace                                                                                                                                                                                                                                       [[java.io.FileInputStream open0 "FileInputStream.java" -2]                                                                                                                                                                                    [java.io.FileInputStream open "FileInputStream.java" 216]                                                                                                                                                                                    [java.io.FileInputStream <init> "FileInputStream.java" 157]                                                                                                                                                                                  [java.io.FileInputStream <init> "FileInputStream.java" 111]                                                                                                                                                                                  [clojure.lang.Compiler loadFile "Compiler.java" 7575]                                                                                                                                                                                        [clojure.main$load_script invokeStatic "main.clj" 475]                                                                                                                                                                                       [clojure.main$script_opt invokeStatic "main.clj" 535]                                                                                                                                                                                        [clojure.main$script_opt invoke "main.clj" 530]                                                                                                                                                                                              [clojure.main$main invokeStatic "main.clj" 664]                                                                                                                                                                                              [clojure.main$main doInvoke "main.clj" 616]                                                                                                                                                                                                  [clojure.lang.RestFn applyTo "RestFn.java" 137]                                                                                                                                                                                              [clojure.lang.Var applyTo "Var.java" 705]                                                                                                                                                                                                    [clojure.main main "main.java" 40]],                                                                                                                                                                                                        :cause "-M (No such file or directory)"}} 
it looks like the filenotfound in question is related to java itself?

phronmophobic19:01:30

Where are you typing "npm run"? This output looks like the output from running the clojure or clj commands.

phronmophobic19:01:48

As a side note, if you have a long question, it can be helpful to summarize in a top-level message and post the details in a thread 🧵 .

Andrew Carlile21:01:31

I have a shadow-cljs app that somehow is running out of java heap space when I try to run it using npm start . can I increase the heap size in my shadow-cljs.edn?

noonian21:01:59

You should look at shadow-cljs.edn to see if it is using deps.edn or keeping all config in shadow-cljs.edn Either way you can probably fix the issue by adding :jvm-opts but where it goes will depend on the shadow-cljs config. https://shadow-cljs.github.io/docs/UsersGuide.html#jvm-opts I had a similar issue where it was blowing the stack size and adding :jvm-opts ["-Xss2m"] fixed my issue. For the heap size I believe the flags are Xms and Xmx https://alvinalexander.com/blog/post/java/java-xmx-xms-memory-heap-size-control/

kennytilton10:01:43

Meta-thought: support is pretty decent over at #C6N245JGG. hth.