noprompt / meander

Tools for transparent data transformation
MIT License
918 stars 55 forks source link

Optional keys in the result #242

Open fhalde opened 1 year ago

fhalde commented 1 year ago

Is there a better way to achieve the following?

e..g assume I will get the following kinds of inputs

{:name "foo" :address {:postal 1111}}
{:name "bar" :address {:postal nil}}

and I want to transform it into the following map

#1 {:Name "foo" :postal 1111}
#2 {:Name "bar"}

i.e i don't want the key :postal to even be present if the value of it is going to be nil

(m/match {:name "faiz" :addr {:postal nil}}
         {:name ?name :addr {:postal ?post}}
         (merge
           {:Name ?name}
           (when ?post
             {:postal ?post})))

this was my initial attempt but the fact that i am writing some logic ( the how ) defeats the purpose of a declarative matcher such as meander