Skip to content

Commit

Permalink
Remove JSON methods from bson package (#259)
Browse files Browse the repository at this point in the history
Refs #182.
  • Loading branch information
AlekSi committed Jan 14, 2022
1 parent a8d03f5 commit e4e9e24
Show file tree
Hide file tree
Showing 40 changed files with 152 additions and 408 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ linters-settings:
exhaustive:
default-signifies-exhaustive: false
goconst:
min-occurrences: 5
ignore-tests: true
godot:
scope: toplevel
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.18

require (
github.com/AlekSi/pointer v1.2.0
github.com/davecgh/go-spew v1.1.1
github.com/jackc/pgconn v1.10.1
github.com/jackc/pgerrcode v0.0.0-20201024163028-a0d42d470451
github.com/jackc/pgx/v4 v4.14.1
Expand All @@ -22,6 +21,7 @@ require (
github.com/benbjohnson/clock v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
Expand Down
17 changes: 0 additions & 17 deletions internal/bson/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bufio"
"strconv"

"github.com/FerretDB/FerretDB/internal/fjson"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)
Expand Down Expand Up @@ -95,22 +94,6 @@ func (a Array) MarshalBinary() ([]byte, error) {
return b, nil
}

// UnmarshalJSON implements bsontype interface.
func (a *Array) UnmarshalJSON(data []byte) error {
v, err := fjson.Unmarshal(data)
if err != nil {
return lazyerrors.Error(err)
}

*a = *(toBSON(v).(*Array))
return nil
}

// MarshalJSON implements bsontype interface.
func (a Array) MarshalJSON() ([]byte, error) {
return fjson.Marshal(fromBSON(&a))
}

// check interfaces
var (
_ bsontype = (*Array)(nil)
Expand Down
17 changes: 0 additions & 17 deletions internal/bson/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/binary"
"io"

"github.com/FerretDB/FerretDB/internal/fjson"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)
Expand Down Expand Up @@ -80,22 +79,6 @@ func (bin Binary) MarshalBinary() ([]byte, error) {
return buf.Bytes(), nil
}

// UnmarshalJSON implements bsontype interface.
func (bin *Binary) UnmarshalJSON(data []byte) error {
v, err := fjson.Unmarshal(data)
if err != nil {
return lazyerrors.Error(err)
}

*bin = *(toBSON(v).(*Binary))
return nil
}

// MarshalJSON implements bsontype interface.
func (bin Binary) MarshalJSON() ([]byte, error) {
return fjson.Marshal(fromBSON(&bin))
}

// check interfaces
var (
_ bsontype = (*Binary)(nil)
Expand Down
17 changes: 0 additions & 17 deletions internal/bson/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package bson
import (
"bufio"

"github.com/FerretDB/FerretDB/internal/fjson"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

Expand Down Expand Up @@ -69,22 +68,6 @@ func (b Bool) MarshalBinary() ([]byte, error) {
}
}

// UnmarshalJSON implements bsontype interface.
func (b *Bool) UnmarshalJSON(data []byte) error {
v, err := fjson.Unmarshal(data)
if err != nil {
return lazyerrors.Error(err)
}

*b = *(toBSON(v).(*Bool))
return nil
}

// MarshalJSON implements bsontype interface.
func (b Bool) MarshalJSON() ([]byte, error) {
return fjson.Marshal(fromBSON(&b))
}

// check interfaces
var (
_ bsontype = (*Bool)(nil)
Expand Down
4 changes: 1 addition & 3 deletions internal/bson/bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ package bson
import (
"bufio"
"encoding"
"encoding/json"
"fmt"
"time"

Expand All @@ -41,12 +40,11 @@ type bsontype interface {
ReadFrom(*bufio.Reader) error
WriteTo(*bufio.Writer) error
encoding.BinaryMarshaler
json.Unmarshaler
json.Marshaler
}

//go-sumtype:decl bsontype

//nolint:deadcode // remove later if it is not needed
func fromBSON(v bsontype) any {
switch v := v.(type) {
case *Document:
Expand Down
17 changes: 0 additions & 17 deletions internal/bson/cstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package bson
import (
"bufio"

"github.com/FerretDB/FerretDB/internal/fjson"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

Expand Down Expand Up @@ -59,22 +58,6 @@ func (cstr CString) MarshalBinary() ([]byte, error) {
return b, nil
}

// UnmarshalJSON implements bsontype interface.
func (cstr *CString) UnmarshalJSON(data []byte) error {
v, err := fjson.Unmarshal(data)
if err != nil {
return lazyerrors.Error(err)
}

*cstr = *(toBSON(v).(*CString))
return nil
}

// MarshalJSON implements bsontype interface.
func (cstr CString) MarshalJSON() ([]byte, error) {
return fjson.Marshal(fromBSON(&cstr))
}

// check interfaces
var (
_ bsontype = (*CString)(nil)
Expand Down
17 changes: 0 additions & 17 deletions internal/bson/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/binary"
"time"

"github.com/FerretDB/FerretDB/internal/fjson"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

Expand Down Expand Up @@ -71,22 +70,6 @@ func (dt DateTime) MarshalBinary() ([]byte, error) {
return buf.Bytes(), nil
}

// UnmarshalJSON implements bsontype interface.
func (dt *DateTime) UnmarshalJSON(data []byte) error {
v, err := fjson.Unmarshal(data)
if err != nil {
return lazyerrors.Error(err)
}

*dt = *(toBSON(v).(*DateTime))
return nil
}

// MarshalJSON implements bsontype interface.
func (dt DateTime) MarshalJSON() ([]byte, error) {
return fjson.Marshal(fromBSON(&dt))
}

// check interfaces
var (
_ bsontype = (*DateTime)(nil)
Expand Down
17 changes: 0 additions & 17 deletions internal/bson/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"io"
"time"

"github.com/FerretDB/FerretDB/internal/fjson"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)
Expand Down Expand Up @@ -416,22 +415,6 @@ func (doc Document) MarshalBinary() ([]byte, error) {
return res.Bytes(), nil
}

// UnmarshalJSON implements bsontype interface.
func (doc *Document) UnmarshalJSON(data []byte) error {
v, err := fjson.Unmarshal(data)
if err != nil {
return lazyerrors.Error(err)
}

*doc = *(toBSON(v).(*Document))
return nil
}

// MarshalJSON implements bsontype interface.
func (doc Document) MarshalJSON() ([]byte, error) {
return fjson.Marshal(fromBSON(&doc))
}

// check interfaces
var (
_ bsontype = (*Document)(nil)
Expand Down
17 changes: 0 additions & 17 deletions internal/bson/double.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"encoding/binary"
"math"

"github.com/FerretDB/FerretDB/internal/fjson"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

Expand Down Expand Up @@ -64,22 +63,6 @@ func (d Double) MarshalBinary() ([]byte, error) {
return buf.Bytes(), nil
}

// UnmarshalJSON implements bsontype interface.
func (d *Double) UnmarshalJSON(data []byte) error {
v, err := fjson.Unmarshal(data)
if err != nil {
return lazyerrors.Error(err)
}

*d = *(toBSON(v).(*Double))
return nil
}

// MarshalJSON implements bsontype interface.
func (d Double) MarshalJSON() ([]byte, error) {
return fjson.Marshal(fromBSON(&d))
}

// check interfaces
var (
_ bsontype = (*Double)(nil)
Expand Down
17 changes: 0 additions & 17 deletions internal/bson/int32.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"bytes"
"encoding/binary"

"github.com/FerretDB/FerretDB/internal/fjson"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

Expand Down Expand Up @@ -61,22 +60,6 @@ func (i Int32) MarshalBinary() ([]byte, error) {
return buf.Bytes(), nil
}

// UnmarshalJSON implements bsontype interface.
func (i *Int32) UnmarshalJSON(data []byte) error {
v, err := fjson.Unmarshal(data)
if err != nil {
return lazyerrors.Error(err)
}

*i = *(toBSON(v).(*Int32))
return nil
}

// MarshalJSON implements bsontype interface.
func (i Int32) MarshalJSON() ([]byte, error) {
return fjson.Marshal(fromBSON(&i))
}

// check interfaces
var (
_ bsontype = (*Int32)(nil)
Expand Down
17 changes: 0 additions & 17 deletions internal/bson/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"bytes"
"encoding/binary"

"github.com/FerretDB/FerretDB/internal/fjson"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

Expand Down Expand Up @@ -61,22 +60,6 @@ func (i Int64) MarshalBinary() ([]byte, error) {
return buf.Bytes(), nil
}

// UnmarshalJSON implements bsontype interface.
func (i *Int64) UnmarshalJSON(data []byte) error {
v, err := fjson.Unmarshal(data)
if err != nil {
return lazyerrors.Error(err)
}

*i = *(toBSON(v).(*Int64))
return nil
}

// MarshalJSON implements bsontype interface.
func (i Int64) MarshalJSON() ([]byte, error) {
return fjson.Marshal(fromBSON(&i))
}

// check interfaces
var (
_ bsontype = (*Int64)(nil)
Expand Down
17 changes: 0 additions & 17 deletions internal/bson/objectid.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bufio"
"io"

"github.com/FerretDB/FerretDB/internal/fjson"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)
Expand Down Expand Up @@ -59,22 +58,6 @@ func (obj ObjectID) MarshalBinary() ([]byte, error) {
return b, nil
}

// UnmarshalJSON implements bsontype interface.
func (obj *ObjectID) UnmarshalJSON(data []byte) error {
v, err := fjson.Unmarshal(data)
if err != nil {
return lazyerrors.Error(err)
}

*obj = *(toBSON(v).(*ObjectID))
return nil
}

// MarshalJSON implements bsontype interface.
func (obj ObjectID) MarshalJSON() ([]byte, error) {
return fjson.Marshal(fromBSON(&obj))
}

// check interfaces
var (
_ bsontype = (*ObjectID)(nil)
Expand Down
17 changes: 0 additions & 17 deletions internal/bson/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bufio"
"bytes"

"github.com/FerretDB/FerretDB/internal/fjson"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)
Expand Down Expand Up @@ -77,22 +76,6 @@ func (regex Regex) MarshalBinary() ([]byte, error) {
return buf.Bytes(), nil
}

// UnmarshalJSON implements bsontype interface.
func (regex *Regex) UnmarshalJSON(data []byte) error {
v, err := fjson.Unmarshal(data)
if err != nil {
return lazyerrors.Error(err)
}

*regex = *(toBSON(v).(*Regex))
return nil
}

// MarshalJSON implements bsontype interface.
func (regex Regex) MarshalJSON() ([]byte, error) {
return fjson.Marshal(fromBSON(&regex))
}

// check interfaces
var (
_ bsontype = (*Regex)(nil)
Expand Down
Loading

0 comments on commit e4e9e24

Please sign in to comment.