Skip to content

Commit

Permalink
Merge branch 'v1' into godriver3302
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 authored Sep 14, 2024
2 parents 9bea0eb + 6bcc3dc commit 03aa027
Show file tree
Hide file tree
Showing 239 changed files with 4,487 additions and 2,527 deletions.
169 changes: 99 additions & 70 deletions .evergreen/config.yml

Large diffs are not rendered by default.

21 changes: 9 additions & 12 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
run:
timeout: 5m
skip-dirs-use-default: false
skip-dirs:
- (^|/)vendor($|/)
- (^|/)testdata($|/)
- (^|/)etc($|/)
# Disable all linters for "golang.org/x/exp/rand" package in internal/rand.
- internal/rand

linters:
disable-all: true
Expand Down Expand Up @@ -35,11 +28,7 @@ linters:

linters-settings:
errcheck:
exclude: .errcheck-excludes
gocritic:
enabled-checks:
# Detects suspicious append result assignments. E.g. "b := append(a, 1, 2, 3)"
- appendAssign
exclude-functions: .errcheck-excludes
govet:
disable:
- cgocall
Expand All @@ -55,6 +44,14 @@ linters-settings:
]

issues:
exclude-dirs-use-default: false
exclude-dirs:
- (^|/)testdata($|/)
- (^|/)etc($|/)
# Disable all linters for copied third-party code.
- internal/rand
- internal/aws
- internal/assert
exclude-use-default: false
exclude:
# Add all default excluded issues except issues related to exported types/functions not having
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ repos:
rev: "v2.2.6"
hooks:
- id: codespell
args: ["-L", "te,fo,fle,alo,nin,compres,wil,collone,asess,sav,ot,wll,dne,nulll,hellow"]
args: ["-L", "te,fo,fle,alo,nin,compres,wil,collone,asess,sav,ot,wll,dne,nulll,hellow,aks"]
exclude: ^(vendor/|benchmark/operation_test.go)
exclude_types: [json,yaml,pem]

Expand All @@ -51,6 +51,6 @@ repos:
exclude: ^(vendor)

- repo: https://github.com/golangci/golangci-lint
rev: v1.55.1
rev: v1.59.1
hooks:
- id: golangci-lint
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ check-fmt: install-lll
check-modules:
go mod tidy -v
go mod vendor
git diff --exit-code go.mod go.sum ./vendor
git diff --exit-code go.mod go.sum ./vendor # Compare to the PR / WF Branch.

.PHONY: doc
doc:
Expand All @@ -72,7 +72,7 @@ fmt:

.PHONY: install-golangci-lint
install-golangci-lint:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.1

# Lint with various GOOS and GOARCH targets to catch static analysis failures that may only affect
# specific operating systems or architectures. For example, staticcheck will only check for 64-bit
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@

The MongoDB supported driver for Go.

> \[!NOTE\]
>
> Go Driver 1.17.0 is the last planned 1.x version.
> It will receive critical bug fixes, but future development and features
> will be in the 2.x version of the driver.
______________________________________________________________________

## Requirements

- Go 1.18 or higher. We aim to support the latest versions of Go.
- Go 1.20 or higher is required to run the driver test suite.
- Go 1.22 or higher is required to run the driver test suite.
- MongoDB 3.6 and higher.

