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
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
33 changes: 33 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,26 @@ 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":{}}}`,
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