expound

borkdude 2018-10-18T17:50:27.000200Z

hi

bbrinck 2018-10-18T17:58:09.000200Z

hi!

bbrinck 2018-10-18T19:49:37.000100Z

@borkdude I haven’t had a use case for a transform function before, so I’m a hesitant to add a callback w/o understanding the use case more

bbrinck 2018-10-18T19:49:59.000100Z

I’m curious about why transforming the value is better than changing the printer

borkdude 2018-10-18T19:50:00.000100Z

the use case is printing only a part of the data, not all

borkdude 2018-10-18T19:50:18.000100Z

because I’m happy with how things are printed, it’s just too much

bbrinck 2018-10-18T19:50:39.000100Z

the issue is the map is too large, even when most of it is omitted?

bbrinck 2018-10-18T19:50:53.000100Z

(i.e. printed with ... placeholders)

borkdude 2018-10-18T19:51:44.000100Z

does it do that now?

borkdude 2018-10-18T19:52:11.000100Z

I’ll test

bbrinck 2018-10-18T19:52:25.000100Z

It should do that now e.g. https://github.com/bhb/expound#expound-1

bbrinck 2018-10-18T19:52:34.000100Z

(the example with city/state)

bbrinck 2018-10-18T19:54:00.000100Z

it’s entirely possible that the specific spec+data is not working w/ this feature, which would be good to know

borkdude 2018-10-18T19:54:31.000100Z

I still get a couple of tens of lines from my map that isn’t that relevant

borkdude 2018-10-18T19:54:39.000100Z

(being the system map from component)

bbrinck 2018-10-18T19:56:06.000100Z

ah, this is failing on a keys spec, is that correct?

borkdude 2018-10-18T19:56:11.000100Z

yup

bbrinck 2018-10-18T19:56:34.000100Z

gotcha, yeah, I’ll file an issue but it’s possible to print the summary map in that case

bbrinck 2018-10-18T19:56:38.000100Z

I’ll think about it more

bbrinck 2018-10-18T19:57:04.000100Z

I’m not sure a transform function would be a general solution though - what’s the strategy for picking which part of the map to remove?

bbrinck 2018-10-18T19:57:32.000100Z

It seems like it’d be hard to write a general transformer that doesn’t leave out necessary data in some cases

borkdude 2018-10-18T19:58:32.000100Z

the strategy is up to the user in the same way the the string function is up to the user

borkdude 2018-10-18T19:58:42.000100Z

but if you could explain me how to do the summary thing, that might work for me

bbrinck 2018-10-18T19:59:19.000100Z

Sorry, I was unclear - what’s the strategy in your case? If a map is a component map, which keys would you remove?

borkdude 2018-10-18T19:59:43.000100Z

in my case the strategy would be to just print the keys

bbrinck 2018-10-18T19:59:47.000100Z

Oh sorry, I misspoke: ” it’s possible to print the summary map in that case” - I meant it’s possible expound could do this

bbrinck 2018-10-18T19:59:57.000200Z

🙂

bbrinck 2018-10-18T20:00:35.000100Z

If you want to print the keys, would the custom printer work? I guess I’m wondering how you’d use the transform function in this case

borkdude 2018-10-18T20:00:46.000100Z

(as I do now in the custom printing function, but I lose expound’s nice ability to highlight stuff etc)

borkdude 2018-10-18T20:01:12.000100Z

I would do :transform keys

borkdude 2018-10-18T20:01:23.000100Z

or no

borkdude 2018-10-18T20:01:33.000100Z

(if (map? v) (keys v) v)

bbrinck 2018-10-18T20:01:34.000100Z

Ah, I think that’s going to get confusing though - now the data that is shown to fail is not the same thing

bbrinck 2018-10-18T20:01:42.000200Z

as what you actually entered

bbrinck 2018-10-18T20:01:52.000100Z

so, for instance, the highlighter is going to look really weird

bbrinck 2018-10-18T20:02:10.000100Z

but maybe that’s OK in your use case 🙂

borkdude 2018-10-18T20:02:26.000100Z

ah, I understand. so falling back on the default printing function is the easier way out then

borkdude 2018-10-18T20:02:36.000100Z

if it were easily accessible 🙂

bbrinck 2018-10-18T20:02:37.000100Z

In any case, I think you can try this out today, although with some work, because you can modify the explain-data if you wanted

borkdude 2018-10-18T20:05:28.000100Z

my custom string function now looks like this:

