Skip to content

Commit

Permalink
Merge branch 'main' into issue-693-new-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rumyantseva committed Oct 14, 2022
2 parents f5594c2 + b45db81 commit 0fe5803
Show file tree
Hide file tree
Showing 27 changed files with 3 additions and 689 deletions.
34 changes: 0 additions & 34 deletions internal/types/fjson/array.go
Expand Up @@ -16,7 +16,6 @@ package fjson

import (
"bytes"
"encoding/json"

"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
Expand All @@ -28,39 +27,6 @@ type arrayType types.Array
// fjsontype implements fjsontype interface.
func (a *arrayType) fjsontype() {}

// UnmarshalJSON implements fjsontype interface.
func (a *arrayType) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, []byte("null")) {
panic("null data")
}

r := bytes.NewReader(data)
dec := json.NewDecoder(r)

var rawMessages []json.RawMessage
if err := dec.Decode(&rawMessages); err != nil {
return lazyerrors.Error(err)
}
if err := checkConsumed(dec, r); err != nil {
return lazyerrors.Error(err)
}

ta := types.MakeArray(len(rawMessages))
for _, el := range rawMessages {
v, err := Unmarshal(el)
if err != nil {
return lazyerrors.Error(err)
}

if err = ta.Append(v); err != nil {
return lazyerrors.Error(err)
}
}

*a = arrayType(*ta)
return nil
}

// MarshalJSON implements fjsontype interface.
func (a *arrayType) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
Expand Down
8 changes: 0 additions & 8 deletions internal/types/fjson/array_test.go
Expand Up @@ -52,11 +52,3 @@ func TestArray(t *testing.T) {
t.Parallel()
testJSON(t, arrayTestCases, func() fjsontype { return new(arrayType) })
}

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

func BenchmarkArray(b *testing.B) {
benchmark(b, arrayTestCases, func() fjsontype { return new(arrayType) })
}
25 changes: 0 additions & 25 deletions internal/types/fjson/binary.go
Expand Up @@ -15,7 +15,6 @@
package fjson

import (
"bytes"
"encoding/json"

"github.com/FerretDB/FerretDB/internal/types"
Expand All @@ -34,30 +33,6 @@ type binaryJSON struct {
S byte `json:"s"`
}

// UnmarshalJSON implements fjsontype interface.
func (bin *binaryType) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, []byte("null")) {
panic("null data")
}

r := bytes.NewReader(data)
dec := json.NewDecoder(r)
dec.DisallowUnknownFields()

var o binaryJSON
err := dec.Decode(&o)
if err != nil {
return lazyerrors.Error(err)
}
if err = checkConsumed(dec, r); err != nil {
return lazyerrors.Error(err)
}

bin.B = o.B
bin.Subtype = types.BinarySubtype(o.S)
return nil
}

