Skip to content

Commit b533b61

Browse files
author
Divjot Arora
committed
GODRIVER-1729 Fix vet and fmt errors for Go 1.15
- go vet disallows string(int) conversions, which the driver uses to append null terminators in bsoncore. This commit switches those conversions from string(0) to string(byte(0)). - gofmt fails if a function's body is on the same line as it's declarations so such functions have been changed to span multiple lines.
1 parent dfc551c commit b533b61

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

bson/bson_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ func noerr(t *testing.T, err error) {
3030
}
3131
}
3232

33-
func requireErrEqual(t *testing.T, err1 error, err2 error) { require.True(t, compareErrors(err1, err2)) }
33+
func requireErrEqual(t *testing.T, err1 error, err2 error) {
34+
require.True(t, compareErrors(err1, err2))
35+
}
3436

3537
func TestTimeRoundTrip(t *testing.T) {
3638
val := struct {

bson/raw_value.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ func (rv RawValue) UnmarshalWithContext(dc *bsoncodec.DecodeContext, val interfa
104104
}
105105

106106
func convertFromCoreValue(v bsoncore.Value) RawValue { return RawValue{Type: v.Type, Value: v.Data} }
107-
func convertToCoreValue(v RawValue) bsoncore.Value { return bsoncore.Value{Type: v.Type, Data: v.Value} }
107+
func convertToCoreValue(v RawValue) bsoncore.Value {
108+
return bsoncore.Value{Type: v.Type, Data: v.Value}
109+
}
108110

109111
// Validate ensures the value is a valid BSON value.
110112
func (rv RawValue) Validate() error { return convertToCoreValue(rv).Validate() }
@@ -176,7 +178,9 @@ func (rv RawValue) ObjectID() primitive.ObjectID { return convertToCoreValue(rv)
176178

177179
// ObjectIDOK is the same as ObjectID, except it returns a boolean instead of
178180
// panicking.
179-
func (rv RawValue) ObjectIDOK() (primitive.ObjectID, bool) { return convertToCoreValue(rv).ObjectIDOK() }
181+
func (rv RawValue) ObjectIDOK() (primitive.ObjectID, bool) {
182+
return convertToCoreValue(rv).ObjectIDOK()
183+
}
180184

181185
// Boolean returns the boolean value the Value represents. It panics if the
182186
// value is a BSON type other than boolean.
@@ -214,7 +218,9 @@ func (rv RawValue) RegexOK() (pattern, options string, ok bool) {
214218

215219
// DBPointer returns the BSON dbpointer value the Value represents. It panics if the value is a BSON
216220
// type other than DBPointer.
217-
func (rv RawValue) DBPointer() (string, primitive.ObjectID) { return convertToCoreValue(rv).DBPointer() }
221+
func (rv RawValue) DBPointer() (string, primitive.ObjectID) {
222+
return convertToCoreValue(rv).DBPointer()
223+
}
218224

219225
// DBPointerOK is the same as DBPoitner, except that it returns a boolean
220226
// instead of panicking.

mongo/mongo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestMongoHelpers(t *testing.T) {
116116
got, id, err := transformAndEnsureID(bson.DefaultRegistry, doc)
117117
assert.Nil(t, err, "transformAndEnsureID error: %v", err)
118118
_, ok := id.(string)
119-
assert.True(t, ok, "expected returned id type %T, got %T", string(0), id)
119+
assert.True(t, ok, "expected returned id type string, got %T", id)
120120
assert.Equal(t, got, want, "expected document %v, got %v", got, want)
121121
})
122122
})

x/bsonx/bsoncore/bsoncore.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ import (
3939
// EmptyDocumentLength is the length of a document that has been started/ended but has no elements.
4040
const EmptyDocumentLength = 5
4141

42+
// nullTerminator is a string version of the 0 byte that is appended at the end of cstrings.
43+
const nullTerminator = string(byte(0))
44+
4245
// AppendType will append t to dst and return the extended buffer.
4346
func AppendType(dst []byte, t bsontype.Type) []byte { return append(dst, byte(t)) }
4447

4548
// AppendKey will append key to dst and return the extended buffer.
46-
func AppendKey(dst []byte, key string) []byte { return append(dst, key+string(0x00)...) }
49+
func AppendKey(dst []byte, key string) []byte { return append(dst, key+nullTerminator...) }
4750

4851
// AppendHeader will append Type t and key to dst and return the extended
4952
// buffer.
@@ -427,7 +430,7 @@ func AppendNullElement(dst []byte, key string) []byte { return AppendHeader(dst,
427430

428431
// AppendRegex will append pattern and options to dst and return the extended buffer.
429432
func AppendRegex(dst []byte, pattern, options string) []byte {
430-
return append(dst, pattern+string(0x00)+options+string(0x00)...)
433+
return append(dst, pattern+nullTerminator+options+nullTerminator...)
431434
}
432435

433436
// AppendRegexElement will append a BSON regex element using key, pattern, and

x/mongo/driver/wiremessage/wiremessage.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,9 @@ func ReadCompressedOriginalOpCode(src []byte) (opcode OpCode, rem []byte, ok boo
490490

491491
// ReadCompressedUncompressedSize reads the uncompressed size of a
492492
// compressed wiremessage to dst.
493-
func ReadCompressedUncompressedSize(src []byte) (size int32, rem []byte, ok bool) { return readi32(src) }
493+
func ReadCompressedUncompressedSize(src []byte) (size int32, rem []byte, ok bool) {
494+
return readi32(src)
495+
}
494496

495497
// ReadCompressedCompressorID reads the ID of the compressor to dst.
496498
func ReadCompressedCompressorID(src []byte) (id CompressorID, rem []byte, ok bool) {

0 commit comments

Comments
 (0)