Skip to content

Commit

Permalink
Do not use fjson (#4175)
Browse files Browse the repository at this point in the history
Closes #4157.
  • Loading branch information
AlekSi committed Mar 13, 2024
1 parent c89b88e commit 34a6fa5
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 271 deletions.
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 @@ func convertToTypes(v any) (any, error) {
}
}

// 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)
}

// 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

0 comments on commit 34a6fa5

Please sign in to comment.