// MarshalJSON implements fjsontype interface.
func (bin *binaryType) MarshalJSON() ([]byte, error) {
res, err := json.Marshal(binaryJSON{
Expand Down
8 changes: 0 additions & 8 deletions internal/types/fjson/binary_test.go
Expand Up @@ -61,11 +61,3 @@ func TestBinary(t *testing.T) {
t.Parallel()
testJSON(t, binaryTestCases, func() fjsontype { return new(binaryType) })
}

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

func BenchmarkBinary(b *testing.B) {
benchmark(b, binaryTestCases, func() fjsontype { return new(binaryType) })
}
22 changes: 0 additions & 22 deletions internal/types/fjson/bool.go
Expand Up @@ -15,7 +15,6 @@
package fjson

import (
"bytes"
"encoding/json"

"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
Expand All @@ -27,27 +26,6 @@ type boolType bool
// fjsontype implements fjsontype interface.
func (b *boolType) fjsontype() {}

// UnmarshalJSON implements fjsontype interface.
func (b *boolType) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, []byte("null")) {
panic("null data")
}

r := bytes.NewReader(data)
dec := json.NewDecoder(r)

var o bool
if err := dec.Decode(&o); err != nil {
return lazyerrors.Error(err)
}
if err := checkConsumed(dec, r); err != nil {
return lazyerrors.Error(err)
}

*b = boolType(o)
return nil
}

// MarshalJSON implements fjsontype interface.
func (b *boolType) MarshalJSON() ([]byte, error) {
res, err := json.Marshal(bool(*b))
Expand Down
8 changes: 0 additions & 8 deletions internal/types/fjson/bool_test.go
Expand Up @@ -34,11 +34,3 @@ func TestBool(t *testing.T) {
t.Parallel()
testJSON(t, boolTestCases, func() fjsontype { return new(boolType) })
}

func FuzzBool(f *testing.F) {
fuzzJSON(f, boolTestCases, func() fjsontype { return new(boolType) })
}

func BenchmarkBool(b *testing.B) {
benchmark(b, boolTestCases, func() fjsontype { return new(boolType) })
}
24 changes: 0 additions & 24 deletions internal/types/fjson/datetime.go
Expand Up @@ -15,7 +15,6 @@
package fjson

import (
"bytes"
"encoding/json"
"time"

Expand All @@ -38,29 +37,6 @@ type dateTimeJSON struct {
D int64 `json:"$d"`
}

// UnmarshalJSON implements fjsontype interface.
func (dt *dateTimeType) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, []byte("null")) {
panic("null data")
}

r := bytes.NewReader(data)
dec := json.NewDecoder(r)
dec.DisallowUnknownFields()

var o dateTimeJSON
if err := dec.Decode(&o); err != nil {
return lazyerrors.Error(err)
}
if err := checkConsumed(dec, r); err != nil {
return lazyerrors.Error(err)
}

// TODO Use .UTC(): https://github.com/FerretDB/FerretDB/issues/43
*dt = dateTimeType(time.UnixMilli(o.D))
return nil
}

// MarshalJSON implements fjsontype interface.
func (dt *dateTimeType) MarshalJSON() ([]byte, error) {
res, err := json.Marshal(dateTimeJSON{
Expand Down
8 changes: 0 additions & 8 deletions internal/types/fjson/datetime_test.go
Expand Up @@ -47,11 +47,3 @@ func TestDateTime(t *testing.T) {
t.Parallel()
testJSON(t, dateTimeTestCases, func() fjsontype { return new(dateTimeType) })
}

func FuzzDateTime(f *testing.F) {
fuzzJSON(f, dateTimeTestCases, func() fjsontype { return new(dateTimeType) })
}

func BenchmarkDateTime(b *testing.B) {
benchmark(b, dateTimeTestCases, func() fjsontype { return new(dateTimeType) })
}
50 changes: 0 additions & 50 deletions internal/types/fjson/document.go
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
)

// documentType represents BSON Document type.
Expand All @@ -29,55 +28,6 @@ type documentType types.Document
// fjsontype implements fjsontype interface.
func (doc *documentType) fjsontype() {}

// UnmarshalJSON implements fjsontype interface.
func (doc *documentType) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, []byte("null")) {
panic("null data")
}

r := bytes.NewReader(data)
dec := json.NewDecoder(r)

var rawMessages map[string]json.RawMessage
if err := dec.Decode(&rawMessages); err != nil {
return lazyerrors.Error(err)
}
if err := checkConsumed(dec, r); err != nil {
return lazyerrors.Error(err)
}

b, ok := rawMessages["$k"]
if !ok {
return lazyerrors.Errorf("fjson.documentType.UnmarshalJSON: missing $k")
}

var keys []string
if err := json.Unmarshal(b, &keys); err != nil {
return lazyerrors.Error(err)
}
if len(keys)+1 != len(rawMessages) {
return lazyerrors.Errorf("fjson.documentType.UnmarshalJSON: %d elements in $k, %d in total", len(keys), len(rawMessages))
}

td := must.NotFail(types.NewDocument())
for _, key := range keys {
b, ok = rawMessages[key]
if !ok {
return lazyerrors.Errorf("fjson.documentType.UnmarshalJSON: missing key %q", key)
}
v, err := Unmarshal(b)
if err != nil {
return lazyerrors.Error(err)
}
if err = td.Set(key, v); err != nil {
return lazyerrors.Error(err)
}
}

*doc = documentType(*td)
return nil
}

// MarshalJSON implements fjsontype interface.
func (doc *documentType) MarshalJSON() ([]byte, error) {
td := types.Document(*doc)
Expand Down
8 changes: 0 additions & 8 deletions internal/types/fjson/document_test.go
Expand Up @@ -221,11 +221,3 @@ func TestDocument(t *testing.T) {
t.Parallel()
testJSON(t, documentTestCases, func() fjsontype { return new(documentType) })
}

func FuzzDocument(f *testing.F) {
fuzzJSON(f, documentTestCases, func() fjsontype { return new(documentType) })
}

func BenchmarkDocument(b *testing.B) {
benchmark(b, documentTestCases, func() fjsontype { return new(documentType) })
}
42 changes: 0 additions & 42 deletions internal/types/fjson/double.go
Expand Up @@ -15,7 +15,6 @@
package fjson

import (
"bytes"
"encoding/json"
"math"

Expand All @@ -33,47 +32,6 @@ type doubleJSON struct {
F any `json:"$f"`
}

// UnmarshalJSON implements fjsontype interface.
func (d *doubleType) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, []byte("null")) {
panic("null data")
}

r := bytes.NewReader(data)
dec := json.NewDecoder(r)
dec.DisallowUnknownFields()

var o doubleJSON
if err := dec.Decode(&o); err != nil {
return lazyerrors.Error(err)
}
if err := checkConsumed(dec, r); err != nil {
return lazyerrors.Error(err)
}

switch f := o.F.(type) {
case float64:
*d = doubleType(f)
case string:
switch f {
case "-0":
*d = doubleType(math.Copysign(0, -1))
case "Infinity":
*d = doubleType(math.Inf(+1))
case "-Infinity":
*d = doubleType(math.Inf(-1))
case "NaN":
*d = doubleType(math.NaN())
default:
return lazyerrors.Errorf("fjson.Double.UnmarshalJSON: unexpected string %q", f)
}
default:
return lazyerrors.Errorf("fjson.Double.UnmarshalJSON: unexpected type %[1]T: %[1]v", f)
}

return nil
}

// MarshalJSON implements fjsontype interface.
func (d *doubleType) MarshalJSON() ([]byte, error) {
f := float64(*d)
Expand Down
8 changes: 0 additions & 8 deletions internal/types/fjson/double_test.go
Expand Up @@ -63,11 +63,3 @@ func TestDouble(t *testing.T) {
t.Parallel()
testJSON(t, doubleTestCases, func() fjsontype { return new(doubleType) })
}

func FuzzDouble(f *testing.F) {
fuzzJSON(f, doubleTestCases, func() fjsontype { return new(doubleType) })
}

func BenchmarkDouble(b *testing.B) {
benchmark(b, doubleTestCases, func() fjsontype { return new(doubleType) })
}

0 comments on commit 0fe5803

Please sign in to comment.