(defn my-value-str [_spec-name form path value]
  (if (and (map? value)
           (every? (fn [ns]
                     (when ns
                       (str/starts-with? ns "<http://dre.app|dre.app>")))
                   (map namespace (keys value))))
    (str (keys value))
    (str value)))

bbrinck 2018-10-18T20:06:45.000100Z

e.g.

(set! s/*explain-out* 
  (fn [explain-data] 
    (let [new-explain-data (replace-map-with-keys explain-data)]
      ((expound/custom-printer {,,,}) new-explain-data)))

bbrinck 2018-10-18T20:06:59.000100Z

(untested, and granted it’s not very pretty)

borkdude 2018-10-18T20:07:48.000100Z

interesting

bbrinck 2018-10-18T20:09:19.000100Z

you should be able to do something like this (untested)

(def default-value-str #'expound/value-in-context)

(defn my-value-str [_spec-name form path value]
  (if (and (map? value)
           (every? (fn [ns]
                     (when ns
                       (str/starts-with? ns "<http://dre.app|dre.app>")))
                   (map namespace (keys value))))
    (str (keys value))
    (default-value-str my-printer-opts spec-name form path value)))

bbrinck 2018-10-18T20:09:26.000100Z

untested, and also not super pretty 😞

borkdude 2018-10-18T20:10:13.000100Z

k, I’ll play a little with this, thanks 🙂

bbrinck 2018-10-18T20:11:49.000200Z

sounds good, I think the second one is probably going to be less work overall, but I could be mistaken

bbrinck 2018-10-18T20:12:34.000100Z

in any case, I agree with you that this is painful, I’ll think more about how to best allow users to control this while allowing them to fallback in a sensible way

borkdude 2018-10-18T20:31:59.000100Z

This worked:

(declare my-expound-value-str)

(def expound-opts
  {:show-valid-values? false
   :value-str-fn #'my-expound-value-str
   :print-specs? true})

(defn my-expound-value-str
  [_spec-name form path value]
  (if (and (map? value)
           (every? (fn [ns]
                     (when ns
                       (str/starts-with? ns "<http://dre.app|dre.app>")))
                   (map (fn [k]
                          (and (keyword? k)
                               (namespace k)))
                        (keys value))))
    (str (keys value))
    (#'expound/value-in-context expound-opts _spec-name form path value)))

(defn my-explain-out
  [explain-data]
  ((expound/custom-printer
    expound-opts) explain-data))

(defn activate-specs
  "Check spec asserts and fdefs"
  []
  (alter-var-root #'s/*explain-out*
                  (constantly #'my-explain-out))
  (s/check-asserts true)
  (st/instrument))

bbrinck 2018-10-18T20:34:33.000100Z

Nice work! 💯

borkdude 2018-10-18T20:34:53.000100Z

It’s mostly your work, so thank you 🙂

bbrinck 2018-10-18T20:34:53.000200Z

Can you remove my-explain-out and just incline the custom-printer in this case?

borkdude 2018-10-18T20:35:08.000100Z

yeah I can, but it gives options 😉

bbrinck 2018-10-18T20:35:13.000100Z

good point 😉

borkdude 2018-10-18T20:35:34.000100Z

you were right that editing explain-data was a bit more work, so I didn’t do it now. I could walk the tree

borkdude 2018-10-18T20:35:47.000100Z

I guess I could do the same with the thrown exception

bbrinck 2018-10-18T20:36:34.000100Z

the trick with the thrown exception is that it has already used expound to create the string, so you’d have to do string replacement, which might be unreliable

borkdude 2018-10-18T20:37:04.000100Z

yes, I would walk both trees. so explain-data before it gets to expound and the one packaged in the ex-info

bbrinck 2018-10-18T20:37:15.000100Z

ah, right

bbrinck 2018-10-18T20:38:01.000100Z

yeah, could do. in principle you can modify explain-data but you may run into weird expound errors if the explain-data isn’t consistent. I haven’t tried it though

borkdude 2018-10-18T20:38:38.000200Z

ah right

bbrinck 2018-10-18T20:38:58.000100Z

expound doesn’t do anything to protect itself against invalid explain-data. in principle, it could use a spec for explain-data, but I haven’t gotten around to writing one, since it’s not easy 🙂

borkdude 2018-10-18T20:39:16.000100Z

meh, I think it’s good now. thanks a lot

bbrinck 2018-10-18T20:42:05.000100Z

np, thanks for the helpful questions and feedback!