fix(proto): implement fmt.Formatter for Field and Value #521
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are two objectives that this PR solves by implementing fmt.Formatter for
proto.Field
andproto.Value
.Reduce gotcha when printing
proto.Value
, asproto.Value
has the same implementation of fmt.Stringer, but we treat it as a special case method to retrive string value, rather than the underlying value formatted as a string. Example:And thing gets trickier when we print a type containing
proto.Value
such asproto.Field
or maybe if we print the wholeproto.FIT
itself, as it will triggerfmt
to call(proto.Value) String() string
method. Related issue: Totals not complete, most of the unknown fields are missing #520Now we can just do:
Enable more verbose printing format for
proto.Field
for debugging purposes. Sinceproto.FieldBase
is embedded inproto.Field
as a pointer,fmt
prints the pointer address toproto.FieldBase
instead of its value. In general, we need to know the description of the field such as field number, field name, type etc when printingproto.Field
. Now, we have it:We keep the pointer address in the formatted string in case it matters.