Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If logging as JSON and the type is json.RawMessage log it "as-is" #147

Merged
merged 7 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test for empty array so in JSON mode emit {} instead of []
  • Loading branch information
sfc-gh-jchacon committed Aug 2, 2022
commit 01ef30de308edd3de5462f221b41c123280a4d0f
8 changes: 7 additions & 1 deletion funcr/funcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,13 @@ func (f Formatter) prettyWithFlags(value interface{}, flags uint32, depth int) s
// it as [X,Y,Z,...] which isn't terribly useful vs the string form you really want.
if f.outputFormat == outputJSON {
if rm, ok := value.(json.RawMessage); ok {
buf.Write(rm)
// If it's empty make sure we emit an empty value as the array style would below.
if len(rm) > 0 {
buf.Write(rm)
} else {
buf.WriteByte('{')
sfc-gh-jchacon marked this conversation as resolved.
Show resolved Hide resolved
buf.WriteByte('}')
}
return buf.String()
}
}
Expand Down
10 changes: 10 additions & 0 deletions funcr/funcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,16 @@ func TestRender(t *testing.T) {
args: makeKV("key", []byte{1, 2, 3, 4}),
expectKV: `"key"=[1,2,3,4]`,
expectJSON: `{"key":[1,2,3,4]}`,
}, {
name: "json rendering with empty json.RawMessage",
args: makeKV("key", &Trawjson{}),
expectKV: `"key"={"message":[]}`,
expectJSON: `{"key":{"message":{}}}`,
sfc-gh-jchacon marked this conversation as resolved.
Show resolved Hide resolved
}, {
name: "byte array not json.RawMessage",
sfc-gh-jchacon marked this conversation as resolved.
Show resolved Hide resolved
args: makeKV("key", []byte{1, 2, 3, 4}),
expectKV: `"key"=[1,2,3,4]`,
expectJSON: `{"key":[1,2,3,4]}`,
}}

for _, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/sfc-gh-jchacon/logr
module github.com/go-logr/logr

go 1.16