Fork me on GitHub
#meander
<
2023-01-26
>
Roma14:01:03

Hello, I'm learning meander and trying to migrate my existing clojure code for some API client to it. So far it goes pretty good, but I have one use case I'd like to implement and I'm not sure how to do that. The input looks like the following:

{:etag      "doesn't matter"
 :uuid      "E6FDD9A4-3820-11ED-A2D1-70110F7B78B9"
 :called_on "2022-09-19 13:42:28.394228+00"
 :entity
 {:prop
  [{:name "advertiser_id" :value "1"}
   {:name "concept_id" :value "2"}
   {:name "name" :value "Some name"}
   {:name "width" :value "320"}
   {:name "height" :value "50"}
   {:name "file_type" :value "jpg"}]
  :version 3
  :type    "some type"
  :name    "Some name"
  :id      5
  :entity
  [{:id   6
    :prop
    [{:name "atomic_creative_id" :value "9"}
     {:name "is_https" :value "YES"}
     {:name "url" :value ""}]
    :rel  "atomic_creative_detected_tag_urls"
    :type "atomic_creative_detected_tag_url"
    :name "Atomic Creative Detected Tag Urls 9"}
   {:id   7
    :prop
    [{:name "atomic_creative_id" :value "10"}
     {:name "is_https" :value "YES"}
     {:name "url" :value ""}
     {:name "vendor_domain_id" :value "641"}]
    :rel  "atomic_creative_detected_tag_urls"
    :type "atomic_creative_detected_tag_url"
    :name "Atomic Creative Detected Tag Urls 10"}
   {:id   8
    :prop
    [{:name "atomic_creative_id" :value "11"}
     {:name "is_https" :value "YES"}
     {:name "url" :value ""}
     {:name "vendor_domain_id" :value "812"}]
    :rel  "atomic_creative_detected_tag_urls"
    :type "atomic_creative_detected_tag_url"
    :name "Atomic Creative Detected Tag Urls 11"}]}
 :status    {:code "ok"}}
I need to fetch url value for :prop where vendor_domain_id value is 812. So the correct result would be "" . Not sure what I should use, I'm trying rewrite :
(m/rewrite input
  {:entity {:entity [!entities ...]}}
  [(m/cata !entities) ...]

  {:prop [!props ...]}
  [(m/cata !props) ...]

  {:name ?name :value ?value}
  [(m/keyword ?name) ?value])
But I guess I need to combine it with pred or gather? Is it good use case for meander or maybe I should rather stick with pure clojure maps and filters? Thank you in advance.

Richie03:01:58

At a glance it seems like a fine use of meander. I’ll check it tomorrow when I get time.

Richie15:01:43

This has shown me that I don't remember much about meander. I really struggled. Anyways, here's one way.

(-> input
    (m/rewrite
      {:entity {:entity [{:prop [{:name !name :value !value} ..!n]} ...]}}
      [{& ([!name !value] ..!n)} ...])
    (m/match
      (m/gather {"vendor_domain_id" "812"
                 "url" !url})
      !url))
;; [""]

Roma10:01:18

Thank you! That's very helpful. I was trying to achieve it with a single m/rewrite call and didn't think about combining rewrite with match. Also ..!n syntax is something new to me.

👍 2
Richie15:01:39

Awesome; I'm glad that I could help.