Skip to content

Commit

Permalink
minor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Jan 22, 2024
1 parent 52c6fb2 commit f4b4062
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
12 changes: 2 additions & 10 deletions bson/bsonrw/extjson_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@ type extJSONValueWriter struct {
frame int64
canonical bool
escapeHTML bool
newlines bool
}

// NewExtJSONValueWriter creates a ValueWriter that writes Extended JSON to w.
func NewExtJSONValueWriter(w io.Writer, canonical, escapeHTML bool) ValueWriter {
// Enable newlines for all Extended JSON value writers created by NewExtJSONValueWriter. We
// expect these value writers to be used with an Encoder, which should add newlines after
// encoded Extended JSON documents.
return newExtJSONWriter(w, canonical, escapeHTML, false)
return newExtJSONWriter(w, canonical, escapeHTML)
}

func newExtJSONWriter(w io.Writer, canonical, escapeHTML, newlines bool) *extJSONValueWriter {
func newExtJSONWriter(w io.Writer, canonical, escapeHTML bool) *extJSONValueWriter {
stack := make([]ejvwState, 1, 5)
stack[0] = ejvwState{mode: mTopLevel}

Expand All @@ -54,7 +53,6 @@ func newExtJSONWriter(w io.Writer, canonical, escapeHTML, newlines bool) *extJSO
stack: stack,
canonical: canonical,
escapeHTML: escapeHTML,
newlines: newlines,
}
}

Expand Down Expand Up @@ -504,12 +502,6 @@ func (ejvw *extJSONValueWriter) WriteDocumentEnd() error {
case mDocument:
ejvw.buf = append(ejvw.buf, ',')
case mTopLevel:
// If the value writer has newlines enabled, end top-level documents with a newline so that
// multiple documents encoded to the same writer are separated by newlines. That matches the
// Go json.Encoder behavior and also works with bsonrw.NewExtJSONValueReader.
if ejvw.newlines {
ejvw.buf = append(ejvw.buf, '\n')
}
if ejvw.w != nil {
if _, err := ejvw.w.Write(ejvw.buf); err != nil {
return err
Expand Down
16 changes: 8 additions & 8 deletions bson/bsonrw/extjson_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestExtJSONValueWriter(t *testing.T) {
t.Fatalf("fn must have one return value and it must be an error.")
}
params := make([]reflect.Value, 1, len(tc.params)+1)
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true)
params[0] = reflect.ValueOf(ejvw)
for _, param := range tc.params {
params = append(params, reflect.ValueOf(param))
Expand All @@ -162,7 +162,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}

t.Run("WriteArray", func(t *testing.T) {
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true)
ejvw.push(mArray)
want := TransitionError{current: mArray, destination: mArray, parent: mTopLevel,
name: "WriteArray", modes: []mode{mElement, mValue}, action: "write"}
Expand All @@ -172,7 +172,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}
})
t.Run("WriteCodeWithScope", func(t *testing.T) {
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true)
ejvw.push(mArray)
want := TransitionError{current: mArray, destination: mCodeWithScope, parent: mTopLevel,
name: "WriteCodeWithScope", modes: []mode{mElement, mValue}, action: "write"}
Expand All @@ -182,7 +182,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}
})
t.Run("WriteDocument", func(t *testing.T) {
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true)
ejvw.push(mArray)
want := TransitionError{current: mArray, destination: mDocument, parent: mTopLevel,
name: "WriteDocument", modes: []mode{mElement, mValue, mTopLevel}, action: "write"}
Expand All @@ -192,7 +192,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}
})
t.Run("WriteDocumentElement", func(t *testing.T) {
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true)
ejvw.push(mElement)
want := TransitionError{current: mElement,
destination: mElement,
Expand All @@ -206,7 +206,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}
})
t.Run("WriteDocumentEnd", func(t *testing.T) {
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true)
ejvw.push(mElement)
want := fmt.Errorf("incorrect mode to end document: %s", mElement)
got := ejvw.WriteDocumentEnd()
Expand All @@ -215,7 +215,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}
})
t.Run("WriteArrayElement", func(t *testing.T) {
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true)
ejvw.push(mElement)
want := TransitionError{current: mElement,
destination: mValue,
Expand All @@ -229,7 +229,7 @@ func TestExtJSONValueWriter(t *testing.T) {
}
})
t.Run("WriteArrayEnd", func(t *testing.T) {
ejvw := newExtJSONWriter(io.Discard, true, true, false)
ejvw := newExtJSONWriter(io.Discard, true, true)
ejvw.push(mElement)
want := fmt.Errorf("incorrect mode to end array: %s", mElement)
got := ejvw.WriteArrayEnd()
Expand Down
9 changes: 9 additions & 0 deletions bson/bsonrw/value_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ func NewValueWriter(w io.Writer) ValueWriter {
return newValueWriter(w)
}

// NewValueWriterFlusher retrieves a ValueWriterFlusher.
//
// Deprecated: NewValueWriterFlusher will not be supported in Go Driver 2.0.
func NewValueWriterFlusher(w io.Writer) ValueWriterFlusher {
vw := newValueWriter(w)
vw.push(mElement)
return vw
}

func newValueWriter(w io.Writer) *valueWriter {
vw := new(valueWriter)
stack := make([]vwState, 1, 5)
Expand Down
10 changes: 8 additions & 2 deletions bson/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,23 @@ func MarshalValue(val interface{}) (bsontype.Type, []byte, error) {
// Driver 2.0.
func MarshalValueWithRegistry(r *bsoncodec.Registry, val interface{}) (bsontype.Type, []byte, error) {
sw := bsonrw.SliceWriter(make([]byte, 0))
vw := bsonrw.NewValueWriter(&sw)
vwFlusher := bsonrw.NewValueWriterFlusher(&sw)

// get an Encoder and encode the value
enc := encPool.Get().(*Encoder)
defer encPool.Put(enc)
enc.Reset(vw)
enc.Reset(vwFlusher)
enc.ec = bsoncodec.EncodeContext{Registry: r}
if err := enc.Encode(val); err != nil {
return 0, nil, err
}

// flush the bytes written because we cannot guarantee that a full document has been written
// after the flush, *sw will be in the format
// [value type, 0 (null byte to indicate end of empty element name), value bytes..]
if err := vwFlusher.Flush(); err != nil {
return 0, nil, err
}
return bsontype.Type(sw[0]), sw[2:], nil
}

Expand Down

0 comments on commit f4b4062

Please sign in to comment.