fix: print user keys that shadow reserved key names as regular fields#208
Open
troclaux wants to merge 1 commit into
Open
fix: print user keys that shadow reserved key names as regular fields#208troclaux wants to merge 1 commit into
troclaux wants to merge 1 commit into
Conversation
443b191 to
d4c5671
Compare
When a user-supplied key has the same string value as a built-in key (TimestampKey/"time", LevelKey/"level", CallerKey/"caller", PrefixKey/"prefix") but carries a value of a different type, the text and JSON formatters silently dropped the key-value pair because the type assertion failed with no fallback. The logfmt formatter already handled this correctly by falling through to EncodeKeyval. This commit applies the same principle to the text and JSON formatters: - text.go: extracts the default KV-writing logic into writeKVPair and calls it as the else branch for each affected special-key case. - json.go: adds an else branch calling the existing jsonFormatterItem helper for each affected special-key case. Fixes: gum issue charmbracelet/gum#967
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
When using
gum log --structured, passing a key namedtimecauses that key-value pair to be silently dropped in the text and JSON formatters.Reproduction (from charmbracelet/gum#967):
The same silent-drop affects any user key whose name matches a built-in key (
time,level,caller,prefix) when the value's type does not match the expected built-in type.Root cause
In
textFormatterandjsonFormatterRoot, each built-in key case does a type assertion and renders the value in a special style. When the assertion fails, the code falls through with no output. ThelogfmtFormatteralready handles this correctly by always encoding the key-value regardless of the type assertion result.Fix
writeKVPairhelper and calls it as theelsebranch forTimestampKey,LevelKey,CallerKey, andPrefixKey.elsebranch calling the existingjsonFormatterItemhelper for the same set of keys.TestReservedKeyNonBuiltinValueto cover the reported scenario.MessageKeyis intentionally left unchanged — a user passingmsgas a structured key would produce a duplicate message field, which is a separate discussion.