pinterest / elixir-thrift

A Pure Elixir Thrift Implementation
https://hexdocs.pm/thrift/
Apache License 2.0
214 stars 44 forks source link

RFC Generate a prettyprinter for structs #203

Open dantswain opened 7 years ago

dantswain commented 7 years ago

This is a little off-the-walll, but in the Ruby thrift lib, enum values get printed out as their name, which can be pretty nice for debugging purposes. We could theoretically generate a defimpl Inspect for structs and have it do this. For the most part I think this would just hijack the normal struct implementation but it could replace the enum entries with something like user_status: 1 (UserStatus.active).

OTOH it could be not worth the complexity. Something opt-in might be a compromise? I.e., the generated module could include a prettyprint function or something and then we provide docs showing how you could roll your own defimpl around that.

jparise commented 7 years ago

I like this idea and spent a little time looking into a possible implementation. It unfortunately doesn't appear to be an easy thing to build, however. Because structs are just maps, the only want to determine which fields contain enums is based on matching against their names, and hooking into the Inspect protocol at that level appears to involve re-implementing the full struct/map Inspect.inspect/2 code path.

I think that could be done somewhat generically and reused for each of our generated structs, but it's still a good amount of complexity.

I think that work is roughly equivalent to rolling our own pretty printing function, so I'm not sure there's a benefit to not using Inspect here, unless the algebraic nature of Inspect gets in our way.