writer.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package bsonrw
  7. import (
  8. "go.mongodb.org/mongo-driver/bson/bsontype"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. )
  11. // ArrayWriter is the interface used to create a BSON or BSON adjacent array.
  12. // Callers must ensure they call WriteArrayEnd when they have finished creating
  13. // the array.
  14. type ArrayWriter interface {
  15. WriteArrayElement() (ValueWriter, error)
  16. WriteArrayEnd() error
  17. }
  18. // DocumentWriter is the interface used to create a BSON or BSON adjacent
  19. // document. Callers must ensure they call WriteDocumentEnd when they have
  20. // finished creating the document.
  21. type DocumentWriter interface {
  22. WriteDocumentElement(string) (ValueWriter, error)
  23. WriteDocumentEnd() error
  24. }
  25. // ValueWriter is the interface used to write BSON values. Implementations of
  26. // this interface handle creating BSON or BSON adjacent representations of the
  27. // values.
  28. type ValueWriter interface {
  29. WriteArray() (ArrayWriter, error)
  30. WriteBinary(b []byte) error
  31. WriteBinaryWithSubtype(b []byte, btype byte) error
  32. WriteBoolean(bool) error
  33. WriteCodeWithScope(code string) (DocumentWriter, error)
  34. WriteDBPointer(ns string, oid primitive.ObjectID) error
  35. WriteDateTime(dt int64) error
  36. WriteDecimal128(primitive.Decimal128) error
  37. WriteDouble(float64) error
  38. WriteInt32(int32) error
  39. WriteInt64(int64) error
  40. WriteJavascript(code string) error
  41. WriteMaxKey() error
  42. WriteMinKey() error
  43. WriteNull() error
  44. WriteObjectID(primitive.ObjectID) error
  45. WriteRegex(pattern, options string) error
  46. WriteString(string) error
  47. WriteDocument() (DocumentWriter, error)
  48. WriteSymbol(symbol string) error
  49. WriteTimestamp(t, i uint32) error
  50. WriteUndefined() error
  51. }
  52. // ValueWriterFlusher is a superset of ValueWriter that exposes functionality to flush to the underlying buffer.
  53. //
  54. // Deprecated: ValueWriterFlusher will not be supported in Go Driver 2.0.
  55. type ValueWriterFlusher interface {
  56. ValueWriter
  57. Flush() error
  58. }
  59. // BytesWriter is the interface used to write BSON bytes to a ValueWriter.
  60. // This interface is meant to be a superset of ValueWriter, so that types that
  61. // implement ValueWriter may also implement this interface.
  62. //
  63. // Deprecated: BytesWriter will not be supported in Go Driver 2.0.
  64. type BytesWriter interface {
  65. WriteValueBytes(t bsontype.Type, b []byte) error
  66. }
  67. // SliceWriter allows a pointer to a slice of bytes to be used as an io.Writer.
  68. //
  69. // Deprecated: SliceWriter will not be supported in Go Driver 2.0.
  70. type SliceWriter []byte
  71. // Write writes the bytes to the underlying slice.
  72. //
  73. // Deprecated: SliceWriter will not be supported in Go Driver 2.0.
  74. func (sw *SliceWriter) Write(p []byte) (int, error) {
  75. written := len(p)
  76. *sw = append(*sw, p...)
  77. return written, nil
  78. }