Skip to content

Commit

Permalink
If logging as JSON and the type is json.RawMessage log it "as-is" (#147)
Browse files Browse the repository at this point in the history
If we're already producing JSON and a sub message field is json.RawMessage (i.e. []byte) then emit it as-is.

Otherwise you get [X,Y,Z,..] style which is pretty useless in log in that form.
  • Loading branch information
sfc-gh-jchacon committed Nov 10, 2022
1 parent a49c971 commit 8f2e557
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
15 changes: 15 additions & 0 deletions funcr/funcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ package funcr
import (
"bytes"
"encoding"
"encoding/json"
"fmt"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -500,6 +501,20 @@ func (f Formatter) prettyWithFlags(value interface{}, flags uint32, depth int) s
}
return buf.String()
case reflect.Slice, reflect.Array:
// If this is outputing as JSON make sure this isn't really a json.RawMessage.
// If so just emit "as-is" and don't pretty it as that will just print
// 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 {
// 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.WriteString("null")
}
return buf.String()
}
}
buf.WriteByte('[')
for i := 0; i < v.Len(); i++ {
if i > 0 {
Expand Down
28 changes: 28 additions & 0 deletions funcr/funcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ type Tembedjsontags struct {
Tinner6 `json:"inner6,omitempty"`
}

type Trawjson struct {
Message json.RawMessage `json:"message"`
}

func TestPretty(t *testing.T) {
// used below
newStr := func(s string) *string {
Expand Down Expand Up @@ -669,6 +673,15 @@ func makeKV(args ...interface{}) []interface{} {
}

func TestRender(t *testing.T) {
// used below
raw := &Trawjson{}
marshal := &TjsontagsInt{}
var err error
raw.Message, err = json.Marshal(marshal)
if err != nil {
t.Fatalf("json.Marshal error: %v", err)
}

testCases := []struct {
name string
builtins []interface{}
Expand Down Expand Up @@ -738,6 +751,21 @@ func TestRender(t *testing.T) {
}{"arg", 789}, "val"),
expectKV: `"<non-string-key: {"F1":"builtin",>"="val" "<non-string-key: {\"F1\":\"value\",\"F>"="val" "<non-string-key: {\"F1\":\"arg\",\"F2\">"="val"`,
expectJSON: `{"<non-string-key: {"F1":"builtin",>":"val","<non-string-key: {\"F1\":\"value\",\"F>":"val","<non-string-key: {\"F1\":\"arg\",\"F2\">":"val"}`,
}, {
name: "json rendering with json.RawMessage",
args: makeKV("key", raw),
expectKV: `"key"={"message":[123,34,105,110,116,49,34,58,48,44,34,45,34,58,48,44,34,73,110,116,53,34,58,48,125]}`,
expectJSON: `{"key":{"message":{"int1":0,"-":0,"Int5":0}}}`,
}, {
name: "byte array not json.RawMessage",
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":null}}`,
}}

for _, tc := range testCases {
Expand Down

0 comments on commit 8f2e557

Please sign in to comment.