Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve bson2 and wire logging #4148

Merged
merged 31 commits into from Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci-new.yml
Expand Up @@ -81,6 +81,7 @@ linters:
- lll
- misspell
- nolintlint
- sloglint
- unused
- whitespace

Expand Down
12 changes: 11 additions & 1 deletion .golangci.yml
Expand Up @@ -27,11 +27,11 @@ linters-settings:
- pkg: github.com/jackc/pgx/v4
desc: use `github.com/jackc/pgx/v5` package instead
fjson:
# TODO https://github.com/FerretDB/FerretDB/issues/4157
files:
- $all
- "!**/internal/bson/*_test.go"
- "!**/internal/util/testutil/*.go"
- "!**/internal/wire/*.go"
deny:
- pkg: github.com/FerretDB/FerretDB/internal/types/fjson
bson:
Expand Down Expand Up @@ -209,6 +209,15 @@ linters-settings:
ignore-generated-header: true
severity: error
rules: []
sloglint:
no-mixed-args: true
kv-only: false
attr-only: true
context-only: false # https://github.com/go-simpler/sloglint/issues/29
static-msg: false # TODO https://github.com/FerretDB/FerretDB/issues/3421
no-raw-keys: false # TODO https://github.com/FerretDB/FerretDB/issues/3421
key-naming-case: snake
args-on-sep-lines: false
staticcheck:
checks:
- all
Expand Down Expand Up @@ -241,6 +250,7 @@ linters:
- misspell
- nolintlint
- revive
- sloglint
- staticcheck
- unused
- whitespace
Expand Down
1 change: 1 addition & 0 deletions integration/.golangci-new.yml
Expand Up @@ -73,6 +73,7 @@ linters:
- lll
- misspell
- nolintlint
- sloglint
- unused
- whitespace

Expand Down
10 changes: 10 additions & 0 deletions integration/.golangci.yml
Expand Up @@ -107,6 +107,15 @@ linters-settings:
ignore-generated-header: true
severity: error
rules: []
sloglint:
no-mixed-args: true
kv-only: false
attr-only: true
context-only: true
static-msg: true
no-raw-keys: false # TODO https://github.com/FerretDB/FerretDB/issues/3421
key-naming-case: snake
args-on-sep-lines: false
staticcheck:
checks:
- all
Expand Down Expand Up @@ -138,6 +147,7 @@ linters:
- misspell
- nolintlint
- revive
- sloglint
- staticcheck
- unused
- whitespace
Expand Down
31 changes: 16 additions & 15 deletions integration/basic_test.go
Expand Up @@ -553,11 +553,11 @@ func TestDebugError(t *testing.T) {
}

