tomland icon indicating copy to clipboard operation
tomland copied to clipboard

Better ways of custom key sorting

Open chshersh opened this issue 5 years ago • 5 comments

See the discussion here:

  • https://github.com/kowainik/tomland/pull/192#discussion_r291416727

It would be nice to implement several utility functions (if possible) to help writing custom sorters for keys. If not, at least the example can be put into the documentation.

chshersh avatar Jun 10 '19 04:06 chshersh

Thanks for logging this, and excuse the silence - life and work caught up with me :)

I have a couple of thoughts on key sorting:

  • If I were to want utility functions, they'd allow me to pass a list of keys to the pretty-printer and come back with TOML that uses the listed key ordering, doing something reasonable with the keys not on the list.
  • One thing we didn't grapple with in the previous issue/PR is that our key sorting strategy puts all of the keys in a single namespace. If key names are re-used in a nested data structure, there are pretty-printings that we can't express with our new printOptionsSorting. I didn't dwell on this concern too much, because I think what we implemented will be adequately flexible for many realistic use-cases. But I'm sure someone will come up with a byzantine "enterprise" data structure that can't be pretty-printed they way they want it to be. Personally, I'd be happy with some note of this limitation in the documentation, but I'd be interested in your thoughts.

ramanshah avatar Jun 28 '19 01:06 ramanshah

@ramanshah I think your first idea can be addressed. We can have a data type like this:

data Order = Order [Key] PutRest
data PutRest = Beginning | End

But this seems to ad-hoc, and the design space is too broad:

  1. Should we sort the rest keys not in list lexicographically?
  2. How can we specify custom sorting for the rest of the keys?
  3. It's just a solution for a particular problem, doesn't look general enough.

So I think that we are in the area of experiments and I would prefer to play with it in an application code first and if I find it general and useful enough, it can be brought into the library. But at this point, it looks more like an idea for a separate package like sorting-orderings or something like that.

But I'm sure someone will come up with a byzantine "enterprise" data structure that can't be pretty-printed they way they want it to be.

I don't have an elegant solution for namespace-and-context-sensitive sorting at the moment. A simple solution would be to pass a name of the closest table to the sorting function, but since the use-case is very rare and it will make the UX worse for all users, let's think about it only when somebody has this use-case.

chshersh avatar Jun 28 '19 02:06 chshersh

I agree on both points - I'm actually behind on bringing this feature into use in my own use-case :)

In the meantime, should I get an example into the documentation so it's clear how to make tomland sort keys? Where would you like this documentation to reside?

ramanshah avatar Jun 30 '19 20:06 ramanshah

@ramanshah I think that your example with products under the previous PR is excellent. It would be nice to have it in the library documentation! I think you can add this example (and couple other words regarding how to disable sorting, how to have the default sorting and how to have custom sorting on your example) to the documentation for the PrintOptions data type. You can even use the syntax for the expandable sections in Haddock for that, it would be helpful.

chshersh avatar Jul 01 '19 11:07 chshersh

Perfect - I'll work on this, and we'll talk once I'm ready to open a PR :)

ramanshah avatar Jul 01 '19 21:07 ramanshah