@@ -45,6 +45,22 @@ func (c Copier) CopyDocument(dst ValueWriter, src ValueReader) error {
45
45
return c .copyDocumentCore (dw , dr )
46
46
}
47
47
48
+ // CopyArrayFromBytes copies the values from a BSON array represented as a
49
+ // []byte to a ValueWriter.
50
+ func (c Copier ) CopyArrayFromBytes (dst ValueWriter , src []byte ) error {
51
+ aw , err := dst .WriteArray ()
52
+ if err != nil {
53
+ return err
54
+ }
55
+
56
+ err = c .CopyBytesToArrayWriter (aw , src )
57
+ if err != nil {
58
+ return err
59
+ }
60
+
61
+ return aw .WriteArrayEnd ()
62
+ }
63
+
48
64
// CopyDocumentFromBytes copies the values from a BSON document represented as a
49
65
// []byte to a ValueWriter.
50
66
func (c Copier ) CopyDocumentFromBytes (dst ValueWriter , src []byte ) error {
@@ -61,9 +77,29 @@ func (c Copier) CopyDocumentFromBytes(dst ValueWriter, src []byte) error {
61
77
return dw .WriteDocumentEnd ()
62
78
}
63
79
80
+ type writeElementFn func (key string ) (ValueWriter , error )
81
+
82
+ // CopyBytesToArrayWriter copies the values from a BSON Array represented as a []byte to an
83
+ // ArrayWriter.
84
+ func (c Copier ) CopyBytesToArrayWriter (dst ArrayWriter , src []byte ) error {
85
+ wef := func (_ string ) (ValueWriter , error ) {
86
+ return dst .WriteArrayElement ()
87
+ }
88
+
89
+ return c .copyBytesToValueWriter (src , wef )
90
+ }
91
+
64
92
// CopyBytesToDocumentWriter copies the values from a BSON document represented as a []byte to a
65
93
// DocumentWriter.
66
94
func (c Copier ) CopyBytesToDocumentWriter (dst DocumentWriter , src []byte ) error {
95
+ wef := func (key string ) (ValueWriter , error ) {
96
+ return dst .WriteDocumentElement (key )
97
+ }
98
+
99
+ return c .copyBytesToValueWriter (src , wef )
100
+ }
101
+
102
+ func (c Copier ) copyBytesToValueWriter (src []byte , wef writeElementFn ) error {
67
103
// TODO(skriptble): Create errors types here. Anything thats a tag should be a property.
68
104
length , rem , ok := bsoncore .ReadLength (src )
69
105
if ! ok {
@@ -93,15 +129,18 @@ func (c Copier) CopyBytesToDocumentWriter(dst DocumentWriter, src []byte) error
93
129
if ! ok {
94
130
return fmt .Errorf ("invalid key found. remaining bytes=%v" , rem )
95
131
}
96
- dvw , err := dst .WriteDocumentElement (key )
132
+
133
+ // write as either array element or document element using writeElementFn
134
+ vw , err := wef (key )
97
135
if err != nil {
98
136
return err
99
137
}
138
+
100
139
val , rem , ok = bsoncore .ReadValue (rem , t )
101
140
if ! ok {
102
141
return fmt .Errorf ("not enough bytes available to read type. bytes=%d type=%s" , len (rem ), t )
103
142
}
104
- err = c .CopyValueFromBytes (dvw , t , val .Data )
143
+ err = c .CopyValueFromBytes (vw , t , val .Data )
105
144
if err != nil {
106
145
return err
107
146
}
@@ -133,6 +172,23 @@ func (c Copier) AppendDocumentBytes(dst []byte, src ValueReader) ([]byte, error)
133
172
return dst , err
134
173
}
135
174
175
+ // AppendArrayBytes copies an array from the ValueReader to dst.
176
+ func (c Copier ) AppendArrayBytes (dst []byte , src ValueReader ) ([]byte , error ) {
177
+ if br , ok := src .(BytesReader ); ok {
178
+ _ , dst , err := br .ReadValueBytes (dst )
179
+ return dst , err
180
+ }
181
+
182
+ vw := vwPool .Get ().(* valueWriter )
183
+ defer vwPool .Put (vw )
184
+
185
+ vw .reset (dst )
186
+
187
+ err := c .copyArray (vw , src )
188
+ dst = vw .buf
189
+ return dst , err
190
+ }
191
+
136
192
// CopyValueFromBytes will write the value represtend by t and src to dst.
137
193
func (c Copier ) CopyValueFromBytes (dst ValueWriter , t bsontype.Type , src []byte ) error {
138
194
if wvb , ok := dst .(BytesWriter ); ok {
0 commit comments