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

Use types.Null instead of nil #268

Merged
merged 2 commits into from
Jan 16, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ jobs:
git config user.name ferretdb-bot
git config user.email ferretdb-bot@ferretdb.io
git add --all .
git commit --message 'Update corpus'
git diff-index --quiet HEAD || git commit --message 'Update corpus'
git fetch
git rebase origin/main
git push
Expand Down
2 changes: 1 addition & 1 deletion internal/bson/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// Array represents BSON Array data type.
// Array represents BSON Array type.
type Array types.Array

func (a *Array) bsontype() {}
Expand Down
2 changes: 1 addition & 1 deletion internal/bson/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var arrayTestCases = []testCase{{
int32(42),
int64(42),
"foo",
nil,
types.Null,
)),
b: testutil.MustParseDumpFile("testdata", "array_all.hex"),
}, {
Expand Down
2 changes: 1 addition & 1 deletion internal/bson/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// Bool represents BSON Bool data type.
// Bool represents BSON Boolean type.
type Bool bool

func (b *Bool) bsontype() {}
Expand Down
8 changes: 4 additions & 4 deletions internal/bson/bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func fromBSON(v bsontype) any {
return bool(*v)
case *DateTime:
return time.Time(*v)
case nil:
return nil
case *nullType:
return types.Null
case *Regex:
return types.Regex(*v)
case *Int32:
Expand Down Expand Up @@ -93,8 +93,8 @@ func toBSON(v any) bsontype {
return pointer.To(Bool(v))
case time.Time:
return pointer.To(DateTime(v))
case nil:
return nil
case types.NullType:
return pointer.To(nullType(v))
case types.Regex:
return pointer.To(Regex(v))
case int32:
Expand Down
2 changes: 1 addition & 1 deletion internal/bson/cstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// CString represents BSON CString data type.
// CString represents BSON zero-terminated UTF-8 string type.
type CString string

func (cstr *CString) bsontype() {}
Expand Down
2 changes: 1 addition & 1 deletion internal/bson/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// DateTime represents BSON DateTime data type.
// DateTime represents BSON UTC datetime type.
type DateTime time.Time

func (dt DateTime) String() string {
Expand Down
8 changes: 5 additions & 3 deletions internal/bson/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type document interface {
Keys() []string
}

// Document represents BSON Document data type.
// Document represents BSON Document type.
type Document struct {
m map[string]any
keys []string
Expand Down Expand Up @@ -204,7 +204,8 @@ func (doc *Document) ReadFrom(r *bufio.Reader) error {
doc.m[string(ename)] = time.Time(v)

case tagNull:
doc.m[string(ename)] = nil
// skip calling ReadFrom that does nothing
doc.m[string(ename)] = types.Null

case tagRegex:
var v Regex
Expand Down Expand Up @@ -352,11 +353,12 @@ func (doc Document) MarshalBinary() ([]byte, error) {
return nil, lazyerrors.Error(err)
}

case nil:
case types.NullType:
bufw.WriteByte(byte(tagNull))
if err := ename.WriteTo(bufw); err != nil {
return nil, lazyerrors.Error(err)
}
// skip calling WriteTo that does nothing

case types.Regex:
bufw.WriteByte(byte(tagRegex))
Expand Down
2 changes: 1 addition & 1 deletion internal/bson/double.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// Double represents BSON Double data type.
// Double represents BSON 64-bit binary floating point type.
type Double float64

func (d *Double) bsontype() {}
Expand Down
2 changes: 1 addition & 1 deletion internal/bson/int32.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// Int32 represents BSON Int32 data type.
// Int32 represents BSON 32-bit integer type.
type Int32 int32

func (i *Int32) bsontype() {}
Expand Down
2 changes: 1 addition & 1 deletion internal/bson/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// Int64 represents BSON Int64 data type.
// Int64 represents BSON 64-bit integer type.
type Int64 int64

func (i *Int64) bsontype() {}
Expand Down
46 changes: 46 additions & 0 deletions internal/bson/null.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 bson

import (
"bufio"

"github.com/FerretDB/FerretDB/internal/types"
)

// nullType represents BSON Null type.
type nullType types.NullType

func (*nullType) bsontype() {}

// ReadFrom implements bsontype interface.
func (*nullType) ReadFrom(r *bufio.Reader) error {
return nil
}

// WriteTo implements bsontype interface.
func (nullType) WriteTo(w *bufio.Writer) error {
return nil
}

// MarshalBinary implements bsontype interface.
func (nullType) MarshalBinary() ([]byte, error) {
return nil, nil
}

// check interfaces
var (
_ bsontype = (*nullType)(nil)
)
2 changes: 1 addition & 1 deletion internal/bson/objectid.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// ObjectID represents BSON ObjectID data type.
// ObjectID represents BSON ObjectId type.
type ObjectID types.ObjectID

func (obj *ObjectID) bsontype() {}
Expand Down
2 changes: 1 addition & 1 deletion internal/bson/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// Regex represents BSON Regex data type.
// Regex represents BSON Regular expression type.
type Regex types.Regex

func (regex *Regex) bsontype() {}
Expand Down
2 changes: 1 addition & 1 deletion internal/bson/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// String represents BSON String data type.
// String represents BSON UTF-8 string type.
type String string

func (str *String) bsontype() {}
Expand Down
2 changes: 1 addition & 1 deletion internal/bson/timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// Timestamp represents BSON Timestamp data type.
// Timestamp represents BSON Timestamp type.
type Timestamp types.Timestamp

func (ts *Timestamp) bsontype() {}
Expand Down
14 changes: 7 additions & 7 deletions internal/fjson/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// fjsonArray represents BSON fjsonArray data type.
type fjsonArray types.Array
// arrayType represents BSON Array type.
type arrayType types.Array

// fjsontype implements fjsontype interface.
func (a *fjsonArray) fjsontype() {}
func (a *arrayType) fjsontype() {}

// UnmarshalJSON implements fjsontype interface.
func (a *fjsonArray) UnmarshalJSON(data []byte) error {
func (a *arrayType) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, []byte("null")) {
panic("null data")
}
Expand Down Expand Up @@ -57,12 +57,12 @@ func (a *fjsonArray) UnmarshalJSON(data []byte) error {
}
}

*a = fjsonArray(*ta)
*a = arrayType(*ta)
return nil
}

// MarshalJSON implements fjsontype interface.
func (a *fjsonArray) MarshalJSON() ([]byte, error) {
func (a *arrayType) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
buf.WriteByte('[')

Expand Down Expand Up @@ -91,5 +91,5 @@ func (a *fjsonArray) MarshalJSON() ([]byte, error) {

// check interfaces
var (
_ fjsontype = (*fjsonArray)(nil)
_ fjsontype = (*arrayType)(nil)
)
12 changes: 6 additions & 6 deletions internal/fjson/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"github.com/FerretDB/FerretDB/internal/types"
)

func convertArray(a *types.Array) *fjsonArray {
res := fjsonArray(*a)
func convertArray(a *types.Array) *arrayType {
res := arrayType(*a)
return &res
}

Expand All @@ -38,7 +38,7 @@ var arrayTestCases = []testCase{{
int32(42),
int64(42),
"foo",
nil,
types.Null,
)),
j: `[[],{"$b":"Qg==","s":128},true,{"$d":1627378542123},{"$k":[]},{"$f":42.13},42,{"$l":"42"},"foo",null]`,
}, {
Expand All @@ -49,13 +49,13 @@ var arrayTestCases = []testCase{{

func TestArray(t *testing.T) {
t.Parallel()
testJSON(t, arrayTestCases, func() fjsontype { return new(fjsonArray) })
testJSON(t, arrayTestCases, func() fjsontype { return new(arrayType) })
}

func FuzzArray(f *testing.F) {
fuzzJSON(f, arrayTestCases, func() fjsontype { return new(fjsonArray) })
fuzzJSON(f, arrayTestCases, func() fjsontype { return new(arrayType) })
}

func BenchmarkArray(b *testing.B) {
benchmark(b, arrayTestCases, func() fjsontype { return new(fjsonArray) })
benchmark(b, arrayTestCases, func() fjsontype { return new(arrayType) })
}
12 changes: 6 additions & 6 deletions internal/fjson/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ import (
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// fjsonBinary represents BSON fjsonBinary data type.
type fjsonBinary types.Binary
// binaryType represents BSON Binary data type.
type binaryType types.Binary

// fjsontype implements fjsontype interface.
func (bin *fjsonBinary) fjsontype() {}
func (bin *binaryType) fjsontype() {}

type binaryJSON struct {
B []byte `json:"$b"`
S byte `json:"s"`
}

// UnmarshalJSON implements fjsontype interface.
func (bin *fjsonBinary) UnmarshalJSON(data []byte) error {
func (bin *binaryType) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, []byte("null")) {
panic("null data")
}
Expand All @@ -58,7 +58,7 @@ func (bin *fjsonBinary) UnmarshalJSON(data []byte) error {
}

// MarshalJSON implements fjsontype interface.
func (bin *fjsonBinary) MarshalJSON() ([]byte, error) {
func (bin *binaryType) MarshalJSON() ([]byte, error) {
res, err := json.Marshal(binaryJSON{
B: bin.B,
S: byte(bin.Subtype),
Expand All @@ -71,5 +71,5 @@ func (bin *fjsonBinary) MarshalJSON() ([]byte, error) {

// check interfaces
var (
_ fjsontype = (*fjsonBinary)(nil)
_ fjsontype = (*binaryType)(nil)
)
14 changes: 7 additions & 7 deletions internal/fjson/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ import (

var binaryTestCases = []testCase{{
name: "foo",
v: &fjsonBinary{
v: &binaryType{
Subtype: types.BinaryUser,
B: []byte("foo"),
},
j: `{"$b":"Zm9v","s":128}`,
}, {
name: "empty",
v: &fjsonBinary{
v: &binaryType{
Subtype: types.BinaryGeneric,
B: []byte{},
},
j: `{"$b":""}`,
canonJ: `{"$b":"","s":0}`,
}, {
name: "invalid subtype",
v: &fjsonBinary{
v: &binaryType{
Subtype: 0xff,
B: []byte{},
},
j: `{"$b":"","s":255}`,
}, {
name: "extra JSON fields",
v: &fjsonBinary{
v: &binaryType{
Subtype: types.BinaryUser,
B: []byte("foo"),
},
Expand All @@ -59,13 +59,13 @@ var binaryTestCases = []testCase{{

func TestBinary(t *testing.T) {
t.Parallel()
testJSON(t, binaryTestCases, func() fjsontype { return new(fjsonBinary) })
testJSON(t, binaryTestCases, func() fjsontype { return new(binaryType) })
}

func FuzzBinary(f *testing.F) {
fuzzJSON(f, binaryTestCases, func() fjsontype { return new(fjsonBinary) })
fuzzJSON(f, binaryTestCases, func() fjsontype { return new(binaryType) })
}

func BenchmarkBinary(b *testing.B) {
benchmark(b, binaryTestCases, func() fjsontype { return new(fjsonBinary) })
benchmark(b, binaryTestCases, func() fjsontype { return new(binaryType) })
}
Loading