You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.8 KiB

  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. // BytesWriter is the interface used to write BSON bytes to a ValueWriter.
  53. // This interface is meant to be a superset of ValueWriter, so that types that
  54. // implement ValueWriter may also implement this interface.
  55. type BytesWriter interface {
  56. WriteValueBytes(t bsontype.Type, b []byte) error
  57. }
  58. // SliceWriter allows a pointer to a slice of bytes to be used as an io.Writer.
  59. type SliceWriter []byte
  60. func (sw *SliceWriter) Write(p []byte) (int, error) {
  61. written := len(p)
  62. *sw = append(*sw, p...)
  63. return written, nil
  64. }
  65. type writer []byte
  66. func (w *writer) Write(p []byte) (int, error) {
  67. index := len(*w)
  68. return w.WriteAt(p, int64(index))
  69. }
  70. func (w *writer) WriteAt(p []byte, off int64) (int, error) {
  71. newend := off + int64(len(p))
  72. if newend < int64(len(*w)) {
  73. newend = int64(len(*w))
  74. }
  75. if newend > int64(cap(*w)) {
  76. buf := make([]byte, int64(2*cap(*w))+newend)
  77. copy(buf, *w)
  78. *w = buf
  79. }
  80. *w = []byte(*w)[:newend]
  81. copy([]byte(*w)[off:], p)
  82. return len(p), nil
  83. }