Skip to content

Commit

Permalink
refactor: Replace standard JSON with jsoniter
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitsridhar16 authored and efirs committed Jun 2, 2023
1 parent cba2e33 commit 666eb08
Show file tree
Hide file tree
Showing 28 changed files with 90 additions and 77 deletions.
6 changes: 3 additions & 3 deletions api/server/v1/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package api

import (
"encoding/json"
"fmt"
"time"

"github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
jsoniter "github.com/json-iterator/go"
"google.golang.org/genproto/googleapis/rpc/errdetails"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -268,7 +268,7 @@ func MarshalStatus(status *spb.Status) ([]byte, error) {
}
}

return json.Marshal(&resp)
return jsoniter.Marshal(&resp)
}

// FromErrorDetails construct TigrisError from the ErrorDetails,
Expand Down Expand Up @@ -297,7 +297,7 @@ func UnmarshalStatus(b []byte) *TigrisError {
Error ErrorDetails `json:"error"`
}{}

if err := json.Unmarshal(b, &resp); err != nil {
if err := jsoniter.Unmarshal(b, &resp); err != nil {
return &TigrisError{Code: Code_UNKNOWN, Message: err.Error()}
}

Expand Down
4 changes: 2 additions & 2 deletions driver/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ package driver

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"strings"
"unsafe"

jsoniter "github.com/json-iterator/go"
api "github.com/tigrisdata/tigris-client-go/api/server/v1"
"github.com/tigrisdata/tigris-client-go/config"
"google.golang.org/grpc"
Expand Down Expand Up @@ -576,7 +576,7 @@ func (c *grpcCRUD) search(ctx context.Context, collection string, req *SearchReq
var err error

if req.Sort != nil {
if b, err = json.Marshal(req.Sort); err != nil {
if b, err = jsoniter.Marshal(req.Sort); err != nil {
return nil, err
}
}
Expand Down
3 changes: 2 additions & 1 deletion driver/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"
"unsafe"

jsoniter "github.com/json-iterator/go"
apiHTTP "github.com/tigrisdata/tigris-client-go/api/client/v1/api"
api "github.com/tigrisdata/tigris-client-go/api/server/v1"
"github.com/tigrisdata/tigris-client-go/config"
Expand Down Expand Up @@ -1161,7 +1162,7 @@ func getAccessToken(ctx context.Context, tokenURL string, cfg *config.Driver, cl

var tr TokenResponse

err = json.Unmarshal(body, &tr)
err = jsoniter.Unmarshal(body, &tr)
if err != nil {
return nil, api.Errorf(api.Code_INTERNAL, "failed to parse external response: reason = %s", err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions driver/search_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package driver

import (
"context"
"encoding/json"
"unsafe"

jsoniter "github.com/json-iterator/go"
api "github.com/tigrisdata/tigris-client-go/api/server/v1"
)

Expand Down Expand Up @@ -181,7 +181,7 @@ func (c *grpcSearch) Search(ctx context.Context, name string, req *SearchRequest
var err error

if req.Sort != nil {
if b, err = json.Marshal(req.Sort); err != nil {
if b, err = jsoniter.Marshal(req.Sort); err != nil {
return nil, err
}
}
Expand Down
5 changes: 3 additions & 2 deletions driver/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/golang/mock/gomock"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
api "github.com/tigrisdata/tigris-client-go/api/server/v1"
Expand Down Expand Up @@ -109,10 +110,10 @@ func testSearchBasic(t *testing.T, c Driver, mc *mock.MockSearchServer) {
assert.True(t, sit.Next(&r))
require.NoError(t, sit.Err())

res, err := json.Marshal(r)
res, err := jsoniter.Marshal(r)
require.NoError(t, err)

exp, err := json.Marshal(&searchResp)
exp, err := jsoniter.Marshal(&searchResp)
require.NoError(t, err)

require.JSONEq(t, string(exp), string(res))
Expand Down
18 changes: 13 additions & 5 deletions driver/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package driver

import (
"encoding/json"
"fmt"
"testing"
"unsafe"

"github.com/golang/mock/gomock"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
Expand All @@ -34,7 +34,15 @@ type JSONMatcher struct {
}

func (matcher *JSONMatcher) Matches(actual any) bool {
return assert.JSONEq(matcher.T, string(matcher.Expected), string(actual.(Schema)))
var s string
switch t := actual.(type) {
case Schema:
s = string(t)
case Projection:
s = string(t)
}

return assert.JSONEq(matcher.T, string(matcher.Expected), s)
}

func (matcher *JSONMatcher) String() string {
Expand All @@ -57,9 +65,9 @@ type JSONArrMatcher struct {
}

func (matcher *JSONArrMatcher) Matches(actual any) bool {
act, err := json.Marshal(actual)
act, err := jsoniter.Marshal(actual)
require.NoError(matcher.T, err)
exp, err := json.Marshal(actual)
exp, err := jsoniter.Marshal(actual)
require.NoError(matcher.T, err)

assert.JSONEq(matcher.T, string(exp), string(act))
Expand All @@ -82,7 +90,7 @@ func JAM(t *testing.T, expected []string) gomock.Matcher {
}

func ToDocument(t *testing.T, doc any) Document {
b, err := json.Marshal(doc)
b, err := jsoniter.Marshal(doc)
require.NoError(t, err)

return b
Expand Down
5 changes: 2 additions & 3 deletions fields/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
package fields

import (
"encoding/json"

jsoniter "github.com/json-iterator/go"
"github.com/tigrisdata/tigris-client-go/driver"
)

Expand Down Expand Up @@ -68,7 +67,7 @@ func (pr *Read) Build() (*Read, error) {

var err error

pr.built, err = json.Marshal(pr.fields)
pr.built, err = jsoniter.Marshal(pr.fields)

return pr, err
}
Expand Down
4 changes: 2 additions & 2 deletions fields/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
package fields

import (
"encoding/json"
"fmt"

jsoniter "github.com/json-iterator/go"
"github.com/tigrisdata/tigris-client-go/driver"
)

Expand Down Expand Up @@ -60,7 +60,7 @@ func (u *Update) Build() (*Update, error) {

var err error

u.built, err = json.Marshal(u)
u.built, err = jsoniter.Marshal(u)
return u, err
}

Expand Down
2 changes: 1 addition & 1 deletion fields/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestUpdateBasic(t *testing.T) {
t.Run(v.name, func(t *testing.T) {
b, err := v.fields.Build()
assert.Equal(t, v.err, err)
assert.Equal(t, v.exp, string(b.Built()))
assert.JSONEq(t, v.exp, string(b.Built()))
})
}

Expand Down
5 changes: 2 additions & 3 deletions filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
package filter

import (
"encoding/json"

jsoniter "github.com/json-iterator/go"
"github.com/tigrisdata/tigris-client-go/driver"
"github.com/tigrisdata/tigris-client-go/schema"
)
Expand Down Expand Up @@ -116,5 +115,5 @@ func (prev Expr) Build() (driver.Filter, error) {
return nil, nil
}

return json.Marshal(prev)
return jsoniter.Marshal(prev)
}
9 changes: 4 additions & 5 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
package schema

import (
"encoding/json"
"fmt"
"reflect"
"sort"
Expand Down Expand Up @@ -259,7 +258,7 @@ func isPrimaryKeyType(tp string) bool {

// Build converts structured schema to driver schema.
func Build(sch *Schema) (driver.Schema, error) {
return json.Marshal(sch)
return jsoniter.Marshal(sch)
}

// Build converts structured schema to driver schema.
Expand Down Expand Up @@ -619,14 +618,14 @@ func (f *FieldMultiType) UnmarshalJSON(b []byte) error {

func (f *FieldMultiType) MarshalJSON() ([]byte, error) {
if !f.isNullable && len(f.Type) == 1 {
return json.Marshal(f.Type[0])
return jsoniter.Marshal(f.Type[0])
}

if !f.isNullable {
return json.Marshal(f.Type)
return jsoniter.Marshal(f.Type)
}

return json.Marshal(append(f.Type, "null"))
return jsoniter.Marshal(append(f.Type, "null"))
}

func (f *FieldMultiType) First() string {
Expand Down
10 changes: 5 additions & 5 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
package schema

import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"testing"
"time"

"github.com/google/uuid"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -417,11 +417,11 @@ func TestCollectionSchema(t *testing.T) {
require.NoError(t, err)

res := `{"version":5,"title":"all_types","properties":{"PtrStruct":{"type":["object","null"],"properties":{"ss_field_1":{"type":"string"}}},"Tm":{"type":"string","format":"date-time"},"TmPtr":{"type":["string","null"],"format":"date-time"},"UUID":{"type":"string","format":"uuid"},"UUIDPtr":{"type":["string","null"],"format":"uuid"},"arr_1":{"type":"array","items":{"type":"string"},"maxItems":3},"bool_1":{"type":"boolean"},"bool_123":{"type":"boolean"},"bytes_1":{"type":"string","format":"byte"},"bytes_2":{"type":"string","format":"byte"},"data_1":{"type":"object","properties":{"Nested":{"type":"object","properties":{"ss_field_1":{"type":"string"}}},"field_1":{"type":"string"}}},"float_32":{"type":"number"},"float_64":{"type":"number"},"int_1":{"type":"integer"},"int_32":{"type":"integer","format":"int32"},"int_64":{"type":"integer"},"map_1":{"type":"object","additionalProperties":true},"map_2":{"type":"object","additionalProperties":true},"map_any":{"type":"object","additionalProperties":true},"slice_1":{"type":"array","items":{"type":"string"}},"slice_2":{"type":"array","items":{"type":"object","properties":{"Nested":{"type":"object","properties":{"ss_field_1":{"type":"string"}}},"field_1":{"type":"string"}}}},"string_1":{"type":"string"}},"primary_key":["string_1"]}`
require.Equal(t, res, string(b))
require.JSONEq(t, res, string(b))

var sch Schema

err = json.Unmarshal([]byte(res), &sch)
err = jsoniter.Unmarshal([]byte(res), &sch)
require.NoError(t, err)

require.Equal(t, s, &sch)
Expand Down Expand Up @@ -534,8 +534,8 @@ func TestDefaultsNegative(t *testing.T) {
{"int", "integer", "vbn", fmt.Errorf("%w: %s", ErrInvalidDefaultTag, "strconv.ParseInt: parsing \"vbn\": invalid syntax")},
{"float", "number", "vbn", fmt.Errorf("%w: %s", ErrInvalidDefaultTag, "strconv.ParseFloat: parsing \"vbn\": invalid syntax")},
{"bool", "boolean", "vbn", fmt.Errorf("%w: %s", ErrInvalidDefaultTag, "invalid bool value: vbn")},
{"array", "array", "vbn", fmt.Errorf("%w: %s", ErrInvalidDefaultTag, "invalid character 'v' looking for beginning of value")},
{"object", "object", "vbn", fmt.Errorf("%w: %s", ErrInvalidDefaultTag, "invalid character 'v' looking for beginning of value")},
{"array", "array", "vbn", fmt.Errorf("%w: %s", ErrInvalidDefaultTag, "[]interface {}: decode slice: expect [ or n, but found v, error found in #1 byte of ...|vbn|..., bigger context ...|vbn|...")},
{"object", "object", "vbn", fmt.Errorf("%w: %s", ErrInvalidDefaultTag, "ReadMapCB: expect { or n, but found v, error found in #1 byte of ...|vbn|..., bigger context ...|vbn|...")},
}

for _, c := range cases {
Expand Down
6 changes: 3 additions & 3 deletions schema/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package schema

import (
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/iancoleman/strcase"
jsoniter "github.com/json-iterator/go"
)

var (
Expand Down Expand Up @@ -171,14 +171,14 @@ func parseDefaultTag(f *Field, val string) error {
case typeObject:
var o map[string]any

if err := json.Unmarshal([]byte(val), &o); err != nil {
if err := jsoniter.Unmarshal([]byte(val), &o); err != nil {
return tagError(ErrInvalidDefaultTag, err.Error())
}

f.Default = o
case typeArray:
var a []any
if err := json.Unmarshal([]byte(val), &a); err != nil {
if err := jsoniter.Unmarshal([]byte(val), &a); err != nil {
return tagError(ErrInvalidDefaultTag, err.Error())
}

Expand Down
6 changes: 3 additions & 3 deletions search/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ package search

import (
"context"
"encoding/json"

jsoniter "github.com/json-iterator/go"
api "github.com/tigrisdata/tigris-client-go/api/server/v1"
"github.com/tigrisdata/tigris-client-go/driver"
"github.com/tigrisdata/tigris-client-go/filter"
Expand Down Expand Up @@ -69,7 +69,7 @@ func (c *Index[T]) createOrReplace(ctx context.Context, tp int, docs ...*T) (*Re
bdocs := make([]driver.Document, len(docs))

for k, v := range docs {
if bdocs[k], err = json.Marshal(v); err != nil {
if bdocs[k], err = jsoniter.Marshal(v); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func (c *Index[T]) Get(ctx context.Context, ids []string) ([]T, error) {
continue
}

err = json.Unmarshal(v.Data, &doc)
err = jsoniter.Unmarshal(v.Data, &doc)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions search/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/golang/mock/gomock"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/require"
api "github.com/tigrisdata/tigris-client-go/api/server/v1"
"github.com/tigrisdata/tigris-client-go/code"
Expand Down Expand Up @@ -305,7 +306,7 @@ func TestSearchIndex(t *testing.T) {
func createSearchResponse(t *testing.T, doc any) driver.SearchIndexResponse {
t.Helper()

d, err := json.Marshal(doc)
d, err := jsoniter.Marshal(doc)
require.NoError(t, err)
tm := time.Now()
return &api.SearchIndexResponse{
Expand Down Expand Up @@ -434,7 +435,7 @@ func TestSearchIndex_Search(t *testing.T) {
require.Nil(t, searchIter.err)
require.False(t, searchIter.Next(&rs))
require.NotNil(t, searchIter.err)
require.ErrorContains(t, searchIter.err, "cannot unmarshal string")
require.ErrorContains(t, searchIter.err, "readObjectStart")
})
}

Expand Down

0 comments on commit 666eb08

Please sign in to comment.