______________________________________________________________________
Expand Down
9 changes: 5 additions & 4 deletions bson/bson_corpus_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,14 @@ func unescapeUnicode(s, bsonType string) string {

func formatDouble(f float64) string {
var s string
if math.IsInf(f, 1) {
switch {
case math.IsInf(f, 1):
s = "Infinity"
} else if math.IsInf(f, -1) {
case math.IsInf(f, -1):
s = "-Infinity"
} else if math.IsNaN(f) {
case math.IsNaN(f):
s = "NaN"
} else {
default:
// Print exactly one decimalType place for integers; otherwise, print as many are necessary to
// perfectly represent it.
s = strconv.FormatFloat(f, 'G', -1, 64)
Expand Down
4 changes: 2 additions & 2 deletions bson/bsoncodec/bsoncodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func ExampleValueEncoder() {
var _ ValueEncoderFunc = func(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
var _ ValueEncoderFunc = func(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
if val.Kind() != reflect.String {
return ValueEncoderError{Name: "StringEncodeValue", Kinds: []reflect.Kind{reflect.String}, Received: val}
}
Expand All @@ -28,7 +28,7 @@ func ExampleValueEncoder() {
}

func ExampleValueDecoder() {
var _ ValueDecoderFunc = func(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
var _ ValueDecoderFunc = func(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Kind() != reflect.String {
return ValueDecoderError{Name: "StringDecodeValue", Kinds: []reflect.Kind{reflect.String}, Received: val}
}
Expand Down
4 changes: 2 additions & 2 deletions bson/bsoncodec/default_value_encoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ func TestDefaultValueEncoders(t *testing.T) {
},
{
"WriteArrayElement Error",
bsoncore.Array(buildDocumentArray(func(doc []byte) []byte {
bsoncore.Array(buildDocumentArray(func([]byte) []byte {
return bsoncore.AppendNullElement(nil, "foo")
})),
nil,
Expand All @@ -1169,7 +1169,7 @@ func TestDefaultValueEncoders(t *testing.T) {
},
{
"encodeValue error",
bsoncore.Array(buildDocumentArray(func(doc []byte) []byte {
bsoncore.Array(buildDocumentArray(func([]byte) []byte {
return bsoncore.AppendNullElement(nil, "foo")
})),
nil,
Expand Down
8 changes: 4 additions & 4 deletions bson/bsoncodec/registry_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func ExampleRegistry_customEncoder() {
negatedIntType := reflect.TypeOf(negatedInt(0))

negatedIntEncoder := func(
ec bsoncodec.EncodeContext,
_ bsoncodec.EncodeContext,
vw bsonrw.ValueWriter,
val reflect.Value,
) error {
Expand Down Expand Up @@ -83,7 +83,7 @@ func ExampleRegistry_customDecoder() {
lenientBoolType := reflect.TypeOf(lenientBool(true))

lenientBoolDecoder := func(
dc bsoncodec.DecodeContext,
_ bsoncodec.DecodeContext,
vr bsonrw.ValueReader,
val reflect.Value,
) error {
Expand Down Expand Up @@ -160,7 +160,7 @@ func ExampleRegistry_RegisterKindEncoder() {
// encoder for kind reflect.Int32. That way, even user-defined types with
// underlying type int32 will be encoded as a BSON int64.
int32To64Encoder := func(
ec bsoncodec.EncodeContext,
_ bsoncodec.EncodeContext,
vw bsonrw.ValueWriter,
val reflect.Value,
) error {
Expand Down Expand Up @@ -215,7 +215,7 @@ func ExampleRegistry_RegisterKindDecoder() {
// "kind" decoder for kind reflect.Int64. That way, we can even decode to
// user-defined types with underlying type int64.
flexibleInt64KindDecoder := func(
dc bsoncodec.DecodeContext,
_ bsoncodec.DecodeContext,
vr bsonrw.ValueReader,
val reflect.Value,
) error {
Expand Down
4 changes: 2 additions & 2 deletions bson/bsoncodec/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func TestRegistryBuilder(t *testing.T) {
}

allowunexported := cmp.AllowUnexported(fakeCodec{}, fakeStructCodec{}, fakeSliceCodec{}, fakeMapCodec{})
comparepc := func(pc1, pc2 *PointerCodec) bool { return true }
comparepc := func(pc1, pc2 *PointerCodec) bool { return pc1 == pc2 }
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Run("Encoder", func(t *testing.T) {
Expand Down Expand Up @@ -754,7 +754,7 @@ func TestRegistry(t *testing.T) {
}

allowunexported := cmp.AllowUnexported(fakeCodec{}, fakeStructCodec{}, fakeSliceCodec{}, fakeMapCodec{})
comparepc := func(pc1, pc2 *PointerCodec) bool { return true }
comparepc := func(pc1, pc2 *PointerCodec) bool { return pc1 == pc2 }
for _, tc := range testCases {
tc := tc

Expand Down
2 changes: 1 addition & 1 deletion bson/bsonrw/extjson_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (ejp *extJSONParser) readValue(t bsontype.Type) (*extJSONValue, error) {
}

// remove hyphens
uuidNoHyphens := strings.Replace(uuid, "-", "", -1)
uuidNoHyphens := strings.ReplaceAll(uuid, "-", "")
if len(uuidNoHyphens) != 32 {
return nil, fmt.Errorf("$uuid value does not follow RFC 4122 format regarding length and hyphens")
}
Expand Down
13 changes: 6 additions & 7 deletions bson/bsonrw/extjson_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,13 +628,14 @@ func (ejvw *extJSONValueWriter) WriteArrayEnd() error {

func formatDouble(f float64) string {
var s string
if math.IsInf(f, 1) {
switch {
case math.IsInf(f, 1):
s = "Infinity"
} else if math.IsInf(f, -1) {
case math.IsInf(f, -1):
s = "-Infinity"
} else if math.IsNaN(f) {
case math.IsNaN(f):
s = "NaN"
} else {
default:
// Print exactly one decimalType place for integers; otherwise, print as many are necessary to
// perfectly represent it.
s = strconv.FormatFloat(f, 'G', -1, 64)
Expand Down Expand Up @@ -739,9 +740,7 @@ func (ss sortableString) Less(i, j int) bool {
}

func (ss sortableString) Swap(i, j int) {
oldI := ss[i]
ss[i] = ss[j]
ss[j] = oldI
ss[i], ss[j] = ss[j], ss[i]
}

func sortStringAlphebeticAscending(s string) string {
Expand Down
37 changes: 21 additions & 16 deletions bson/bsonrw/json_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ func (js *jsonScanner) nextToken() (*jsonToken, error) {
return js.scanString()
default:
// check if it's a number
if c == '-' || isDigit(c) {
switch {
case c == '-' || isDigit(c):
return js.scanNumber(c)
} else if c == 't' || c == 'f' || c == 'n' {
case c == 't' || c == 'f' || c == 'n':
// maybe a literal
return js.scanLiteral(c)
} else {
default:
return nil, fmt.Errorf("invalid JSON input. Position: %d. Character: %c", js.pos-1, c)
}
}
Expand Down Expand Up @@ -174,7 +175,7 @@ func getu4(s []byte) rune {
for _, c := range s[:4] {
switch {
case '0' <= c && c <= '9':
c = c - '0'
c -= '0'
case 'a' <= c && c <= 'f':
c = c - 'a' + 10
case 'A' <= c && c <= 'F':
Expand Down Expand Up @@ -325,13 +326,14 @@ func (js *jsonScanner) scanLiteral(first byte) (*jsonToken, error) {

c5, err := js.readNextByte()

if bytes.Equal([]byte("true"), lit) && (isValueTerminator(c5) || errors.Is(err, io.EOF)) {
switch {
case bytes.Equal([]byte("true"), lit) && (isValueTerminator(c5) || errors.Is(err, io.EOF)):
js.pos = int(math.Max(0, float64(js.pos-1)))
return &jsonToken{t: jttBool, v: true, p: p}, nil
} else if bytes.Equal([]byte("null"), lit) && (isValueTerminator(c5) || errors.Is(err, io.EOF)) {
case bytes.Equal([]byte("null"), lit) && (isValueTerminator(c5) || errors.Is(err, io.EOF)):
js.pos = int(math.Max(0, float64(js.pos-1)))
return &jsonToken{t: jttNull, v: nil, p: p}, nil
} else if bytes.Equal([]byte("fals"), lit) {
case bytes.Equal([]byte("fals"), lit):
if c5 == 'e' {
c5, err = js.readNextByte()

Expand Down Expand Up @@ -430,12 +432,13 @@ func (js *jsonScanner) scanNumber(first byte) (*jsonToken, error) {
case '}', ']', ',':
s = nssDone
default:
if isWhiteSpace(c) || errors.Is(err, io.EOF) {
switch {
case isWhiteSpace(c) || errors.Is(err, io.EOF):
s = nssDone
} else if isDigit(c) {
case isDigit(c):
s = nssSawIntegerDigits
b.WriteByte(c)
} else {
default:
s = nssInvalid
}
}
Expand All @@ -455,12 +458,13 @@ func (js *jsonScanner) scanNumber(first byte) (*jsonToken, error) {
case '}', ']', ',':
s = nssDone
default:
if isWhiteSpace(c) || errors.Is(err, io.EOF) {
switch {
case isWhiteSpace(c) || errors.Is(err, io.EOF):
s = nssDone
} else if isDigit(c) {
case isDigit(c):
s = nssSawFractionDigits
b.WriteByte(c)
} else {
default:
s = nssInvalid
}
}
Expand Down Expand Up @@ -490,12 +494,13 @@ func (js *jsonScanner) scanNumber(first byte) (*jsonToken, error) {
case '}', ']', ',':
s = nssDone
default:
if isWhiteSpace(c) || errors.Is(err, io.EOF) {
switch {
case isWhiteSpace(c) || errors.Is(err, io.EOF):
s = nssDone
} else if isDigit(c) {
case isDigit(c):
s = nssSawExponentDigits
b.WriteByte(c)
} else {
default:
s = nssInvalid
}
}
Expand Down
2 changes: 1 addition & 1 deletion bson/bsonrw/value_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ func (vw *valueWriter) writeLength() error {
return errMaxDocumentSizeExceeded{size: int64(len(vw.buf))}
}
frame := &vw.stack[vw.frame]
length = length - int(frame.start)
length -= int(frame.start)
start := frame.start

_ = vw.buf[start+3] // BCE
Expand Down
4 changes: 3 additions & 1 deletion bson/mgocompat/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ var allItems = []testItemType{
{bson.M{"_": primitive.Undefined{}}, // Obsolete, but still seen in the wild.
"\x06_\x00"},
{bson.M{"_": primitive.ObjectID{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B}},
"\x07_\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B"}, //technically this is not the same as the original mgo test
"\x07_\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B"}, // technically this is not the same as the original mgo test
{bson.M{"_": primitive.DBPointer{DB: "testnamespace", Pointer: primitive.ObjectID{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B}}},
"\x0C_\x00\x0e\x00\x00\x00testnamespace\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B"},
{bson.M{"_": false},
Expand Down Expand Up @@ -1581,6 +1581,8 @@ var jsonIDTests = []struct {

func TestObjectIdJSONMarshaling(t *testing.T) {
for i, test := range jsonIDTests {
test := test // Capture range variable.

t.Run(strconv.Itoa(i), func(t *testing.T) {
if test.marshal {
data, err := json.Marshal(&test.value)
Expand Down
6 changes: 2 additions & 4 deletions bson/primitive/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func (d Decimal128) String() string {
// Bits: 1*sign 2*ignored 14*exponent 111*significand.
// Implicit 0b100 prefix in significand.
exp = int(d.h >> 47 & (1<<14 - 1))
//high = 4<<47 | d.h&(1<<47-1)
// Spec says all of these values are out of range.
high, low = 0, 0
} else {
Expand Down Expand Up @@ -152,13 +151,12 @@ func (d Decimal128) BigInt() (*big.Int, int, error) {
// Bits: 1*sign 2*ignored 14*exponent 111*significand.
// Implicit 0b100 prefix in significand.
exp = int(high >> 47 & (1<<14 - 1))
//high = 4<<47 | d.h&(1<<47-1)
// Spec says all of these values are out of range.
high, low = 0, 0
} else {
// Bits: 1*sign 14*exponent 113*significand
exp = int(high >> 49 & (1<<14 - 1))
high = high & (1<<49 - 1)
high &= (1<<49 - 1)
}
exp += MinDecimal128Exp

Expand Down Expand Up @@ -352,7 +350,7 @@ var (

// ParseDecimal128FromBigInt attempts to parse the given significand and exponent into a valid Decimal128 value.
func ParseDecimal128FromBigInt(bi *big.Int, exp int) (Decimal128, bool) {
//copy
// copy
bi = new(big.Int).Set(bi)

q := new(big.Int)
Expand Down
Loading

0 comments on commit 03aa027

Please sign in to comment.