k0kubun/pp

Feature request: omitempty

nothingmuch opened this issue · 8 comments

For sparse data structures omission of uninitialized members by default would help with readability.

Please give me more details. You mean []byte{0x0, 0x0, ...} or something?

Since we have #12, I guess you print empty buffers whose size is less than 1024.

I couldn't understand "uninitialized members". All members may be initialized with zero.

Something like:

type Foo struct { Foo, Bar, Baz string }

pp.Println(Foo{Bar: "some value"}) // suppress Foo: "", Baz: "" from output

How do you distinguish uninitialized "" and explicitly-set ""? It's the same value and at least I want to show the latter value. In addition, I think omitting a member is misleading since it is shown like the member does not exist.

I don't propose making any distinction between unset and cleared, just a configurable parameter you can enable. Whether it's helpful or harmful for readability depends on the use case.

(I'm using pp to read through dumps of structs which are rather big but sparsely populated, so there's a lot of noise surrounding the actual data)

That makes sense. Since we have already configurable parameter for colors, it may be good to have such a configuration.

Here's an implementation. Default mode is omitempty:
https://github.com/k0kubun/pp/pull/30/files

I believe this has been implemented in #49 too.