Skip to content

Commit deab332

Browse files
author
Divjot Arora
committed
Encode ints within 32 bits as int32 by default
GODRIVER-717 Change-Id: I33179c41c3c39027a941f19511b01f2bab895195
1 parent fa07c0b commit deab332

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

bson/bsoncodec/default_value_encoders.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,24 @@ func (dve DefaultValueEncoders) BooleanEncodeValue(ectx EncodeContext, vw bsonrw
117117
return vw.WriteBoolean(val.Bool())
118118
}
119119

120+
func fitsIn32Bits(i int64) bool {
121+
return math.MinInt32 <= i && i <= math.MaxInt32
122+
}
123+
120124
// IntEncodeValue is the ValueEncoderFunc for int types.
121125
func (dve DefaultValueEncoders) IntEncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
122126
switch val.Kind() {
123127
case reflect.Int8, reflect.Int16, reflect.Int32:
124128
return vw.WriteInt32(int32(val.Int()))
125-
case reflect.Int, reflect.Int64:
129+
case reflect.Int:
130+
i64 := val.Int()
131+
if fitsIn32Bits(i64) {
132+
return vw.WriteInt32(int32(i64))
133+
}
134+
return vw.WriteInt64(i64)
135+
case reflect.Int64:
126136
i64 := val.Int()
127-
if ec.MinSize && i64 <= math.MaxInt32 {
137+
if ec.MinSize && fitsIn32Bits(i64) {
128138
return vw.WriteInt32(int32(i64))
129139
}
130140
return vw.WriteInt64(i64)

bson/bsoncodec/default_value_encoders_test.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/mongodb/mongo-go-driver/bson/bsontype"
2222
"github.com/mongodb/mongo-go-driver/bson/primitive"
2323
"github.com/mongodb/mongo-go-driver/x/bsonx/bsoncore"
24+
"math"
2425
)
2526

2627
func TestDefaultValueEncoders(t *testing.T) {
@@ -97,20 +98,28 @@ func TestDefaultValueEncoders(t *testing.T) {
9798
{"int16/fast path", int16(32767), nil, nil, bsonrwtest.WriteInt32, nil},
9899
{"int32/fast path", int32(2147483647), nil, nil, bsonrwtest.WriteInt32, nil},
99100
{"int64/fast path", int64(1234567890987), nil, nil, bsonrwtest.WriteInt64, nil},
100-
{"int/fast path", int(1234567), nil, nil, bsonrwtest.WriteInt64, nil},
101-
{"int64/fast path - minsize", int64(2147483647), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt32, nil},
102-
{"int/fast path - minsize", int(2147483647), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt32, nil},
103-
{"int64/fast path - minsize too large", int64(2147483648), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt64, nil},
104-
{"int/fast path - minsize too large", int(2147483648), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt64, nil},
101+
{"int64/fast path - minsize", int64(math.MaxInt32), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt32, nil},
102+
{"int64/fast path - minsize too large", int64(math.MaxInt32 + 1), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt64, nil},
103+
{"int64/fast path - minsize too small", int64(math.MinInt32 - 1), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt64, nil},
104+
{"int/fast path - positive int32", int(math.MaxInt32 - 1), nil, nil, bsonrwtest.WriteInt32, nil},
105+
{"int/fast path - negative int32", int(math.MinInt32 + 1), nil, nil, bsonrwtest.WriteInt32, nil},
106+
{"int/fast path - MaxInt32", int(math.MaxInt32), nil, nil, bsonrwtest.WriteInt32, nil},
107+
{"int/fast path - MinInt32", int(math.MinInt32), nil, nil, bsonrwtest.WriteInt32, nil},
108+
{"int/fast path - larger than MaxInt32", int(math.MaxInt32 + 1), nil, nil, bsonrwtest.WriteInt64, nil},
109+
{"int/fast path - smaller than MinInt32", int(math.MinInt32 - 1), nil, nil, bsonrwtest.WriteInt64, nil},
105110
{"int8/reflection path", myint8(127), nil, nil, bsonrwtest.WriteInt32, nil},
106111
{"int16/reflection path", myint16(32767), nil, nil, bsonrwtest.WriteInt32, nil},
107112
{"int32/reflection path", myint32(2147483647), nil, nil, bsonrwtest.WriteInt32, nil},
108113
{"int64/reflection path", myint64(1234567890987), nil, nil, bsonrwtest.WriteInt64, nil},
109-
{"int/reflection path", myint(1234567890987), nil, nil, bsonrwtest.WriteInt64, nil},
110-
{"int64/reflection path - minsize", myint64(2147483647), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt32, nil},
111-
{"int/reflection path - minsize", myint(2147483647), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt32, nil},
112-
{"int64/reflection path - minsize too large", myint64(2147483648), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt64, nil},
113-
{"int/reflection path - minsize too large", myint(2147483648), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt64, nil},
114+
{"int64/reflection path - minsize", myint64(math.MaxInt32), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt32, nil},
115+
{"int64/reflection path - minsize too large", myint64(math.MaxInt32 + 1), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt64, nil},
116+
{"int64/reflection path - minsize too small", myint64(math.MinInt32 - 1), &EncodeContext{MinSize: true}, nil, bsonrwtest.WriteInt64, nil},
117+
{"int/reflection path - positive int32", myint(math.MaxInt32 - 1), nil, nil, bsonrwtest.WriteInt32, nil},
118+
{"int/reflection path - negative int32", myint(math.MinInt32 + 1), nil, nil, bsonrwtest.WriteInt32, nil},
119+
{"int/reflection path - MaxInt32", myint(math.MaxInt32), nil, nil, bsonrwtest.WriteInt32, nil},
120+
{"int/reflection path - MinInt32", myint(math.MinInt32), nil, nil, bsonrwtest.WriteInt32, nil},
121+
{"int/reflection path - larger than MaxInt32", myint(math.MaxInt32 + 1), nil, nil, bsonrwtest.WriteInt64, nil},
122+
{"int/reflection path - smaller than MinInt32", myint(math.MinInt32 - 1), nil, nil, bsonrwtest.WriteInt64, nil},
114123
},
115124
},
116125
{

bson/marshal_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestMarshalExtJSONAppendWithContext(t *testing.T) {
126126
ec := bsoncodec.EncodeContext{Registry: DefaultRegistry}
127127
got, err := MarshalExtJSONAppendWithContext(ec, dst, val, true, false)
128128
noerr(t, err)
129-
want := []byte(`{"foo":{"$numberLong":"1"}}`)
129+
want := []byte(`{"foo":{"$numberInt":"1"}}`)
130130
if !bytes.Equal(got, want) {
131131
t.Errorf("Bytes are not equal. got %v; want %v", got, want)
132132
t.Errorf("Bytes:\n%s\n%s", got, want)
@@ -141,7 +141,7 @@ func TestMarshalExtJSONWithContext(t *testing.T) {
141141
ec := bsoncodec.EncodeContext{Registry: DefaultRegistry}
142142
got, err := MarshalExtJSONWithContext(ec, val, true, false)
143143
noerr(t, err)
144-
want := []byte(`{"foo":{"$numberLong":"1"}}`)
144+
want := []byte(`{"foo":{"$numberInt":"1"}}`)
145145
if !bytes.Equal(got, want) {
146146
t.Errorf("Bytes are not equal. got %v; want %v", got, want)
147147
t.Errorf("Bytes:\n%s\n%s", got, want)

bson/primitive_codecs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func TestDefaultValueEncoders(t *testing.T) {
418418
primitive.DateTime(now.UnixNano() / int64(time.Millisecond)),
419419
}},
420420
{"ae", A{"hello", "world"}},
421-
{"af", A{D{{"foo", "bar"}}, D{{"hello", "world"}, {"number", int64(12345)}}}},
421+
{"af", A{D{{"foo", "bar"}}, D{{"hello", "world"}, {"number", int32(12345)}}}},
422422
{"ag", A{D{{"pi", float64(3.14159)}}, nil}},
423423
}),
424424
nil,

0 commit comments

Comments
 (0)