Skip to content

Commit f30bcf8

Browse files
GODRIVER-3565 Add UnmarshalBSON to GridFSFile
1 parent 43105f8 commit f30bcf8

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

mongo/gridfs_download_stream.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ type GridFSFile struct {
7373
Metadata bson.Raw
7474
}
7575

76+
var _ bson.Unmarshaler = &GridFSFile{}
77+
7678
// findFileResponse is a temporary type used to unmarshal documents from the
7779
// files collection and can be transformed into a File instance. This type
7880
// exists to avoid adding BSON struct tags to the exported File type.
@@ -96,6 +98,23 @@ func newFileFromResponse(resp findFileResponse) *GridFSFile {
9698
}
9799
}
98100

101+
// UnmarshalBSON implements the bson.Unmarshaler interface.
102+
func (f *GridFSFile) UnmarshalBSON(data []byte) error {
103+
var temp findFileResponse
104+
if err := bson.Unmarshal(data, &temp); err != nil {
105+
return err
106+
}
107+
108+
f.ID = temp.ID
109+
f.Length = temp.Length
110+
f.ChunkSize = temp.ChunkSize
111+
f.UploadDate = temp.UploadDate
112+
f.Name = temp.Name
113+
f.Metadata = temp.Metadata
114+
115+
return nil
116+
}
117+
99118
func newGridFSDownloadStream(
100119
ctx context.Context,
101120
cancel context.CancelFunc,

mongo/gridfs_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"context"
1111
"testing"
1212

13+
"go.mongodb.org/mongo-driver/v2/bson"
1314
"go.mongodb.org/mongo-driver/v2/event"
1415
"go.mongodb.org/mongo-driver/v2/internal/assert"
1516
"go.mongodb.org/mongo-driver/v2/internal/integtest"
@@ -109,3 +110,54 @@ func TestGridFS(t *testing.T) {
109110
}
110111
})
111112
}
113+
114+
func TestGridFSFile_UnmarshalBSON(t *testing.T) {
115+
client, err := Connect()
116+
require.NoError(t, err)
117+
118+
defer func() {
119+
err := client.Disconnect(context.Background())
120+
require.NoError(t, err)
121+
}()
122+
123+
// Get the database and create a GridFS bucket
124+
db := client.Database("gridfs_test_db")
125+
126+
// Drop the collection
127+
err = db.Collection("myfiles.files").Drop(context.Background())
128+
require.NoError(t, err)
129+
130+
err = db.Collection("myfiles.chunks").Drop(context.Background())
131+
require.NoError(t, err)
132+
133+
bucket := db.GridFSBucket(options.GridFSBucket().SetName("myfiles"))
134+
135+
// Data to upload
136+
fileName := "example-file.txt"
137+
fileContent := []byte("Hello GridFS! This is a test file.")
138+
139+
// Upload data into GridFS
140+
uploadStream, err := bucket.OpenUploadStream(context.Background(), fileName)
141+
require.NoError(t, err)
142+
143+
_, err = uploadStream.Write(fileContent)
144+
require.NoError(t, err)
145+
146+
uploadStream.Close()
147+
148+
// Verify the file metadata
149+
fileCursor, err := bucket.Find(context.Background(), bson.D{})
150+
require.NoError(t, err)
151+
152+
for fileCursor.Next(context.Background()) {
153+
var file GridFSFile
154+
err := fileCursor.Decode(&file)
155+
require.NoError(t, err)
156+
157+
assert.NotNil(t, file.ID)
158+
assert.Equal(t, int64(34), file.Length)
159+
assert.Equal(t, int32(261120), file.ChunkSize)
160+
assert.NotNil(t, file.UploadDate)
161+
assert.Equal(t, fileName, file.Name)
162+
}
163+
}

0 commit comments

Comments
 (0)