func TestCheckingNestedDocuments(t *testing.T) {
t.Parallel()
t.Skip("https://github.com/FerretDB/FerretDB/issues/3759")

for name, tc := range map[string]struct {
doc any
err error
err *mongo.CommandError
}{
"1ok": {
doc: CreateNestedDocument(1),
Expand All @@ -573,24 +573,29 @@ func TestCheckingNestedDocuments(t *testing.T) {
},
"180fail": {
doc: CreateNestedDocument(180),
err: fmt.Errorf("bson.Array.ReadFrom (document has exceeded the max supported nesting: 179."),
err: &mongo.CommandError{
Message: "bson.Array.ReadFrom (document has exceeded the max supported nesting: 179.",
},
},
"180endedWithDocumentFail": {
doc: bson.D{{"v", CreateNestedDocument(179)}},
err: fmt.Errorf("bson.Document.ReadFrom (document has exceeded the max supported nesting: 179."),
err: &mongo.CommandError{
Message: "bson.Document.ReadFrom (document has exceeded the max supported nesting: 179.",
},
},
"1000fail": {
doc: CreateNestedDocument(1000),
err: fmt.Errorf("bson.Document.ReadFrom (document has exceeded the max supported nesting: 179."),
err: &mongo.CommandError{
Message: "bson.Document.ReadFrom (document has exceeded the max supported nesting: 179.",
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx, collection := setup.Setup(t)

_, err := collection.InsertOne(ctx, tc.doc)
if tc.err != nil {
require.Error(t, tc.err)
AssertEqualCommandError(t, *tc.err, err)
return
}

Expand Down Expand Up @@ -699,23 +704,19 @@ func TestMutatingClientMetadata(t *testing.T) {
},
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

res := db.RunCommand(ctx, tc.command)
var res bson.D

err := db.RunCommand(ctx, tc.command).Decode(&res)
if tc.err != nil {
err := res.Decode(&res)
AssertEqualCommandError(t, *tc.err, err)
return
}

var actualRes bson.D
err := res.Decode(&actualRes)

require.NoError(t, err)
require.NotNil(t, actualRes)
require.NotNil(t, res)
})
}
}
2 changes: 0 additions & 2 deletions integration/create_test.go
Expand Up @@ -176,8 +176,6 @@ func TestCreateOnInsertStressDiffCollection(t *testing.T) {
}

func TestCreateStressSameCollection(tt *testing.T) {
tt.Parallel()

t := setup.FailsForFerretDB(tt, "https://github.com/FerretDB/FerretDB/issues/3853")

// It should be rewritten to use teststress.Stress.
Expand Down
1 change: 1 addition & 0 deletions internal/bson/bson_test.go
Expand Up @@ -206,6 +206,7 @@ func fuzzBinary(f *testing.F, testCases []testCase, newFunc func() bsontype) {
t.Skip()
}

// TODO https://github.com/FerretDB/FerretDB/issues/4157
mB, err := fjson.Marshal(fromBSON(v))
require.NoError(t, err)
assert.NotEmpty(t, mB)
Expand Down
40 changes: 40 additions & 0 deletions internal/bson2/array.go
Expand Up @@ -31,6 +31,28 @@
elements []any
}

// NewArray creates a new Array from the given values.
func NewArray(values ...any) (*Array, error) {
res := &Array{
elements: make([]any, 0, len(values)),
}

for i, v := range values {
if err := res.Add(v); err != nil {
return nil, lazyerrors.Errorf("%d: %w", i, err)

Check warning on line 42 in internal/bson2/array.go

View check run for this annotation

Codecov / codecov/patch

internal/bson2/array.go#L42

Added line #L42 was not covered by tests
}
}

return res, nil
}

// MakeArray creates a new empty Array with the given capacity.
func MakeArray(cap int) *Array {
return &Array{
elements: make([]any, 0, cap),
}
}

// ConvertArray converts [*types.Array] to Array.
func ConvertArray(arr *types.Array) (*Array, error) {
iter := arr.Iterator()
Expand Down Expand Up @@ -80,6 +102,17 @@
return res, nil
}

// Add adds a new element to the Array.
func (arr *Array) Add(value any) error {
if err := validBSONType(value); err != nil {
return lazyerrors.Error(err)

Check warning on line 108 in internal/bson2/array.go

View check run for this annotation

Codecov / codecov/patch

internal/bson2/array.go#L108

Added line #L108 was not covered by tests
}

arr.elements = append(arr.elements, value)

return nil
}

// Encode encodes BSON array.
//
// TODO https://github.com/FerretDB/FerretDB/issues/3759
Expand Down Expand Up @@ -111,6 +144,13 @@
return slogValue(arr)
}

// LogMessage returns an indented representation as a string,
// somewhat similar (but not identical) to JSON or Go syntax.
// It may change over time.
func (arr *Array) LogMessage() string {
return logMessage(arr)

Check warning on line 151 in internal/bson2/array.go

View check run for this annotation

Codecov / codecov/patch

internal/bson2/array.go#L150-L151

Added lines #L150 - L151 were not covered by tests
}

// check interfaces
var (
_ slog.LogValuer = (*Array)(nil)
Expand Down
2 changes: 1 addition & 1 deletion internal/bson2/bson2.go
Expand Up @@ -213,7 +213,7 @@ func convertToTypes(v any) (any, error) {
case string:
return v, nil
case Binary:
// Special case to prevent it from being stored as null in sjson / logged as null in fjson.
// Special case to prevent it from being stored as null in sjson.
// TODO https://github.com/FerretDB/FerretDB/issues/260
if v.B == nil {
v.B = []byte{}
Expand Down