Prettify arrays in the console output #7233
Totktonada
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First, two examples.
Examples
Space format.
Crud select metadata.
What is going on? Why it looks so messy?
Without
array_mt
Those Lua tables are decoded from msgpack. The msgpack decoder adds a metatable to each table:
{__serialize = 'seq'}
for arrays and{__serialize = 'map'}
for maps1 (seemsgpack.cfg({decode_save_metatables = <boolean>})
).How the examples above would look without the metatable on the array?
Space format.
Crud select metadata.
So the obvious solution would be don't add those metatables. The drawback here is that in this case we can loss information whether the Lua table should be interpreted as an array or a map. It may be crucial at encoding to msgpack back.
(The similar idea is to take knowledge how our serializers work and use it in deserializers: add the metatables only when the Lua table would be interpreted differently without it. Possibly would require more CPU cycles in deserializers. Possibly would save some CPU cycles on access to fields/elements of the resulting tables.)
However it is interesting how the examples would look without the metatables at all (both for arrays and for maps).
Without
array_mt
andmap_mt
Space format.
Crud select metadata.
seq
vssequence
We can look at this from another angle. I mentioned two
__serialize
values:'seq'
and'map'
. There are four ones in fact (plus one alias).src/lua/serializer.h#L122-L132:
The YAML serializer may be instructed, whether one-line or block serialization should be used. Considering the examples above we can judge that
msgpack.array_mt.__serialize = 'sequence'
would make things nice (the output would be the same as withoutarray_mt
).Let's consider the example with a mapping at the outmost level instead of an array.
Here the best result would be with
msgpack.map_mt.__serialize = 'mapping'
.Conclusion
In my understanding, it is the deal of YAML serializer how to present data in the most readable way. I guess a nesting level plus an amount of elements would give a good heuristic. It worth to check what other formatters do.
Summary:
seq
/sequence
(and itsarray
alias) andmap
/mapping
in the YAML serializer and do the best based on source data.msgpack.array_mt.__serialize = 'sequence'
would make things better at the first glance.(It is not an in-depth investigation, just brief glance. I can miss details about existing YAML serializer heuristics and
seq
/sequence
plusmap
/mapping
interpretation. I doubt that manual setting of one-line/block formatting makes any sense, but I can miss places, where it does.)Footnotes
And
__newindex
, which drops the metatable if a new element/field is added. The tables can be accessed asmsgpack.array_mt
andmsgpack.map_mt
2. ↩I doubt that it is documented API, don't lean on it in a modules/application. ↩
Beta Was this translation helpful? Give feedback.
All reactions