Skip to content

GODRIVER-946: Add IndentExtJSON and MarshalExtJSONIndent functions #781

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

Merged
merged 9 commits into from
Nov 2, 2021
Merged
22 changes: 22 additions & 0 deletions bson/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
package bson

import (
"bytes"
"encoding/json"

"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/bson/bsontype"
Expand Down Expand Up @@ -221,3 +224,22 @@ func MarshalExtJSONAppendWithContext(ec bsoncodec.EncodeContext, dst []byte, val

return *sw, nil
}

func IndentExtJSON(dst *bytes.Buffer, src []byte, prefix, indent string) error {
return json.Indent(dst, src, prefix, indent)
}

func MarshalExtJSONIndent(val interface{}, canonical, escapeHTML bool, prefix, indent string) ([]byte, error) {
marshaled, err := MarshalExtJSON(val, canonical, escapeHTML)
if err != nil {
return nil, err
}

var buf bytes.Buffer
err = IndentExtJSON(&buf, marshaled, prefix, indent)
if err != nil {
return nil, err
}

return buf.Bytes(), nil
}
71 changes: 71 additions & 0 deletions bson/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"reflect"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -302,3 +303,73 @@ func TestNullBytes(t *testing.T) {
}
})
}

func TestMarshalExtJSONIndent(t *testing.T) {
type indentTestCase struct {
name string
val interface{}
expectedExtJSON string
}

// expectedExtJSON must be written as below because single-quoted
// literal strings capture undesired code formatting tabs
testCases := []indentTestCase{
{
"empty val",
struct{}{},
`{}`,
},
{
"embedded struct",
struct {
Embedded interface{} `json:"embedded"`
Foo string `json:"foo"`
}{
Embedded: struct {
Name string `json:"name"`
Word string `json:"word"`
}{
Name: "test",
Word: "word",
},
Foo: "bar",
},
"{\n\t\"embedded\": {\n\t\t\"name\": \"test\",\n\t\t\"word\": \"word\"\n\t},\n\t\"foo\": \"bar\"\n}",
},
{
"date struct",
struct {
Foo string `json:"foo"`
Date time.Time `json:"date"`
}{
Foo: "bar",
Date: time.Date(2000, time.January, 1, 12, 0, 0, 0, time.UTC),
},
"{\n\t\"foo\": \"bar\",\n\t\"date\": {\n\t\t\"$date\": {\n\t\t\t\"$numberLong\": \"946728000000\"\n\t\t}\n\t}\n}",
},
{
"float struct",
struct {
Foo string `json:"foo"`
Float float32 `json:"float"`
}{
Foo: "bar",
Float: 3.14,
},
"{\n\t\"foo\": \"bar\",\n\t\"float\": {\n\t\t\"$numberDouble\": \"3.140000104904175\"\n\t}\n}",
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
extJSONBytes, err := MarshalExtJSONIndent(tc.val, true, false, "", "\t")
assert.Nil(t, err, "Marshal indent error: %v", err)

expectedExtJSONBytes := []byte(tc.expectedExtJSON)

assert.Equal(t, expectedExtJSONBytes, extJSONBytes, "expected:\n%s\ngot:\n%s", expectedExtJSONBytes, extJSONBytes)
})
}
}