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

Do not use fjson #4175

Merged
merged 7 commits into from Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .golangci.yml
Expand Up @@ -43,6 +43,8 @@ linters-settings:
bson2:
files:
- $all
- "!**/internal/bson2/*_test.go"
- "!**/internal/util/testutil/*.go"
- "!**/internal/wire/*.go"
deny:
- pkg: github.com/FerretDB/FerretDB/internal/bson2
Expand Down
14 changes: 9 additions & 5 deletions integration/users/connection_test.go
Expand Up @@ -361,11 +361,15 @@ func TestAuthenticationLocalhostException(tt *testing.T) {
{"mechanisms", bson.A{mechanism}},
}
err = db.RunCommand(ctx, secondUser).Err()
integration.AssertEqualCommandError(t, mongo.CommandError{
Code: 18,
Name: "AuthenticationFailed",
Message: "Authentication failed",
}, err)
integration.AssertEqualCommandError(
t,
mongo.CommandError{
Code: 18,
Name: "AuthenticationFailed",
Message: "Authentication failed",
},
err,
)

credential := options.Credential{
AuthMechanism: mechanism,
Expand Down
36 changes: 0 additions & 36 deletions internal/bson/bson.go
Expand Up @@ -65,42 +65,6 @@ type bsontype interface {
encoding.BinaryMarshaler
}

// TODO https://github.com/FerretDB/FerretDB/issues/260
func fromBSON(v bsontype) any {
switch v := v.(type) {
case *Document:
return must.NotFail(types.ConvertDocument(v))
case *arrayType:
return pointer.To(types.Array(*v))
case *doubleType:
return float64(*v)
case *stringType:
return string(*v)
case *binaryType:
return types.Binary(*v)
case *objectIDType:
return types.ObjectID(*v)
case *boolType:
return bool(*v)
case *dateTimeType:
return time.Time(*v)
case *nullType:
return types.Null
case *regexType:
return types.Regex(*v)
case *int32Type:
return int32(*v)
case *timestampType:
return types.Timestamp(*v)
case *int64Type:
return int64(*v)
case *CString:
panic("CString should not be there")
}

panic(fmt.Sprintf("not reached: %T", v)) // for sumtype to work
}

// TODO https://github.com/FerretDB/FerretDB/issues/260
//
//nolint:deadcode,unused // remove later if it is not needed
Expand Down
15 changes: 0 additions & 15 deletions internal/bson/bson_test.go
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/FerretDB/FerretDB/internal/types/fjson"
"github.com/FerretDB/FerretDB/internal/util/testutil/testtb"
)

Expand Down Expand Up @@ -197,20 +196,6 @@ func fuzzBinary(f *testing.F, testCases []testCase, newFunc func() bsontype) {
require.NoError(t, err)
assert.Equal(t, expectedB, bw.Bytes(), "WriteTo results differ")
}

// Test that generated value can be marshaled for logging.
// Currently, that seems to be the best place to check it since generating values from BSON bytes is very easy.
{
// not a "real" type
if _, ok := v.(*CString); ok {
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
12 changes: 0 additions & 12 deletions internal/bson2/array.go
Expand Up @@ -144,18 +144,6 @@ func (arr *Array) LogValue() slog.Value {
return slogValue(arr, 1)
}

// 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, logMaxFlowLength, "", 1)
}

// LogMessageBlock is a variant of [Array.LogMessage] that never uses a flow style.
func (arr *Array) LogMessageBlock() string {
return logMessage(arr, 0, "", 1)
}

// check interfaces
var (
_ slog.LogValuer = (*Array)(nil)
Expand Down
8 changes: 7 additions & 1 deletion internal/bson2/bson2.go
Expand Up @@ -248,9 +248,15 @@
}
}

// convertFromTypes converts valid types package values to BSON values of that package.
// Convert converts valid types package values to BSON values of that package.
//
// Conversions of composite types may cause errors.
func Convert[T types.Type](v T) (any, error) {
return convertFromTypes(v)

Check warning on line 255 in internal/bson2/bson2.go

View check run for this annotation

Codecov / codecov/patch

internal/bson2/bson2.go#L254-L255

Added lines #L254 - L255 were not covered by tests
}

// convertFromTypes is a variant of [Convert] without type parameters (generics).
//
// Invalid types cause panics.
func convertFromTypes(v any) (any, error) {
switch v := v.(type) {
Expand Down