diff --git a/.golangci.yml b/.golangci.yml index 393b17e6d2b2..4db9da06965c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -26,14 +26,6 @@ linters-settings: desc: use `github.com/jackc/pgx/v5` package instead - 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" - deny: - - pkg: github.com/FerretDB/FerretDB/internal/types/fjson bson: files: - $all diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 38c6e5da529a..c6a12601a580 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -158,8 +158,6 @@ The `internal` subpackages contain most of the FerretDB code: - `types` package provides Go types matching BSON types that don't have built-in Go equivalents: we use `int32` for BSON's int32, but `types.ObjectID` for BSON's ObjectId. -- `types/fjson` provides converters from/to FJSON for built-in and `types` types. - It is used for logging of BSON values and wire protocol messages. - `bson` package provides converters from/to BSON for built-in and `types` types. - `wire` package provides wire protocol implementation. - `clientconn` package provides client connection implementation. diff --git a/internal/types/fjson/array.go b/internal/types/fjson/array.go deleted file mode 100644 index d4300e9588e3..000000000000 --- a/internal/types/fjson/array.go +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "bytes" - - "github.com/FerretDB/FerretDB/internal/types" - "github.com/FerretDB/FerretDB/internal/util/lazyerrors" -) - -// arrayType represents BSON Array type. -type arrayType types.Array - -// fjsontype implements fjsontype interface. -func (a *arrayType) fjsontype() {} - -// MarshalJSON implements fjsontype interface. -func (a *arrayType) MarshalJSON() ([]byte, error) { - var buf bytes.Buffer - buf.WriteByte('[') - - ta := types.Array(*a) - l := ta.Len() - for i := 0; i < l; i++ { - if i != 0 { - buf.WriteByte(',') - } - - el, err := ta.Get(i) - if err != nil { - return nil, lazyerrors.Error(err) - } - b, err := Marshal(el) - if err != nil { - return nil, lazyerrors.Error(err) - } - - buf.Write(b) - } - - buf.WriteByte(']') - return buf.Bytes(), nil -} - -// check interfaces -var ( - _ fjsontype = (*arrayType)(nil) -) diff --git a/internal/types/fjson/array_test.go b/internal/types/fjson/array_test.go deleted file mode 100644 index c80572540c0a..000000000000 --- a/internal/types/fjson/array_test.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "testing" - "time" - - "github.com/FerretDB/FerretDB/internal/types" - "github.com/FerretDB/FerretDB/internal/util/must" -) - -func convertArray(a *types.Array) *arrayType { - res := arrayType(*a) - return &res -} - -var arrayTestCases = []testCase{{ - name: "array_all", - v: convertArray(must.NotFail(types.NewArray( - must.NotFail(types.NewArray()), - types.Binary{Subtype: types.BinaryUser, B: []byte{0x42}}, - true, - time.Date(2021, 7, 27, 9, 35, 42, 123000000, time.UTC).Local(), - must.NotFail(types.NewDocument()), - 42.13, - int32(42), - int64(42), - "foo", - types.Null, - ))), - j: `[[],{"$b":"Qg==","s":128},true,{"$d":1627378542123},{"$k":[]},{"$f":42.13},42,{"$l":"42"},"foo",null]`, -}} - -func TestArray(t *testing.T) { - t.Parallel() - testJSON(t, arrayTestCases, func() fjsontype { return new(arrayType) }) -} diff --git a/internal/types/fjson/binary.go b/internal/types/fjson/binary.go deleted file mode 100644 index d5dee67f4480..000000000000 --- a/internal/types/fjson/binary.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "encoding/json" - - "github.com/FerretDB/FerretDB/internal/types" - "github.com/FerretDB/FerretDB/internal/util/lazyerrors" -) - -// binaryType represents BSON Binary data type. -type binaryType types.Binary - -// fjsontype implements fjsontype interface. -func (bin *binaryType) fjsontype() {} - -// binaryJSON is a JSON object representation of the binaryType. -type binaryJSON struct { - B []byte `json:"$b"` - S byte `json:"s"` -} - -// MarshalJSON implements fjsontype interface. -func (bin *binaryType) MarshalJSON() ([]byte, error) { - res, err := json.Marshal(binaryJSON{ - B: bin.B, - S: byte(bin.Subtype), - }) - if err != nil { - return nil, lazyerrors.Error(err) - } - return res, nil -} - -// check interfaces -var ( - _ fjsontype = (*binaryType)(nil) -) diff --git a/internal/types/fjson/binary_test.go b/internal/types/fjson/binary_test.go deleted file mode 100644 index 6bf80e11b2a2..000000000000 --- a/internal/types/fjson/binary_test.go +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "testing" - - "github.com/FerretDB/FerretDB/internal/types" -) - -var binaryTestCases = []testCase{{ - name: "foo", - v: &binaryType{ - Subtype: types.BinaryUser, - B: []byte("foo"), - }, - j: `{"$b":"Zm9v","s":128}`, -}, { - name: "empty", - v: &binaryType{ - Subtype: types.BinaryGeneric, - B: []byte{}, - }, - j: `{"$b":"","s":0}`, -}, { - name: "invalid subtype", - v: &binaryType{ - Subtype: 0xff, - B: []byte{}, - }, - j: `{"$b":"","s":255}`, -}} - -func TestBinary(t *testing.T) { - t.Parallel() - testJSON(t, binaryTestCases, func() fjsontype { return new(binaryType) }) -} diff --git a/internal/types/fjson/bool.go b/internal/types/fjson/bool.go deleted file mode 100644 index e22806151644..000000000000 --- a/internal/types/fjson/bool.go +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "encoding/json" - - "github.com/FerretDB/FerretDB/internal/util/lazyerrors" -) - -// boolType represents BSON Boolean type. -type boolType bool - -// fjsontype implements fjsontype interface. -func (b *boolType) fjsontype() {} - -// MarshalJSON implements fjsontype interface. -func (b *boolType) MarshalJSON() ([]byte, error) { - res, err := json.Marshal(bool(*b)) - if err != nil { - return nil, lazyerrors.Error(err) - } - return res, nil -} - -// check interfaces -var ( - _ fjsontype = (*boolType)(nil) -) diff --git a/internal/types/fjson/bool_test.go b/internal/types/fjson/bool_test.go deleted file mode 100644 index b4c894c44c0f..000000000000 --- a/internal/types/fjson/bool_test.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "testing" - - "github.com/AlekSi/pointer" -) - -var boolTestCases = []testCase{{ - name: "false", - v: pointer.To(boolType(false)), - j: `false`, -}, { - name: "true", - v: pointer.To(boolType(true)), - j: `true`, -}} - -func TestBool(t *testing.T) { - t.Parallel() - testJSON(t, boolTestCases, func() fjsontype { return new(boolType) }) -} diff --git a/internal/types/fjson/datetime.go b/internal/types/fjson/datetime.go deleted file mode 100644 index 9f9831b5e755..000000000000 --- a/internal/types/fjson/datetime.go +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "encoding/json" - "time" - - "github.com/FerretDB/FerretDB/internal/util/lazyerrors" -) - -// dateTimeType represents BSON UTC datetime type. -type dateTimeType time.Time - -// fjsontype implements fjsontype interface. -func (dt *dateTimeType) fjsontype() {} - -// String returns formatted time for debugging. -func (dt *dateTimeType) String() string { - return time.Time(*dt).Format(time.RFC3339Nano) -} - -// dateTimeJSON is a JSON object representation of the dateTimeType. -type dateTimeJSON struct { - D int64 `json:"$d"` -} - -// MarshalJSON implements fjsontype interface. -func (dt *dateTimeType) MarshalJSON() ([]byte, error) { - res, err := json.Marshal(dateTimeJSON{ - D: time.Time(*dt).UnixMilli(), - }) - if err != nil { - return nil, lazyerrors.Error(err) - } - return res, nil -} - -// check interfaces -var ( - _ fjsontype = (*dateTimeType)(nil) -) diff --git a/internal/types/fjson/datetime_test.go b/internal/types/fjson/datetime_test.go deleted file mode 100644 index c0616f321821..000000000000 --- a/internal/types/fjson/datetime_test.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "testing" - "time" - - "github.com/AlekSi/pointer" -) - -var dateTimeTestCases = []testCase{{ - name: "2021", - v: pointer.To(dateTimeType(time.Date(2021, 11, 1, 10, 18, 42, 123000000, time.UTC).Local())), - j: `{"$d":1635761922123}`, -}, { - name: "unix_zero", - v: pointer.To(dateTimeType(time.Unix(0, 0))), - j: `{"$d":0}`, -}, { - name: "0", - v: pointer.To(dateTimeType(time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC).Local())), - j: `{"$d":-62167219200000}`, -}, { - name: "9999", - v: pointer.To(dateTimeType(time.Date(9999, 12, 31, 23, 59, 59, 999000000, time.UTC).Local())), - j: `{"$d":253402300799999}`, -}} - -func TestDateTime(t *testing.T) { - t.Parallel() - testJSON(t, dateTimeTestCases, func() fjsontype { return new(dateTimeType) }) -} diff --git a/internal/types/fjson/document.go b/internal/types/fjson/document.go deleted file mode 100644 index 37d531d0dc14..000000000000 --- a/internal/types/fjson/document.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "bytes" - "encoding/json" - - "github.com/FerretDB/FerretDB/internal/types" - "github.com/FerretDB/FerretDB/internal/util/lazyerrors" -) - -// documentType represents BSON Document type. -type documentType types.Document - -// fjsontype implements fjsontype interface. -func (doc *documentType) fjsontype() {} - -// MarshalJSON implements fjsontype interface. -func (doc *documentType) MarshalJSON() ([]byte, error) { - td := types.Document(*doc) - - var buf bytes.Buffer - - buf.WriteString(`{"$k":`) - keys := td.Keys() - if keys == nil { - keys = []string{} - } - b, err := json.Marshal(keys) - if err != nil { - return nil, lazyerrors.Error(err) - } - buf.Write(b) - - values := td.Values() - - for i, key := range keys { - buf.WriteByte(',') - - if b, err = json.Marshal(key); err != nil { - return nil, lazyerrors.Error(err) - } - buf.Write(b) - buf.WriteByte(':') - - b, err := Marshal(values[i]) - if err != nil { - return nil, lazyerrors.Error(err) - } - - buf.Write(b) - } - - buf.WriteByte('}') - return buf.Bytes(), nil -} - -// check interfaces -var ( - _ fjsontype = (*documentType)(nil) -) diff --git a/internal/types/fjson/document_test.go b/internal/types/fjson/document_test.go deleted file mode 100644 index 560dd4ca2277..000000000000 --- a/internal/types/fjson/document_test.go +++ /dev/null @@ -1,217 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "testing" - "time" - - "github.com/FerretDB/FerretDB/internal/types" - "github.com/FerretDB/FerretDB/internal/util/must" -) - -func convertDocument(d *types.Document) *documentType { - res := documentType(*d) - return &res -} - -var ( - handshake1 = testCase{ - name: "handshake1", - v: convertDocument(must.NotFail(types.NewDocument( - "ismaster", true, - "client", must.NotFail(types.NewDocument( - "driver", must.NotFail(types.NewDocument( - "name", "nodejs", - "version", "4.0.0-beta.6", - )), - "os", must.NotFail(types.NewDocument( - "type", "Darwin", - "name", "darwin", - "architecture", "x64", - "version", "20.6.0", - )), - "platform", "Node.js v14.17.3, LE (unified)|Node.js v14.17.3, LE (unified)", - "application", must.NotFail(types.NewDocument( - "name", "mongosh 1.0.1", - )), - )), - "compression", must.NotFail(types.NewArray("none")), - "loadBalanced", false, - ))), - j: `{"$k":["ismaster","client","compression","loadBalanced"],"ismaster":true,` + - `"client":{"$k":["driver","os","platform","application"],"driver":{"$k":["name","version"],` + - `"name":"nodejs","version":"4.0.0-beta.6"},"os":{"$k":["type","name","architecture","version"],` + - `"type":"Darwin","name":"darwin","architecture":"x64","version":"20.6.0"},` + - `"platform":"Node.js v14.17.3, LE (unified)|Node.js v14.17.3, LE (unified)",` + - `"application":{"$k":["name"],"name":"mongosh 1.0.1"}},"compression":["none"],"loadBalanced":false}`, - } - - handshake2 = testCase{ - name: "handshake2", - v: convertDocument(must.NotFail(types.NewDocument( - "ismaster", true, - "client", must.NotFail(types.NewDocument( - "driver", must.NotFail(types.NewDocument( - "name", "nodejs", - "version", "4.0.0-beta.6", - )), - "os", must.NotFail(types.NewDocument( - "type", "Darwin", - "name", "darwin", - "architecture", "x64", - "version", "20.6.0", - )), - "platform", "Node.js v14.17.3, LE (unified)|Node.js v14.17.3, LE (unified)", - "application", must.NotFail(types.NewDocument( - "name", "mongosh 1.0.1", - )), - )), - "compression", must.NotFail(types.NewArray("none")), - "loadBalanced", false, - ))), - j: `{"$k":["ismaster","client","compression","loadBalanced"],"ismaster":true,` + - `"client":{"$k":["driver","os","platform","application"],"driver":{"$k":["name","version"],` + - `"name":"nodejs","version":"4.0.0-beta.6"},"os":{"$k":["type","name","architecture","version"],` + - `"type":"Darwin","name":"darwin","architecture":"x64","version":"20.6.0"},` + - `"platform":"Node.js v14.17.3, LE (unified)|Node.js v14.17.3, LE (unified)",` + - `"application":{"$k":["name"],"name":"mongosh 1.0.1"}},"compression":["none"],"loadBalanced":false}`, - } - - handshake3 = testCase{ - name: "handshake3", - v: convertDocument(must.NotFail(types.NewDocument( - "buildInfo", int32(1), - "lsid", must.NotFail(types.NewDocument( - "id", types.Binary{ - Subtype: types.BinaryUUID, - B: []byte{ - 0xa3, 0x19, 0xf2, 0xb4, 0xa1, 0x75, 0x40, 0xc7, - 0xb8, 0xe7, 0xa3, 0xa3, 0x2e, 0xc2, 0x56, 0xbe, - }, - }, - )), - "$db", "admin", - ))), - j: `{"$k":["buildInfo","lsid","$db"],"buildInfo":1,` + - `"lsid":{"$k":["id"],"id":{"$b":"oxnytKF1QMe456OjLsJWvg==","s":4}},"$db":"admin"}`, - } - - handshake4 = testCase{ - name: "handshake4", - v: convertDocument(must.NotFail(types.NewDocument( - "version", "5.0.0", - "gitVersion", "1184f004a99660de6f5e745573419bda8a28c0e9", - "modules", must.NotFail(types.NewArray()), - "allocator", "tcmalloc", - "javascriptEngine", "mozjs", - "sysInfo", "deprecated", - "versionArray", must.NotFail(types.NewArray(int32(5), int32(0), int32(0), int32(0))), - "openssl", must.NotFail(types.NewDocument( - "running", "OpenSSL 1.1.1f 31 Mar 2020", - "compiled", "OpenSSL 1.1.1f 31 Mar 2020", - )), - "buildEnvironment", must.NotFail(types.NewDocument( - "distmod", "ubuntu2004", - "distarch", "x86_64", - "cc", "/opt/mongodbtoolchain/v3/bin/gcc: gcc (GCC) 8.5.0", - "ccflags", "-Werror -include mongo/platform/basic.h -fasynchronous-unwind-tables -ggdb "+ - "-Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -fno-omit-frame-pointer "+ - "-fno-strict-aliasing -O2 -march=sandybridge -mtune=generic -mprefer-vector-width=128 "+ - "-Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations "+ - "-Wno-unused-const-variable -Wno-unused-but-set-variable -Wno-missing-braces "+ - "-fstack-protector-strong -Wa,--nocompress-debug-sections -fno-builtin-memcmp", - "cxx", "/opt/mongodbtoolchain/v3/bin/g++: g++ (GCC) 8.5.0", - "cxxflags", "-Woverloaded-virtual -Wno-maybe-uninitialized -fsized-deallocation -std=c++17", - "linkflags", "-Wl,--fatal-warnings -pthread -Wl,-z,now -fuse-ld=gold -fstack-protector-strong "+ - "-Wl,--no-threads -Wl,--build-id -Wl,--hash-style=gnu -Wl,-z,noexecstack -Wl,--warn-execstack "+ - "-Wl,-z,relro -Wl,--compress-debug-sections=none -Wl,-z,origin -Wl,--enable-new-dtags", - "target_arch", "x86_64", - "target_os", "linux", - "cppdefines", "SAFEINT_USE_INTRINSICS 0 PCRE_STATIC NDEBUG _XOPEN_SOURCE 700 _GNU_SOURCE "+ - "_REENTRANT 1 _FORTIFY_SOURCE 2 BOOST_THREAD_VERSION 5 BOOST_THREAD_USES_DATETIME "+ - "BOOST_SYSTEM_NO_DEPRECATED BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS "+ - "BOOST_ENABLE_ASSERT_DEBUG_HANDLER BOOST_LOG_NO_SHORTHAND_NAMES BOOST_LOG_USE_NATIVE_SYSLOG "+ - "BOOST_LOG_WITHOUT_THREAD_ATTR ABSL_FORCE_ALIGNED_ACCESS", - )), - "bits", int32(64), - "debug", false, - "maxBsonObjectSize", int32(16777216), - "storageEngines", must.NotFail(types.NewArray("devnull", "ephemeralForTest", "wiredTiger")), - "ok", float64(1), - ))), - j: `{"$k":["version","gitVersion","modules","allocator","javascriptEngine","sysInfo","versionArray",` + - `"openssl","buildEnvironment","bits","debug","maxBsonObjectSize","storageEngines","ok"],` + - `"version":"5.0.0","gitVersion":"1184f004a99660de6f5e745573419bda8a28c0e9","modules":[],` + - `"allocator":"tcmalloc","javascriptEngine":"mozjs","sysInfo":"deprecated","versionArray":[5,0,0,0],` + - `"openssl":{"$k":["running","compiled"],"running":"OpenSSL 1.1.1f 31 Mar 2020",` + - `"compiled":"OpenSSL 1.1.1f 31 Mar 2020"},` + - `"buildEnvironment":{"$k":["distmod","distarch","cc","ccflags","cxx","cxxflags","linkflags",` + - `"target_arch","target_os","cppdefines"],"distmod":"ubuntu2004","distarch":"x86_64",` + - `"cc":"/opt/mongodbtoolchain/v3/bin/gcc: gcc (GCC) 8.5.0",` + - `"ccflags":"-Werror -include mongo/platform/basic.h -fasynchronous-unwind-tables -ggdb -Wall ` + - `-Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -fno-omit-frame-pointer -fno-strict-aliasing ` + - `-O2 -march=sandybridge -mtune=generic -mprefer-vector-width=128 -Wno-unused-local-typedefs ` + - `-Wno-unused-function -Wno-deprecated-declarations -Wno-unused-const-variable ` + - `-Wno-unused-but-set-variable -Wno-missing-braces -fstack-protector-strong ` + - `-Wa,--nocompress-debug-sections -fno-builtin-memcmp",` + - `"cxx":"/opt/mongodbtoolchain/v3/bin/g++: g++ (GCC) 8.5.0",` + - `"cxxflags":"-Woverloaded-virtual -Wno-maybe-uninitialized -fsized-deallocation -std=c++17",` + - `"linkflags":"-Wl,--fatal-warnings -pthread -Wl,-z,now -fuse-ld=gold -fstack-protector-strong ` + - `-Wl,--no-threads -Wl,--build-id -Wl,--hash-style=gnu -Wl,-z,noexecstack -Wl,--warn-execstack ` + - `-Wl,-z,relro -Wl,--compress-debug-sections=none -Wl,-z,origin -Wl,--enable-new-dtags",` + - `"target_arch":"x86_64","target_os":"linux",` + - `"cppdefines":"SAFEINT_USE_INTRINSICS 0 PCRE_STATIC NDEBUG _XOPEN_SOURCE 700 _GNU_SOURCE ` + - `_REENTRANT 1 _FORTIFY_SOURCE 2 BOOST_THREAD_VERSION 5 BOOST_THREAD_USES_DATETIME ` + - `BOOST_SYSTEM_NO_DEPRECATED BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS BOOST_ENABLE_ASSERT_DEBUG_HANDLER ` + - `BOOST_LOG_NO_SHORTHAND_NAMES BOOST_LOG_USE_NATIVE_SYSLOG BOOST_LOG_WITHOUT_THREAD_ATTR ` + - `ABSL_FORCE_ALIGNED_ACCESS"},"bits":64,"debug":false,"maxBsonObjectSize":16777216,` + - `"storageEngines":["devnull","ephemeralForTest","wiredTiger"],"ok":{"$f":1}}`, - } - - all = testCase{ - name: "all", - v: convertDocument(must.NotFail(types.NewDocument( - "binary", must.NotFail(types.NewArray( - types.Binary{Subtype: types.BinaryUser, B: []byte{0x42}}, - types.Binary{Subtype: types.BinaryGeneric, B: []byte{}}, - )), - "bool", must.NotFail(types.NewArray(true, false)), - "datetime", must.NotFail(types.NewArray( - time.Date(2021, 7, 27, 9, 35, 42, 123000000, time.UTC).Local(), - time.Time{}.Local(), - )), - "double", must.NotFail(types.NewArray(42.13, 0.0)), - "int32", must.NotFail(types.NewArray(int32(42), int32(0))), - "int64", must.NotFail(types.NewArray(int64(42), int64(0))), - "objectID", must.NotFail(types.NewArray(types.ObjectID{0x42}, types.ObjectID{})), - "string", must.NotFail(types.NewArray("foo", "")), - "timestamp", must.NotFail(types.NewArray(types.Timestamp(42), types.Timestamp(0))), - ))), - j: `{"$k":["binary","bool","datetime","double","int32","int64","objectID","string","timestamp"],` + - `"binary":[{"$b":"Qg==","s":128},{"$b":"","s":0}],"bool":[true,false],` + - `"datetime":[{"$d":1627378542123},{"$d":-62135596800000}],"double":[{"$f":42.13},{"$f":0}],` + - `"int32":[42,0],"int64":[{"$l":"42"},{"$l":"0"}],` + - `"objectID":[{"$o":"420000000000000000000000"},{"$o":"000000000000000000000000"}],` + - `"string":["foo",""],"timestamp":[{"$t":"42"},{"$t":"0"}]}`, - } - - documentTestCases = []testCase{handshake1, handshake2, handshake3, handshake4, all} -) - -func TestDocument(t *testing.T) { - t.Parallel() - testJSON(t, documentTestCases, func() fjsontype { return new(documentType) }) -} diff --git a/internal/types/fjson/double.go b/internal/types/fjson/double.go deleted file mode 100644 index 3f4dd36a9d67..000000000000 --- a/internal/types/fjson/double.go +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "encoding/json" - "math" - - "github.com/FerretDB/FerretDB/internal/util/lazyerrors" -) - -// doubleType represents BSON 64-bit binary floating point type. -type doubleType float64 - -// fjsontype implements fjsontype interface. -func (d *doubleType) fjsontype() {} - -// doubleJSON is a JSON object representation of the doubleType. -type doubleJSON struct { - F any `json:"$f"` -} - -// MarshalJSON implements fjsontype interface. -func (d *doubleType) MarshalJSON() ([]byte, error) { - f := float64(*d) - var o doubleJSON - switch { - case f == 0 && math.Signbit(f): - o.F = "-0" - case math.IsInf(f, 1): - o.F = "Infinity" - case math.IsInf(f, -1): - o.F = "-Infinity" - case math.IsNaN(f): - o.F = "NaN" - default: - o.F = f - } - - res, err := json.Marshal(o) - if err != nil { - return nil, lazyerrors.Error(err) - } - return res, nil -} - -// check interfaces -var ( - _ fjsontype = (*doubleType)(nil) -) diff --git a/internal/types/fjson/double_test.go b/internal/types/fjson/double_test.go deleted file mode 100644 index 19cd01c27f5a..000000000000 --- a/internal/types/fjson/double_test.go +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "math" - "testing" - - "github.com/AlekSi/pointer" -) - -var doubleTestCases = []testCase{{ - name: "42.13", - v: pointer.To(doubleType(42.13)), - j: `{"$f":42.13}`, -}, { - name: "zero", - v: pointer.To(doubleType(math.Copysign(0, +1))), - j: `{"$f":0}`, -}, { - name: "negative zero", - v: pointer.To(doubleType(math.Copysign(0, -1))), - j: `{"$f":"-0"}`, -}, { - name: "max float64", - v: pointer.To(doubleType(math.MaxFloat64)), - j: `{"$f":1.7976931348623157e+308}`, -}, { - name: "smallest positive float64", - v: pointer.To(doubleType(math.SmallestNonzeroFloat64)), - j: `{"$f":5e-324}`, -}, { - name: "+Infinity", - v: pointer.To(doubleType(math.Inf(+1))), - j: `{"$f":"Infinity"}`, -}, { - name: "-Infinity", - v: pointer.To(doubleType(math.Inf(-1))), - j: `{"$f":"-Infinity"}`, -}, { - name: "NaN", - v: pointer.To(doubleType(math.NaN())), - j: `{"$f":"NaN"}`, -}} - -func TestDouble(t *testing.T) { - t.Parallel() - testJSON(t, doubleTestCases, func() fjsontype { return new(doubleType) }) -} diff --git a/internal/types/fjson/fjson.go b/internal/types/fjson/fjson.go deleted file mode 100644 index 8ebfb0652fbc..000000000000 --- a/internal/types/fjson/fjson.go +++ /dev/null @@ -1,148 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Package fjson provides converters to FJSON (JSON with some extensions) for built-in and `types` types. -// -// See contributing guidelines and documentation for package `types` for details. -// -// # Mapping -// -// Composite types -// -// Alias types package fjson package JSON representation -// -// object *types.Document *fjson.documentType {"$k": ["", "", ...], "": , "": , ...} -// array *types.Array *fjson.arrayType JSON array -// -// Scalar types -// -// Alias types package fjson package JSON representation -// -// double float64 *fjson.doubleType {"$f": JSON number} or {"$f": "Infinity|-Infinity|NaN"} -// string string *fjson.stringType JSON string -// binData types.Binary *fjson.binaryType {"$b": "", "s": } -// objectId types.ObjectID *fjson.objectIDType {"$o": "", "o": ""} -// int int32 *fjson.int32Type JSON number -// timestamp types.Timestamp *fjson.timestampType {"$t": ""} -// long int64 *fjson.int64Type {"$l": ""} -// -//nolint:lll // for readability -//nolint:dupword // false positive -package fjson - -import ( - "encoding/json" - "fmt" - "time" - - "github.com/AlekSi/pointer" - - "github.com/FerretDB/FerretDB/internal/types" - "github.com/FerretDB/FerretDB/internal/util/lazyerrors" -) - -// fjsontype is a type that can be marshaled to FJSON. -// -//sumtype:decl -type fjsontype interface { - fjsontype() // seal for sumtype - - json.Marshaler -} - -// fromFJSON converts fjsontype value to matching built-in or types' package value. -func fromFJSON(v fjsontype) any { - switch v := v.(type) { - case *documentType: - return pointer.To(types.Document(*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) - } - - panic(fmt.Sprintf("not reached: %T", v)) // for sumtype to work -} - -// toFJSON converts built-in or types' package value to fjsontype value. -func toFJSON(v any) fjsontype { - switch v := v.(type) { - case *types.Document: - return pointer.To(documentType(*v)) - case *types.Array: - return pointer.To(arrayType(*v)) - case float64: - return pointer.To(doubleType(v)) - case string: - return pointer.To(stringType(v)) - case types.Binary: - return pointer.To(binaryType(v)) - case types.ObjectID: - return pointer.To(objectIDType(v)) - case bool: - return pointer.To(boolType(v)) - case time.Time: - return pointer.To(dateTimeType(v)) - case types.NullType: - return pointer.To(nullType(v)) - case types.Regex: - return pointer.To(regexType(v)) - case int32: - return pointer.To(int32Type(v)) - case types.Timestamp: - return pointer.To(timestampType(v)) - case int64: - return pointer.To(int64Type(v)) - } - - panic(fmt.Sprintf("not reached: %T", v)) // for sumtype to work -} - -// Marshal encodes given built-in or types' package value into fjson. -func Marshal(v any) ([]byte, error) { - if v == nil { - panic("v is nil") - } - - b, err := toFJSON(v).MarshalJSON() - if err != nil { - return nil, lazyerrors.Error(err) - } - - return b, nil -} diff --git a/internal/types/fjson/fjson_test.go b/internal/types/fjson/fjson_test.go deleted file mode 100644 index e531c1397d48..000000000000 --- a/internal/types/fjson/fjson_test.go +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -type testCase struct { - name string - v fjsontype - j string -} - -func testJSON(t *testing.T, testCases []testCase, newFunc func() fjsontype) { - for _, tc := range testCases { - tc := tc - t.Run(tc.name, func(t *testing.T) { - require.NotEmpty(t, tc.name, "name should not be empty") - require.NotNil(t, tc.v, "v should not be nil") - require.NotEmpty(t, tc.j, "j should not be empty") - - t.Parallel() - - t.Run("MarshalJSON", func(t *testing.T) { - t.Parallel() - - actualJ, err := tc.v.MarshalJSON() - require.NoError(t, err) - assert.Equal(t, tc.j, string(actualJ)) - }) - - t.Run("Marshal", func(t *testing.T) { - t.Parallel() - - actualJ, err := Marshal(fromFJSON(tc.v)) - require.NoError(t, err) - assert.Equal(t, tc.j, string(actualJ)) - }) - }) - } -} diff --git a/internal/types/fjson/int32.go b/internal/types/fjson/int32.go deleted file mode 100644 index 233961c9ed5a..000000000000 --- a/internal/types/fjson/int32.go +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "encoding/json" - - "github.com/FerretDB/FerretDB/internal/util/lazyerrors" -) - -// int32Type represents BSON 32-bit integer type. -type int32Type int32 - -// fjsontype implements fjsontype interface. -func (i *int32Type) fjsontype() {} - -// MarshalJSON implements fjsontype interface. -func (i *int32Type) MarshalJSON() ([]byte, error) { - res, err := json.Marshal(int32(*i)) - if err != nil { - return nil, lazyerrors.Error(err) - } - return res, nil -} - -// check interfaces -var ( - _ fjsontype = (*int32Type)(nil) -) diff --git a/internal/types/fjson/int32_test.go b/internal/types/fjson/int32_test.go deleted file mode 100644 index d5b9ad88ebcf..000000000000 --- a/internal/types/fjson/int32_test.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "math" - "testing" - - "github.com/AlekSi/pointer" -) - -var int32TestCases = []testCase{{ - name: "42", - v: pointer.To(int32Type(42)), - j: `42`, -}, { - name: "zero", - v: pointer.To(int32Type(0)), - j: `0`, -}, { - name: "max int32", - v: pointer.To(int32Type(math.MaxInt32)), - j: `2147483647`, -}, { - name: "min int32", - v: pointer.To(int32Type(math.MinInt32)), - j: `-2147483648`, -}} - -func TestInt32(t *testing.T) { - t.Parallel() - testJSON(t, int32TestCases, func() fjsontype { return new(int32Type) }) -} diff --git a/internal/types/fjson/int64.go b/internal/types/fjson/int64.go deleted file mode 100644 index 9da844df1611..000000000000 --- a/internal/types/fjson/int64.go +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "encoding/json" - - "github.com/FerretDB/FerretDB/internal/util/lazyerrors" -) - -// int64Type represents BSON 64-bit integer type. -type int64Type int64 - -// fjsontype implements fjsontype interface. -func (i *int64Type) fjsontype() {} - -// int64JSON is a JSON object representation of the int64Type. -type int64JSON struct { - L int64 `json:"$l,string"` -} - -// MarshalJSON implements fjsontype interface. -func (i *int64Type) MarshalJSON() ([]byte, error) { - res, err := json.Marshal(int64JSON{ - L: int64(*i), - }) - if err != nil { - return nil, lazyerrors.Error(err) - } - return res, nil -} - -// check interfaces -var ( - _ fjsontype = (*int64Type)(nil) -) diff --git a/internal/types/fjson/int64_test.go b/internal/types/fjson/int64_test.go deleted file mode 100644 index 0a1fa4456c41..000000000000 --- a/internal/types/fjson/int64_test.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "math" - "testing" - - "github.com/AlekSi/pointer" -) - -var int64TestCases = []testCase{{ - name: "42", - v: pointer.To(int64Type(42)), - j: `{"$l":"42"}`, -}, { - name: "zero", - v: pointer.To(int64Type(0)), - j: `{"$l":"0"}`, -}, { - name: "max int64", - v: pointer.To(int64Type(math.MaxInt64)), - j: `{"$l":"9223372036854775807"}`, -}, { - name: "min int64", - v: pointer.To(int64Type(math.MinInt64)), - j: `{"$l":"-9223372036854775808"}`, -}} - -func TestInt64(t *testing.T) { - t.Parallel() - testJSON(t, int64TestCases, func() fjsontype { return new(int64Type) }) -} diff --git a/internal/types/fjson/null.go b/internal/types/fjson/null.go deleted file mode 100644 index 410d490e750c..000000000000 --- a/internal/types/fjson/null.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import "github.com/FerretDB/FerretDB/internal/types" - -// nullType represents BSON Null type. -type nullType types.NullType - -// fjsontype implements fjsontype interface. -func (*nullType) fjsontype() {} - -// MarshalJSON implements fjsontype interface. -func (*nullType) MarshalJSON() ([]byte, error) { - return []byte("null"), nil -} - -// check interfaces -var ( - _ fjsontype = (*nullType)(nil) -) diff --git a/internal/types/fjson/null_test.go b/internal/types/fjson/null_test.go deleted file mode 100644 index 3cfe5d6fdc0d..000000000000 --- a/internal/types/fjson/null_test.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "testing" - - "github.com/AlekSi/pointer" -) - -var nullTestCases = []testCase{{ - name: "null", - v: pointer.To(nullType{}), - j: `null`, -}} - -func TestNull(t *testing.T) { - t.Parallel() - testJSON(t, nullTestCases, func() fjsontype { return new(nullType) }) -} diff --git a/internal/types/fjson/object_id.go b/internal/types/fjson/object_id.go deleted file mode 100644 index 1c79fddab10e..000000000000 --- a/internal/types/fjson/object_id.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "encoding/hex" - "encoding/json" - - "github.com/FerretDB/FerretDB/internal/types" - "github.com/FerretDB/FerretDB/internal/util/lazyerrors" -) - -// objectIDType represents BSON ObjectId type. -type objectIDType types.ObjectID - -// fjsontype implements fjsontype interface. -func (obj *objectIDType) fjsontype() {} - -// objectIDJSON is a JSON object representation of the objectIDType. -type objectIDJSON struct { - O string `json:"$o"` -} - -// MarshalJSON implements fjsontype interface. -func (obj *objectIDType) MarshalJSON() ([]byte, error) { - res, err := json.Marshal(objectIDJSON{ - O: hex.EncodeToString(obj[:]), - }) - if err != nil { - return nil, lazyerrors.Error(err) - } - return res, nil -} - -// check interfaces -var ( - _ fjsontype = (*objectIDType)(nil) -) diff --git a/internal/types/fjson/object_id_test.go b/internal/types/fjson/object_id_test.go deleted file mode 100644 index 7cacdfaa560b..000000000000 --- a/internal/types/fjson/object_id_test.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "testing" - - "github.com/AlekSi/pointer" -) - -var objectIDTestCases = []testCase{{ - name: "normal", - v: pointer.To(objectIDType{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}), - j: `{"$o":"010101010101010101010101"}`, -}} - -func TestObjectID(t *testing.T) { - t.Parallel() - testJSON(t, objectIDTestCases, func() fjsontype { return new(objectIDType) }) -} diff --git a/internal/types/fjson/regex.go b/internal/types/fjson/regex.go deleted file mode 100644 index 1c9dd8badd80..000000000000 --- a/internal/types/fjson/regex.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "encoding/json" - - "github.com/FerretDB/FerretDB/internal/types" - "github.com/FerretDB/FerretDB/internal/util/lazyerrors" -) - -// regexType represents BSON Regular expression type. -type regexType types.Regex - -// fjsontype implements fjsontype interface. -func (regex *regexType) fjsontype() {} - -// regexJSON is a JSON object representation of the regexType. -type regexJSON struct { - R string `json:"$r"` - O string `json:"o"` -} - -// MarshalJSON implements fjsontype interface. -func (regex *regexType) MarshalJSON() ([]byte, error) { - res, err := json.Marshal(regexJSON{ - R: regex.Pattern, - O: regex.Options, - }) - if err != nil { - return nil, lazyerrors.Error(err) - } - return res, nil -} - -// check interfaces -var ( - _ fjsontype = (*regexType)(nil) -) diff --git a/internal/types/fjson/regex_test.go b/internal/types/fjson/regex_test.go deleted file mode 100644 index 535349098361..000000000000 --- a/internal/types/fjson/regex_test.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "testing" - - "github.com/AlekSi/pointer" -) - -var regexTestCases = []testCase{{ - name: "normal", - v: pointer.To(regexType{Pattern: "hoffman", Options: "i"}), - j: `{"$r":"hoffman","o":"i"}`, -}, { - name: "empty", - v: pointer.To(regexType{Pattern: "", Options: ""}), - j: `{"$r":"","o":""}`, -}} - -func TestRegex(t *testing.T) { - t.Parallel() - testJSON(t, regexTestCases, func() fjsontype { return new(regexType) }) -} diff --git a/internal/types/fjson/string.go b/internal/types/fjson/string.go deleted file mode 100644 index 6a61ec3a286d..000000000000 --- a/internal/types/fjson/string.go +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "encoding/json" - - "github.com/FerretDB/FerretDB/internal/util/lazyerrors" -) - -// stringType represents BSON UTF-8 string type. -type stringType string - -// fjsontype implements fjsontype interface. -func (str *stringType) fjsontype() {} - -// MarshalJSON implements fjsontype interface. -func (str *stringType) MarshalJSON() ([]byte, error) { - res, err := json.Marshal(string(*str)) - if err != nil { - return nil, lazyerrors.Error(err) - } - return res, nil -} - -// check interfaces -var ( - _ fjsontype = (*stringType)(nil) -) diff --git a/internal/types/fjson/string_test.go b/internal/types/fjson/string_test.go deleted file mode 100644 index 17a8f61d949d..000000000000 --- a/internal/types/fjson/string_test.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "testing" - - "github.com/AlekSi/pointer" -) - -var stringTestCases = []testCase{{ - name: "foo", - v: pointer.To(stringType("foo")), - j: `"foo"`, -}, { - name: "empty", - v: pointer.To(stringType("")), - j: `""`, -}, { - name: "zero", - v: pointer.To(stringType("\x00")), - j: `"\u0000"`, -}} - -func TestString(t *testing.T) { - t.Parallel() - testJSON(t, stringTestCases, func() fjsontype { return new(stringType) }) -} diff --git a/internal/types/fjson/timestamp.go b/internal/types/fjson/timestamp.go deleted file mode 100644 index c337bfd16b85..000000000000 --- a/internal/types/fjson/timestamp.go +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "encoding/json" - - "github.com/FerretDB/FerretDB/internal/types" - "github.com/FerretDB/FerretDB/internal/util/lazyerrors" -) - -// timestampType represents BSON Timestamp type. -type timestampType types.Timestamp - -// fjsontype implements fjsontype interface. -func (ts *timestampType) fjsontype() {} - -// timestampJSON is a JSON object representation of the timestampType. -type timestampJSON struct { - T uint64 `json:"$t,string"` -} - -// MarshalJSON implements fjsontype interface. -func (ts *timestampType) MarshalJSON() ([]byte, error) { - res, err := json.Marshal(timestampJSON{ - T: uint64(*ts), - }) - if err != nil { - return nil, lazyerrors.Error(err) - } - return res, nil -} - -// check interfaces -var ( - _ fjsontype = (*timestampType)(nil) -) diff --git a/internal/types/fjson/timestamp_test.go b/internal/types/fjson/timestamp_test.go deleted file mode 100644 index 804630995529..000000000000 --- a/internal/types/fjson/timestamp_test.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2021 FerretDB Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package fjson - -import ( - "testing" - - "github.com/AlekSi/pointer" -) - -var timestampTestCases = []testCase{{ - name: "one", - v: pointer.To(timestampType(1)), - j: `{"$t":"1"}`, -}, { - name: "zero", - v: pointer.To(timestampType(0)), - j: `{"$t":"0"}`, -}} - -func TestTimestamp(t *testing.T) { - t.Parallel() - testJSON(t, timestampTestCases, func() fjsontype { return new(timestampType) }) -} diff --git a/internal/types/types.go b/internal/types/types.go index e4e541ba2302..137dc35d8fb7 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -14,12 +14,11 @@ // Package types provides Go types matching BSON types that don't have built-in Go equivalents. // -// All BSON types have four representations in FerretDB: +// All BSON types have three representations in FerretDB: // // 1. As they are used in "business logic" / handlers - `types` package. -// 2. As they are used for logging - `fjson` package. -// 3. As they are used in the wire protocol implementation - `bson` package. -// 4. As they are used to store data in SQL based databases - `sjson` package. +// 2. As they are used in the wire protocol implementation and for logging - `bson` package. +// 3. As they are used to store data in SQL based databases - `sjson` package. // // The reason for that is a separation of concerns: to avoid method names clashes, to simplify type asserts, // to make refactorings and optimizations easier, etc.