Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data prints as expected but cannot be access as expected? #89

Open
jaidetree opened this issue Mar 14, 2022 · 2 comments
Open

Data prints as expected but cannot be access as expected? #89

jaidetree opened this issue Mar 14, 2022 · 2 comments

Comments

@jaidetree
Copy link

jaidetree commented Mar 14, 2022

I'm processing a data structure that is an object with a list of results containing maps of properties describing a row in a data table.

The names of the properties may contain spaces so using cljs-bean's bean function with a custom key fn to process it, lowercase the keys, and replace any spaces with dashes (-).

Test Data:
[{:id 1,
  :properties
  {:uid
   {:type "formula", :formula {:type "string", :string "abcdefg"}}}}
 {:id 2,
  :properties
  {:uid
   {:type "formula", :formula {:type "string", :string "hijklmn"}}}}
 {:id 3,
  :properties
  {:uid
   {:type "formula", :formula {:type "string", :string "opqrstu"}}}}]

Test:
(map #(get-in % [:properties :uid :formula :string]) beaned)

Expected:
 (abcdefg hijklmn opqrstu) 

Actual:
 (nil nil nil)

To make it easier to reproduce, here's a replit with this exact problem recreated with the above test data.

https://replit.com/@eccentric-j/nbb-cljs-bean-oddity-repro#src/cli/core.cljs

Just run it to show the data, the expected, and the actual data which should match the above. It will then enter a repl from which the data may be inspected, or you may fork it to directly edit the files.

I don't really understand why it prints the way I would expect, but processing it shows a different result.

If you run:

(map #(get-in % [:properties :UID :formula :string]) beaned)

That does work, returning the expected list value but it seems to be ignoring the call to bean with a specific prop->key fn to lower-case the keys. It does not match what gets printed.

@p-himik
Copy link

p-himik commented Mar 14, 2022

As far as I can tell, prop->key is used when creating derived values and key->prop is used when pre-processing keys for field lookup. Your prop->key turns "UID" into :uid, but your key->prop does not turn :uid into "UID", so cljs-bean ends up looking for a field named uid.

@jaidetree
Copy link
Author

jaidetree commented Mar 14, 2022

I think you're right, for whatever reason the role of prop->key was not jumping out at me.

Was able to get it working with:

(defn key->prop
  [x]
  (cond
    (= x :uid) "UID"
    
    (and (keyword? x) (s/includes? (name x) "-"))
    (-> (s/split (name x) #"-")
      (->> 
       (map s/capitalize)
       (s/join " "))
      (s/replace #"Url" "URL"))

    (keyword? x) (name x)
    
    :else x)
  )

While I'm glad to have a better understanding of this library now, I'm not confident in the approach given that the prop->key is not always going to be able to follow specific rules and will require a lot of overrides as use-cases come up. Plus I'm not going back to js at all with this data so it may not be fully necessary.

p-himik shared an example of processing it manually in Clojurians Slack which seems to work well for my purposes:

(defn my-js->clj [v key-fn]
  (cond
    (object? v) (into {}
                      (map (fn [[k v]]
                             [(key-fn k) (my-js->clj v key-fn)]))
                      (js/Object.entries v))
    (array? v) (mapv #(my-js->clj % key-fn) v)
    :else v))

@jaidetree jaidetree changed the title [BUG]: Data prints as expected but cannot be access as expected? Data prints as expected but cannot be access as expected? Mar 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants