Skip to content

Commit a4362ec

Browse files
committed
PCSM-261: Add truncate tag to handle floats in BSON
1 parent 52a7f98 commit a4362ec

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

topo/topo.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ type DBStats struct {
127127
// CollStats represents the result of the [GetCollStats].
128128
type CollStats struct {
129129
// Count is the number of documents in the collection.
130-
Count int64 `bson:"count"`
130+
Count int64 `bson:"count,truncate"`
131131
// Size is the total size of the collection.
132-
Size int64 `bson:"size"`
132+
Size int64 `bson:"size,truncate"`
133133
// AvgObjSize is the average size of documents in the collection.
134-
AvgObjSize int64 `bson:"avgObjSize"`
134+
AvgObjSize int64 `bson:"avgObjSize,truncate"`
135135
}
136136

137137
// SayHello runs the db.hello() command and returns the [Hello].

topo/topo_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,33 @@ import (
66
"testing"
77
"time"
88

9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
"go.mongodb.org/mongo-driver/v2/bson"
912
"go.mongodb.org/mongo-driver/v2/mongo"
1013
)
1114

15+
func TestCollStats_DecodeFromFloat64(t *testing.T) {
16+
t.Parallel()
17+
18+
input := bson.M{
19+
"count": 0.0,
20+
"size": 73179136.0,
21+
"avgObjSize": 0.0,
22+
}
23+
24+
data, err := bson.Marshal(input)
25+
require.NoError(t, err)
26+
27+
var stats CollStats
28+
err = bson.Unmarshal(data, &stats)
29+
require.NoError(t, err)
30+
31+
assert.Equal(t, int64(0), stats.Count)
32+
assert.Equal(t, int64(73179136), stats.Size)
33+
assert.Equal(t, int64(0), stats.AvgObjSize)
34+
}
35+
1236
func TestRunWithRetry_NonTransientError(t *testing.T) {
1337
t.Parallel()
1438

0 commit comments

Comments
 (0)