Skip to content

Commit

Permalink
veqryn/marshal-json: also convert byte slices to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
veqryn committed Apr 12, 2024
1 parent 6a6ff19 commit d749f5e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 4 additions & 1 deletion v2/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (s sanitizer) Sanitize(data interface{}) interface{} {
}
}

// Handle certain well known interfaces and types
// Handle certain well known interfaces and types, in preferred order
switch dataT := data.(type) {
case error:
return dataT.Error()
Expand All @@ -120,6 +120,9 @@ func (s sanitizer) Sanitize(data interface{}) interface{} {
if b, err := dataT.MarshalJSON(); err == nil {
return string(b)
}

case []byte:
return string(dataT)
}

switch t.Kind() {
Expand Down
6 changes: 4 additions & 2 deletions v2/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ func TestMetaDataSanitize(t *testing.T) {
"time": time.Date(2023, 12, 5, 23, 59, 59, 123456789, time.UTC),
"duration": 105567462 * time.Millisecond,
"text": _textMarshaller{},
"json": json.RawMessage(`{"hello": "world"}`),
"json": json.RawMessage(`{"json_property": "json_value"}`),
"bytes": []byte(`lots of bytes`),
"array": []hash{{
"creditcard": "1234567812345678",
"broken": broken,
Expand All @@ -242,7 +243,8 @@ func TestMetaDataSanitize(t *testing.T) {
"time": "2023-12-05T23:59:59.123456789Z",
"duration": "29h19m27.462s",
"text": "marshalled text",
"json": `{"hello": "world"}`,
"json": `{"json_property": "json_value"}`,
"bytes": "lots of bytes",
"array": []interface{}{map[string]interface{}{
"creditcard": "[FILTERED]",
"broken": map[string]interface{}{
Expand Down

0 comments on commit d749f5e

Please sign in to comment.